bpp-core3  3.0.0
VectorTools.h
Go to the documentation of this file.
1 //
2 // File: VectorTools.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-03-14 14:16:32
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_VECTORTOOLS_H
42 #define BPP_NUMERIC_VECTORTOOLS_H
43 
44 
45 #include "../App/ApplicationTools.h"
46 #include "../Io/OutputStream.h"
48 #include "Matrix/Matrix.h"
49 #include "NumTools.h"
50 #include "VectorExceptions.h"
51 
52 // From the STL:
53 #include <vector>
54 #include <map>
55 #include <cmath>
56 #include <algorithm>
57 #include <complex>
58 #include <numeric>
59 
60 namespace bpp
61 {
62 typedef std::vector<std::complex<double> > Vcomplex;
63 typedef std::vector<Vcomplex> VVcomplex;
64 typedef std::vector<VVcomplex> VVVcomplex;
65 
66 typedef std::vector<std::complex<long double> > Vlcomplex;
67 typedef std::vector<Vlcomplex> VVlcomplex;
68 typedef std::vector<VVlcomplex> VVVlcomplex;
69 
70 typedef std::vector<double> Vdouble;
71 typedef std::vector<Vdouble> VVdouble;
72 typedef std::vector<VVdouble> VVVdouble;
73 typedef std::vector<VVVdouble> VVVVdouble;
74 
75 typedef std::vector<long double> Vldouble;
76 typedef std::vector<Vldouble> VVldouble;
77 typedef std::vector<VVldouble> VVVldouble;
78 typedef std::vector<VVVldouble> VVVVldouble;
79 
80 typedef std::vector<int> Vint;
81 typedef std::vector<Vint> VVint;
82 typedef std::vector<VVint> VVVint;
83 typedef std::vector<VVVint> VVVVint;
84 
85 typedef std::vector<unsigned int> Vuint;
86 typedef std::vector<Vuint> VVuint;
87 typedef std::vector<VVuint> VVVuint;
88 typedef std::vector<VVVuint> VVVVuint;
89 
95 template<class T>
96 std::vector<T> operator+(const std::vector<T>& v1, const std::vector<T>& v2)
97 {
98  size_t size;
99  if (v1.size() != v2.size())
100  {
101  throw DimensionException("VectorTools::operator+", v1.size(), v2.size());
102  }
103  else
104  {
105  size = v1.size();
106  }
107  std::vector<T> result(size);
108  for (size_t i = 0; i < size; i++)
109  {
110  result[i] = v1[i] + v2[i];
111  }
112  return result;
113 }
114 
115 template<class T>
116 std::vector<T> operator-(const std::vector<T>& v1, const std::vector<T>& v2)
117 {
118  size_t size;
119  if (v1.size() != v2.size())
120  {
121  throw DimensionException("VectorTools::operator-", v1.size(), v2.size());
122  }
123  else
124  {
125  size = v1.size();
126  }
127  std::vector<T> result(size);
128  for (size_t i = 0; i < size; i++)
129  {
130  result[i] = v1[i] - v2[i];
131  }
132  return result;
133 }
134 
135 template<class T>
136 std::vector<T> operator*(const std::vector<T>& v1, const std::vector<T>& v2)
137 {
138  size_t size;
139  if (v1.size() != v2.size())
140  {
141  throw DimensionException("VectorTools::operator*", v1.size(), v2.size());
142  }
143  else
144  {
145  size = v1.size();
146  }
147  std::vector<T> result(size);
148  for (size_t i = 0; i < size; i++)
149  {
150  result[i] = v1[i] * v2[i];
151  }
152  return result;
153 }
154 
155 template<class T>
156 std::vector<T> operator/(const std::vector<T>& v1, const std::vector<T>& v2)
157 {
158  size_t size;
159  if (v1.size() != v2.size())
160  {
161  throw DimensionException("VectorTools::operator/", v1.size(), v2.size());
162  }
163  else
164  {
165  size = v1.size();
166  }
167  std::vector<T> result(size);
168  for (size_t i = 0; i < size; i++)
169  {
170  result[i] = v1[i] / v2[i];
171  }
172  return result;
173 }
174 
175 
176 template<class T, class C>
177 std::vector<T> operator+(const std::vector<T>& v1, const C& c)
178 {
179  std::vector<T> result(v1.size());
180  for (size_t i = 0; i < result.size(); i++)
181  {
182  result[i] = v1[i] + T(c);
183  }
184  return result;
185 }
186 template<class T, class C>
187 std::vector<T> operator+(const C& c, const std::vector<T>& v1)
188 {
189  std::vector<T> result(v1.size());
190  for (size_t i = 0; i < result.size(); i++)
191  {
192  result[i] = T(c) + v1[i];
193  }
194  return result;
195 }
196 
197 template<class T, class C>
198 std::vector<T> operator-(const std::vector<T>& v1, const C& c)
199 {
200  std::vector<T> result(v1.size());
201  for (size_t i = 0; i < result.size(); i++)
202  {
203  result[i] = v1[i] - T(c);
204  }
205  return result;
206 }
207 template<class T, class C>
208 std::vector<T> operator-(const C& c, const std::vector<T>& v1)
209 {
210  std::vector<T> result(v1.size());
211  for (size_t i = 0; i < result.size(); i++)
212  {
213  result[i] = T(c) - v1[i];
214  }
215  return result;
216 }
217 
218 template<class T, class C>
219 std::vector<T> operator*(const std::vector<T>& v1, const C& c)
220 {
221  std::vector<T> result(v1.size());
222  for (size_t i = 0; i < result.size(); i++)
223  {
224  result[i] = v1[i] * c;
225  }
226  return result;
227 }
228 template<class T, class C>
229 std::vector<T> operator*(const C& c, const std::vector<T>& v1)
230 {
231  std::vector<T> result(v1.size());
232  for (size_t i = 0; i < result.size(); i++)
233  {
234  result[i] = c * v1[i];
235  }
236  return result;
237 }
238 
239 template<class T, class C>
240 std::vector<T> operator/(const std::vector<T>& v1, const C& c)
241 {
242  std::vector<T> result(v1.size());
243  for (size_t i = 0; i < result.size(); i++)
244  {
245  result[i] = v1[i] / c;
246  }
247  return result;
248 }
249 template<class T, class C>
250 std::vector<T> operator/(const C& c, const std::vector<T>& v1)
251 {
252  std::vector<T> result(v1.size());
253  for (size_t i = 0; i < result.size(); i++)
254  {
255  result[i] = c / v1[i];
256  }
257  return result;
258 }
259 
260 template<class T>
261 void operator+=(std::vector<T>& v1, const std::vector<T>& v2)
262 {
263  for (size_t i = 0; i < v1.size(); i++)
264  {
265  v1[i] += v2[i];
266  }
267 }
268 
269 template<class T>
270 void operator-=(std::vector<T>& v1, const std::vector<T>& v2)
271 {
272  for (size_t i = 0; i < v1.size(); i++)
273  {
274  v1[i] -= v2[i];
275  }
276 }
277 
278 template<class T>
279 void operator*=(std::vector<T>& v1, const std::vector<T>& v2)
280 {
281  for (size_t i = 0; i < v1.size(); i++)
282  {
283  v1[i] *= v2[i];
284  }
285 }
286 
287 template<class T>
288 void operator/=(std::vector<T>& v1, const std::vector<T>& v2)
289 {
290  for (size_t i = 0; i < v1.size(); i++)
291  {
292  v1[i] /= v2[i];
293  }
294 }
295 
296 template<class T, class C>
297 void operator&=(std::vector<T>& v1, const C& c)
298 {
299  for (auto& x : v1)
300  {
301  x = c;
302  }
303 }
304 
305 template<class T, class C>
306 void operator+=(std::vector<T>& v1, const C& c)
307 {
308  for (auto& x : v1)
309  {
310  x += c;
311  }
312 }
313 
314 template<class T, class C>
315 void operator-=(std::vector<T>& v1, const C& c)
316 {
317  for (auto& x : v1)
318  {
319  x -= c;
320  }
321 }
322 
323 template<class T, class C>
324 void operator*=(std::vector<T>& v1, const C& c)
325 {
326  for (auto& x :v1)
327  {
328  x *= c;
329  }
330 }
331 
332 template<class T, class C>
333 void operator/=(std::vector<T>& v1, const C& c)
334 {
335  for (auto& x : v1)
336  {
337  x /= c;
338  }
339 }
340 
343 /******************************************************************************/
344 
346 {
347 public:
349  virtual ~VectorTools() {}
350 
351 public:
357  static void resize2(VVdouble& vv, size_t n1, size_t n2)
358  {
359  vv.resize(n1);
360  for (auto& x : vv) { x.resize(n2); }
361  }
362 
363  static void resize3(VVVdouble& vvv, size_t n1, size_t n2, size_t n3)
364  {
365  vvv.resize(n1);
366  for (auto& vv : vvv)
367  {
368  vv.resize(n2);
369  for (auto& v : vv)
370  {
371  v.resize(n3);
372  }
373  }
374  }
375 
376  static void resize4(VVVVdouble& vvvv, size_t n1, size_t n2, size_t n3, size_t n4)
377  {
378  vvvv.resize(n1);
379  for (auto& vvv : vvvv)
380  {
381  vvv.resize(n2);
382  for (auto& vv : vvv)
383  {
384  vv.resize(n3);
385  for (auto& v : vv)
386  {
387  v.resize(n4);
388  }
389  }
390  }
391  }
392 
395  template<class T>
396  static void fill(std::vector<T>& v, T value)
397  {
398  std::fill(v.begin(), v.end(), value);
399  }
400 
413  template<class T>
414  static std::vector<T> seq(T from, T to, T by)
415  {
416  std::vector<T> v((size_t)((std::abs(from - to) + by / 100) / by) + 1);
417  T step = from < to ? by : -by;
418  T val(from < to ? from : to);
419  for (auto& vi:v)
420  {
421  vi = val;
422  val += step;
423  }
424 
425  return v;
426  }
427 
438  template<class T>
439  static size_t which(const std::vector<T>& v, const T& which)
440  {
441  for (size_t i = 0; i < v.size(); i++)
442  {
443  if (v[i] == which) return i;
444  }
445  throw ElementNotFoundException<T>("VectorTools::which.", &v, &which);
446  }
447 
458  template<class T>
459  static std::vector<size_t> whichAll(const std::vector<T>& v, const T& which)
460  {
461  std::vector<size_t> w;
462  for (size_t i = 0; i < v.size(); i++)
463  {
464  if (v[i] == which) w.push_back(i);
465  }
466  if (w.size())
467  return w;
468  throw ElementNotFoundException<T>("VectorTools::whichAll.", &v, &which);
469  }
470 
482  template<class T>
483  static std::vector<T> unique(const std::vector<T>& v)
484  {
485  if (v.size() == 0) return v;
486  std::vector<T> sortedV(v.begin(), v.end());
487  sort(sortedV.begin(), sortedV.end());
488  std::vector<T> uniq;
489  uniq.push_back(sortedV[0]);
490  for (size_t i = 1; i < sortedV.size(); i++)
491  {
492  if (sortedV[i] != sortedV[i - 1]) uniq.push_back(sortedV[i]);
493  }
494  return uniq;
495  }
496 
507  template<class T>
508  static bool isUnique(const std::vector<T>& v)
509  {
510  if (v.size() == 0) return true;
511  std::vector<T> sortedV(v.begin(), v.end());
512  sort(sortedV.begin(), sortedV.end());
513  for (size_t i = 1; i < sortedV.size(); i++)
514  {
515  if (sortedV[i] == sortedV[i - 1]) return false;
516  }
517  return true;
518  }
519 
529  template<class T>
530  static std::vector<T> extract(const std::vector<T>& v1, const std::vector<int>& v2)
531  {
532  std::vector<T> v(v2.size());
533  for (size_t i = 0; i < v2.size(); i++)
534  {
535  v[i] = v1[v2[i]];
536  }
537  return v;
538  }
539 
546  template<class T>
547  static std::map<T, size_t> countValues(const std::vector<T>& v)
548  {
549  std::map<T, size_t> c;
550  for (auto x : v)
551  {
552  c[x]++;
553  }
554  return c;
555  }
556 
567  static std::vector<double> breaks(const std::vector<double>& v, unsigned int n);
568 
579  template<class T>
580  static size_t nclassScott(const std::vector<T>& v)
581  {
582  std::vector<T> r1 = VectorTools::range(v);
583  T r = r1[1] - r1[0];
584  double n = v.size();
585  double h = 3.5 * VectorTools::sd<T, double>(v) * std::pow(n, -1. / 3);
586  return (size_t) ceil(r / h);
587  }
588 
593  template<class T>
594  static T prod(const std::vector<T>& v1)
595  {
596  T p = 1;
597  for (auto x : v1)
598  {
599  p *= x;
600  }
601  return p;
602  }
603 
609  template<class T>
610  static std::vector<T> cumProd(const std::vector<T>& v1)
611  {
612  std::vector<T> p(v1.size());
613  if (v1.size() == 0) return p;
614  p[0] = v1[0];
615  for (size_t i = 1; i < v1.size(); i++) { p[i] = v1[i] * p[i - 1]; }
616  return p;
617  }
618 
623  template<class T>
624  static T sum(const std::vector<T>& v1)
625  {
626  T s = 0;
627  for (const auto& x : v1) { s += x; }
628  return s;
629  }
630 
638  template<class T>
639  static T sumProd(const std::vector<T>& v1, const std::vector<T>& v2)
640  {
641  size_t size;
642  if (v1.size() != v2.size())
643  throw DimensionException("VectorTools::sumProd", v1.size(), v2.size());
644  else
645  size = v1.size();
646 
647  T x = v2[0] * v1[0];
648  for (size_t i = 1; i < size; i++)
649  {
650  x += v2[i] * v1[i];
651  }
652 
653  return x;
654  }
655 
661  template<class T>
662  static std::vector<T> cumSum(const std::vector<T>& v1)
663  {
664  auto s(v1);
665  std::partial_sum(s.begin(), s.end(), s.begin());
666  return s;
667  }
668 
675  template<class T>
676  static void logNorm(std::vector<T>& v)
677  {
678  v -= VectorTools::logSumExp(v);
679  }
680 
686  template<class T>
687  static T logSumExp(const std::vector<T>& v1)
688  {
689  if (v1.size() == 1)
690  return v1[0];
691 
692  T M = max(v1);
693  if (std::isinf(M))
694  return M;
695 
696  T x = std::accumulate(std::next(v1.begin()), v1.end(), std::exp(v1[0] - M),
697  [&M](T y, T z){
698  return y + std::exp(z - M);
699  });
700 
701  return std::log(x) + M;
702  }
703 
710  template<class T>
711  static T logSumExp(const std::vector<T>& v1, const std::vector<T>& v2)
712  {
713  if (v1.size() != v2.size())
714  throw DimensionException("VectorTools::logsumexp", v1.size(), v2.size());
715 
716  size_t size = v1.size();
717 
718  T M = max(v1);
719  if (std::isinf(M))
720  throw BadNumberException("VectorTools::logSumExp", M);
721 
722  T x = v2[0] * std::exp(v1[0] - M);
723  for (size_t i = 1; i < size; i++)
724  {
725  x += v2[i] * std::exp(v1[i] - M);
726  }
727  return std::log(x) + M;
728  }
729 
735  template<class T>
736  static T logMeanExp(const std::vector<T>& v1)
737  {
738  return VectorTools::logSumExp(v1) - std::log(v1.size());
739  }
740 
741 
747  template<class T>
748  static T sumExp(const std::vector<T>& v1)
749  {
750  if (v1.size() == 0)
751  return std::exp(v1[0]);
752 
753  T M = max(v1);
754  if (std::isinf(M))
755  return M < 0 ? 0 : M;
756 
757  T x = std::accumulate(std::next(v1.begin()), v1.end(), std::exp(v1[0] - M),
758  [&M](T y, T z){
759  return y + std::exp(z - M);
760  });
761  return x * std::exp(M);
762  }
763 
770  template<class T>
771  static T sumExp(const std::vector<T>& v1, const std::vector<T>& v2)
772  {
773  if (v1.size() != v2.size())
774  throw DimensionException("VectorTools::sumExp", v1.size(), v2.size());
775 
776  size_t size = v1.size();
777 
778  if (size == 1)
779  return v2[0] * std::exp(v1[0]);
780 
781  T M = max(v1);
782  if (std::isinf(M))
783  throw BadNumberException("VectorTools::sumExp", M);
784 
785  T x = v2[0] * std::exp(v1[0] - M);
786  for (size_t i = 1; i < size; i++)
787  {
788  x += v2[i] * std::exp(v1[i] - M);
789  }
790  return x * std::exp(M);
791  }
792 
800  template<class T>
801  static std::vector<T> log(const std::vector<T>& v1)
802  {
803  std::vector<T> v2(v1.size());
804  for (size_t i = 0; i < v2.size(); i++)
805  {
806  v2[i] = log(v1[i]);
807  }
808  return v2;
809  }
810 
811  template<class T>
812  static double log(const T& x)
813  {
814  return std::log(x);
815  }
816 
817  template<class T>
818  static std::vector<double> log(const std::vector<T>& v1, double base)
819  {
820  std::vector<double> v2(v1.size());
821  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::log(v1[i]) / std::log(base); }
822  return v2;
823  }
824 
825  template<class T>
826  static double exp(const T& x)
827  {
828  return std::exp(x);
829  }
830 
831  template<class T>
832  static std::vector<T> exp(const std::vector<T>& v1)
833  {
834  std::vector<T> v2(v1.size());
835  for (size_t i = 0; i < v2.size(); i++) { v2[i] = VectorTools::exp(v1[i]); }
836  return v2;
837  }
838 
839  template<class T>
840  static std::vector<double> cos(const std::vector<T>& v1)
841  {
842  std::vector<double> v2(v1.size());
843  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::cos(v1[i]); }
844  return v2;
845  }
846 
847  template<class T>
848  static std::vector<double> sin(const std::vector<T>& v1)
849  {
850  std::vector<double> v2(v1.size());
851  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::sin(v1[i]); }
852  return v2;
853  }
854 
855  template<class T>
856  static std::vector<double> log10(const std::vector<T>& v1)
857  {
858  std::vector<double> v2(v1.size());
859  for (size_t i = 0; i < v1.size(); i++) { v2[i] = std::log10(v1[i]); }
860  return v2;
861  }
862 
863  template<class T>
864  static std::vector<T> fact(const std::vector<T>& v1)
865  {
866  std::vector<T> v2(v1.size());
867  for (size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::fact<T>(v1[i]); }
868  return v2;
869  }
870 
871  template<class T>
872  static std::vector<T> sqr(const std::vector<T>& v1)
873  {
874  std::vector<T> v2(v1.size());
875  for (size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::sqr<T>(v1[i]); }
876  return v2;
877  }
878 
879  template<class T>
880  static std::vector<T> pow(const std::vector<T>& v1, T& b)
881  {
882  std::vector<T> v2(v1.size());
883  for (size_t i = 0; i < v1.size(); i++) { v2[i] = std::pow(v1[i], b); }
884  return v2;
885  }
894  template<class T>
895  static std::string paste(const std::vector<T>& v, const std::string& delim = " ")
896  {
897  std::ostringstream out;
898  out.precision(12);
899  for (size_t i = 0; i < v.size(); i++)
900  {
901  out << v[i];
902  if (i < v.size() - 1)
903  out << delim;
904  }
905  return out.str();
906  }
907 
914  template<class T>
915  static void print(const std::vector<T>& v1, OutputStream& out = * ApplicationTools::message, const std::string& delim = " ")
916  {
917  for (size_t i = 0; i < v1.size(); i++)
918  {
919  out << v1[i];
920  if (i < v1.size() - 1)
921  out << delim;
922  }
923  out.endLine();
924  }
925 
926  template<class T>
927  static void printRange(const std::vector<T>& v1, OutputStream& out = * ApplicationTools::message, const std::string& delim = ",", const std::string& rangeDelim = "-")
928  {
929  size_t vs = v1.size();
930 
931  for (size_t i = 0; i < vs; i++)
932  {
933  out << v1[i];
934  size_t j = i + 1;
935 
936  while (j < vs)
937  if (v1[j] == v1[j - 1] + 1)
938  j++;
939  else
940  break;
941 
942  if (j > i + 2)
943  out << rangeDelim << v1[j - 1];
944  i = j - 1;
945  if (i < vs - 1)
946  out << delim;
947  }
948  }
949 
950 
957  template<class T>
958  static void printForR(const std::vector<T>& v1, std::string variableName = "x", std::ostream& out = std::cout)
959  {
960  out.precision(12);
961  out << variableName << "<-c(";
962  for (size_t i = 0; i < v1.size(); i++)
963  {
964  out << v1[i];
965  if (i < v1.size() - 1)
966  out << ", ";
967  }
968  out << ")" << std::endl;
969  }
970 
977  template<class InputType, class OutputType>
978  static OutputType scalar(const std::vector<InputType>& v1, const std::vector<InputType>& v2)
979  {
980  if (v1.size() != v2.size())
981  {
982  throw DimensionException("VectorTools::scalar", v1.size(), v2.size());
983  }
984  OutputType result = 0;
985  for (size_t i = 0; i < v1.size(); i++)
986  {
987  result += v1[i] * v2[i];
988  }
989  return result;
990  }
1007  template<class InputType, class OutputType>
1008  static OutputType scalar(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w)
1009  {
1010  if (v1.size() != w.size())
1011  {
1012  throw DimensionException("VectorTools::scalar", v1.size(), w.size());
1013  }
1014  if (v2.size() != w.size())
1015  {
1016  throw DimensionException("VectorTools::scalar", v2.size(), w.size());
1017  }
1018  OutputType result = 0;
1019  for (size_t i = 0; i < v1.size(); i++)
1020  {
1021  result += v1[i] * v2[i] * w[i];
1022  }
1023  return result;
1024  }
1025 
1032  template<class T>
1033  static std::vector<T> kroneckerMult(const std::vector<T>& v1, const std::vector<T>& v2)
1034  {
1035  size_t n1 = v1.size();
1036  size_t n2 = v2.size();
1037  std::vector<T> v3(n1 * n2);
1038  for (size_t i = 0; i < n1; i++)
1039  {
1040  T v1i = v1[i];
1041  for (size_t j = 0; j < n2; j++)
1042  {
1043  v3[i * n2 + j] = v1i * v2[j];
1044  }
1045  }
1046  return v3;
1047  }
1048 
1053  template<class InputType, class OutputType>
1054  static OutputType norm(const std::vector<InputType>& v1)
1055  {
1056  OutputType result = 0;
1057  for (size_t i = 0; i < v1.size(); i++)
1058  {
1059  result += v1[i] * v1[i];
1060  }
1061  return sqrt(result);
1062  }
1063 
1071  template<class InputType, class OutputType>
1072  static OutputType norm(const std::vector<InputType>& v1, const std::vector<InputType>& w)
1073  {
1074  if (v1.size() != w.size())
1075  {
1076  throw DimensionException("VectorTools::norm", v1.size(), w.size());
1077  }
1078  OutputType result = 0;
1079  for (size_t i = 0; i < v1.size(); i++)
1080  {
1081  result += v1[i] * v1[i] * w[i];
1082  }
1083  return sqrt(result);
1084  }
1085 
1092  template<class InputType, class OutputType>
1093  static OutputType cos(const std::vector<InputType>& v1, const std::vector<InputType>& v2)
1094  {
1095  return scalar<InputType, OutputType>(v1, v2)
1096  / (norm<InputType, OutputType>(v1) * norm<InputType, OutputType>(v2));
1097  }
1098 
1106  template<class InputType, class OutputType>
1107  static OutputType cos(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w)
1108  {
1109  return scalar<InputType, OutputType>(v1, v2, w)
1110  / (norm<InputType, OutputType>(v1, w) * norm<InputType, OutputType>(v2, w));
1111  }
1112 
1128  template<class T>
1129  static T min(const std::vector<T>& v)
1130  {
1131  if (v.size() == 0) throw EmptyVectorException<T>("VectorTools::min()", &v);
1132  T mini = v[0];
1133  for (size_t i = 1; i < v.size(); i++)
1134  {
1135  if (v[i] < mini) mini = v[i];
1136  }
1137  return mini;
1138  }
1139 
1149  template<class T>
1150  static T max(const std::vector<T>& v)
1151  {
1152  if (v.size() == 0) throw EmptyVectorException<T>("VectorTools::max()", &v);
1153  T maxi = v[0];
1154  for (size_t i = 1; i < v.size(); i++)
1155  {
1156  if (v[i] > maxi) maxi = v[i];
1157  }
1158  return maxi;
1159  }
1160 
1171  template<class T>
1172  static size_t whichMax(const std::vector<T>& v)
1173  {
1174  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::whichMax()", &v);
1175  T maxi = v[0];
1176  size_t pos = 0;
1177  for (size_t i = 1; i < v.size(); i++)
1178  {
1179  if (v[i] > maxi)
1180  {
1181  maxi = v[i];
1182  pos = i;
1183  }
1184  }
1185  return pos;
1186  }
1187 
1198  template<class T>
1199  static size_t whichMin(const std::vector<T>& v)
1200  {
1201  if (v.size() == 0) throw EmptyVectorException<T>("VectorTools::whichMin()", &v);
1202  T mini = v[0];
1203  size_t pos = 0;
1204  for (size_t i = 1; i < v.size(); i++)
1205  {
1206  if (v[i] < mini)
1207  {
1208  mini = v[i];
1209  pos = i;
1210  }
1211  }
1212  return pos;
1213  }
1214 
1225  template<class T>
1226  static std::vector<size_t> whichMaxAll(const std::vector<T>& v)
1227  {
1228  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::whichMaxAll()", &v);
1229  T maxi = max(v);
1230  std::vector<size_t> pos;
1231  for (size_t i = 0; i < v.size(); i++)
1232  {
1233  if (v[i] == maxi)
1234  {
1235  pos.push_back(i);
1236  }
1237  }
1238  return pos;
1239  }
1240 
1251  template<class T>
1252  static std::vector<size_t> whichMinAll(const std::vector<T>& v)
1253  {
1254  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::whichMinAll()", &v);
1255  T mini = min(v);
1256  std::vector<size_t> pos;
1257  for (size_t i = 0; i < v.size(); i++)
1258  {
1259  if (v[i] == mini)
1260  {
1261  pos.push_back(i);
1262  }
1263  }
1264  return pos;
1265  }
1266 
1267 
1277  template<class T>
1278  static std::vector<T> range(const std::vector<T>& v)
1279  {
1280  if (v.size() == 0)
1281  throw EmptyVectorException<T>("VectorTools::range()", &v);
1282  std::vector<T> r(2);
1283  r[0] = r[1] = v[0];
1284  for (size_t i = 1; i < v.size(); i++)
1285  {
1286  if (v[i] < r[0]) r[0] = v[i];
1287  if (v[i] > r[1]) r[1] = v[i];
1288  }
1289  return r;
1290  }
1291 
1292 private:
1293  template<class T> class order_Cmp_
1294  {
1295  const std::vector<T>& values_;
1296 
1297 public:
1298  order_Cmp_(const std::vector<T>& v) : values_(v) {}
1299  bool operator()(size_t a, size_t b) { return values_[a] < values_[b]; }
1300  };
1301 
1302 public:
1312  template<class T>
1313  static std::vector<size_t> order(const std::vector<T>& v)
1314  {
1315  if (v.size() == 0)
1316  throw EmptyVectorException<T>("VectorTools::sort()", &v);
1317  // Private inner class:
1318  std::vector<size_t> index(v.size());
1319  for (size_t i = 0; i < index.size(); ++i)
1320  {
1321  index[i] = i;
1322  }
1323  sort(index.begin(), index.end(), order_Cmp_<T>(v));
1324  return index;
1325  }
1326 
1336  template<class T>
1337  static std::vector<T> abs(const std::vector<T>& v)
1338  {
1339  std::vector<T> vabs(v.size());
1340  for (size_t i = 0; i < v.size(); i++)
1341  {
1342  vabs[i] = std::abs(v[i]);
1343  }
1344  return vabs;
1345  }
1346 
1351  template<class InputType, class OutputType>
1352  static OutputType mean(const std::vector<InputType>& v1)
1353  {
1354  return (OutputType)sum<InputType>(v1) / (OutputType)v1.size();
1355  }
1362  template<class InputType, class OutputType>
1363  static OutputType mean(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool normalizeWeights = true)
1364  {
1365  if (normalizeWeights)
1366  {
1367  std::vector<InputType> wn = w / sum(w);
1368  return scalar<InputType, OutputType>(v1, wn);
1369  }
1370  else
1371  {
1372  return scalar<InputType, OutputType>(v1, w);
1373  }
1374  }
1375 
1380  template<class InputType>
1381  static InputType median(std::vector<InputType>& v1)
1382  {
1383  InputType med = 0;
1384  if (v1.size() == 0) return med;
1385  if (v1.size() == 1) return v1[0];
1386  sort(v1.begin(), v1.end());
1387  size_t i = v1.size() / 2;
1388  if (v1.size() % 2 == 0)
1389  {
1390  // Vector size is pair
1391  med = double((v1[i - 1] + v1[i]) / 2);
1392  }
1393  else
1394  {
1395  // Vector size is impair
1396  med = v1[i];
1397  }
1398  return med;
1399  }
1400 
1407  template<class InputType, class OutputType>
1408  static std::vector<OutputType> center(const std::vector<InputType>& v1)
1409  {
1410  OutputType m = mean<InputType, OutputType>(v1);
1411  std::vector<OutputType> v(v1.size());
1412  for (size_t i = 0; i < v1.size(); i++)
1413  {
1414  v[i] = (OutputType)v1[i] - m;
1415  }
1416  return v;
1417  }
1426  template<class InputType, class OutputType>
1427  static std::vector<OutputType> center(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool normalizeWeights = true)
1428  {
1429  OutputType m = mean<InputType, OutputType>(v1, w, normalizeWeights);
1430  std::vector<OutputType> v(v1.size());
1431  for (size_t i = 0; i < v1.size(); i++)
1432  {
1433  v[i] = (OutputType)v1[i] - m;
1434  }
1435  return v;
1436  }
1437 
1445  template<class InputType, class OutputType>
1446  static OutputType cov(const std::vector<InputType>& v1, const std::vector<InputType>& v2, bool unbiased = true)
1447  {
1448  OutputType n = (OutputType)v1.size();
1449  OutputType x = scalar<InputType, OutputType>(
1450  center<InputType, OutputType>(v1),
1451  center<InputType, OutputType>(v2)
1452  ) / n;
1453  if (unbiased) x = x * n / (n - 1);
1454  return x;
1455  }
1456 
1467  template<class InputType, class OutputType>
1468  static OutputType cov(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w, bool unbiased = true, bool normalizeWeights = true)
1469  {
1470  if (normalizeWeights)
1471  {
1472  std::vector<InputType> wn = w / sum(w);
1473  OutputType x = scalar<InputType, OutputType>(
1474  center<InputType, OutputType>(v1, wn, false),
1475  center<InputType, OutputType>(v2, wn, false),
1476  wn
1477  );
1478  if (unbiased)
1479  {
1480  x = x / (1 - sum(sqr<double>(wn)));
1481  }
1482  return x;
1483  }
1484  else
1485  {
1486  OutputType x = scalar<InputType, OutputType>(
1487  center<InputType, OutputType>(v1, w, false),
1488  center<InputType, OutputType>(v2, w, false),
1489  w
1490  );
1491  if (unbiased)
1492  {
1493  x = x / (1 - sum(sqr(w)));
1494  }
1495  return x;
1496  }
1497  }
1503  template<class InputType, class OutputType>
1504  static OutputType var(const std::vector<InputType>& v1, bool unbiased = true)
1505  {
1506  return cov<InputType, OutputType>(v1, v1, unbiased);
1507  }
1516  template<class InputType, class OutputType>
1517  static OutputType var(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool unbiased = true, bool normalizeWeights = true)
1518  {
1519  return cov<InputType, OutputType>(v1, v1, w, unbiased, normalizeWeights);
1520  }
1521 
1527  template<class InputType, class OutputType>
1528  static OutputType sd(const std::vector<InputType>& v1, bool unbiased = true)
1529  {
1530  return sqrt(var<InputType, OutputType>(v1, unbiased));
1531  }
1532 
1541  template<class InputType, class OutputType>
1542  static OutputType sd(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool unbiased = true, bool normalizeWeights = true)
1543  {
1544  return sqrt(var<InputType, OutputType>(v1, w, unbiased, normalizeWeights));
1545  }
1546 
1553  template<class InputType, class OutputType>
1554  static OutputType cor(const std::vector<InputType>& v1, const std::vector<InputType>& v2)
1555  {
1556  return cov<InputType, OutputType>(v1, v2)
1557  / ( sd<InputType, OutputType>(v1) * sd<InputType, OutputType>(v2) );
1558  }
1559 
1568  template<class InputType, class OutputType>
1569  static OutputType cor(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w, bool normalizeWeights = true)
1570  {
1571  if (normalizeWeights)
1572  {
1573  std::vector<InputType> wn = w / sum(w);
1574  return cov<InputType, OutputType>(v1, v2, wn, false, false)
1575  / ( sd<InputType, OutputType>(v1, wn, false, false) * sd<InputType, OutputType>(v2, wn, false, false) );
1576  }
1577  else
1578  {
1579  return cov<InputType, OutputType>(v1, v2, w, false, false)
1580  / ( sd<InputType, OutputType>(v1, w, false, false) * sd<InputType, OutputType>(v2, w, false, false) );
1581  }
1582  }
1583 
1598  template<class InputType, class OutputType>
1599  static OutputType shannon(const std::vector<InputType>& v, double base = 2.7182818)
1600  {
1601  OutputType s = 0;
1602  for (auto x : v)
1603  {
1604  if (x > 0)
1605  s += static_cast<OutputType>(x * std::log(x) / std::log(base));
1606  }
1607  return -s;
1608  }
1609 
1624  template<class InputType, class OutputType>
1625  static OutputType shannonDiscrete(const std::vector<InputType>& v, double base = 2.7182818)
1626  {
1627  std::map<InputType, double> counts;
1628  for (auto x : v)
1629  {
1630  counts[x]++;
1631  }
1632  OutputType s = 0;
1633  double n = static_cast<double>(v.size());
1634  for (auto& it : counts)
1635  {
1636  s += static_cast<OutputType>((it.second / n) * std::log(it.second / n) / std::log(base));
1637  }
1638  return -s;
1639  }
1640 
1656  template<class InputType, class OutputType>
1657  static OutputType miDiscrete(const std::vector<InputType>& v1, const std::vector<InputType>& v2, double base = 2.7182818)
1658  {
1659  if (v1.size() != v2.size())
1660  throw DimensionException("VectorTools::miDiscrete. The two samples must have the same length.", v2.size(), v1.size());
1661  std::map<InputType, double> counts1;
1662  std::map<InputType, double> counts2;
1663  std::map<InputType, std::map<InputType, double> > counts12;
1664  for (size_t i = 0; i < v1.size(); i++)
1665  {
1666  counts1[v1[i]]++;
1667  counts2[v2[i]]++;
1668  counts12[v1[i]][v2[i]]++;
1669  }
1670  OutputType s = 0;
1671  double n = static_cast<double>(v1.size());
1672  for (auto& it1 : counts12)
1673  {
1674  for (auto& it2 : it1.second)
1675  {
1676  s += static_cast<OutputType>((it2.second / n) * std::log(it2.second * n / (counts1[it1.first] * counts2[it2.first])) / std::log(base));
1677  }
1678  }
1679  return s;
1680  }
1681 
1697  template<class InputType, class OutputType>
1698  static OutputType shannonContinuous(const std::vector<InputType>& v, double base = 2.7182818)
1699  {
1700  LinearMatrix<InputType> m(1, v.size());
1701  for (size_t i = 0; i < v.size(); i++)
1702  {
1703  m(0, i) = v[i];
1704  }
1706  OutputType s = 0;
1707  std::vector<double> x(1);
1708  for (auto it : v)
1709  {
1710  x[0] = static_cast<double>(it);
1711  s += static_cast<OutputType>(std::log(kd.kDensity(x)) / std::log(base));
1712  }
1713  return -s / static_cast<double>(v.size());
1714  }
1715 
1735  template<class InputType, class OutputType>
1736  static OutputType miContinuous(const std::vector<InputType>& v1, const std::vector<InputType>& v2, double base = 2.7182818)
1737  {
1738  if (v1.size() != v2.size())
1739  throw DimensionException("VectorTools::miContinuous. The two samples must have the same length.", v2.size(), v1.size());
1740  LinearMatrix<InputType> m1(1, v1.size());
1741  LinearMatrix<InputType> m2(1, v2.size());
1742  LinearMatrix<InputType> m12(2, v1.size());
1743  for (size_t i = 0; i < v1.size(); i++)
1744  {
1745  m1(0, i) = m12(0, i) = v1[i];
1746  m2(0, i) = m12(1, i) = v2[i];
1747  }
1751  OutputType s = 0;
1752  std::vector<double> x1(1);
1753  std::vector<double> x2(1);
1754  std::vector<double> x12(2);
1755  for (size_t i = 0; i < v1.size(); i++)
1756  {
1757  x1[0] = x12[0] = static_cast<double>(v1[i]);
1758  x2[0] = x12[1] = static_cast<double>(v2[i]);
1759  s += static_cast<OutputType>(std::log(kd12.kDensity(x12) / (kd1.kDensity(x1) * kd2.kDensity(x2))) / std::log(base));
1760  }
1761  return s / static_cast<double>(v1.size());
1762  }
1763 
1769  template<class T>
1770  static bool haveSameElements(const std::vector<T>& v1, const std::vector<T>& v2)
1771  {
1772  std::vector<T> u1(v1);
1773  std::vector<T> u2(v2);
1774  if (u1.size() != u2.size()) return false;
1775  std::sort(u1.begin(), u1.end());
1776  std::sort(u2.begin(), u2.end());
1777  return u1 == u2;
1778  }
1779 
1788  template<class T>
1789  static bool haveSameElements(std::vector<T>& v1, std::vector<T>& v2)
1790  {
1791  if (v1.size() != v2.size()) return false;
1792  std::sort(v1.begin(), v1.end());
1793  std::sort(v2.begin(), v2.end());
1794  return v1 == v2;
1795  }
1796 
1802  template<class T>
1803  static bool contains(const std::vector<T>& vec, T el)
1804  {
1805  for (auto it : vec)
1806  {
1807  if (it == el) return true;
1808  }
1809  return false;
1810  }
1811 
1812  template<class T, class U>
1813  static bool contains(const std::vector<T>& vec, U el)
1814  {
1815  for (auto it : vec)
1816  {
1817  if (it == (T)el) return true;
1818  }
1819  return false;
1820  }
1821 
1830  template<class T>
1831  static bool containsAll(std::vector<T>& v1, std::vector<T>& v2)
1832  {
1833  std::sort(v1.begin(), v1.end());
1834  std::sort(v2.begin(), v2.end());
1835  size_t j = 0;
1836  for (size_t i = 0; i < v2.size(); i++)
1837  {
1838  if (i > 0 && v2[i] == v2[i - 1]) continue;
1839  while (j < v1.size() - 1 && v1[j] < v2[i]) j++;
1840  if (v1[j] != v2[i]) return false;
1841  }
1842  return true;
1843  }
1844 
1851  template<class T>
1852  static std::vector<T> vectorUnion(const std::vector<T>& vec1, const std::vector<T>& vec2)
1853  {
1854  std::vector<T> unionEl = vec1;
1855  for (auto it : vec2)
1856  {
1857  if (!contains(unionEl, it))
1858  unionEl.push_back(it);
1859  }
1860  return unionEl;
1861  }
1862 
1868  template<class T>
1869  static std::vector<T> vectorUnion(const std::vector< std::vector<T> >& vecElementL)
1870  {
1871  std::vector<T> unionEl;
1872  for (auto it : vecElementL)
1873  {
1874  for (auto it2 : it)
1875  {
1876  if (!contains(unionEl, it2))
1877  unionEl.push_back(it2);
1878  }
1879  }
1880  return unionEl;
1881  }
1882 
1888  template<class T>
1889  static std::vector<T> vectorIntersection(const std::vector<T>& vec1, const std::vector<T>& vec2)
1890  {
1891  std::vector<T> interEl;
1892  for (auto it : vec1)
1893  {
1894  if (contains(vec2, it)) interEl.push_back(it);
1895  }
1896  return interEl;
1897  }
1898 
1899  template<class T, class U>
1900  static std::vector<T> vectorIntersection(const std::vector<T>& vec1, const std::vector<U>& vec2)
1901  {
1902  std::vector<T> interEl;
1903  for (auto it : vec1)
1904  {
1905  if (contains(vec2, it)) interEl.push_back(it);
1906  }
1907  return interEl;
1908  }
1909 
1914  template<class T>
1915  static std::vector<T> vectorIntersection(const std::vector< std::vector<T> >& vecElementL)
1916  {
1917  if (vecElementL.size() == 1) return vecElementL[0];
1918  std::vector<T> interEl;
1919  if (vecElementL.size() == 0) return interEl;
1920  for (auto it : vecElementL[0])
1921  {
1922  bool test = true;
1923  for (size_t j = 1; test && j < vecElementL.size(); j++)
1924  {
1925  if (!contains(vecElementL[j], it)) test = false;
1926  }
1927  if (test) interEl.push_back(it);
1928  }
1929  return interEl;
1930  }
1931 
1937  template<class T>
1938  static void append(std::vector<T>& vec1, const std::vector<T>& vec2)
1939  {
1940  vec1.insert(vec1.end(), vec2.begin(), vec2.end());
1941  }
1942 
1948  template<class T>
1949  static void prepend(std::vector<T>& vec1, const std::vector<T>& vec2)
1950  {
1951  vec1.insert(vec1.begin(), vec2.begin(), vec2.end());
1952  }
1953 
1954 
1959  template<class T>
1960  static std::vector<T> append(const std::vector< std::vector<T> >& vecElementL)
1961  {
1962  if (vecElementL.size() == 1) return vecElementL[0];
1963  std::vector<T> v;
1964  if (vecElementL.size() == 0) return v;
1965  for (auto it : vecElementL[0])
1966  {
1967  v.push_back(it);
1968  }
1969  return v;
1970  }
1971 
1977  template<class T>
1978  static void extend(std::vector<T>& vec1, const std::vector<T>& vec2)
1979  {
1980  for (auto it : vec2)
1981  {
1982  if (!contains(vec1, it))
1983  vec1.push_back(it);
1984  }
1985  }
1986 
1992  template<class T>
1993  static std::vector<T> rep(const std::vector<T>& vec, size_t n)
1994  {
1995  if (n == 1) return vec;
1996  std::vector<T> v;
1997  if (n == 0) return v;
1998  v.resize(vec.size() * n);
1999  for (size_t i = 0; i < v.size(); i++)
2000  {
2001  v[i] = vec[i % vec.size()];
2002  }
2003  return v;
2004  }
2005 
2015  template<class T>
2016  static void diff(std::vector<T>& v1, std::vector<T>& v2, std::vector<T>& v3)
2017  {
2018  if (v2.size() == 0) append(v3, v1);
2019  std::sort(v1.begin(), v1.end());
2020  std::sort(v2.begin(), v2.end());
2021  size_t j = 0;
2022  for (size_t i = 0; i < v1.size(); i++)
2023  {
2024  if (i > 0 && v1[i] == v1[i - 1]) continue;
2025  while (j < v2.size() - 1 && v2[j] < v1[i]) j++;
2026  if (v2[j] != v1[i]) v3.push_back(v1[i]);
2027  }
2028  }
2029 
2034  static bool test();
2035 };
2036 } // end of namespace bpp.
2037 #endif // BPP_NUMERIC_VECTORTOOLS_H
Density estimation using the adaptive kernel method.
double kDensity(const std::vector< double > &x)
static std::shared_ptr< OutputStream > message
The output stream where messages have to be displayed.
Number exception: doubles.
Definition: Exceptions.h:134
Exception thrown when a dimension problem occured.
Exception thrown when a given element was not found in the vector.
Exception thrown when an empty vector was found.
Matrix storage in one vector.
Definition: Matrix.h:357
OutputStream interface.
Definition: OutputStream.h:67
bool operator()(size_t a, size_t b)
Definition: VectorTools.h:1299
order_Cmp_(const std::vector< T > &v)
Definition: VectorTools.h:1298
const std::vector< T > & values_
Definition: VectorTools.h:1295
static double log(const T &x)
Definition: VectorTools.h:812
static OutputType cos(const std::vector< InputType > &v1, const std::vector< InputType > &v2)
Definition: VectorTools.h:1093
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:1252
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:1938
static double exp(const T &x)
Definition: VectorTools.h:826
static T sum(const std::vector< T > &v1)
Definition: VectorTools.h:624
static OutputType var(const std::vector< InputType > &v1, bool unbiased=true)
Definition: VectorTools.h:1504
static void resize3(VVVdouble &vvv, size_t n1, size_t n2, size_t n3)
Definition: VectorTools.h:363
static OutputType sd(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true)
Definition: VectorTools.h:1542
static OutputType sd(const std::vector< InputType > &v1, bool unbiased=true)
Definition: VectorTools.h:1528
static std::vector< T > pow(const std::vector< T > &v1, T &b)
Definition: VectorTools.h:880
static OutputType miDiscrete(const std::vector< InputType > &v1, const std::vector< InputType > &v2, double base=2.7182818)
Definition: VectorTools.h:1657
static std::vector< size_t > whichAll(const std::vector< T > &v, const T &which)
Send the positions of all occurences of 'which'.
Definition: VectorTools.h:459
static T sumProd(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:639
static OutputType scalar(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w)
Definition: VectorTools.h:1008
static T logSumExp(const std::vector< T > &v1)
Definition: VectorTools.h:687
static void logNorm(std::vector< T > &v)
Definition: VectorTools.h:676
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:1468
static OutputType var(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true)
Definition: VectorTools.h:1517
static OutputType shannon(const std::vector< InputType > &v, double base=2.7182818)
Definition: VectorTools.h:1599
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:958
static bool haveSameElements(std::vector< T > &v1, std::vector< T > &v2)
Definition: VectorTools.h:1789
static T prod(const std::vector< T > &v1)
Definition: VectorTools.h:594
static bool isUnique(const std::vector< T > &v)
Tell if the std::vector as unique elements.
Definition: VectorTools.h:508
static std::vector< T > kroneckerMult(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:1033
static std::vector< T > vectorUnion(const std::vector< T > &vec1, const std::vector< T > &vec2)
Definition: VectorTools.h:1852
static bool contains(const std::vector< T > &vec, U el)
Definition: VectorTools.h:1813
static OutputType scalar(const std::vector< InputType > &v1, const std::vector< InputType > &v2)
Definition: VectorTools.h:978
virtual ~VectorTools()
Definition: VectorTools.h:349
static OutputType mean(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool normalizeWeights=true)
Definition: VectorTools.h:1363
static T min(const std::vector< T > &v)
Template function to get the minimum value of a std::vector.
Definition: VectorTools.h:1129
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:1226
static size_t which(const std::vector< T > &v, const T &which)
Send the position of the first occurence of 'which'.
Definition: VectorTools.h:439
static bool contains(const std::vector< T > &vec, T el)
Definition: VectorTools.h:1803
static T sumExp(const std::vector< T > &v1)
Definition: VectorTools.h:748
static void resize2(VVdouble &vv, size_t n1, size_t n2)
Definition: VectorTools.h:357
static size_t nclassScott(const std::vector< T > &v)
Get the optimal class number following Scott's method.
Definition: VectorTools.h:580
static T sumExp(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:771
static std::vector< T > sqr(const std::vector< T > &v1)
Definition: VectorTools.h:872
static std::vector< T > rep(const std::vector< T > &vec, size_t n)
Definition: VectorTools.h:1993
static std::string paste(const std::vector< T > &v, const std::string &delim=" ")
Concatenate a std::vector after converting to string.
Definition: VectorTools.h:895
static std::vector< double > log10(const std::vector< T > &v1)
Definition: VectorTools.h:856
static std::vector< T > vectorIntersection(const std::vector< T > &vec1, const std::vector< U > &vec2)
Definition: VectorTools.h:1900
static T logMeanExp(const std::vector< T > &v1)
Definition: VectorTools.h:736
static void fill(std::vector< T > &v, T value)
Definition: VectorTools.h:396
static std::vector< T > vectorIntersection(const std::vector< T > &vec1, const std::vector< T > &vec2)
Definition: VectorTools.h:1889
static T max(const std::vector< T > &v)
Template function to get the maximum value of a std::vector.
Definition: VectorTools.h:1150
static OutputType cor(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w, bool normalizeWeights=true)
Definition: VectorTools.h:1569
static OutputType cos(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w)
Definition: VectorTools.h:1107
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:1949
static std::vector< T > log(const std::vector< T > &v1)
Definition: VectorTools.h:801
static std::vector< T > cumProd(const std::vector< T > &v1)
Definition: VectorTools.h:610
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:915
static std::vector< T > unique(const std::vector< T > &v)
Send a new std::vector with unique elements.
Definition: VectorTools.h:483
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:1978
static void resize4(VVVVdouble &vvvv, size_t n1, size_t n2, size_t n3, size_t n4)
Definition: VectorTools.h:376
static OutputType miContinuous(const std::vector< InputType > &v1, const std::vector< InputType > &v2, double base=2.7182818)
Definition: VectorTools.h:1736
static OutputType cov(const std::vector< InputType > &v1, const std::vector< InputType > &v2, bool unbiased=true)
Definition: VectorTools.h:1446
static std::vector< double > log(const std::vector< T > &v1, double base)
Definition: VectorTools.h:818
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:1313
static OutputType norm(const std::vector< InputType > &v1, const std::vector< InputType > &w)
Definition: VectorTools.h:1072
static OutputType mean(const std::vector< InputType > &v1)
Definition: VectorTools.h:1352
static OutputType shannonDiscrete(const std::vector< InputType > &v, double base=2.7182818)
Definition: VectorTools.h:1625
static std::map< T, size_t > countValues(const std::vector< T > &v)
Count each element of a std::vector.
Definition: VectorTools.h:547
static OutputType shannonContinuous(const std::vector< InputType > &v, double base=2.7182818)
Definition: VectorTools.h:1698
static T logSumExp(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:711
static OutputType norm(const std::vector< InputType > &v1)
Definition: VectorTools.h:1054
static std::vector< T > fact(const std::vector< T > &v1)
Definition: VectorTools.h:864
static std::vector< T > vectorIntersection(const std::vector< std::vector< T > > &vecElementL)
Definition: VectorTools.h:1915
static bool containsAll(std::vector< T > &v1, std::vector< T > &v2)
Definition: VectorTools.h:1831
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:2016
static std::vector< double > cos(const std::vector< T > &v1)
Definition: VectorTools.h:840
static bool test()
Test function.
Definition: VectorTools.cpp:71
static bool haveSameElements(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:1770
static void printRange(const std::vector< T > &v1, OutputStream &out= *ApplicationTools::message, const std::string &delim=",", const std::string &rangeDelim="-")
Definition: VectorTools.h:927
static InputType median(std::vector< InputType > &v1)
Definition: VectorTools.h:1381
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:1427
static std::vector< OutputType > center(const std::vector< InputType > &v1)
Set the mean of a std::vector to be 0.
Definition: VectorTools.h:1408
static OutputType cor(const std::vector< InputType > &v1, const std::vector< InputType > &v2)
Definition: VectorTools.h:1554
static std::vector< double > sin(const std::vector< T > &v1)
Definition: VectorTools.h:848
static std::vector< T > seq(T from, T to, T by)
Build a sequence std::vector.
Definition: VectorTools.h:414
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:56
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:1199
static std::vector< T > extract(const std::vector< T > &v1, const std::vector< int > &v2)
Takes elements of a vector given a vector of positions.
Definition: VectorTools.h:530
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:1172
static std::vector< T > range(const std::vector< T > &v)
Template function to get both extrema of a std::vector.
Definition: VectorTools.h:1278
static std::vector< T > exp(const std::vector< T > &v1)
Definition: VectorTools.h:832
static std::vector< T > vectorUnion(const std::vector< std::vector< T > > &vecElementL)
Definition: VectorTools.h:1869
static std::vector< T > append(const std::vector< std::vector< T > > &vecElementL)
Definition: VectorTools.h:1960
static std::vector< T > cumSum(const std::vector< T > &v1)
Definition: VectorTools.h:662
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:1337
T to(const std::string &s)
Template to string conversion.
Definition: TextTools.h:208
std::vector< VVVint > VVVVint
Definition: VectorTools.h:83
std::vector< std::complex< double > > Vcomplex
Definition: VectorTools.h:62
std::vector< VVVldouble > VVVVldouble
Definition: VectorTools.h:78
std::vector< double > Vdouble
Definition: VectorTools.h:70
std::vector< VVcomplex > VVVcomplex
Definition: VectorTools.h:64
std::vector< T > operator-(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:116
std::vector< VVVuint > VVVVuint
Definition: VectorTools.h:88
std::vector< VVVdouble > VVVVdouble
Definition: VectorTools.h:73
std::vector< Vuint > VVuint
Definition: VectorTools.h:86
std::vector< std::complex< long double > > Vlcomplex
Definition: VectorTools.h:66
std::vector< Vlcomplex > VVlcomplex
Definition: VectorTools.h:67
std::vector< T > operator*(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:136
void operator/=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:288
std::vector< VVlcomplex > VVVlcomplex
Definition: VectorTools.h:68
std::vector< T > operator+(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:96
std::vector< VVuint > VVVuint
Definition: VectorTools.h:87
std::vector< Vcomplex > VVcomplex
Definition: VectorTools.h:63
std::vector< Vint > VVint
Definition: VectorTools.h:81
std::vector< Vldouble > VVldouble
Definition: VectorTools.h:76
void operator&=(std::vector< T > &v1, const C &c)
Definition: VectorTools.h:297
std::vector< T > operator/(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:156
std::vector< VVint > VVVint
Definition: VectorTools.h:82
std::vector< int > Vint
Definition: VectorTools.h:80
std::vector< long double > Vldouble
Definition: VectorTools.h:75
std::vector< VVdouble > VVVdouble
Definition: VectorTools.h:72
void operator*=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:279
void operator-=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:270
std::vector< VVldouble > VVVldouble
Definition: VectorTools.h:77
void operator+=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:261
std::vector< Vdouble > VVdouble
Definition: VectorTools.h:71
std::vector< unsigned int > Vuint
Definition: VectorTools.h:85