bpp-core  2.2.0
VectorTools.h
Go to the documentation of this file.
1 //
2 // File: VectorTools.h
3 // Created by: Julien Dutheil
4 // Created on: Fri Mar 14 14:16:32 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for numerical calculus.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #ifndef _VECTORTOOLS_H_
41 #define _VECTORTOOLS_H_
42 
43 #include "VectorExceptions.h"
44 #include "NumTools.h"
46 #include "Matrix/Matrix.h"
47 #include "../Io/OutputStream.h"
48 #include "../App/ApplicationTools.h"
49 
50 // From the STL:
51 #include <vector>
52 #include <map>
53 #include <cmath>
54 #include <algorithm>
55 #include <complex>
56 
57 namespace bpp
58 {
59 typedef std::vector<std::complex<double> > Vcomplex;
60 typedef std::vector<Vcomplex> VVcomplex;
61 typedef std::vector<VVcomplex> VVVcomplex;
62 
63 typedef std::vector<std::complex<long double> > Vlcomplex;
64 typedef std::vector<Vlcomplex> VVlcomplex;
65 typedef std::vector<VVlcomplex> VVVlcomplex;
66 
67 typedef std::vector<double> Vdouble;
68 typedef std::vector<Vdouble> VVdouble;
69 typedef std::vector<VVdouble> VVVdouble;
70 typedef std::vector<VVVdouble> VVVVdouble;
71 
72 typedef std::vector<long double> Vldouble;
73 typedef std::vector<Vldouble> VVldouble;
74 typedef std::vector<VVldouble> VVVldouble;
75 typedef std::vector<VVVldouble> VVVVldouble;
76 
77 typedef std::vector<int> Vint;
78 typedef std::vector<Vint> VVint;
79 typedef std::vector<VVint> VVVint;
80 typedef std::vector<VVVint> VVVVint;
81 
87 template<class T>
88 std::vector<T> operator+(const std::vector<T>& v1, const std::vector<T>& v2) throw (DimensionException)
89 {
90  size_t size;
91  if (v1.size() != v2.size())
92  {
93  throw DimensionException("VectorTools::operator+", v1.size(), v2.size());
94  }
95  else
96  {
97  size = v1.size();
98  }
99  std::vector<T> result(size);
100  for (size_t i = 0; i < size; i++)
101  {
102  result[i] = v1[i] + v2[i];
103  }
104  return result;
105 }
106 
107 template<class T>
108 std::vector<T> operator-(const std::vector<T>& v1, const std::vector<T>& v2) throw (DimensionException)
109 {
110  size_t size;
111  if (v1.size() != v2.size())
112  {
113  throw DimensionException("VectorTools::operator-", v1.size(), v2.size());
114  }
115  else
116  {
117  size = v1.size();
118  }
119  std::vector<T> result(size);
120  for (size_t i = 0; i < size; i++)
121  {
122  result[i] = v1[i] - v2[i];
123  }
124  return result;
125 }
126 
127 template<class T>
128 std::vector<T> operator*(const std::vector<T>& v1, const std::vector<T>& v2) throw (DimensionException)
129 {
130  size_t size;
131  if (v1.size() != v2.size())
132  {
133  throw DimensionException("VectorTools::operator*", v1.size(), v2.size());
134  }
135  else
136  {
137  size = v1.size();
138  }
139  std::vector<T> result(size);
140  for (size_t i = 0; i < size; i++)
141  {
142  result[i] = v1[i] * v2[i];
143  }
144  return result;
145 }
146 
147 template<class T>
148 std::vector<T> operator/(const std::vector<T>& v1, const std::vector<T>& v2) throw (DimensionException)
149 {
150  size_t size;
151  if (v1.size() != v2.size())
152  {
153  throw DimensionException("VectorTools::operator/", v1.size(), v2.size());
154  }
155  else
156  {
157  size = v1.size();
158  }
159  std::vector<T> result(size);
160  for (size_t i = 0; i < size; i++)
161  {
162  result[i] = v1[i] / v2[i];
163  }
164  return result;
165 }
166 
167 
168 template<class T, class C>
169 std::vector<T> operator+(const std::vector<T>& v1, const C& c)
170 {
171  std::vector<T> result(v1.size());
172  for (size_t i = 0; i < result.size(); i++)
173  {
174  result[i] = v1[i] + c;
175  }
176  return result;
177 }
178 template<class T, class C>
179 std::vector<T> operator+(const C& c, const std::vector<T>& v1)
180 {
181  std::vector<T> result(v1.size());
182  for (size_t i = 0; i < result.size(); i++)
183  {
184  result[i] = c + v1[i];
185  }
186  return result;
187 }
188 
189 template<class T, class C>
190 std::vector<T> operator-(const std::vector<T>& v1, const C& c)
191 {
192  std::vector<T> result(v1.size());
193  for (size_t i = 0; i < result.size(); i++)
194  {
195  result[i] = v1[i] - c;
196  }
197  return result;
198 }
199 template<class T, class C>
200 std::vector<T> operator-(const C& c, const std::vector<T>& v1)
201 {
202  std::vector<T> result(v1.size());
203  for (size_t i = 0; i < result.size(); i++)
204  {
205  result[i] = c - v1[i];
206  }
207  return result;
208 }
209 
210 template<class T, class C>
211 std::vector<T> operator*(const std::vector<T>& v1, const C& c)
212 {
213  std::vector<T> result(v1.size());
214  for (size_t i = 0; i < result.size(); i++)
215  {
216  result[i] = v1[i] * c;
217  }
218  return result;
219 }
220 template<class T, class C>
221 std::vector<T> operator*(const C& c, const std::vector<T>& v1)
222 {
223  std::vector<T> result(v1.size());
224  for (size_t i = 0; i < result.size(); i++)
225  {
226  result[i] = c * v1[i];
227  }
228  return result;
229 }
230 
231 template<class T, class C>
232 std::vector<T> operator/(const std::vector<T>& v1, const C& c)
233 {
234  std::vector<T> result(v1.size());
235  for (size_t i = 0; i < result.size(); i++)
236  {
237  result[i] = v1[i] / c;
238  }
239  return result;
240 }
241 template<class T, class C>
242 std::vector<T> operator/(const C& c, const std::vector<T>& v1)
243 {
244  std::vector<T> result(v1.size());
245  for (size_t i = 0; i < result.size(); i++)
246  {
247  result[i] = c / v1[i];
248  }
249  return result;
250 }
251 
252 
253 template<class T>
254 void operator+=(std::vector<T>& v1, const std::vector<T>& v2) throw (DimensionException)
255 {
256  for (size_t i = 0; i < v1.size(); i++)
257  {
258  v1[i] += v2[i];
259  }
260 }
261 
262 template<class T>
263 void operator-=(std::vector<T>& v1, const std::vector<T>& v2) throw (DimensionException)
264 {
265  for (size_t i = 0; i < v1.size(); i++)
266  {
267  v1[i] -= v2[i];
268  }
269 }
270 
271 template<class T>
272 void operator*=(std::vector<T>& v1, const std::vector<T>& v2) throw (DimensionException)
273 {
274  for (size_t i = 0; i < v1.size(); i++)
275  {
276  v1[i] *= v2[i];
277  }
278 }
279 
280 template<class T>
281 void operator/=(std::vector<T>& v1, const std::vector<T>& v2) throw (DimensionException)
282 {
283  for (size_t i = 0; i < v1.size(); i++)
284  {
285  v1[i] /= v2[i];
286  }
287 }
288 
289 
290 template<class T, class C>
291 void operator+=(std::vector<T>& v1, const C& c)
292 {
293  for (size_t i = 0; i < v1.size(); i++)
294  {
295  v1[i] += c;
296  }
297 }
298 
299 template<class T, class C>
300 void operator-=(std::vector<T>& v1, const C& c)
301 {
302  for (size_t i = 0; i < v1.size(); i++)
303  {
304  v1[i] -= c;
305  }
306 }
307 
308 template<class T, class C>
309 void operator*=(std::vector<T>& v1, const C& c)
310 {
311  for (size_t i = 0; i < v1.size(); i++)
312  {
313  v1[i] *= c;
314  }
315 }
316 
317 template<class T, class C>
318 void operator/=(std::vector<T>& v1, const C& c)
319 {
320  for (size_t i = 0; i < v1.size(); i++)
321  {
322  v1[i] /= c;
323  }
324 }
327 /******************************************************************************/
328 
330 {
331 public:
333  virtual ~VectorTools() {}
334 
335 public:
341  template<class T>
342  static void resize2(VVdouble& vv, size_t n1, size_t n2)
343  {
344  vv.resize(n1);
345  for (size_t i = 0; i < n1; i++) { vv[i].resize(n2); }
346  }
347 
348  template<class T>
349  static void resize3(VVVdouble& vvv, size_t n1, size_t n2, size_t n3)
350  {
351  vvv.resize(n1);
352  for (size_t i = 0; i < n1; i++)
353  {
354  vvv[i].resize(n2);
355  for (size_t j = 0; j < n2; j++)
356  {
357  vvv[i][j].resize(n3);
358  }
359  }
360  }
361 
362  static void resize4(VVVVdouble& vvvv, size_t n1, size_t n2, size_t n3, size_t n4)
363  {
364  vvvv.resize(n1);
365  for (size_t i = 0; i < n1; i++)
366  {
367  vvvv[i].resize(n2);
368  for (size_t j = 0; j < n2; j++)
369  {
370  vvvv[i][j].resize(n3);
371  for (size_t k = 0; k < n3; k++)
372  {
373  vvvv[i][j][k].resize(n4);
374  }
375  }
376  }
377  }
380  template<class T>
381  static void fill(std::vector<T>& v, T value)
382  {
383  for (typename std::vector<T>::iterator it = v.begin(); it < v.end(); it++)
384  {
385  *it = value;
386  }
387  }
388 
401  template<class T>
402  static std::vector<T> seq(T from, T to, T by)
403  {
404  std::vector<T> v;
405  if (from < to)
406  {
407  // for (T i = from ; i <= to ; i += by) { // Not good for double, 'to'
408  for (T i = from; i <= to + (by / 100); i += by)
409  { // must be a little bit larger
410  v.push_back(i);
411  }
412  }
413  else
414  {
415  for (T i = from; i >= to - (by / 100); i -= by)
416  {
417  v.push_back(i);
418  }
419  }
420  return v;
421  }
422 
433  template<class T>
434  static size_t which(const std::vector<T>& v, const T& which) throw (ElementNotFoundException<T> )
435  {
436  for (size_t i = 0; i < v.size(); i++)
437  {
438  if (v[i] == which) return i;
439  }
440  throw ElementNotFoundException<T>("VectorTools::which.", &v, &which);
441  }
442 
453  template<class T>
454  static std::vector<size_t> whichAll(const std::vector<T>& v, const T& which) throw (ElementNotFoundException<T> )
455  {
456  std::vector<size_t> w;
457  for (size_t i = 0; i < v.size(); i++)
458  {
459  if (v[i] == which) w.push_back(i);
460  }
461  if (w.size())
462  return w;
463  throw ElementNotFoundException<T>("VectorTools::whichAll.", &v, &which);
464  }
465 
477  template<class T>
478  static std::vector<T> unique(const std::vector<T>& v)
479  {
480  if (v.size() == 0) return v;
481  std::vector<T> sortedV(v.begin(), v.end());
482  sort(sortedV.begin(), sortedV.end());
483  std::vector<T> uniq;
484  uniq.push_back(sortedV[0]);
485  for (size_t i = 1; i < sortedV.size(); i++)
486  {
487  if (sortedV[i] != sortedV[i - 1]) uniq.push_back(sortedV[i]);
488  }
489  return uniq;
490  }
491 
502  template<class T>
503  static bool isUnique(const std::vector<T>& v)
504  {
505  if (v.size() == 0) return true;
506  std::vector<T> sortedV(v.begin(), v.end());
507  sort(sortedV.begin(), sortedV.end());
508  for (size_t i = 1; i < sortedV.size(); i++)
509  {
510  if (sortedV[i] == sortedV[i - 1]) return false;
511  }
512  return true;
513  }
514 
522  template<class T>
523  static std::vector<T> extract(const std::vector<T>& v1, const std::vector<int>& v2)
524  {
525  std::vector<T> v(v2.size());
526  for (size_t i = 0; i < v2.size(); i++)
527  {
528  v[i] = v1[v2[i]];
529  }
530  return v;
531  }
532 
539  template<class T>
540  static std::map<T, size_t> countValues(const std::vector<T>& v)
541  {
542  std::map<T, size_t> c;
543  for (size_t i = 0; i < v.size(); i++)
544  {
545  c[v[i]]++;
546  }
547  return c;
548  }
549 
560  static std::vector<double> breaks(const std::vector<double>& v, unsigned int n);
561 
572  template<class T>
573  static size_t nclassScott(const std::vector<T>& v)
574  {
575  std::vector<T> r1 = VectorTools::range(v);
576  T r = r1[1] - r1[0];
577  double n = v.size();
578  double h = 3.5 * VectorTools::sd<T, double>(v) * std::pow(n, -1. / 3);
579  return (size_t) ceil(r / h);
580  }
581 
586  template<class T>
587  static T prod(const std::vector<T>& v1)
588  {
589  T p = 1;
590  for (size_t i = 0; i < v1.size(); i++) { p *= v1[i]; }
591  return p;
592  }
593 
599  template<class T>
600  static std::vector<T> cumProd(const std::vector<T>& v1)
601  {
602  std::vector<T> p(v1.size());
603  if (v1.size() == 0) return p;
604  p[0] = v1[0];
605  for (size_t i = 1; i < v1.size(); i++) { p[i] = v1[i] * p[i - 1]; }
606  return p;
607  }
608 
613  template<class T>
614  static T sum(const std::vector<T>& v1)
615  {
616  T s = 0;
617  for (size_t i = 0; i < v1.size(); i++) { s += v1[i]; }
618  return s;
619  }
620 
626  template<class T>
627  static std::vector<T> cumSum(const std::vector<T>& v1)
628  {
629  std::vector<T> s(v1.size());
630  if (v1.size() == 0) return s;
631  s[0] = v1[0];
632  for (size_t i = 1; i < v1.size(); i++) { s[i] = v1[i] + s[i - 1]; }
633  return s;
634  }
635 
642  template<class T>
643  static void logNorm(std::vector<T>& v)
644  {
645  T M = max(v);
646  T x = std::exp(v[0] - M);
647  for (size_t i = 1; i < v.size(); i++)
648  {
649  x += std::exp(v[i] - M);
650  }
651  v -= M + std::log(x);
652  }
653 
659  template<class T>
660  static T logSumExp(const std::vector<T>& v1)
661  {
662  T M = max(v1);
663  T x = std::exp(v1[0] - M);
664  for (size_t i = 1; i < v1.size(); i++)
665  {
666  x += std::exp(v1[i] - M);
667  }
668  return std::log(x) + M;
669  }
670 
677  template<class T>
678  static T logSumExp(const std::vector<T>& v1, const std::vector<T>& v2)
679  {
680  size_t size;
681  if (v1.size() != v2.size())
682  throw DimensionException("VectorTools::logsumexp", v1.size(), v2.size());
683  else
684  size = v1.size();
685 
686  T M = max(v1);
687  T x = v2[0] * std::exp(v1[0] - M);
688  for (size_t i = 1; i < size; i++)
689  {
690  x += v2[i] * std::exp(v1[i] - M);
691  }
692  return std::log(x) + M;
693  }
694 
700  template<class T>
701  static T logMeanExp(const std::vector<T>& v1)
702  {
703  T M = max(v1);
704  T x = std::exp(v1[0] - M);
705  for (size_t i = 1; i < v1.size(); i++)
706  {
707  x += std::exp(v1[i] - M);
708  }
709  return std::log(x) + M - std::log(v1.size());
710  }
711 
712 
718  template<class T>
719  static T sumExp(const std::vector<T>& v1)
720  {
721  T M = max(v1);
722  T x = std::exp(v1[0] - M);
723  for (size_t i = 1; i < v1.size(); i++)
724  {
725  x += std::exp(v1[i] - M);
726  }
727  return x * std::exp(M);
728  }
729 
736  template<class T>
737  static T sumExp(const std::vector<T>& v1, const std::vector<T>& v2)
738  {
739  size_t size;
740  if (v1.size() != v2.size())
741  throw DimensionException("VectorTools::logsumexp", v1.size(), v2.size());
742  else
743  size = v1.size();
744 
745  T M = max(v1);
746  T x = v2[0] * std::exp(v1[0] - M);
747  for (size_t i = 1; i < size; i++)
748  {
749  x += v2[i] * std::exp(v1[i] - M);
750  }
751  return x * std::exp(M);
752  }
753 
760  template<class T>
761  static std::vector<double> log(const std::vector<T>& v1)
762  {
763  std::vector<double> v2(v1.size());
764  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::log(v1[i]); }
765  return v2;
766  }
767  template<class T>
768  static std::vector<double> log(const std::vector<T>& v1, double base)
769  {
770  std::vector<double> v2(v1.size());
771  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::log(v1[i]) / std::log(base); }
772  return v2;
773  }
774 
775  template<class T>
776  static std::vector<double> exp(const std::vector<T>& v1)
777  {
778  std::vector<double> v2(v1.size());
779  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::exp(v1[i]); }
780  return v2;
781  }
782 
783  template<class T>
784  static std::vector<double> cos(const std::vector<T>& v1)
785  {
786  std::vector<double> v2(v1.size());
787  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::cos(v1[i]); }
788  return v2;
789  }
790 
791  template<class T>
792  static std::vector<double> sin(const std::vector<T>& v1)
793  {
794  std::vector<double> v2(v1.size());
795  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::sin(v1[i]); }
796  return v2;
797  }
798 
799  template<class T>
800  static std::vector<double> log10(const std::vector<T>& v1)
801  {
802  std::vector<double> v2(v1.size());
803  for (size_t i = 0; i < v1.size(); i++) { v2[i] = std::log10(v1[i]); }
804  return v2;
805  }
806 
807  template<class T>
808  static std::vector<T> fact(const std::vector<T>& v1)
809  {
810  std::vector<T> v2(v1.size());
811  for (size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::fact<T>(v1[i]); }
812  return v2;
813  }
814 
815  template<class T>
816  static std::vector<T> sqr(const std::vector<T>& v1)
817  {
818  std::vector<T> v2(v1.size());
819  for (size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::sqr<T>(v1[i]); }
820  return v2;
821  }
822 
823  template<class T>
824  static std::vector<T> pow(const std::vector<T>& v1, T& b)
825  {
826  std::vector<T> v2(v1.size());
827  for (size_t i = 0; i < v1.size(); i++) { v2[i] = std::pow(v1[i], b); }
828  return v2;
829  }
838  template<class T>
839  static std::string paste(const std::vector<T>& v, const std::string& delim = " ")
840  {
841  std::ostringstream out;
842  out.precision(12);
843  for (size_t i = 0; i < v.size(); i++)
844  {
845  out << v[i];
846  if (i < v.size() - 1)
847  out << delim;
848  }
849  return out.str();
850  }
851 
858  template<class T>
859  static void print(const std::vector<T>& v1, OutputStream& out = * ApplicationTools::message, const std::string& delim = " ")
860  {
861  for (size_t i = 0; i < v1.size(); i++)
862  {
863  out << v1[i];
864  if (i < v1.size() - 1)
865  out << delim;
866  }
867  out.endLine();
868  }
869 
876  template<class T>
877  static void printForR(const std::vector<T>& v1, std::string variableName = "x", std::ostream& out = std::cout)
878  {
879  out.precision(12);
880  out << variableName << "<-c(";
881  for (size_t i = 0; i < v1.size(); i++)
882  {
883  out << v1[i];
884  if (i < v1.size() - 1)
885  out << ", ";
886  }
887  out << ")" << std::endl;
888  }
889 
896  template<class InputType, class OutputType>
897  static OutputType scalar(const std::vector<InputType>& v1, const std::vector<InputType>& v2) throw (DimensionException)
898  {
899  if (v1.size() != v2.size())
900  {
901  throw DimensionException("VectorFunctions::scalar", v1.size(), v2.size());
902  }
903  OutputType result = 0;
904  for (size_t i = 0; i < v1.size(); i++)
905  {
906  result += v1[i] * v2[i];
907  }
908  return result;
909  }
926  template<class InputType, class OutputType>
927  static OutputType scalar(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w) throw (DimensionException)
928  {
929  if (v1.size() != w.size())
930  {
931  throw DimensionException("VectorFunctions::scalar", v1.size(), w.size());
932  }
933  if (v2.size() != w.size())
934  {
935  throw DimensionException("VectorFunctions::scalar", v2.size(), w.size());
936  }
937  OutputType result = 0;
938  for (size_t i = 0; i < v1.size(); i++)
939  {
940  result += v1[i] * v2[i] * w[i];
941  }
942  return result;
943  }
944 
951  template<class T>
952  static std::vector<T> kroneckerMult(const std::vector<T>& v1, const std::vector<T>& v2) throw (DimensionException)
953  {
954  size_t n1 = v1.size();
955  size_t n2 = v2.size();
956  std::vector<T> v3(n1 * n2);
957  for (size_t i = 0; i < n1; i++)
958  {
959  T v1i = v1[i];
960  for (size_t j = 0; j < n2; j++)
961  {
962  v3[i * n2 + j] = v1i * v2[j];
963  }
964  }
965  return v3;
966  }
967 
972  template<class InputType, class OutputType>
973  static OutputType norm(const std::vector<InputType>& v1)
974  {
975  OutputType result = 0;
976  for (size_t i = 0; i < v1.size(); i++)
977  {
978  result += v1[i] * v1[i];
979  }
980  return sqrt(result);
981  }
982 
990  template<class InputType, class OutputType>
991  static OutputType norm(const std::vector<InputType>& v1, const std::vector<InputType>& w) throw (DimensionException)
992  {
993  if (v1.size() != w.size())
994  {
995  throw DimensionException("VectorFunctions::norm", v1.size(), w.size());
996  }
997  OutputType result = 0;
998  for (size_t i = 0; i < v1.size(); i++)
999  {
1000  result += v1[i] * v1[i] * w[i];
1001  }
1002  return sqrt(result);
1003  }
1004 
1011  template<class InputType, class OutputType>
1012  static OutputType cos(const std::vector<InputType>& v1, const std::vector<InputType>& v2) throw (DimensionException)
1013  {
1014  return scalar<InputType, OutputType>(v1, v2)
1015  / (norm<InputType, OutputType>(v1) * norm<InputType, OutputType>(v2));
1016  }
1017 
1025  template<class InputType, class OutputType>
1026  static OutputType cos(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w) throw (DimensionException)
1027  {
1028  return scalar<InputType, OutputType>(v1, v2, w)
1029  / (norm<InputType, OutputType>(v1, w) * norm<InputType, OutputType>(v2, w));
1030  }
1031 
1047  template<class T>
1048  static T min(const std::vector<T>& v) throw (EmptyVectorException<T> )
1049  {
1050  if (v.size() == 0) throw EmptyVectorException<T>("VectorFunctions::min()", &v);
1051  T mini = v[0];
1052  for (size_t i = 1; i < v.size(); i++)
1053  {
1054  if (v[i] < mini) mini = v[i];
1055  }
1056  return mini;
1057  }
1058 
1068  template<class T>
1069  static T max(const std::vector<T>& v) throw (EmptyVectorException<T> )
1070  {
1071  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::max()", &v);
1072  T maxi = v[0];
1073  for (size_t i = 1; i < v.size(); i++)
1074  {
1075  if (v[i] > maxi) maxi = v[i];
1076  }
1077  return maxi;
1078  }
1079 
1090  template<class T>
1091  static size_t whichMax(const std::vector<T>& v) throw (EmptyVectorException<T> )
1092  {
1093  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::whichMax()", &v);
1094  T maxi = v[0];
1095  size_t pos = 0;
1096  for (size_t i = 1; i < v.size(); i++)
1097  {
1098  if (v[i] > maxi)
1099  {
1100  maxi = v[i];
1101  pos = i;
1102  }
1103  }
1104  return pos;
1105  }
1106 
1117  template<class T>
1118  static size_t whichMin(const std::vector<T>& v) throw (EmptyVectorException<T> )
1119  {
1120  if (v.size() == 0) throw EmptyVectorException<T>("VectorFunctions::whichMin()", &v);
1121  T mini = v[0];
1122  size_t pos = 0;
1123  for (size_t i = 1; i < v.size(); i++)
1124  {
1125  if (v[i] < mini)
1126  {
1127  mini = v[i];
1128  pos = i;
1129  }
1130  }
1131  return pos;
1132  }
1133 
1144  template<class T>
1145  static std::vector<size_t> whichMaxAll(const std::vector<T>& v) throw (EmptyVectorException<T> )
1146  {
1147  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::whichMaxAll()", &v);
1148  T maxi = max(v);
1149  std::vector<size_t> pos;
1150  for (size_t i = 0; i < v.size(); i++)
1151  {
1152  if (v[i] == maxi)
1153  {
1154  pos.push_back(i);
1155  }
1156  }
1157  return pos;
1158  }
1159 
1170  template<class T>
1171  static std::vector<size_t> whichMinAll(const std::vector<T>& v) throw (EmptyVectorException<T> )
1172  {
1173  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::whichMinAll()", &v);
1174  T mini = min(v);
1175  std::vector<size_t> pos;
1176  for (size_t i = 0; i < v.size(); i++)
1177  {
1178  if (v[i] == mini)
1179  {
1180  pos.push_back(i);
1181  }
1182  }
1183  return pos;
1184  }
1185 
1186 
1196  template<class T>
1197  static std::vector<T> range(const std::vector<T>& v) throw (EmptyVectorException<T> )
1198  {
1199  if (v.size() == 0)
1200  throw EmptyVectorException<T>("VectorTools::range()", &v);
1201  std::vector<T> r(2);
1202  r[0] = r[1] = v[0];
1203  for (size_t i = 1; i < v.size(); i++)
1204  {
1205  if (v[i] < r[0]) r[0] = v[i];
1206  if (v[i] > r[1]) r[1] = v[i];
1207  }
1208  return r;
1209  }
1210 
1211 private:
1212  template<class T> class order_Cmp_
1213  {
1214  const std::vector<T>& values_;
1215 
1216 public:
1217  order_Cmp_(const std::vector<T>& v) : values_(v) {}
1218  bool operator()(size_t a, size_t b) { return values_[a] < values_[b]; }
1219  };
1220 
1221 public:
1231  template<class T>
1232  static std::vector<size_t> order(const std::vector<T>& v) throw (EmptyVectorException<T> )
1233  {
1234  if (v.size() == 0)
1235  throw EmptyVectorException<T>("VectorTools::sort()", &v);
1236  // Private inner class:
1237  std::vector<size_t> index(v.size());
1238  for (size_t i = 0; i < index.size(); ++i)
1239  {
1240  index[i] = i;
1241  }
1242  sort(index.begin(), index.end(), order_Cmp_<T>(v));
1243  return index;
1244  }
1245 
1255  template<class T>
1256  static std::vector<T> abs(const std::vector<T>& v) throw (EmptyVectorException<T> )
1257  {
1258  std::vector<T> vabs(v.size());
1259  for (size_t i = 1; i < v.size(); i++)
1260  {
1261  vabs[i] = std::abs(v[i]);
1262  }
1263  return vabs;
1264  }
1265 
1270  template<class InputType, class OutputType>
1271  static OutputType mean(const std::vector<InputType>& v1)
1272  {
1273  return (OutputType)sum<InputType>(v1) / (OutputType)v1.size();
1274  }
1281  template<class InputType, class OutputType>
1282  static OutputType mean(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool normalizeWeights = true)
1283  {
1284  if (normalizeWeights)
1285  {
1286  std::vector<InputType> wn = w / sum(w);
1287  return scalar<InputType, OutputType>(v1, wn);
1288  }
1289  else
1290  {
1291  return scalar<InputType, OutputType>(v1, w);
1292  }
1293  }
1294 
1299  template<class InputType>
1300  static InputType median(std::vector<InputType>& v1)
1301  {
1302  InputType med = 0;
1303  if (v1.size() == 0) return med;
1304  if (v1.size() == 1) return v1[0];
1305  sort(v1.begin(), v1.end());
1306  size_t i = v1.size() / 2;
1307  if (v1.size() % 2 == 0)
1308  {
1309  // Vector size is pair
1310  med = double((v1[i - 1] + v1[i]) / 2);
1311  }
1312  else
1313  {
1314  // Vector size is impair
1315  med = v1[i];
1316  }
1317  return med;
1318  }
1319 
1326  template<class InputType, class OutputType>
1327  static std::vector<OutputType> center(const std::vector<InputType>& v1)
1328  {
1329  OutputType m = mean<InputType, OutputType>(v1);
1330  std::vector<OutputType> v(v1.size());
1331  for (size_t i = 0; i < v1.size(); i++)
1332  {
1333  v[i] = (OutputType)v1[i] - m;
1334  }
1335  return v;
1336  }
1345  template<class InputType, class OutputType>
1346  static std::vector<OutputType> center(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool normalizeWeights = true)
1347  {
1348  OutputType m = mean<InputType, OutputType>(v1, w, normalizeWeights);
1349  std::vector<OutputType> v(v1.size());
1350  for (size_t i = 0; i < v1.size(); i++)
1351  {
1352  v[i] = (OutputType)v1[i] - m;
1353  }
1354  return v;
1355  }
1356 
1364  template<class InputType, class OutputType>
1365  static OutputType cov(const std::vector<InputType>& v1, const std::vector<InputType>& v2, bool unbiased = true) throw (DimensionException)
1366  {
1367  OutputType n = (OutputType)v1.size();
1368  OutputType x = scalar<InputType, OutputType>(
1369  center<InputType, OutputType>(v1),
1370  center<InputType, OutputType>(v2)
1371  ) / n;
1372  if (unbiased) x = x * n / (n - 1);
1373  return x;
1374  }
1375 
1386  template<class InputType, class OutputType>
1387  static OutputType cov(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w, bool unbiased = true, bool normalizeWeights = true) throw (DimensionException)
1388  {
1389  if (normalizeWeights)
1390  {
1391  std::vector<InputType> wn = w / sum(w);
1392  OutputType x = scalar<InputType, OutputType>(
1393  center<InputType, OutputType>(v1, wn, false),
1394  center<InputType, OutputType>(v2, wn, false),
1395  wn
1396  );
1397  if (unbiased)
1398  {
1399  x = x / (1 - sum(sqr<double>(wn)));
1400  }
1401  return x;
1402  }
1403  else
1404  {
1405  OutputType x = scalar<InputType, OutputType>(
1406  center<InputType, OutputType>(v1, w, false),
1407  center<InputType, OutputType>(v2, w, false),
1408  w
1409  );
1410  if (unbiased)
1411  {
1412  x = x / (1 - sum(sqr(w)));
1413  }
1414  return x;
1415  }
1416  }
1422  template<class InputType, class OutputType>
1423  static OutputType var(const std::vector<InputType>& v1, bool unbiased = true)
1424  {
1425  return cov<InputType, OutputType>(v1, v1, unbiased);
1426  }
1435  template<class InputType, class OutputType>
1436  static OutputType var(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool unbiased = true, bool normalizeWeights = true) throw (DimensionException)
1437  {
1438  return cov<InputType, OutputType>(v1, v1, w, unbiased, normalizeWeights);
1439  }
1440 
1446  template<class InputType, class OutputType>
1447  static OutputType sd(const std::vector<InputType>& v1, bool unbiased = true)
1448  {
1449  return sqrt(var<InputType, OutputType>(v1, unbiased));
1450  }
1451 
1460  template<class InputType, class OutputType>
1461  static OutputType sd(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool unbiased = true, bool normalizeWeights = true) throw (DimensionException)
1462  {
1463  return sqrt(var<InputType, OutputType>(v1, w, unbiased, normalizeWeights));
1464  }
1465 
1472  template<class InputType, class OutputType>
1473  static OutputType cor(const std::vector<InputType>& v1, const std::vector<InputType>& v2) throw (DimensionException)
1474  {
1475  return cov<InputType, OutputType>(v1, v2)
1476  / ( sd<InputType, OutputType>(v1) * sd<InputType, OutputType>(v2) );
1477  }
1478 
1487  template<class InputType, class OutputType>
1488  static OutputType cor(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w, bool normalizeWeights = true) throw (DimensionException)
1489  {
1490  if (normalizeWeights)
1491  {
1492  std::vector<InputType> wn = w / sum(w);
1493  return cov<InputType, OutputType>(v1, v2, wn, false, false)
1494  / ( sd<InputType, OutputType>(v1, wn, false, false) * sd<InputType, OutputType>(v2, wn, false, false) );
1495  }
1496  else
1497  {
1498  return cov<InputType, OutputType>(v1, v2, w, false, false)
1499  / ( sd<InputType, OutputType>(v1, w, false, false) * sd<InputType, OutputType>(v2, w, false, false) );
1500  }
1501  }
1502 
1517  template<class InputType, class OutputType>
1518  static OutputType shannon(const std::vector<InputType>& v, double base = 2.7182818)
1519  {
1520  OutputType s = 0;
1521  for (size_t i = 0; i < v.size(); i++)
1522  {
1523  if (v[i] > 0) s += static_cast<OutputType>(v[i] * std::log(v[i]) / std::log(base));
1524  }
1525  return -s;
1526  }
1527 
1542  template<class InputType, class OutputType>
1543  static OutputType shannonDiscrete(const std::vector<InputType>& v, double base = 2.7182818)
1544  {
1545  std::map<InputType, double> counts;
1546  for (size_t i = 0; i < v.size(); i++)
1547  {
1548  counts[v[i]]++;
1549  }
1550  OutputType s = 0;
1551  double n = static_cast<double>(v.size());
1552  for (typename std::map<InputType, double>::iterator it = counts.begin(); it != counts.end(); it++)
1553  {
1554  s += static_cast<OutputType>((it->second / n) * std::log(it->second / n) / std::log(base));
1555  }
1556  return -s;
1557  }
1558 
1574  template<class InputType, class OutputType>
1575  static OutputType miDiscrete(const std::vector<InputType>& v1, const std::vector<InputType>& v2, double base = 2.7182818) throw (DimensionException)
1576  {
1577  if (v1.size() != v2.size())
1578  throw DimensionException("VectorTools::miDiscrete. The two samples must have the same length.", v2.size(), v1.size());
1579  std::map<InputType, double> counts1;
1580  std::map<InputType, double> counts2;
1581  std::map<InputType, std::map<InputType, double> > counts12;
1582  for (size_t i = 0; i < v1.size(); i++)
1583  {
1584  counts1[v1[i]]++;
1585  counts2[v2[i]]++;
1586  counts12[v1[i]][v2[i]]++;
1587  }
1588  OutputType s = 0;
1589  double n = static_cast<double>(v1.size());
1590  for (typename std::map<InputType, std::map<InputType, double> >::iterator it1 = counts12.begin(); it1 != counts12.end(); it1++)
1591  {
1592  for (typename std::map<InputType, double>::iterator it2 = it1->second.begin(); it2 != it1->second.end(); it2++)
1593  {
1594  s += static_cast<OutputType>((it2->second / n) * std::log(it2->second * n / (counts1[it1->first] * counts2[it2->first])) / std::log(base));
1595  }
1596  }
1597  return s;
1598  }
1599 
1615  template<class InputType, class OutputType>
1616  static OutputType shannonContinuous(const std::vector<InputType>& v, double base = 2.7182818)
1617  {
1618  LinearMatrix<InputType> m(1, v.size());
1619  for (size_t i = 0; i < v.size(); i++)
1620  {
1621  m(0, i) = v[i];
1622  }
1624  OutputType s = 0;
1625  std::vector<double> x(1);
1626  for (size_t i = 0; i < v.size(); i++)
1627  {
1628  x[0] = static_cast<double>(v[i]);
1629  s += static_cast<OutputType>(std::log(kd.kDensity(x)) / std::log(base));
1630  }
1631  return -s / static_cast<double>(v.size());
1632  }
1633 
1653  template<class InputType, class OutputType>
1654  static OutputType miContinuous(const std::vector<InputType>& v1, const std::vector<InputType>& v2, double base = 2.7182818) throw (DimensionException)
1655  {
1656  if (v1.size() != v2.size())
1657  throw DimensionException("VectorTools::miContinuous. The two samples must have the same length.", v2.size(), v1.size());
1658  LinearMatrix<InputType> m1(1, v1.size());
1659  LinearMatrix<InputType> m2(1, v2.size());
1660  LinearMatrix<InputType> m12(2, v1.size());
1661  for (size_t i = 0; i < v1.size(); i++)
1662  {
1663  m1(0, i) = m12(0, i) = v1[i];
1664  m2(0, i) = m12(1, i) = v2[i];
1665  }
1669  OutputType s = 0;
1670  std::vector<double> x1(1);
1671  std::vector<double> x2(1);
1672  std::vector<double> x12(2);
1673  for (size_t i = 0; i < v1.size(); i++)
1674  {
1675  x1[0] = x12[0] = static_cast<double>(v1[i]);
1676  x2[0] = x12[1] = static_cast<double>(v2[i]);
1677  s += static_cast<OutputType>(std::log(kd12.kDensity(x12) / (kd1.kDensity(x1) * kd2.kDensity(x2))) / std::log(base));
1678  }
1679  return s / static_cast<double>(v1.size());
1680  }
1681 
1687  template<class T>
1688  static bool haveSameElements(const std::vector<T>& v1, const std::vector<T>& v2)
1689  {
1690  std::vector<T> u1(v1);
1691  std::vector<T> u2(v2);
1692  if (u1.size() != u2.size()) return false;
1693  std::sort(u1.begin(), u1.end());
1694  std::sort(u2.begin(), u2.end());
1695  return u1 == u2;
1696  }
1697 
1706  template<class T>
1707  static bool haveSameElements(std::vector<T>& v1, std::vector<T>& v2)
1708  {
1709  if (v1.size() != v2.size()) return false;
1710  std::sort(v1.begin(), v1.end());
1711  std::sort(v2.begin(), v2.end());
1712  return v1 == v2;
1713  }
1714 
1720  template<class T>
1721  static bool contains(const std::vector<T>& vec, T el)
1722  {
1723  for (size_t i = 0; i < vec.size(); i++)
1724  {
1725  if (vec[i] == el) return true;
1726  }
1727  return false;
1728  }
1729 
1738  template<class T>
1739  static bool containsAll(std::vector<T>& v1, std::vector<T>& v2)
1740  {
1741  std::sort(v1.begin(), v1.end());
1742  std::sort(v2.begin(), v2.end());
1743  size_t j = 0;
1744  for (size_t i = 0; i < v2.size(); i++)
1745  {
1746  if (i > 0 && v2[i] == v2[i - 1]) continue;
1747  while (j < v1.size() - 1 && v1[j] < v2[i]) j++;
1748  if (v1[j] != v2[i]) return false;
1749  }
1750  return true;
1751  }
1752 
1759  template<class T>
1760  static std::vector<T> vectorUnion(const std::vector<T>& vec1, const std::vector<T>& vec2)
1761  {
1762  std::vector<T> unionEl = vec1;
1763  for (size_t j = 0; j < vec2.size(); j++)
1764  {
1765  if (!contains(unionEl, vec2[j]))
1766  unionEl.push_back(vec2[j]);
1767  }
1768  return unionEl;
1769  }
1770 
1776  template<class T>
1777  static std::vector<T> vectorUnion(const std::vector< std::vector<T> >& vecElementL)
1778  {
1779  std::vector<T> unionEl;
1780  for (size_t i = 0; i < vecElementL.size(); i++)
1781  {
1782  for (size_t j = 0; j < vecElementL[i].size(); j++)
1783  {
1784  if (!contains(unionEl, vecElementL[i][j]))
1785  unionEl.push_back(vecElementL[i][j]);
1786  }
1787  }
1788  return unionEl;
1789  }
1790 
1796  template<class T>
1797  static std::vector<T> vectorIntersection(const std::vector<T>& vec1, const std::vector<T>& vec2)
1798  {
1799  std::vector<T> interEl;
1800  for (size_t i = 0; i < vec1.size(); i++)
1801  {
1802  if (contains(vec2, vec1[i])) interEl.push_back(vec1[i]);
1803  }
1804  return interEl;
1805  }
1806 
1811  template<class T>
1812  static std::vector<T> vectorIntersection(const std::vector< std::vector<T> >& vecElementL)
1813  {
1814  if (vecElementL.size() == 1) return vecElementL[0];
1815  std::vector<T> interEl;
1816  if (vecElementL.size() == 0) return interEl;
1817  for (size_t i = 0; i < vecElementL[0].size(); i++)
1818  {
1819  bool test = true;
1820  for (size_t j = 1; test && j < vecElementL.size(); j++)
1821  {
1822  if (!contains(vecElementL[j], vecElementL[0][i])) test = false;
1823  }
1824  if (test) interEl.push_back(vecElementL[0][i]);
1825  }
1826  return interEl;
1827  }
1828 
1834  template<class T>
1835  static void append(std::vector<T>& vec1, const std::vector<T>& vec2)
1836  {
1837  vec1.insert(vec1.end(), vec2.begin(), vec2.end());
1838  // for(size_t i = 0; i < vec2.size(); i++)
1839  // {
1840  // vec1.push_back(vec2[i]);
1841  // }
1842  }
1843 
1849  template<class T>
1850  static void prepend(std::vector<T>& vec1, const std::vector<T>& vec2)
1851  {
1852  vec1.insert(vec1.begin(), vec2.begin(), vec2.end());
1853  }
1854 
1855 
1860  template<class T>
1861  static std::vector<T> append(const std::vector< std::vector<T> >& vecElementL)
1862  {
1863  if (vecElementL.size() == 1) return vecElementL[0];
1864  std::vector<T> v;
1865  if (vecElementL.size() == 0) return v;
1866  for (size_t i = 0; i < vecElementL[0].size(); i++)
1867  {
1868  v.push_back(vecElementL[0][i]);
1869  }
1870  return v;
1871  }
1872 
1878  template<class T>
1879  static void extend(std::vector<T>& vec1, const std::vector<T>& vec2)
1880  {
1881  for (size_t i = 0; i < vec2.size(); i++)
1882  {
1883  if (!contains(vec1, vec2[i]))
1884  vec1.push_back(vec2[i]);
1885  }
1886  }
1887 
1893  template<class T>
1894  static std::vector<T> rep(const std::vector<T>& vec, size_t n)
1895  {
1896  if (n == 1) return vec;
1897  std::vector<T> v;
1898  if (n == 0) return v;
1899  v.resize(vec.size() * n);
1900  for (size_t i = 0; i < v.size(); i++)
1901  {
1902  v[i] = vec[i % vec.size()];
1903  }
1904  return v;
1905  }
1906 
1916  template<class T>
1917  static void diff(std::vector<T>& v1, std::vector<T>& v2, std::vector<T>& v3)
1918  {
1919  if (v2.size() == 0) append(v3, v1);
1920  std::sort(v1.begin(), v1.end());
1921  std::sort(v2.begin(), v2.end());
1922  size_t j = 0;
1923  for (size_t i = 0; i < v1.size(); i++)
1924  {
1925  if (i > 0 && v1[i] == v1[i - 1]) continue;
1926  while (j < v2.size() - 1 && v2[j] < v1[i]) j++;
1927  if (v2[j] != v1[i]) v3.push_back(v1[i]);
1928  }
1929  }
1930 
1935  static bool test();
1936 };
1937 } // end of namespace bpp.
1938 
1939 #endif // _VECTORTOOLS_H_
1940 
void operator/=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:281
static T sumExp(const std::vector< T > &v1)
Definition: VectorTools.h:719
static std::vector< T > pow(const std::vector< T > &v1, T &b)
Definition: VectorTools.h:824
virtual ~VectorTools()
Definition: VectorTools.h:333
static std::vector< T > unique(const std::vector< T > &v)
Send a new std::vector with unique elements.
Definition: VectorTools.h:478
static bool containsAll(std::vector< T > &v1, std::vector< T > &v2)
Definition: VectorTools.h:1739
static size_t whichMax(const std::vector< T > &v)
Template function to get the index of the maximum value of a std::vector.
Definition: VectorTools.h:1091
static OutputType cor(const std::vector< InputType > &v1, const std::vector< InputType > &v2)
Definition: VectorTools.h:1473
static OutputType shannonDiscrete(const std::vector< InputType > &v, double base=2.7182818)
Definition: VectorTools.h:1543
static std::vector< double > log(const std::vector< T > &v1, double base)
Definition: VectorTools.h:768
static T prod(const std::vector< T > &v1)
Definition: VectorTools.h:587
static std::vector< T > vectorUnion(const std::vector< T > &vec1, const std::vector< T > &vec2)
Definition: VectorTools.h:1760
static T max(const std::vector< T > &v)
Template function to get the maximum value of a std::vector.
Definition: VectorTools.h:1069
order_Cmp_(const std::vector< T > &v)
Definition: VectorTools.h:1217
This class allows to perform a correspondence analysis.
static OutputType miContinuous(const std::vector< InputType > &v1, const std::vector< InputType > &v2, double base=2.7182818)
Definition: VectorTools.h:1654
static size_t whichMin(const std::vector< T > &v)
Template function to get the index of the minimum value of a std::vector.
Definition: VectorTools.h:1118
std::vector< T > operator+(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:88
static std::vector< double > log10(const std::vector< T > &v1)
Definition: VectorTools.h:800
static void resize4(VVVVdouble &vvvv, size_t n1, size_t n2, size_t n3, size_t n4)
Definition: VectorTools.h:362
static T sum(const std::vector< T > &v1)
Definition: VectorTools.h:614
std::vector< long double > Vldouble
Definition: VectorTools.h:72
static std::vector< T > vectorIntersection(const std::vector< T > &vec1, const std::vector< T > &vec2)
Definition: VectorTools.h:1797
Exception thrown when a given element was not found in the vector.
static OutputType norm(const std::vector< InputType > &v1)
Definition: VectorTools.h:973
static std::vector< T > cumProd(const std::vector< T > &v1)
Definition: VectorTools.h:600
static std::vector< OutputType > center(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool normalizeWeights=true)
Set the weighted mean of a std::vector to be 0.
Definition: VectorTools.h:1346
static bool contains(const std::vector< T > &vec, T el)
Definition: VectorTools.h:1721
std::vector< VVlcomplex > VVVlcomplex
Definition: VectorTools.h:65
static std::vector< T > abs(const std::vector< T > &v)
Template function to get the absolute value of all elements of a std::vector.
Definition: VectorTools.h:1256
Matrix storage in one vector.
Definition: Matrix.h:345
static std::vector< size_t > order(const std::vector< T > &v)
Template function to get the order of elements in the input vector.
Definition: VectorTools.h:1232
static std::string paste(const std::vector< T > &v, const std::string &delim=" ")
Concatenate a std::vector after converting to string.
Definition: VectorTools.h:839
bool operator()(size_t a, size_t b)
Definition: VectorTools.h:1218
std::vector< VVldouble > VVVldouble
Definition: VectorTools.h:74
std::vector< VVcomplex > VVVcomplex
Definition: VectorTools.h:61
static size_t which(const std::vector< T > &v, const T &which)
Send the position of the first occurence of &#39;which&#39;.
Definition: VectorTools.h:434
static std::vector< OutputType > center(const std::vector< InputType > &v1)
Set the mean of a std::vector to be 0.
Definition: VectorTools.h:1327
static OutputType var(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true)
Definition: VectorTools.h:1436
static std::vector< T > append(const std::vector< std::vector< T > > &vecElementL)
Definition: VectorTools.h:1861
static OutputType sd(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true)
Definition: VectorTools.h:1461
static void append(std::vector< T > &vec1, const std::vector< T > &vec2)
Append the content of a std::vector to another one.
Definition: VectorTools.h:1835
static OutputType cov(const std::vector< InputType > &v1, const std::vector< InputType > &v2, bool unbiased=true)
Definition: VectorTools.h:1365
static void diff(std::vector< T > &v1, std::vector< T > &v2, std::vector< T > &v3)
This function returns the difference of two std::vectors.
Definition: VectorTools.h:1917
static OutputType cos(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w)
Definition: VectorTools.h:1026
void operator+=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:254
std::vector< T > operator-(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:108
static std::vector< double > sin(const std::vector< T > &v1)
Definition: VectorTools.h:792
static OutputType scalar(const std::vector< InputType > &v1, const std::vector< InputType > &v2)
Definition: VectorTools.h:897
std::vector< VVint > VVVint
Definition: VectorTools.h:79
static OutputType cov(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true)
Definition: VectorTools.h:1387
std::vector< std::complex< double > > Vcomplex
Definition: VectorTools.h:59
std::vector< VVVint > VVVVint
Definition: VectorTools.h:80
static std::vector< size_t > whichAll(const std::vector< T > &v, const T &which)
Send the positions of all occurences of &#39;which&#39;.
Definition: VectorTools.h:454
static std::vector< double > cos(const std::vector< T > &v1)
Definition: VectorTools.h:784
std::vector< Vcomplex > VVcomplex
Definition: VectorTools.h:60
static std::vector< double > log(const std::vector< T > &v1)
Definition: VectorTools.h:761
static std::vector< T > fact(const std::vector< T > &v1)
Definition: VectorTools.h:808
static T logSumExp(const std::vector< T > &v1)
Definition: VectorTools.h:660
static OutputType var(const std::vector< InputType > &v1, bool unbiased=true)
Definition: VectorTools.h:1423
static OutputType scalar(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w)
Definition: VectorTools.h:927
static OutputType sd(const std::vector< InputType > &v1, bool unbiased=true)
Definition: VectorTools.h:1447
std::vector< int > Vint
Definition: VectorTools.h:77
static std::vector< T > rep(const std::vector< T > &vec, size_t n)
Definition: VectorTools.h:1894
static OutputType cos(const std::vector< InputType > &v1, const std::vector< InputType > &v2)
Definition: VectorTools.h:1012
static OutputStream * message
The output stream where messages have to be displayed.
Exception thrown when an empty vector was found.
static T min(const std::vector< T > &v)
Template function to get the minimum value of a std::vector.
Definition: VectorTools.h:1048
static void extend(std::vector< T > &vec1, const std::vector< T > &vec2)
Extend the content of a std::vector with another one. Only the elements not present in the first vect...
Definition: VectorTools.h:1879
static std::map< T, size_t > countValues(const std::vector< T > &v)
Count each element of a std::vector.
Definition: VectorTools.h:540
static std::vector< double > breaks(const std::vector< double > &v, unsigned int n)
Get the break points for a given number of classes.
Definition: VectorTools.cpp:53
std::vector< Vint > VVint
Definition: VectorTools.h:78
static T logSumExp(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:678
std::vector< double > Vdouble
Definition: VectorTools.h:67
static std::vector< T > extract(const std::vector< T > &v1, const std::vector< int > &v2)
Definition: VectorTools.h:523
static std::vector< T > vectorIntersection(const std::vector< std::vector< T > > &vecElementL)
Definition: VectorTools.h:1812
static size_t nclassScott(const std::vector< T > &v)
Get the optimal class number following Scott&#39;s method.
Definition: VectorTools.h:573
static std::vector< T > sqr(const std::vector< T > &v1)
Definition: VectorTools.h:816
static std::vector< T > cumSum(const std::vector< T > &v1)
Definition: VectorTools.h:627
static T sumExp(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:737
std::vector< T > operator/(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:148
static void resize3(VVVdouble &vvv, size_t n1, size_t n2, size_t n3)
Definition: VectorTools.h:349
OutputStream interface.
Definition: OutputStream.h:64
static void fill(std::vector< T > &v, T value)
Definition: VectorTools.h:381
static void logNorm(std::vector< T > &v)
Definition: VectorTools.h:643
static OutputType shannonContinuous(const std::vector< InputType > &v, double base=2.7182818)
Definition: VectorTools.h:1616
std::vector< T > operator*(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:128
static std::vector< double > exp(const std::vector< T > &v1)
Definition: VectorTools.h:776
static void printForR(const std::vector< T > &v1, std::string variableName="x", std::ostream &out=std::cout)
Print a std::vector to a stream in R format.
Definition: VectorTools.h:877
std::vector< std::complex< long double > > Vlcomplex
Definition: VectorTools.h:63
const std::vector< T > & values_
Definition: VectorTools.h:1214
static std::vector< size_t > whichMaxAll(const std::vector< T > &v)
Template function to get the indices of the maximum value of a std::vector.
Definition: VectorTools.h:1145
static std::vector< size_t > whichMinAll(const std::vector< T > &v)
Template function to get the indices of the minimum value of a std::vector.
Definition: VectorTools.h:1171
std::vector< VVVdouble > VVVVdouble
Definition: VectorTools.h:70
static void prepend(std::vector< T > &vec1, const std::vector< T > &vec2)
Prepend the content of a std::vector to another one.
Definition: VectorTools.h:1850
static bool isUnique(const std::vector< T > &v)
Tell if the std::vector as unique elements.
Definition: VectorTools.h:503
static T logMeanExp(const std::vector< T > &v1)
Definition: VectorTools.h:701
static bool test()
Test function.
Definition: VectorTools.cpp:68
static OutputType mean(const std::vector< InputType > &v1)
Definition: VectorTools.h:1271
double kDensity(const std::vector< double > &x)
static bool haveSameElements(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:1688
static OutputType norm(const std::vector< InputType > &v1, const std::vector< InputType > &w)
Definition: VectorTools.h:991
Density estimation using the adaptive kernel method.
static void print(const std::vector< T > &v1, OutputStream &out= *ApplicationTools::message, const std::string &delim=" ")
Print a std::vector to a stream.
Definition: VectorTools.h:859
std::vector< Vldouble > VVldouble
Definition: VectorTools.h:73
static OutputType mean(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool normalizeWeights=true)
Definition: VectorTools.h:1282
static void resize2(VVdouble &vv, size_t n1, size_t n2)
Definition: VectorTools.h:342
static std::vector< T > range(const std::vector< T > &v)
Template function to get both extrema of a std::vector.
Definition: VectorTools.h:1197
static OutputType shannon(const std::vector< InputType > &v, double base=2.7182818)
Definition: VectorTools.h:1518
static OutputType cor(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w, bool normalizeWeights=true)
Definition: VectorTools.h:1488
static bool haveSameElements(std::vector< T > &v1, std::vector< T > &v2)
Definition: VectorTools.h:1707
static std::vector< T > kroneckerMult(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:952
static std::vector< T > seq(T from, T to, T by)
Build a sequence std::vector.
Definition: VectorTools.h:402
void operator-=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:263
Exception thrown when a dimension problem occured.
std::vector< VVVldouble > VVVVldouble
Definition: VectorTools.h:75
std::vector< VVdouble > VVVdouble
Definition: VectorTools.h:69
std::vector< Vdouble > VVdouble
Definition: VectorTools.h:68
static InputType median(std::vector< InputType > &v1)
Definition: VectorTools.h:1300
static OutputType miDiscrete(const std::vector< InputType > &v1, const std::vector< InputType > &v2, double base=2.7182818)
Definition: VectorTools.h:1575
void operator*=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:272
std::vector< Vlcomplex > VVlcomplex
Definition: VectorTools.h:64
static std::vector< T > vectorUnion(const std::vector< std::vector< T > > &vecElementL)
Definition: VectorTools.h:1777