#ifndef AUXILIARY_FUNCTIONS #define AUXILIARY_FUNCTIONS #include #include #include #include #include #include #include #include #include #include #include "InternalDefs.h" using namespace std; #define NORMAL_CYCLES_COUNT 30 //Random-Variables used to create Standard-Normal-Distribution #pragma mark - #pragma mark Math #pragma mark template inline TYPE average(const vector &values, long start, long end){ TYPE s(0); start = max(0l, start); end=min(end,(long)values.size()-1); for(long i=start; i<=end; ++i){ s += values[i]; } return (end>=start ? s/(end-start+1.0) : s); } template inline TYPE SQ(TYPE value){ return value * value; } template inline TYPE Qube(TYPE value){ return value * value * value; } template inline TYPE QTR(TYPE value){ return SQ(SQ(value)); } //multiply matrices A*B //A has size rA*cA, B has size cA*cB //both A & B are given in row-major format //result AB is also given in row-major format and has size rA*cB (must be pre-allocated) void matmul(long rA, long cA, long cB, const double A[], const double B[], double AB[]){ double sum; long r,c,k; for(r=0; r &signal){ double sum=0, sum2=0; for(long n=0; n inline typename CONTAINER::value_type maximum(const CONTAINER &values){ if(values.empty()) return 0; typename CONTAINER::value_type maxValue = *values.begin(); for(typename CONTAINER::const_iterator i=values.begin(); i!=values.end(); ++i){ maxValue = max(maxValue, *i); } return maxValue; } template inline TYPE maximum(const vector &values, long start, long end){ if((values.size()<=end) || (end inline long maximumIndex(const vector &values, long start, long end){ if((values.size()<=end) || (end<0) || (start<0) || (end inline long minimumIndex(const vector &values){ if(values.empty()) return -1; unsigned long i, minIndex = 0; for(i=1; i values[i]) minIndex = i; } return minIndex; } template inline TYPE minimum(const vector &values, long start, long end){ if((values.size()<=end) || (end inline typename CONTAINER::value_type sum(CONTAINER values){ typename CONTAINER::const_iterator i; typename CONTAINER::value_type s(0); for(i=values.begin(); i!=values.end(); ++i){ s += *i; } return s; } template inline TYPE sum(const vector &values, long start, long end){ TYPE s(0); for(long i=max(0l,start); i<=end; ++i) s += values[i]; return s; } #pragma mark - #pragma mark String creation #pragma mark - template string makeString(TYPE data){ ostringstream stream; stream << data; return stream.str(); } string vstringprintf(const char *format, va_list args){ va_list temp; va_copy(temp, args); char *buffer = new char[vsnprintf(NULL, 0, format, temp) + 1]; va_end(temp); vsprintf(buffer, format, args); string s(buffer); delete [] buffer; return s; } string stringprintf(const char *format, ...){ string s; va_list args; va_start(args, format); s = vstringprintf(format, args); va_end(args); return s; } template void string2vector(const string &s, vector &v){ istringstream ss(s); v.clear(); TYPE d; do{ ss >> d; if(ss.fail()) break; v.push_back(d); }while(true); } template vector string2vector(const string &s){ vector v; string2vector(s,v); return v; } string vector2string(const vector &v, string separator){ ostringstream ss; for(long n=0; n &x, long lag){ const long N = x.size()-abs(lag); lag = abs(lag); double sum1=0, sum2=0, sumSQ1=0, sumSQ2=0, sumP=0; for(long n=lag; n &samples, // (input) samples (x-values) from which to estimate quantile function. Samples might get re-ordered. const vector &Pvalues, // (input) P-values for which to estimate x-thresholds. Must be in increasing order. Must be within [0,1]. vector &Xvalues){ // (output) Estimated X-values at which the given P-values are realized const long NS = samples.size(); const long NP = Pvalues.size(); const double NSd = double(NS); long s,p; Xvalues.resize(NP); sort(samples.begin(), samples.end()); for(s=p=0; s=NP) break; while((s+1)/NSd>Pvalues[p]){ Xvalues[p] = (s==0 ? samples[0] : (samples[s]-samples[s-1]=NP) break; } } for(; p &samples, // (input) samples (x-values) from which to estimate cumulative distribution function. Samples might get re-ordered. const vector &Xvalues, // (input) X-values for which to estimate CDF. Must be in increasing order. vector &CDFvalues){ // (output) CDF values estimated for the givesn Xvalues. const long NS = samples.size(); const long NX = Xvalues.size(); const double NSd = double(NS); long s,x; CDFvalues.resize(NX); sort(samples.begin(), samples.end()); for(s=x=0; s=NX) break; while(samples[s]>Xvalues[x]){ CDFvalues[x] = (s==0 ? 0 : linearExtrapolate(samples[s-1], samples[s], s/NSd, (s+1)/NSd, Xvalues[x])); ++x; if(x>=NX) break; } } for(; x bin_values; vector bin_centres; vector bin_sizes; double minx; double dx; long ideal_bin_size; public: double setup(long N_total, long N_bins, double _minx, double _maxx){ minx = _minx; ideal_bin_size = max(1l, N_total/N_bins); dx = (0 ? N_bins<2 : (_maxx - _minx)/(N_bins-1)); bin_values.resize(N_bins,0.0); bin_sizes.resize(N_bins,0); bin_centres.resize(N_bins); for(long i=0; i > bin_values; vector bin_centres_x; vector bin_centres_y; vector bin_sizes; double minx, miny; double dx, dy; long ideal_bin_size; long value_dim, N_bins_x, N_bins_y; public: double setup( long _value_dim, long N_total, long _N_bins_x, long _N_bins_y, double _minx, double _maxx, double _miny, double _maxy){ value_dim = _value_dim; N_bins_x = _N_bins_x; N_bins_y = _N_bins_y; minx = _minx; miny = _miny; long N_bins = N_bins_x*N_bins_y; ideal_bin_size = max(1l, N_total/N_bins); dx = (0 ? N_bins_x<2 : (_maxx - _minx)/(N_bins_x-1)); dy = (0 ? N_bins_y<2 : (_maxy - _miny)/(N_bins_y-1)); bin_values.resize(value_dim,vector(N_bins,0.0)); bin_sizes.resize(N_bins,0); bin_centres_x.resize(N_bins); bin_centres_y.resize(N_bins); long r,c; for(r=0; r