27 #ifndef math_vectorUtils_hpp
28 #define math_vectorUtils_hpp
61 template<
typename vectorT>
64 typename vectorT::value_type scale = 0,
65 typename vectorT::value_type offset = 0
68 if(scale == 0) scale = 1.0;
70 if(N > 0) vec.resize(N);
72 for(
int i=0;i<vec.size(); ++i) vec[i] = i*scale + offset;
91 template <
typename memberT>
94 std::vector<size_t> indices(values.size());
96 std::iota(begin(indices), end(indices),
static_cast<size_t>(0));
98 std::sort( begin(indices), end(indices), [&](
size_t a,
size_t b) {
return values[a] < values[b]; } );
111 template<
typename valueT>
118 for(
size_t i=0; i< sz; ++i) sum += vec[i];
131 template<
typename vectorT>
132 typename vectorT::value_type
vectorSum(
const vectorT & vec )
134 typename vectorT::value_type sum = 0;
136 for(
size_t i=0; i< vec.size(); ++i) sum += vec[i];
149 template<
typename valueT>
156 for(
size_t i=0; i< sz; ++i) mean += vec[i];
171 template<
typename vectorT>
172 typename vectorT::value_type
vectorMean(
const vectorT & vec )
174 typename vectorT::value_type mean = 0;
176 for(
size_t i=0; i< vec.size(); ++i) mean += vec[i];
190 template<
typename vectorT>
191 typename vectorT::value_type
vectorMean(
const vectorT & vec,
195 typename vectorT::value_type mean = 0, wsum=0;
197 for(
size_t i=0; i< vec.size(); ++i)
217 template<
typename vectorT>
220 typename vectorT::value_type med;
222 int n = 0.5*vec.size();
224 std::nth_element(vec.begin(), vec.begin()+n, vec.end());
229 if(vec.size()%2 == 0)
231 med = 0.5*(med + *std::max_element(vec.begin(), vec.begin()+n));
246 template<
typename vectorT>
251 typename vectorT::value_type med;
253 bool localWork =
false;
260 work->resize(vec.size());
262 for(
int i=0;i<vec.size();++i)
269 if(localWork)
delete work;
281 template<
typename valueT>
290 for(
size_t i=0; i<sz; ++i) var += pow(vec[i]-mean,2);
304 template<
typename vectorT>
306 const typename vectorT::value_type & mean
309 typename vectorT::value_type var;
312 for(
size_t i=0; i< vec.size(); ++i) var += (vec[i]-mean)*(vec[i]-mean);
314 var /= (vec.size()-1);
326 template<
typename valueT>
345 template<
typename vectorT>
348 typename vectorT::value_type mean;
364 template<
typename vectorT>
366 const vectorT * weights,
367 typename vectorT::value_type
sigma,
373 typename vectorT::value_type med, var, Vsig, dev;
376 bool doWeight =
false;
379 if( weights->size() == vec.size()) doWeight =
true;
395 wwork.resize(vec.size());
396 for(
int i=0; i< vec.size(); ++i)
399 wwork[i] = (*weights)[i];
404 while(passes < maxPasses || maxPasses == 0)
410 for(
size_t i=0; i<work.size(); ++i)
412 dev = pow(work[i] - med,2)/var;
415 work.erase(work.begin()+i);
417 if(doWeight) wwork.erase(wwork.begin() + i);
424 if(nclip == 0)
break;
445 template<
typename vectorT>
447 typename vectorT::value_type
sigma
458 template<
typename vectorT>
460 const vectorT & weights,
461 typename vectorT::value_type
sigma,
472 template<
typename vectorT>
474 const vectorT & weights,
475 typename vectorT::value_type
sigma
485 template<
typename valueT,
typename constT>
491 for(
size_t n=0; n< sz;++n) vec[n] -=
c;
495 template<
typename vecT,
typename constT>
504 template<
typename valueT>
514 template<
typename vecT>
521 template<
typename vecT>
529 template<
typename realT>
538 for(
int i=0; i < vecSize; ++i)
545 while( j <= i+0.5*win && j < vecSize)
561 template<
typename realT>
563 std::vector<realT> & vec,
567 smVec.resize(vec.size());
579 template<
typename realT>
581 std::vector<realT> & vec,
582 std::vector<int> & wins,
590 for(
int i=0; i < vec.size(); ++i)
592 int j = i - 0.5*wins[i];
597 while( j <= i+0.5*wins[i] && j < vec.size())
610 realT sumin = 0, sumout = 0;
612 for(
int i=0; i < vec.size(); ++i)
618 for(
int i=0; i < vec.size(); ++i)
620 smVec[i] *= sumin/sumout;
628 template<
typename realT>
630 std::vector<realT> & vec,
636 std::vector<realT> tvec;
638 for(
int i=0; i < vec.size(); ++i)
644 while( j <= i+0.5*win && j < vec.size())
646 tvec.push_back(vec[j]);
658 template<
typename realT>
660 std::vector<realT> & vec,
666 for(
int i=0; i < vec.size(); ++i)
673 while( j <= i+0.5*win && j < vec.size())
675 if(vec[j] > smVec[i]) smVec[i] = vec[j];
690 template<
typename vectorT>
697 if( n==0 )
return -1;
699 binv.resize( v.size()/n );
701 for(
size_t i=0; i< binv.size(); ++i)
708 if(i*n + j >= v.size())
break;
710 binv[i] += v[ i*n + j];
715 if(j > 0) binv[i] /= j;
729 template<
typename vectorT,
typename binVectorT>
737 means.resize(binSzs.size());
739 for(
size_t i=0; i< binSzs.size(); ++i)
743 means[i].resize(means[i].size() + binv.size());
744 for(
size_t j=0; j< binv.size(); ++j) means[i][ means[i].size() - binv.size() + j] = binv[j];
757 template<
typename realT>
759 const std::vector<realT> & dataIn,
760 const std::vector<realT> & scale,
766 if(dataIn.size() != scale.size())
return -1;
768 dataOut.resize(dataIn.size());
770 realT
sigma = func::fwhm2sigma<realT>(fwhm);
772 for(
int i=0; i< dataIn.size(); ++i)
777 for(
int j=i-winhw; j<i+winhw; ++j)
780 if(j > dataIn.size()-1)
continue;
782 G = func::gaussian<realT>( scale[j], 0.0, 1.0, scale[i],
sigma);
788 dataOut[i] = sum/norm;
804 template<
typename floatT>
806 std::vector<floatT> & sum,
807 std::vector<floatT> & vec
812 std::sort(svec.begin(), svec.end());
814 sum.resize(svec.size());
818 for(
int i=1; i< svec.size(); ++i)
820 sum[i] = sum[i-1] + svec[i];
833 template<
typename floatT>
835 std::vector<floatT> & sum,
836 std::vector<floatT> & vec
841 std::sort(svec.begin(), svec.end(), std::greater<floatT>());
843 sum.resize(svec.size());
847 for(
int i=1; i< svec.size(); ++i)
849 sum[i] = sum[i-1] + svec[i];
Declarations for utilities related to the Gaussian function.
constexpr units::realT G()
Newton's Gravitational Constant.
constexpr units::realT c()
The speed of light.
constexpr units::realT sigma()
Stefan-Boltzmann Constant.
void vectorScale(vectorT &vec, size_t N=0, typename vectorT::value_type scale=0, typename vectorT::value_type offset=0)
Fill in a vector with a regularly spaced scale.
int vectorRebin(vectorT &binv, const vectorT &v, unsigned n, bool binMean=false)
Re-bin a vector by summing (or averaging) in bins of size n points.
vectorT::value_type vectorMedianInPlace(vectorT &vec)
Calculate median of a vector in-place, altering the vector.
void vectorMeanSub(vecT &vec)
Subtract the mean from a vector.
vectorT::value_type vectorSum(const vectorT &vec)
Calculate the sum of a vector.
int vectorBinMeans(std::vector< vectorT > &means, binVectorT &binSzs, const vectorT &v)
Calculate and accumulate the means of a timeseries in bins of various sizes.
vectorT::value_type vectorMean(const vectorT &vec, const vectorT &w)
Calculate the weighted mean of a vector.
int vectorCumHistReverse(std::vector< floatT > &svec, std::vector< floatT > &sum, std::vector< floatT > &vec)
Calculate a reverse cumulative histogram of a vector.
void vectorMedianSub(vecT &vec)
Subtract the median from a vector.
vectorT::value_type vectorMedian(const vectorT &vec, vectorT *work=0)
Calculate median of a vector, leaving the vector unaltered.
int vectorCumHist(std::vector< floatT > &svec, std::vector< floatT > &sum, std::vector< floatT > &vec)
Calculate a cumulative histogram of a vector.
vectorT::value_type vectorVariance(const vectorT &vec)
Calculate the variance of a vector.
int vectorGaussConvolve(std::vector< realT > &dataOut, const std::vector< realT > &dataIn, const std::vector< realT > &scale, const realT fwhm, const int winhw)
Convolve (smooth) a vector with a Gaussian.
int vectorSmoothMedian(std::vector< realT > &smVec, std::vector< realT > &vec, int win)
Smooth a vector using the median in a window specified by its full-width.
int vectorSmoothMean(std::vector< realT > &smVec, std::vector< realT > &vec, std::vector< int > &wins, bool norm=false)
Smooth a vector using the mean in windows specified by their full-widths.
int vectorSmoothMax(std::vector< realT > &smVec, std::vector< realT > &vec, int win)
Smooth a vector using the max in a window specified by its full-width.
std::vector< size_t > vectorSortOrder(std::vector< memberT > const &values)
Return the indices of the vector in sorted order, without altering the vector itself.
void vectorSub(vecT &vec, const constT &c)
Subtract a constant value from a vector.
vectorT::value_type vectorSigmaMean(const vectorT &vec, const vectorT &weights, typename vectorT::value_type sigma)