#ifndef __AMSCPPNARRAY_MATHIMPL_HPP__ #define __AMSCPPNARRAY_MATHIMPL_HPP__ namespace ams { namespace narray { template T narray_max(const narray &q) { return q.max(); } template T narray_min(const narray &q) { return q.min(); } template void narray_sum_tf( const narray *in, narray *sums, int threadnum, int nthreads ) { int I0,I1,Is,I; Is = in->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); for(I=I0;Idata[threadnum] = sums->data[threadnum] + in->data[I]; } } template T narray_sum(const narray &q) { T ret = T(); narray sums; narray_size_t I; int J; int nthreads; std::vector threads; if(q.length<=0) { return ret; } if(q.lengthnarray_max_threads) ? narray_max_threads : nthreads; threads.resize(nthreads); sums.resize(nthreads); sums.setall(T()); for(J=0;J, &q, &sums, J,nthreads ); if(threads[J]==NULL) { printf("narray_sum: warning: thread %d failed to initialize.\n",J); //handle errors } } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } ret = T(); for(J=0;J double narray_mean(const narray &q) { double ret = 0.0; if(q.length>0) { T sum = narray_sum(q); ret = (double)sum/((double)q.length); } return ret; } template void narray_variance_tf( const narray *in, narray *sums, double mean, int threadnum, int nthreads ) { int I0,I1,Is,I; double q2; Is = in->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); for(I=I0;Idata[I]-mean; sums->data[threadnum] = sums->data[threadnum] + q2*q2/((double)in->length); } } template double narray_var(const narray &q) { double ret = 0.0; narray sums; narray_size_t I; int J; int nthreads; std::vector threads; double mn = ams::narray::narray_mean(q); double q2; if(q.length<=0) { return ret; } if(q.lengthnarray_max_threads) ? narray_max_threads : nthreads; threads.resize(nthreads); sums.resize(nthreads); sums.setall(T()); for(J=0;J, &q, &sums, mn, J,nthreads ); if(threads[J]==NULL) { printf("narray_var: warning: thread %d failed to initialize.\n",J); //handle errors } } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } ret = T(); for(J=0;J double narray_stdev(const narray &q) { double ret = 0.0; if(q.length>0) { ret = narray_var(q); ret = ::sqrt(ret); } return ret; } template void narray_L2norm_tf( const narray *in, narray *sums, int threadnum, int nthreads ) { int I0,I1,Is,I; double q2; Is = in->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); for(I=I0;Idata[I]; sums->data[threadnum] = sums->data[threadnum] + q2*q2; } } template double narray_L2norm(const narray &q) { double ret = 0.0; narray sums; narray_size_t I; int J; int nthreads; std::vector threads; double q2; if(q.length<=0) { return ret; } if(q.lengthnarray_max_threads) ? narray_max_threads : nthreads; threads.resize(nthreads); sums.resize(nthreads); sums.setall(T()); for(J=0;J, &q, &sums, J,nthreads ); if(threads[J]==NULL) { printf("narray_L2norm: warning: thread %d failed to initialize.\n",J); //handle errors } } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } ret = T(); for(J=0;J void narray_abs_tf( const narray *in, narray *out, int threadnum, int nthreads ) { int I0,I1,Is,I; double q2; Is = in->length/nthreads; I0 = Is*threadnum; I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); for(I=I0;Idata[I] = (in->data[I]data[I] : in->data[I]; } } template narray narray_abs(narray &q) { narray ret; narray_size_t I; int J; int nthreads; std::vector threads; if(q.length<=0) { ret.resize(0); return ret; } ret.resize(q.length); if(ret.length!=q.length) { printf("narray_abs: error - could not allocate return array.\n"); ret.resize(0); return ret; } if(q.lengthnarray_max_threads) ? narray_max_threads : nthreads; threads.resize(nthreads); for(J=0;J, &q, &ret, J,nthreads ); if(threads[J]==NULL) { printf("narray_abs: warning: thread %d failed to initialize.\n",J); //handle errors } } for(J=0;Jjoin(); delete threads[J]; threads[J]= NULL; } } } return ret; } }; //end namespaces }; #endif