mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
vectorUtils.hpp
Go to the documentation of this file.
1 /** \file vectorUtils.hpp
2  * \author Jared R. Males
3  * \brief Header for the std::vector utilities
4  * \ingroup gen_math_files
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2015, 2016, 2017, 2018 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef math_vectorUtils_hpp
28 #define math_vectorUtils_hpp
29 
30 
31 #include <vector>
32 #include <algorithm>
33 #include <functional>
34 #include <numeric>
35 
36 #include "func/gaussian.hpp"
37 
38 namespace mx
39 {
40 namespace math
41 {
42 
43 /** \ingroup vectorutils
44  *@{
45  */
46 
47 ///Fill in a vector with a regularly spaced scale
48 /** Fills in the vector with a 0....N-1 scale. The spacing of the points
49  * can be changed with the scale parameter, and the starting point can be
50  * changed with the offset.
51  *
52  * Example:
53  \code
54  std::vector vec;
55 
56  mx::vectorScale(vec, 1000, 0.001, 0.001); // Produces a vector with values 0.001,0.002,.... 1.000
57  \endcode
58  *
59  * \tparam vectorT is a std::vector type.
60  */
61 template<typename vectorT>
62 void vectorScale( vectorT & vec, ///< [out] the vector to fill in, can be pre-allocated or not
63  size_t N = 0, ///< [in] [optional] if specified > 0, then vec is resize()-ed. Default is 0, and vec is not resize()-ed.
64  typename vectorT::value_type scale = 0, ///< [in] [optional] if specified !=0, then the points are spaced by this value. Default spacing is 1.
65  typename vectorT::value_type offset = 0 ///< [in] [optional] if specified !=0, then the starting point of the scale is this value.
66  )
67 {
68  if(scale == 0) scale = 1.0;
69 
70  if(N > 0) vec.resize(N);
71 
72  for(int i=0;i<vec.size(); ++i) vec[i] = i*scale + offset;
73 }
74 
75 
76 ///Return the indices of the vector in sorted order, without altering the vector itself
77 /** Example:
78  \code
79  std::vector<double> x;
80  // -> fill in x with values
81 
82  std::vector<size_t> idx;
83  idx = mx::sortOrder(x);
84 
85  //Print x to stdout in sorted order
86  for(int i=0; i< x.size(); ++i) std::cout << x[idx[i]] << "\n";
87  \endcode
88 
89  * \tparam memberT is the member type of the vector. Must have < comparison defined.
90  */
91 template <typename memberT>
92 std::vector<size_t> vectorSortOrder( std::vector<memberT> const& values /**< [in] the vector to sort */)
93 {
94  std::vector<size_t> indices(values.size());
95 
96  std::iota(begin(indices), end(indices), static_cast<size_t>(0));
97 
98  std::sort( begin(indices), end(indices), [&](size_t a, size_t b) { return values[a] < values[b]; } );
99 
100  return indices; /// \returns the indices of the vector in sorted order.
101 }
102 
103 ///Calculate the sum of a vector.
104 /**
105  *
106  * \returns the sum of vec
107  *
108  * \tparam vectorT the std::vector type of vec
109  *
110  */
111 template<typename valueT>
112 valueT vectorSum( const valueT * vec, ///< [in] the vector
113  size_t sz ///< [in] the size of the vector
114  )
115 {
116  valueT sum = 0;
117 
118  for(size_t i=0; i< sz; ++i) sum += vec[i];
119 
120  return sum;
121 }
122 
123 ///Calculate the sum of a vector.
124 /**
125  *
126  * \returns the sum of vec
127  *
128  * \tparam vectorT the std::vector type of vec
129  *
130  */
131 template<typename vectorT>
132 typename vectorT::value_type vectorSum(const vectorT & vec /**< [in] the vector */)
133 {
134  typename vectorT::value_type sum = 0;
135 
136  for(size_t i=0; i< vec.size(); ++i) sum += vec[i];
137 
138  return sum;
139 }
140 
141 ///Calculate the mean of a vector.
142 /**
143  *
144  * \returns the mean of vec
145  *
146  * \tparam vectorT the std::vector type of vec
147  *
148  */
149 template<typename valueT>
150 valueT vectorMean( const valueT * vec, ///< [in] the vector
151  size_t sz ///< [in] the size of the vector
152  )
153 {
154  valueT mean = 0;
155 
156  for(size_t i=0; i< sz; ++i) mean += vec[i];
157 
158  mean /= sz;
159 
160  return mean;
161 }
162 
163 ///Calculate the mean of a vector.
164 /**
165  *
166  * \returns the mean of vec
167  *
168  * \tparam vectorT the std::vector type of vec
169  *
170  */
171 template<typename vectorT>
172 typename vectorT::value_type vectorMean(const vectorT & vec /**< [in] the vector */)
173 {
174  typename vectorT::value_type mean = 0;
175 
176  for(size_t i=0; i< vec.size(); ++i) mean += vec[i];
177 
178  mean/=vec.size();
179 
180  return mean;
181 }
182 
183 ///Calculate the weighted mean of a vector.
184 /**
185  * \returns the weighted mean of vec
186  *
187  * \tparam vectorT the std::vector type of vec and w
188  *
189  */
190 template<typename vectorT>
191 typename vectorT::value_type vectorMean( const vectorT & vec, ///< [in] the vector */
192  const vectorT & w ///< [in] the weights
193  )
194 {
195  typename vectorT::value_type mean = 0, wsum=0;
196 
197  for(size_t i=0; i< vec.size(); ++i)
198  {
199  mean += w[i]*vec[i];
200  wsum += w[i];
201  }
202 
203  mean /= wsum;
204 
205  return mean;
206 }
207 
208 ///Calculate median of a vector in-place, altering the vector.
209 /** Returns the center element if vec has an odd number of elements. Returns the mean of the center 2 elements if vec has
210  * an even number of elements.
211  *
212  * \returns the median of vec
213  *
214  * \tparam vectorT the std::vector type of vec
215  *
216  */
217 template<typename vectorT>
218 typename vectorT::value_type vectorMedianInPlace(vectorT & vec /**< [in] the vector, is altered by std::nth_element*/)
219 {
220  typename vectorT::value_type med;
221 
222  int n = 0.5*vec.size();
223 
224  std::nth_element(vec.begin(), vec.begin()+n, vec.end());
225 
226  med = vec[n];
227 
228  //Average two points if even number of points
229  if(vec.size()%2 == 0)
230  {
231  med = 0.5*(med + *std::max_element(vec.begin(), vec.begin()+n));
232  }
233 
234  return med;
235 }
236 
237 ///Calculate median of a vector, leaving the vector unaltered.
238 /** Returns the center element if vec has an odd number of elements. Returns the mean of the center 2 elements if vec has
239  * an even number of elements.
240  *
241  * \returns the median of vec
242  *
243  * \tparam vectorT the std::vector type of vec
244  *
245  */
246 template<typename vectorT>
247 typename vectorT::value_type vectorMedian( const vectorT & vec, ///< [in] the vector for which the median is desired
248  vectorT * work =0 ///< [in] [optional] an optional vector to use as workspace, use to avoid re-allocation
249  )
250 {
251  typename vectorT::value_type med;
252 
253  bool localWork = false;
254  if(work == 0)
255  {
256  work = new vectorT;
257  localWork = true;
258  }
259 
260  work->resize(vec.size());
261 
262  for(int i=0;i<vec.size();++i)
263  {
264  (*work)[i] = vec[i];
265  }
266 
267  med = vectorMedianInPlace(*work);
268 
269  if(localWork) delete work;
270 
271  return med;
272 }
273 
274 ///Calculate the variance of a vector relative to a supplied mean value.
275 /**
276  * \returns the variance of vec w.r.t. mean
277  *
278  * \tparam valueT the data type
279  *
280  */
281 template<typename valueT>
282 valueT vectorVariance( const valueT * vec, ///< [in] the vector
283  size_t sz, ///< [in] the size of the vector
284  valueT mean ///< [in] the mean value with which to calculate the variance
285  )
286 {
287  valueT var;
288 
289  var = 0;
290  for(size_t i=0; i<sz; ++i) var += pow(vec[i]-mean,2);
291 
292  var /= (sz-1);
293 
294  return var;
295 }
296 
297 ///Calculate the variance of a vector relative to a supplied mean value.
298 /**
299  * \returns the variance of vec w.r.t. mean
300  *
301  * \tparam vectorT the std::vector type of vec
302  *
303  */
304 template<typename vectorT>
305 typename vectorT::value_type vectorVariance( const vectorT & vec, ///< [in] the vector
306  const typename vectorT::value_type & mean ///< [in] the mean value with which to calculate the variance
307  )
308 {
309  typename vectorT::value_type var;
310 
311  var = 0;
312  for(size_t i=0; i< vec.size(); ++i) var += (vec[i]-mean)*(vec[i]-mean);
313 
314  var /= (vec.size()-1);
315 
316  return var;
317 }
318 
319 ///Calculate the variance of a vector.
320 /**
321  * \returns the variance of vec
322  *
323  * \tparam vectorT the std::vector type of vec
324  *
325  */
326 template<typename valueT>
327 valueT vectorVariance( const valueT * vec, ///< [in] the vector
328  size_t sz ///< [in] the size of the vector
329  )
330 {
331  valueT mean;
332  mean = vectorMean(vec, sz);
333 
334  return vectorVariance(vec, sz, mean);
335 }
336 
337 ///Calculate the variance of a vector.
338 /**
339  * \returns the variance of vec
340  *
341  * \tparam vectorT the std::vector type of vec
342  *
343  * \overload
344  */
345 template<typename vectorT>
346 typename vectorT::value_type vectorVariance( const vectorT & vec /**< [in] the vector */)
347 {
348  typename vectorT::value_type mean;
349  mean = vectorMean(vec);
350 
351  return vectorVariance(vec, mean);
352 }
353 
354 
355 ///Calculate the sigma-clipped mean of a vector
356 /** Performas sigma-clipping relative to the median, removing any values with deviation from the median > sigma.
357  * Continues until either no values are removed, or maxPasses iterations. If maxPasses == 0, then it is ignored.
358  *
359  * \returns the sigma clipped mean of vec
360  *
361  * \tparam vectorT the std::vector type of vec
362  *
363  */
364 template<typename vectorT>
365 typename vectorT::value_type vectorSigmaMean( const vectorT & vec, ///< [in] the vector (unaltered)
366  const vectorT * weights, ///< [in] [optional] the weights (unaltered)
367  typename vectorT::value_type sigma, ///< [in] the standard deviation threshold to apply.
368  int & maxPasses ///< [in.out] [optional] the maximum number of sigma-clipping passes. Set to actual number of passes on return.
369  )
370 {
371  vectorT work, wwork;
372 
373  typename vectorT::value_type med, var, Vsig, dev;
374 
375 
376  bool doWeight = false;
377  if( weights )
378  {
379  if( weights->size() == vec.size()) doWeight = true;
380  }
381 
382  Vsig = sigma*sigma;
383 
384  med = vectorMedian(vec, &work);
385  var = vectorVariance( work, med );
386 
387  int nclip;
388  int passes = 0;
389 
390 
391  //If weighting, have to synchronize work with weights since work will be
392  //partially sorted by median.
393  if(doWeight)
394  {
395  wwork.resize(vec.size());
396  for(int i=0; i< vec.size(); ++i)
397  {
398  work[i] = vec[i];
399  wwork[i] = (*weights)[i];
400  }
401  }
402 
403 
404  while(passes < maxPasses || maxPasses == 0)
405  {
406  ++passes;
407 
408  nclip = 0;
409 
410  for(size_t i=0; i<work.size(); ++i)
411  {
412  dev = pow(work[i] - med,2)/var;
413  if( dev > Vsig )
414  {
415  work.erase(work.begin()+i);
416 
417  if(doWeight) wwork.erase(wwork.begin() + i);
418 
419  --i;
420  ++nclip;
421  }
422  }
423 
424  if(nclip == 0) break;
425  med = vectorMedian(work);
426  var = vectorVariance( work, med );
427  }
428 
429  maxPasses = passes;
430 
431  if(doWeight)
432  {
433  return vectorMean(work, wwork);
434  }
435  else
436  {
437  return vectorMean(work);
438  }
439 }
440 
441 
442 /**
443  * \overload
444  */
445 template<typename vectorT>
446 typename vectorT::value_type vectorSigmaMean( const vectorT & vec, ///< [in] the vector (unaltered)
447  typename vectorT::value_type sigma ///< [in] the standard deviation threshold to apply.
448  )
449 {
450  int maxPasses = 0;
451 
452  return vectorSigmaMean(vec, (vectorT *) 0, sigma, maxPasses);
453 }
454 
455 /**
456  * \overload
457  */
458 template<typename vectorT>
459 typename vectorT::value_type vectorSigmaMean( const vectorT & vec, ///< [in] the vector (unaltered)
460  const vectorT & weights, ///< [in] [optional] the weights (unaltered)
461  typename vectorT::value_type sigma, ///< [in] the standard deviation threshold to apply.
462  int & maxPasses ///< [in.out] [optional] the maximum number of sigma-clipping passes. Set to actual number of passes on return.
463  )
464 {
465 
466  return vectorSigmaMean(vec, &weights, sigma, maxPasses);
467 }
468 
469 /**
470  * \overload
471  */
472 template<typename vectorT>
473 typename vectorT::value_type vectorSigmaMean( const vectorT & vec, ///< [in] the vector (unaltered)
474  const vectorT & weights, ///< [in] [optional] the weights (unaltered)
475  typename vectorT::value_type sigma ///< [in] the standard deviation threshold to apply.
476  )
477 {
478  int maxPasses = 0;
479 
480  return vectorSigmaMean(vec, &weights, sigma, maxPasses);
481 }
482 
483 
484 /// Subtract a constant value from a vector
485 template<typename valueT, typename constT>
486 void vectorSub( valueT *vec, ///< [in.out] the vector, each element will have the constant subtracted from it
487  size_t sz, ///< [in] the size of the vector
488  const constT & c ///< [in] the constant to subtract from each element
489  )
490 {
491  for(size_t n=0; n< sz;++n) vec[n] -= c;
492 }
493 
494 /// Subtract a constant value from a vector
495 template<typename vecT, typename constT>
496 void vectorSub( vecT &vec, ///< [in.out] the vector, each element will have the constant subtracted from it
497  const constT & c ///< [in] the constant to subtract from each element
498  )
499 {
500  vectorSub(vec.data(), vec.size(), c);
501 }
502 
503 /// Subtract the mean from a vector
504 template<typename valueT>
505 void vectorMeanSub( valueT *vec, ///< [in.out] the vector, each element will have the mean subtracted from it
506  size_t sz ///< [in] the vector size
507  )
508 {
509  valueT m = vectorMean(vec,sz);
510  vectorSub(vec,sz,m);
511 }
512 
513 /// Subtract the mean from a vector
514 template<typename vecT>
515 void vectorMeanSub( vecT &vec /**< [in.out] the vector, each element will have the mean subtracted from it*/)
516 {
517  vectorMeanSub( vec.data(), vec.size());
518 }
519 
520 /// Subtract the median from a vector
521 template<typename vecT>
522 void vectorMedianSub( vecT &vec /**< [in.out] the vector, each element will have the median subtracted from it*/)
523 {
524  typename vecT::value_type m = vectorMedian(vec);
525  vectorSub(vec,m);
526 }
527 
528 ///Smooth a vector using the mean in a window specified by its full-width
529 template<typename realT>
530 int vectorSmoothMean( realT * smVec, ///< [out] the smoothed version of the vector. At least as large as \p vec.
531  realT * vec, ///< [in] the input vector, unaltered.
532  size_t vecSize, ///< [in] the size of \p vec
533  int win ///< [in] the full-width of the smoothing window. Should be even. 0 results in a slow memcpy.
534  )
535 {
536  realT sum;
537  int n;
538  for(int i=0; i < vecSize; ++i)
539  {
540  int j = i - 0.5*win;
541  if(j < 0) j = 0;
542 
543  sum = 0;
544  n = 0;
545  while( j <= i+0.5*win && j < vecSize)
546  {
547  sum += vec[j];
548  ++n;
549  ++j;
550  }
551 
552  smVec[i] = sum/n;
553  }
554 
555  return 0;
556 }
557 
558 ///Smooth a vector using the mean in a window specified by its full-width
559 /** \overload
560  */
561 template<typename realT>
562 int vectorSmoothMean( std::vector<realT> &smVec, ///< [out] the smoothed version of the vector. Will be resize()-ed.
563  std::vector<realT> & vec, ///< [in] the input vector, unaltered.
564  int win ///< [in] the full-width of the smoothing window. Should be even. 0 results in a slow memcpy.
565  )
566 {
567  smVec.resize(vec.size());
568  return vectorSmoothMean(smVec.data(), vec.data(), vec.size(), win);
569 }
570 
571 /// Smooth a vector using the mean in windows specified by their full-widths
572 /** You supply a window width for each point. This is useful for, say, logarithmically growing bin sizes in a
573  * PSD.
574  *
575  * \returns 0 on success
576  * \returns -1 but who are we kidding it we don't check for errors
577  *
578  */
579 template<typename realT>
580 int vectorSmoothMean( std::vector<realT> &smVec, ///< [out] the smoothed version of the vector
581  std::vector<realT> & vec, ///< [in] the input vector, unaltered.
582  std::vector<int> & wins, ///< [in] the full-widths of the smoothing windows, same size as vec.
583  bool norm = false ///< [in] if true the output will normalized to have the same integral as the input.
584  )
585 {
586  smVec = vec;
587 
588  realT sum;
589  int n;
590  for(int i=0; i < vec.size(); ++i)
591  {
592  int j = i - 0.5*wins[i];
593  if(j < 0) j = 0;
594 
595  sum = 0;
596  n = 0;
597  while( j <= i+0.5*wins[i] && j < vec.size())
598  {
599  sum += vec[j];
600  ++n;
601  ++j;
602  }
603 
604  smVec[i] = sum/n;
605 
606  }
607 
608  if(norm)
609  {
610  realT sumin = 0, sumout = 0;
611 
612  for(int i=0; i < vec.size(); ++i)
613  {
614  sumin += vec[i];
615  sumout += smVec[i];
616  }
617 
618  for(int i=0; i < vec.size(); ++i)
619  {
620  smVec[i] *= sumin/sumout;
621  }
622  }
623 
624  return 0;
625 }
626 
627 ///Smooth a vector using the median in a window specified by its full-width
628 template<typename realT>
629 int vectorSmoothMedian( std::vector<realT> &smVec, ///< [out] the smoothed version of the vector
630  std::vector<realT> & vec, ///< [in] the input vector, unaltered.
631  int win ///< [in] the full-width of the smoothing window
632  )
633 {
634  smVec = vec;
635 
636  std::vector<realT> tvec;
637  int n;
638  for(int i=0; i < vec.size(); ++i)
639  {
640  int j = i - 0.5*win;
641  if(j < 0) j = 0;
642 
643  tvec.clear();
644  while( j <= i+0.5*win && j < vec.size())
645  {
646  tvec.push_back(vec[j]);
647  ++j;
648  }
649 
650  smVec[i] = vectorMedianInPlace(tvec);
651 
652  }
653 
654  return 0;
655 }
656 
657 ///Smooth a vector using the max in a window specified by its full-width
658 template<typename realT>
659 int vectorSmoothMax( std::vector<realT> &smVec, ///< [out] the smoothed version of the vector
660  std::vector<realT> & vec, ///< [in] the input vector, unaltered.
661  int win ///< [in] the full-width of the smoothing window
662  )
663 {
664  smVec = vec;
665 
666  for(int i=0; i < vec.size(); ++i)
667  {
668  int j = i - 0.5*win;
669  if(j < 0) j = 0;
670 
671  smVec[i] = vec[j];
672  ++j;
673  while( j <= i+0.5*win && j < vec.size())
674  {
675  if(vec[j] > smVec[i]) smVec[i] = vec[j];
676  ++j;
677  }
678  }
679 
680  return 0;
681 }
682 
683 /// Re-bin a vector by summing (or averaging) in bins of size n points.
684 /**
685  * \returns 0 on success
686  * \returns -1 on error
687  *
688  * \tparam vectorT is any vector-like type with resize(), size(), and the operator()[].
689  */
690 template<typename vectorT>
691 int vectorRebin( vectorT & binv, ///< [out] the re-binned vector. will be resized.
692  const vectorT & v, ///< [in] the vector to bin.
693  unsigned n, ///< [in] the size of the bins, in points
694  bool binMean = false ///< [in] [optional] flag controlling whether sums (false) or means (true) are calculated.
695  )
696 {
697  if( n==0 ) return -1;
698 
699  binv.resize( v.size()/n );
700 
701  for(size_t i=0; i< binv.size(); ++i)
702  {
703  binv[i] = 0;
704 
705  unsigned j;
706  for(j=0; j < n; ++j)
707  {
708  if(i*n + j >= v.size()) break;
709 
710  binv[i] += v[ i*n + j];
711  }
712 
713  if(binMean)
714  {
715  if(j > 0) binv[i] /= j;
716  }
717  }
718 
719  return 0;
720 }
721 
722 /// Calculate and accumulate the means of a timeseries in bins of various sizes.
723 /** Useful mainly to calculate the variance of the mean as a function of sample size.
724  * The output is a vector of vectors, where each element is a vector which contains the means in the
725  * unique bins of size corresponding to the same index in the in put binSzs vector.
726  *
727  * \returns 0 on success.
728  */
729 template<typename vectorT, typename binVectorT>
730 int vectorBinMeans( std::vector<vectorT> & means, ///< [out] the means in each distinct bin. Not cleared, but Will be resized with new means appended.
731  binVectorT & binSzs, ///< [in] the bin sizes in which to calculate the means
732  const vectorT & v ///< [in] the input vector to bin .
733  )
734 {
735  vectorT binv;
736 
737  means.resize(binSzs.size());
738 
739  for(size_t i=0; i< binSzs.size(); ++i)
740  {
741  vectorRebin(binv, v, binSzs[i], true);
742 
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];
745  }
746 
747  return 0;
748 }
749 
750 /// Convolve (smooth) a vector with a Gaussian.
751 /**
752  * \returns 0 on success.
753  * \returns -1 on error.
754  *
755  * \tparam realT is the real floating point type of the data.
756  */
757 template<typename realT>
758 int vectorGaussConvolve( std::vector<realT> & dataOut, ///< [out] The smoothed data vector. Resized.
759  const std::vector<realT> & dataIn, ///< [in] The data vector to smooth.
760  const std::vector<realT> & scale, ///< [in] The scale vector used to calculate the kernel.
761  const realT fwhm, ///< [in] The FWHM of the Gaussian kernel, same units as scale.
762  const int winhw ///< [in] The window half-width in pixels.
763  )
764 {
765 
766  if(dataIn.size() != scale.size()) return -1;
767 
768  dataOut.resize(dataIn.size());
769 
770  realT sigma = func::fwhm2sigma<realT>(fwhm);
771 
772  for(int i=0; i< dataIn.size(); ++i)
773  {
774  realT G;
775  realT sum = 0;
776  realT norm = 0;
777  for(int j=i-winhw; j<i+winhw; ++j)
778  {
779  if(j < 0) continue;
780  if(j > dataIn.size()-1) continue;
781 
782  G = func::gaussian<realT>( scale[j], 0.0, 1.0, scale[i], sigma);
783 
784  sum += G*dataIn[j];
785  norm += G;
786  }
787 
788  dataOut[i] = sum/norm;
789  }
790 
791  return 0;
792 }
793 
794 
795 
796 
797 /// Calculate a cumulative histogram of a vector.
798 /** Sorts the vector and sums.
799  *
800  * \retval 0 on success, -1 otherwise.
801  *
802  * \tparam floatT the floating point type of the vector contens.
803  */
804 template<typename floatT>
805 int vectorCumHist( std::vector<floatT> & svec, ///< [out] Contains the sorted vector.
806  std::vector<floatT> & sum, ///< [out] Contains the cumulative or running sum of the sorted vector
807  std::vector<floatT> & vec ///< [in] The vector to sort and sum.
808  )
809 {
810  svec = vec;
811 
812  std::sort(svec.begin(), svec.end());
813 
814  sum.resize(svec.size());
815 
816  sum[0] = svec[0];
817 
818  for(int i=1; i< svec.size(); ++i)
819  {
820  sum[i] = sum[i-1] + svec[i];
821  }
822 
823  return 0;
824 }
825 
826 /// Calculate a reverse cumulative histogram of a vector.
827 /** Reverse-sorts the vector and sums.
828  *
829  * \retval 0 on success, -1 otherwise.
830  *
831  * \tparam floatT the floating point type of the vector contens.
832  */
833 template<typename floatT>
834 int vectorCumHistReverse( std::vector<floatT> & svec, ///< [out] Contains the reverse-sorted vector.
835  std::vector<floatT> & sum, ///< [out] Contains the cumulative or running sum of the reverse-sorted vector
836  std::vector<floatT> & vec ///< [in] The vector to reverse-sort and sum.
837  )
838 {
839  svec = vec;
840 
841  std::sort(svec.begin(), svec.end(), std::greater<floatT>());
842 
843  sum.resize(svec.size());
844 
845  sum[0] = svec[0];
846 
847  for(int i=1; i< svec.size(); ++i)
848  {
849  sum[i] = sum[i-1] + svec[i];
850 
851  }
852 
853  return 0;
854 }
855 
856 ///@}
857 
858 } //namespace math
859 } //namespace mx
860 
861 
862 
863 #endif // math_vectorUtils_hpp
Declarations for utilities related to the Gaussian function.
constexpr units::realT G()
Newton's Gravitational Constant.
Definition: constants.hpp:51
constexpr units::realT c()
The speed of light.
Definition: constants.hpp:60
constexpr units::realT sigma()
Stefan-Boltzmann Constant.
Definition: constants.hpp:82
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.
Definition: vectorUtils.hpp:62
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.
Definition: vectorUtils.hpp:92
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)
The mxlib c++ namespace.
Definition: mxError.hpp:107