bpp-core3  3.0.0
VectorTools.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_VECTORTOOLS_H
6 #define BPP_NUMERIC_VECTORTOOLS_H
7 
8 
9 #include "../App/ApplicationTools.h"
10 #include "../Io/OutputStream.h"
12 #include "Matrix/Matrix.h"
13 #include "NumTools.h"
14 #include "VectorExceptions.h"
15 
16 // From the STL:
17 #include <vector>
18 #include <map>
19 #include <cmath>
20 #include <algorithm>
21 #include <complex>
22 #include <numeric>
23 
24 namespace bpp
25 {
26 typedef std::vector<std::complex<double>> Vcomplex;
27 typedef std::vector<Vcomplex> VVcomplex;
28 typedef std::vector<VVcomplex> VVVcomplex;
29 
30 typedef std::vector<std::complex<long double>> Vlcomplex;
31 typedef std::vector<Vlcomplex> VVlcomplex;
32 typedef std::vector<VVlcomplex> VVVlcomplex;
33 
34 typedef std::vector<double> Vdouble;
35 typedef std::vector<Vdouble> VVdouble;
36 typedef std::vector<VVdouble> VVVdouble;
37 typedef std::vector<VVVdouble> VVVVdouble;
38 
39 typedef std::vector<long double> Vldouble;
40 typedef std::vector<Vldouble> VVldouble;
41 typedef std::vector<VVldouble> VVVldouble;
42 typedef std::vector<VVVldouble> VVVVldouble;
43 
44 typedef std::vector<int> Vint;
45 typedef std::vector<Vint> VVint;
46 typedef std::vector<VVint> VVVint;
47 typedef std::vector<VVVint> VVVVint;
48 
49 typedef std::vector<unsigned int> Vuint;
50 typedef std::vector<Vuint> VVuint;
51 typedef std::vector<VVuint> VVVuint;
52 typedef std::vector<VVVuint> VVVVuint;
53 
58 template<class T>
59 std::vector<T> operator+(const std::vector<T>& v1, const std::vector<T>& v2)
60 {
61  size_t size;
62  if (v1.size() != v2.size())
63  {
64  throw DimensionException("VectorTools::operator+", v1.size(), v2.size());
65  }
66  else
67  {
68  size = v1.size();
69  }
70  std::vector<T> result(size);
71  for (size_t i = 0; i < size; i++)
72  {
73  result[i] = v1[i] + v2[i];
74  }
75  return result;
76 }
77 
78 template<class T>
79 std::vector<T> operator-(const std::vector<T>& v1, const std::vector<T>& v2)
80 {
81  size_t size;
82  if (v1.size() != v2.size())
83  {
84  throw DimensionException("VectorTools::operator-", v1.size(), v2.size());
85  }
86  else
87  {
88  size = v1.size();
89  }
90  std::vector<T> result(size);
91  for (size_t i = 0; i < size; i++)
92  {
93  result[i] = v1[i] - v2[i];
94  }
95  return result;
96 }
97 
98 template<class T>
99 std::vector<T> operator*(const std::vector<T>& v1, const std::vector<T>& v2)
100 {
101  size_t size;
102  if (v1.size() != v2.size())
103  {
104  throw DimensionException("VectorTools::operator*", v1.size(), v2.size());
105  }
106  else
107  {
108  size = v1.size();
109  }
110  std::vector<T> result(size);
111  for (size_t i = 0; i < size; i++)
112  {
113  result[i] = v1[i] * v2[i];
114  }
115  return result;
116 }
117 
118 template<class T>
119 std::vector<T> operator/(const std::vector<T>& v1, const std::vector<T>& v2)
120 {
121  size_t size;
122  if (v1.size() != v2.size())
123  {
124  throw DimensionException("VectorTools::operator/", v1.size(), v2.size());
125  }
126  else
127  {
128  size = v1.size();
129  }
130  std::vector<T> result(size);
131  for (size_t i = 0; i < size; i++)
132  {
133  result[i] = v1[i] / v2[i];
134  }
135  return result;
136 }
137 
138 
139 template<class T, class C>
140 std::vector<T> operator+(const std::vector<T>& v1, const C& c)
141 {
142  std::vector<T> result(v1.size());
143  for (size_t i = 0; i < result.size(); i++)
144  {
145  result[i] = v1[i] + T(c);
146  }
147  return result;
148 }
149 template<class T, class C>
150 std::vector<T> operator+(const C& c, const std::vector<T>& v1)
151 {
152  std::vector<T> result(v1.size());
153  for (size_t i = 0; i < result.size(); i++)
154  {
155  result[i] = T(c) + v1[i];
156  }
157  return result;
158 }
159 
160 template<class T, class C>
161 std::vector<T> operator-(const std::vector<T>& v1, const C& c)
162 {
163  std::vector<T> result(v1.size());
164  for (size_t i = 0; i < result.size(); i++)
165  {
166  result[i] = v1[i] - T(c);
167  }
168  return result;
169 }
170 template<class T, class C>
171 std::vector<T> operator-(const C& c, const std::vector<T>& v1)
172 {
173  std::vector<T> result(v1.size());
174  for (size_t i = 0; i < result.size(); i++)
175  {
176  result[i] = T(c) - v1[i];
177  }
178  return result;
179 }
180 
181 template<class T, class C>
182 std::vector<T> operator*(const std::vector<T>& v1, const C& c)
183 {
184  std::vector<T> result(v1.size());
185  for (size_t i = 0; i < result.size(); i++)
186  {
187  result[i] = v1[i] * c;
188  }
189  return result;
190 }
191 template<class T, class C>
192 std::vector<T> operator*(const C& c, const std::vector<T>& v1)
193 {
194  std::vector<T> result(v1.size());
195  for (size_t i = 0; i < result.size(); i++)
196  {
197  result[i] = c * v1[i];
198  }
199  return result;
200 }
201 
202 template<class T, class C>
203 std::vector<T> operator/(const std::vector<T>& v1, const C& c)
204 {
205  std::vector<T> result(v1.size());
206  for (size_t i = 0; i < result.size(); i++)
207  {
208  result[i] = v1[i] / c;
209  }
210  return result;
211 }
212 template<class T, class C>
213 std::vector<T> operator/(const C& c, const std::vector<T>& v1)
214 {
215  std::vector<T> result(v1.size());
216  for (size_t i = 0; i < result.size(); i++)
217  {
218  result[i] = c / v1[i];
219  }
220  return result;
221 }
222 
223 template<class T>
224 void operator+=(std::vector<T>& v1, const std::vector<T>& v2)
225 {
226  for (size_t i = 0; i < v1.size(); i++)
227  {
228  v1[i] += v2[i];
229  }
230 }
231 
232 template<class T>
233 void operator-=(std::vector<T>& v1, const std::vector<T>& v2)
234 {
235  for (size_t i = 0; i < v1.size(); i++)
236  {
237  v1[i] -= v2[i];
238  }
239 }
240 
241 template<class T>
242 void operator*=(std::vector<T>& v1, const std::vector<T>& v2)
243 {
244  for (size_t i = 0; i < v1.size(); i++)
245  {
246  v1[i] *= v2[i];
247  }
248 }
249 
250 template<class T>
251 void operator/=(std::vector<T>& v1, const std::vector<T>& v2)
252 {
253  for (size_t i = 0; i < v1.size(); i++)
254  {
255  v1[i] /= v2[i];
256  }
257 }
258 
259 template<class T, class C>
260 void operator&=(std::vector<T>& v1, const C& c)
261 {
262  for (auto& x : v1)
263  {
264  x = c;
265  }
266 }
267 
268 template<class T, class C>
269 void operator+=(std::vector<T>& v1, const C& c)
270 {
271  for (auto& x : v1)
272  {
273  x += c;
274  }
275 }
276 
277 template<class T, class C>
278 void operator-=(std::vector<T>& v1, const C& c)
279 {
280  for (auto& x : v1)
281  {
282  x -= c;
283  }
284 }
285 
286 template<class T, class C>
287 void operator*=(std::vector<T>& v1, const C& c)
288 {
289  for (auto& x :v1)
290  {
291  x *= c;
292  }
293 }
294 
295 template<class T, class C>
296 void operator/=(std::vector<T>& v1, const C& c)
297 {
298  for (auto& x : v1)
299  {
300  x /= c;
301  }
302 }
303 
306 /******************************************************************************/
307 
309 {
310 public:
312  virtual ~VectorTools() {}
313 
314 public:
320  static void resize2(VVdouble& vv, size_t n1, size_t n2)
321  {
322  vv.resize(n1);
323  for (auto& x : vv) { x.resize(n2); }
324  }
325 
326  static void resize3(VVVdouble& vvv, size_t n1, size_t n2, size_t n3)
327  {
328  vvv.resize(n1);
329  for (auto& vv : vvv)
330  {
331  vv.resize(n2);
332  for (auto& v : vv)
333  {
334  v.resize(n3);
335  }
336  }
337  }
338 
339  static void resize4(VVVVdouble& vvvv, size_t n1, size_t n2, size_t n3, size_t n4)
340  {
341  vvvv.resize(n1);
342  for (auto& vvv : vvvv)
343  {
344  vvv.resize(n2);
345  for (auto& vv : vvv)
346  {
347  vv.resize(n3);
348  for (auto& v : vv)
349  {
350  v.resize(n4);
351  }
352  }
353  }
354  }
355 
358  template<class T>
359  static void fill(std::vector<T>& v, T value)
360  {
361  std::fill(v.begin(), v.end(), value);
362  }
363 
376  template<class T>
377  static std::vector<T> seq(T from, T to, T by)
378  {
379  std::vector<T> v((size_t)((std::abs(from - to) + by / 100) / by) + 1);
380  T step = from < to ? by : -by;
381  T val(from < to ? from : to);
382  for (auto& vi:v)
383  {
384  vi = val;
385  val += step;
386  }
387 
388  return v;
389  }
390 
401  template<class T>
402  static size_t which(const std::vector<T>& v, const T& which)
403  {
404  for (size_t i = 0; i < v.size(); i++)
405  {
406  if (v[i] == which) return i;
407  }
408  throw ElementNotFoundException<T>("VectorTools::which.", &v, &which);
409  }
410 
421  template<class T>
422  static std::vector<size_t> whichAll(const std::vector<T>& v, const T& which)
423  {
424  std::vector<size_t> w;
425  for (size_t i = 0; i < v.size(); i++)
426  {
427  if (v[i] == which) w.push_back(i);
428  }
429  if (w.size())
430  return w;
431  throw ElementNotFoundException<T>("VectorTools::whichAll.", &v, &which);
432  }
433 
445  template<class T>
446  static std::vector<T> unique(const std::vector<T>& v)
447  {
448  if (v.size() == 0) return v;
449  std::vector<T> sortedV(v.begin(), v.end());
450  sort(sortedV.begin(), sortedV.end());
451  std::vector<T> uniq;
452  uniq.push_back(sortedV[0]);
453  for (size_t i = 1; i < sortedV.size(); i++)
454  {
455  if (sortedV[i] != sortedV[i - 1]) uniq.push_back(sortedV[i]);
456  }
457  return uniq;
458  }
459 
470  template<class T>
471  static bool isUnique(const std::vector<T>& v)
472  {
473  if (v.size() == 0) return true;
474  std::vector<T> sortedV(v.begin(), v.end());
475  sort(sortedV.begin(), sortedV.end());
476  for (size_t i = 1; i < sortedV.size(); i++)
477  {
478  if (sortedV[i] == sortedV[i - 1]) return false;
479  }
480  return true;
481  }
482 
492  template<class T>
493  static std::vector<T> extract(const std::vector<T>& v1, const std::vector<size_t>& v2)
494  {
495  std::vector<T> v(v2.size());
496  for (size_t i = 0; i < v2.size(); ++i)
497  {
498  v[i] = v1[v2[i]];
499  }
500  return v;
501  }
502 
509  template<class T>
510  static std::map<T, size_t> countValues(const std::vector<T>& v)
511  {
512  std::map<T, size_t> c;
513  for (auto x : v)
514  {
515  c[x]++;
516  }
517  return c;
518  }
519 
530  static std::vector<double> breaks(const std::vector<double>& v, unsigned int n);
531 
542  template<class T>
543  static size_t nclassScott(const std::vector<T>& v)
544  {
545  std::vector<T> r1 = VectorTools::range(v);
546  T r = r1[1] - r1[0];
547  double n = v.size();
548  double h = 3.5 * VectorTools::sd<T, double>(v) * std::pow(n, -1. / 3);
549  return (size_t) ceil(r / h);
550  }
551 
556  template<class T>
557  static T prod(const std::vector<T>& v1)
558  {
559  T p = 1;
560  for (auto x : v1)
561  {
562  p *= x;
563  }
564  return p;
565  }
566 
572  template<class T>
573  static std::vector<T> cumProd(const std::vector<T>& v1)
574  {
575  std::vector<T> p(v1.size());
576  if (v1.size() == 0) return p;
577  p[0] = v1[0];
578  for (size_t i = 1; i < v1.size(); i++) { p[i] = v1[i] * p[i - 1]; }
579  return p;
580  }
581 
586  template<class T>
587  static T sum(const std::vector<T>& v1)
588  {
589  T s = 0;
590  for (const auto& x : v1) { s += x; }
591  return s;
592  }
593 
600  template<class T>
601  static T sumProd(const std::vector<T>& v1, const std::vector<T>& v2)
602  {
603  size_t size;
604  if (v1.size() != v2.size())
605  throw DimensionException("VectorTools::sumProd", v1.size(), v2.size());
606  else
607  size = v1.size();
608 
609  T x = v2[0] * v1[0];
610  for (size_t i = 1; i < size; i++)
611  {
612  x += v2[i] * v1[i];
613  }
614 
615  return x;
616  }
617 
623  template<class T>
624  static std::vector<T> cumSum(const std::vector<T>& v1)
625  {
626  auto s(v1);
627  std::partial_sum(s.begin(), s.end(), s.begin());
628  return s;
629  }
630 
637  template<class T>
638  static void logNorm(std::vector<T>& v)
639  {
640  v -= VectorTools::logSumExp(v);
641  }
642 
648  template<class T>
649  static T logSumExp(const std::vector<T>& v1)
650  {
651  if (v1.size() == 1)
652  return v1[0];
653 
654  T M = max(v1);
655  if (std::isinf(M))
656  return M;
657 
658  T x = std::accumulate(std::next(v1.begin()), v1.end(), std::exp(v1[0] - M),
659  [&M](T y, T z){
660  return y + std::exp(z - M);
661  });
662 
663  return std::log(x) + M;
664  }
665 
672  template<class T>
673  static T logSumExp(const std::vector<T>& v1, const std::vector<T>& v2)
674  {
675  if (v1.size() != v2.size())
676  throw DimensionException("VectorTools::logsumexp", v1.size(), v2.size());
677 
678  size_t size = v1.size();
679 
680  T M = max(v1);
681  if (std::isinf(M))
682  throw BadNumberException("VectorTools::logSumExp", M);
683 
684  T x = v2[0] * std::exp(v1[0] - M);
685  for (size_t i = 1; i < size; i++)
686  {
687  x += v2[i] * std::exp(v1[i] - M);
688  }
689  return std::log(x) + M;
690  }
691 
697  template<class T>
698  static T logMeanExp(const std::vector<T>& v1)
699  {
700  return VectorTools::logSumExp(v1) - std::log(v1.size());
701  }
702 
703 
709  template<class T>
710  static T sumExp(const std::vector<T>& v1)
711  {
712  if (v1.size() == 0)
713  return std::exp(v1[0]);
714 
715  T M = max(v1);
716  if (std::isinf(M))
717  return M < 0 ? 0 : M;
718 
719  T x = std::accumulate(std::next(v1.begin()), v1.end(), std::exp(v1[0] - M),
720  [&M](T y, T z){
721  return y + std::exp(z - M);
722  });
723  return x * std::exp(M);
724  }
725 
732  template<class T>
733  static T sumExp(const std::vector<T>& v1, const std::vector<T>& v2)
734  {
735  if (v1.size() != v2.size())
736  throw DimensionException("VectorTools::sumExp", v1.size(), v2.size());
737 
738  size_t size = v1.size();
739 
740  if (size == 1)
741  return v2[0] * std::exp(v1[0]);
742 
743  T M = max(v1);
744  if (std::isinf(M))
745  throw BadNumberException("VectorTools::sumExp", M);
746 
747  T x = v2[0] * std::exp(v1[0] - M);
748  for (size_t i = 1; i < size; i++)
749  {
750  x += v2[i] * std::exp(v1[i] - M);
751  }
752  return x * std::exp(M);
753  }
754 
761  template<class T>
762  static std::vector<T> log(const std::vector<T>& v1)
763  {
764  std::vector<T> v2(v1.size());
765  for (size_t i = 0; i < v2.size(); i++)
766  {
767  v2[i] = log(v1[i]);
768  }
769  return v2;
770  }
771 
772  template<class T>
773  static double log(const T& x)
774  {
775  return std::log(x);
776  }
777 
778  template<class T>
779  static std::vector<double> log(const std::vector<T>& v1, double base)
780  {
781  std::vector<double> v2(v1.size());
782  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::log(v1[i]) / std::log(base); }
783  return v2;
784  }
785 
786  template<class T>
787  static double exp(const T& x)
788  {
789  return std::exp(x);
790  }
791 
792  template<class T>
793  static std::vector<T> exp(const std::vector<T>& v1)
794  {
795  std::vector<T> v2(v1.size());
796  for (size_t i = 0; i < v2.size(); i++) { v2[i] = VectorTools::exp(v1[i]); }
797  return v2;
798  }
799 
800  template<class T>
801  static std::vector<double> cos(const std::vector<T>& v1)
802  {
803  std::vector<double> v2(v1.size());
804  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::cos(v1[i]); }
805  return v2;
806  }
807 
808  template<class T>
809  static std::vector<double> sin(const std::vector<T>& v1)
810  {
811  std::vector<double> v2(v1.size());
812  for (size_t i = 0; i < v2.size(); i++) { v2[i] = std::sin(v1[i]); }
813  return v2;
814  }
815 
816  template<class T>
817  static std::vector<double> log10(const std::vector<T>& v1)
818  {
819  std::vector<double> v2(v1.size());
820  for (size_t i = 0; i < v1.size(); i++) { v2[i] = std::log10(v1[i]); }
821  return v2;
822  }
823 
824  template<class T>
825  static std::vector<T> fact(const std::vector<T>& v1)
826  {
827  std::vector<T> v2(v1.size());
828  for (size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::fact<T>(v1[i]); }
829  return v2;
830  }
831 
832  template<class T>
833  static std::vector<T> sqr(const std::vector<T>& v1)
834  {
835  std::vector<T> v2(v1.size());
836  for (size_t i = 0; i < v1.size(); i++) { v2[i] = NumTools::sqr<T>(v1[i]); }
837  return v2;
838  }
839 
840  template<class T>
841  static std::vector<T> pow(const std::vector<T>& v1, T& b)
842  {
843  std::vector<T> v2(v1.size());
844  for (size_t i = 0; i < v1.size(); i++) { v2[i] = std::pow(v1[i], b); }
845  return v2;
846  }
855  template<class T>
856  static std::string paste(const std::vector<T>& v, const std::string& delim = " ")
857  {
858  std::ostringstream out;
859  out.precision(12);
860  for (size_t i = 0; i < v.size(); i++)
861  {
862  out << v[i];
863  if (i < v.size() - 1)
864  out << delim;
865  }
866  return out.str();
867  }
868 
875  template<class T>
876  static void print(const std::vector<T>& v1, OutputStream& out = * ApplicationTools::message, const std::string& delim = " ")
877  {
878  for (size_t i = 0; i < v1.size(); i++)
879  {
880  out << v1[i];
881  if (i < v1.size() - 1)
882  out << delim;
883  }
884  out.endLine();
885  }
886 
887  template<class T>
888  static void printRange(const std::vector<T>& v1, OutputStream& out = * ApplicationTools::message, const std::string& delim = ",", const std::string& rangeDelim = "-")
889  {
890  size_t vs = v1.size();
891 
892  for (size_t i = 0; i < vs; i++)
893  {
894  out << v1[i];
895  size_t j = i + 1;
896 
897  while (j < vs)
898  if (v1[j] == v1[j - 1] + 1)
899  j++;
900  else
901  break;
902 
903  if (j > i + 2)
904  out << rangeDelim << v1[j - 1];
905  i = j - 1;
906  if (i < vs - 1)
907  out << delim;
908  }
909  }
910 
911 
918  template<class T>
919  static void printForR(const std::vector<T>& v1, std::string variableName = "x", std::ostream& out = std::cout)
920  {
921  out.precision(12);
922  out << variableName << "<-c(";
923  for (size_t i = 0; i < v1.size(); i++)
924  {
925  out << v1[i];
926  if (i < v1.size() - 1)
927  out << ", ";
928  }
929  out << ")" << std::endl;
930  }
931 
938  template<class InputType, class OutputType>
939  static OutputType scalar(const std::vector<InputType>& v1, const std::vector<InputType>& v2)
940  {
941  if (v1.size() != v2.size())
942  {
943  throw DimensionException("VectorTools::scalar", v1.size(), v2.size());
944  }
945  OutputType result = 0;
946  for (size_t i = 0; i < v1.size(); i++)
947  {
948  result += v1[i] * v2[i];
949  }
950  return result;
951  }
968  template<class InputType, class OutputType>
969  static OutputType scalar(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w)
970  {
971  if (v1.size() != w.size())
972  {
973  throw DimensionException("VectorTools::scalar", v1.size(), w.size());
974  }
975  if (v2.size() != w.size())
976  {
977  throw DimensionException("VectorTools::scalar", v2.size(), w.size());
978  }
979  OutputType result = 0;
980  for (size_t i = 0; i < v1.size(); i++)
981  {
982  result += v1[i] * v2[i] * w[i];
983  }
984  return result;
985  }
986 
993  template<class T>
994  static std::vector<T> kroneckerMult(const std::vector<T>& v1, const std::vector<T>& v2)
995  {
996  size_t n1 = v1.size();
997  size_t n2 = v2.size();
998  std::vector<T> v3(n1 * n2);
999  for (size_t i = 0; i < n1; i++)
1000  {
1001  T v1i = v1[i];
1002  for (size_t j = 0; j < n2; j++)
1003  {
1004  v3[i * n2 + j] = v1i * v2[j];
1005  }
1006  }
1007  return v3;
1008  }
1009 
1014  template<class InputType, class OutputType>
1015  static OutputType norm(const std::vector<InputType>& v1)
1016  {
1017  OutputType result = 0;
1018  for (size_t i = 0; i < v1.size(); i++)
1019  {
1020  result += v1[i] * v1[i];
1021  }
1022  return sqrt(result);
1023  }
1024 
1032  template<class InputType, class OutputType>
1033  static OutputType norm(const std::vector<InputType>& v1, const std::vector<InputType>& w)
1034  {
1035  if (v1.size() != w.size())
1036  {
1037  throw DimensionException("VectorTools::norm", v1.size(), w.size());
1038  }
1039  OutputType result = 0;
1040  for (size_t i = 0; i < v1.size(); i++)
1041  {
1042  result += v1[i] * v1[i] * w[i];
1043  }
1044  return sqrt(result);
1045  }
1046 
1053  template<class InputType, class OutputType>
1054  static OutputType cos(const std::vector<InputType>& v1, const std::vector<InputType>& v2)
1055  {
1056  return scalar<InputType, OutputType>(v1, v2)
1057  / (norm<InputType, OutputType>(v1) * norm<InputType, OutputType>(v2));
1058  }
1059 
1067  template<class InputType, class OutputType>
1068  static OutputType cos(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w)
1069  {
1070  return scalar<InputType, OutputType>(v1, v2, w)
1071  / (norm<InputType, OutputType>(v1, w) * norm<InputType, OutputType>(v2, w));
1072  }
1073 
1089  template<class T>
1090  static T min(const std::vector<T>& v)
1091  {
1092  if (v.size() == 0) throw EmptyVectorException<T>("VectorTools::min()", &v);
1093  T mini = v[0];
1094  for (size_t i = 1; i < v.size(); i++)
1095  {
1096  if (v[i] < mini) mini = v[i];
1097  }
1098  return mini;
1099  }
1100 
1110  template<class T>
1111  static T max(const std::vector<T>& v)
1112  {
1113  if (v.size() == 0) throw EmptyVectorException<T>("VectorTools::max()", &v);
1114  T maxi = v[0];
1115  for (size_t i = 1; i < v.size(); i++)
1116  {
1117  if (v[i] > maxi) maxi = v[i];
1118  }
1119  return maxi;
1120  }
1121 
1132  template<class T>
1133  static size_t whichMax(const std::vector<T>& v)
1134  {
1135  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::whichMax()", &v);
1136  T maxi = v[0];
1137  size_t pos = 0;
1138  for (size_t i = 1; i < v.size(); i++)
1139  {
1140  if (v[i] > maxi)
1141  {
1142  maxi = v[i];
1143  pos = i;
1144  }
1145  }
1146  return pos;
1147  }
1148 
1159  template<class T>
1160  static size_t whichMin(const std::vector<T>& v)
1161  {
1162  if (v.size() == 0) throw EmptyVectorException<T>("VectorTools::whichMin()", &v);
1163  T mini = v[0];
1164  size_t pos = 0;
1165  for (size_t i = 1; i < v.size(); i++)
1166  {
1167  if (v[i] < mini)
1168  {
1169  mini = v[i];
1170  pos = i;
1171  }
1172  }
1173  return pos;
1174  }
1175 
1186  template<class T>
1187  static std::vector<size_t> whichMaxAll(const std::vector<T>& v)
1188  {
1189  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::whichMaxAll()", &v);
1190  T maxi = max(v);
1191  std::vector<size_t> pos;
1192  for (size_t i = 0; i < v.size(); i++)
1193  {
1194  if (v[i] == maxi)
1195  {
1196  pos.push_back(i);
1197  }
1198  }
1199  return pos;
1200  }
1201 
1212  template<class T>
1213  static std::vector<size_t> whichMinAll(const std::vector<T>& v)
1214  {
1215  if (v.size() == 0) throw EmptyVectorException<T>("VectorFuntions::whichMinAll()", &v);
1216  T mini = min(v);
1217  std::vector<size_t> pos;
1218  for (size_t i = 0; i < v.size(); i++)
1219  {
1220  if (v[i] == mini)
1221  {
1222  pos.push_back(i);
1223  }
1224  }
1225  return pos;
1226  }
1227 
1228 
1238  template<class T>
1239  static std::vector<T> range(const std::vector<T>& v)
1240  {
1241  if (v.size() == 0)
1242  throw EmptyVectorException<T>("VectorTools::range()", &v);
1243  std::vector<T> r(2);
1244  r[0] = r[1] = v[0];
1245  for (size_t i = 1; i < v.size(); i++)
1246  {
1247  if (v[i] < r[0]) r[0] = v[i];
1248  if (v[i] > r[1]) r[1] = v[i];
1249  }
1250  return r;
1251  }
1252 
1253 private:
1254  template<class T> class order_Cmp_
1255  {
1256  const std::vector<T>& values_;
1257 
1258 public:
1259  order_Cmp_(const std::vector<T>& v) : values_(v) {}
1260  bool operator()(size_t a, size_t b) { return values_[a] < values_[b]; }
1261  };
1262 
1263 public:
1273  template<class T>
1274  static std::vector<size_t> order(const std::vector<T>& v)
1275  {
1276  if (v.size() == 0)
1277  throw EmptyVectorException<T>("VectorTools::sort()", &v);
1278  // Private inner class:
1279  std::vector<size_t> index(v.size());
1280  for (size_t i = 0; i < index.size(); ++i)
1281  {
1282  index[i] = i;
1283  }
1284  sort(index.begin(), index.end(), order_Cmp_<T>(v));
1285  return index;
1286  }
1287 
1297  template<class T>
1298  static std::vector<T> abs(const std::vector<T>& v)
1299  {
1300  std::vector<T> vabs(v.size());
1301  for (size_t i = 0; i < v.size(); i++)
1302  {
1303  vabs[i] = std::abs(v[i]);
1304  }
1305  return vabs;
1306  }
1307 
1312  template<class InputType, class OutputType>
1313  static OutputType mean(const std::vector<InputType>& v1)
1314  {
1315  return (OutputType)sum<InputType>(v1) / (OutputType)v1.size();
1316  }
1323  template<class InputType, class OutputType>
1324  static OutputType mean(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool normalizeWeights = true)
1325  {
1326  if (normalizeWeights)
1327  {
1328  std::vector<InputType> wn = w / sum(w);
1329  return scalar<InputType, OutputType>(v1, wn);
1330  }
1331  else
1332  {
1333  return scalar<InputType, OutputType>(v1, w);
1334  }
1335  }
1336 
1341  template<class InputType>
1342  static InputType median(std::vector<InputType>& v1)
1343  {
1344  InputType med = 0;
1345  if (v1.size() == 0) return med;
1346  if (v1.size() == 1) return v1[0];
1347  sort(v1.begin(), v1.end());
1348  size_t i = v1.size() / 2;
1349  if (v1.size() % 2 == 0)
1350  {
1351  // Vector size is pair
1352  med = double((v1[i - 1] + v1[i]) / 2);
1353  }
1354  else
1355  {
1356  // Vector size is impair
1357  med = v1[i];
1358  }
1359  return med;
1360  }
1361 
1368  template<class InputType, class OutputType>
1369  static std::vector<OutputType> center(const std::vector<InputType>& v1)
1370  {
1371  OutputType m = mean<InputType, OutputType>(v1);
1372  std::vector<OutputType> v(v1.size());
1373  for (size_t i = 0; i < v1.size(); i++)
1374  {
1375  v[i] = (OutputType)v1[i] - m;
1376  }
1377  return v;
1378  }
1387  template<class InputType, class OutputType>
1388  static std::vector<OutputType> center(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool normalizeWeights = true)
1389  {
1390  OutputType m = mean<InputType, OutputType>(v1, w, normalizeWeights);
1391  std::vector<OutputType> v(v1.size());
1392  for (size_t i = 0; i < v1.size(); i++)
1393  {
1394  v[i] = (OutputType)v1[i] - m;
1395  }
1396  return v;
1397  }
1398 
1406  template<class InputType, class OutputType>
1407  static OutputType cov(const std::vector<InputType>& v1, const std::vector<InputType>& v2, bool unbiased = true)
1408  {
1409  OutputType n = (OutputType)v1.size();
1410  OutputType x = scalar<InputType, OutputType>(
1411  center<InputType, OutputType>(v1),
1412  center<InputType, OutputType>(v2)
1413  ) / n;
1414  if (unbiased) x = x * n / (n - 1);
1415  return x;
1416  }
1417 
1428  template<class InputType, class OutputType>
1429  static OutputType cov(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w, bool unbiased = true, bool normalizeWeights = true)
1430  {
1431  if (normalizeWeights)
1432  {
1433  std::vector<InputType> wn = w / sum(w);
1434  OutputType x = scalar<InputType, OutputType>(
1435  center<InputType, OutputType>(v1, wn, false),
1436  center<InputType, OutputType>(v2, wn, false),
1437  wn
1438  );
1439  if (unbiased)
1440  {
1441  x = x / (1 - sum(sqr<double>(wn)));
1442  }
1443  return x;
1444  }
1445  else
1446  {
1447  OutputType x = scalar<InputType, OutputType>(
1448  center<InputType, OutputType>(v1, w, false),
1449  center<InputType, OutputType>(v2, w, false),
1450  w
1451  );
1452  if (unbiased)
1453  {
1454  x = x / (1 - sum(sqr(w)));
1455  }
1456  return x;
1457  }
1458  }
1464  template<class InputType, class OutputType>
1465  static OutputType var(const std::vector<InputType>& v1, bool unbiased = true)
1466  {
1467  return cov<InputType, OutputType>(v1, v1, unbiased);
1468  }
1477  template<class InputType, class OutputType>
1478  static OutputType var(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool unbiased = true, bool normalizeWeights = true)
1479  {
1480  return cov<InputType, OutputType>(v1, v1, w, unbiased, normalizeWeights);
1481  }
1482 
1488  template<class InputType, class OutputType>
1489  static OutputType sd(const std::vector<InputType>& v1, bool unbiased = true)
1490  {
1491  return sqrt(var<InputType, OutputType>(v1, unbiased));
1492  }
1493 
1502  template<class InputType, class OutputType>
1503  static OutputType sd(const std::vector<InputType>& v1, const std::vector<InputType>& w, bool unbiased = true, bool normalizeWeights = true)
1504  {
1505  return sqrt(var<InputType, OutputType>(v1, w, unbiased, normalizeWeights));
1506  }
1507 
1514  template<class InputType, class OutputType>
1515  static OutputType cor(const std::vector<InputType>& v1, const std::vector<InputType>& v2)
1516  {
1517  return cov<InputType, OutputType>(v1, v2)
1518  / ( sd<InputType, OutputType>(v1) * sd<InputType, OutputType>(v2) );
1519  }
1520 
1529  template<class InputType, class OutputType>
1530  static OutputType cor(const std::vector<InputType>& v1, const std::vector<InputType>& v2, const std::vector<InputType>& w, bool normalizeWeights = true)
1531  {
1532  if (normalizeWeights)
1533  {
1534  std::vector<InputType> wn = w / sum(w);
1535  return cov<InputType, OutputType>(v1, v2, wn, false, false)
1536  / ( sd<InputType, OutputType>(v1, wn, false, false) * sd<InputType, OutputType>(v2, wn, false, false) );
1537  }
1538  else
1539  {
1540  return cov<InputType, OutputType>(v1, v2, w, false, false)
1541  / ( sd<InputType, OutputType>(v1, w, false, false) * sd<InputType, OutputType>(v2, w, false, false) );
1542  }
1543  }
1544 
1559  template<class InputType, class OutputType>
1560  static OutputType shannon(const std::vector<InputType>& v, double base = 2.7182818)
1561  {
1562  OutputType s = 0;
1563  for (auto x : v)
1564  {
1565  if (x > 0)
1566  s += static_cast<OutputType>(x * std::log(x) / std::log(base));
1567  }
1568  return -s;
1569  }
1570 
1585  template<class InputType, class OutputType>
1586  static OutputType shannonDiscrete(const std::vector<InputType>& v, double base = 2.7182818)
1587  {
1588  std::map<InputType, double> counts;
1589  for (auto x : v)
1590  {
1591  counts[x]++;
1592  }
1593  OutputType s = 0;
1594  double n = static_cast<double>(v.size());
1595  for (auto& it : counts)
1596  {
1597  s += static_cast<OutputType>((it.second / n) * std::log(it.second / n) / std::log(base));
1598  }
1599  return -s;
1600  }
1601 
1617  template<class InputType, class OutputType>
1618  static OutputType miDiscrete(const std::vector<InputType>& v1, const std::vector<InputType>& v2, double base = 2.7182818)
1619  {
1620  if (v1.size() != v2.size())
1621  throw DimensionException("VectorTools::miDiscrete. The two samples must have the same length.", v2.size(), v1.size());
1622  std::map<InputType, double> counts1;
1623  std::map<InputType, double> counts2;
1624  std::map<InputType, std::map<InputType, double>> counts12;
1625  for (size_t i = 0; i < v1.size(); i++)
1626  {
1627  counts1[v1[i]]++;
1628  counts2[v2[i]]++;
1629  counts12[v1[i]][v2[i]]++;
1630  }
1631  OutputType s = 0;
1632  double n = static_cast<double>(v1.size());
1633  for (auto& it1 : counts12)
1634  {
1635  for (auto& it2 : it1.second)
1636  {
1637  s += static_cast<OutputType>((it2.second / n) * std::log(it2.second * n / (counts1[it1.first] * counts2[it2.first])) / std::log(base));
1638  }
1639  }
1640  return s;
1641  }
1642 
1658  template<class InputType, class OutputType>
1659  static OutputType shannonContinuous(const std::vector<InputType>& v, double base = 2.7182818)
1660  {
1661  LinearMatrix<InputType> m(1, v.size());
1662  for (size_t i = 0; i < v.size(); i++)
1663  {
1664  m(0, i) = v[i];
1665  }
1667  OutputType s = 0;
1668  std::vector<double> x(1);
1669  for (auto it : v)
1670  {
1671  x[0] = static_cast<double>(it);
1672  s += static_cast<OutputType>(std::log(kd.kDensity(x)) / std::log(base));
1673  }
1674  return -s / static_cast<double>(v.size());
1675  }
1676 
1696  template<class InputType, class OutputType>
1697  static OutputType miContinuous(const std::vector<InputType>& v1, const std::vector<InputType>& v2, double base = 2.7182818)
1698  {
1699  if (v1.size() != v2.size())
1700  throw DimensionException("VectorTools::miContinuous. The two samples must have the same length.", v2.size(), v1.size());
1701  LinearMatrix<InputType> m1(1, v1.size());
1702  LinearMatrix<InputType> m2(1, v2.size());
1703  LinearMatrix<InputType> m12(2, v1.size());
1704  for (size_t i = 0; i < v1.size(); i++)
1705  {
1706  m1(0, i) = m12(0, i) = v1[i];
1707  m2(0, i) = m12(1, i) = v2[i];
1708  }
1712  OutputType s = 0;
1713  std::vector<double> x1(1);
1714  std::vector<double> x2(1);
1715  std::vector<double> x12(2);
1716  for (size_t i = 0; i < v1.size(); i++)
1717  {
1718  x1[0] = x12[0] = static_cast<double>(v1[i]);
1719  x2[0] = x12[1] = static_cast<double>(v2[i]);
1720  s += static_cast<OutputType>(std::log(kd12.kDensity(x12) / (kd1.kDensity(x1) * kd2.kDensity(x2))) / std::log(base));
1721  }
1722  return s / static_cast<double>(v1.size());
1723  }
1724 
1730  template<class T>
1731  static bool haveSameElements(const std::vector<T>& v1, const std::vector<T>& v2)
1732  {
1733  std::vector<T> u1(v1);
1734  std::vector<T> u2(v2);
1735  if (u1.size() != u2.size()) return false;
1736  std::sort(u1.begin(), u1.end());
1737  std::sort(u2.begin(), u2.end());
1738  return u1 == u2;
1739  }
1740 
1749  template<class T>
1750  static bool haveSameElements(std::vector<T>& v1, std::vector<T>& v2)
1751  {
1752  if (v1.size() != v2.size()) return false;
1753  std::sort(v1.begin(), v1.end());
1754  std::sort(v2.begin(), v2.end());
1755  return v1 == v2;
1756  }
1757 
1763  template<class T>
1764  static bool contains(const std::vector<T>& vec, T el)
1765  {
1766  for (auto it : vec)
1767  {
1768  if (it == el) return true;
1769  }
1770  return false;
1771  }
1772 
1773  template<class T, class U>
1774  static bool contains(const std::vector<T>& vec, U el)
1775  {
1776  for (auto it : vec)
1777  {
1778  if (it == (T)el) return true;
1779  }
1780  return false;
1781  }
1782 
1791  template<class T>
1792  static bool containsAll(std::vector<T>& v1, std::vector<T>& v2)
1793  {
1794  std::sort(v1.begin(), v1.end());
1795  std::sort(v2.begin(), v2.end());
1796  size_t j = 0;
1797  for (size_t i = 0; i < v2.size(); i++)
1798  {
1799  if (i > 0 && v2[i] == v2[i - 1]) continue;
1800  while (j < v1.size() - 1 && v1[j] < v2[i]) j++;
1801  if (v1[j] != v2[i]) return false;
1802  }
1803  return true;
1804  }
1805 
1812  template<class T>
1813  static std::vector<T> vectorUnion(const std::vector<T>& vec1, const std::vector<T>& vec2)
1814  {
1815  std::vector<T> unionEl = vec1;
1816  for (auto it : vec2)
1817  {
1818  if (!contains(unionEl, it))
1819  unionEl.push_back(it);
1820  }
1821  return unionEl;
1822  }
1823 
1829  template<class T>
1830  static std::vector<T> vectorUnion(const std::vector< std::vector<T>>& vecElementL)
1831  {
1832  std::vector<T> unionEl;
1833  for (auto it : vecElementL)
1834  {
1835  for (auto it2 : it)
1836  {
1837  if (!contains(unionEl, it2))
1838  unionEl.push_back(it2);
1839  }
1840  }
1841  return unionEl;
1842  }
1843 
1849  template<class T>
1850  static std::vector<T> vectorIntersection(const std::vector<T>& vec1, const std::vector<T>& vec2)
1851  {
1852  std::vector<T> interEl;
1853  for (auto it : vec1)
1854  {
1855  if (contains(vec2, it)) interEl.push_back(it);
1856  }
1857  return interEl;
1858  }
1859 
1860  template<class T, class U>
1861  static std::vector<T> vectorIntersection(const std::vector<T>& vec1, const std::vector<U>& vec2)
1862  {
1863  std::vector<T> interEl;
1864  for (auto it : vec1)
1865  {
1866  if (contains(vec2, it)) interEl.push_back(it);
1867  }
1868  return interEl;
1869  }
1870 
1875  template<class T>
1876  static std::vector<T> vectorIntersection(const std::vector< std::vector<T>>& vecElementL)
1877  {
1878  if (vecElementL.size() == 1) return vecElementL[0];
1879  std::vector<T> interEl;
1880  if (vecElementL.size() == 0) return interEl;
1881  for (auto it : vecElementL[0])
1882  {
1883  bool test = true;
1884  for (size_t j = 1; test && j < vecElementL.size(); j++)
1885  {
1886  if (!contains(vecElementL[j], it)) test = false;
1887  }
1888  if (test) interEl.push_back(it);
1889  }
1890  return interEl;
1891  }
1892 
1898  template<class T>
1899  static void append(std::vector<T>& vec1, const std::vector<T>& vec2)
1900  {
1901  vec1.insert(vec1.end(), vec2.begin(), vec2.end());
1902  }
1903 
1909  template<class T>
1910  static void prepend(std::vector<T>& vec1, const std::vector<T>& vec2)
1911  {
1912  vec1.insert(vec1.begin(), vec2.begin(), vec2.end());
1913  }
1914 
1915 
1920  template<class T>
1921  static std::vector<T> append(const std::vector< std::vector<T>>& vecElementL)
1922  {
1923  if (vecElementL.size() == 1) return vecElementL[0];
1924  std::vector<T> v;
1925  if (vecElementL.size() == 0) return v;
1926  for (auto it : vecElementL[0])
1927  {
1928  v.push_back(it);
1929  }
1930  return v;
1931  }
1932 
1938  template<class T>
1939  static void extend(std::vector<T>& vec1, const std::vector<T>& vec2)
1940  {
1941  for (auto it : vec2)
1942  {
1943  if (!contains(vec1, it))
1944  vec1.push_back(it);
1945  }
1946  }
1947 
1953  template<class T>
1954  static std::vector<T> rep(const std::vector<T>& vec, size_t n)
1955  {
1956  if (n == 1) return vec;
1957  std::vector<T> v;
1958  if (n == 0) return v;
1959  v.resize(vec.size() * n);
1960  for (size_t i = 0; i < v.size(); i++)
1961  {
1962  v[i] = vec[i % vec.size()];
1963  }
1964  return v;
1965  }
1966 
1976  template<class T>
1977  static void diff(std::vector<T>& v1, std::vector<T>& v2, std::vector<T>& v3)
1978  {
1979  if (v2.size() == 0) append(v3, v1);
1980  std::sort(v1.begin(), v1.end());
1981  std::sort(v2.begin(), v2.end());
1982  size_t j = 0;
1983  for (size_t i = 0; i < v1.size(); i++)
1984  {
1985  if (i > 0 && v1[i] == v1[i - 1]) continue;
1986  while (j < v2.size() - 1 && v2[j] < v1[i]) j++;
1987  if (v2[j] != v1[i]) v3.push_back(v1[i]);
1988  }
1989  }
1990 
1995  static bool test();
1996 };
1997 } // end of namespace bpp.
1998 #endif // BPP_NUMERIC_VECTORTOOLS_H
void operator/=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:251
static T sumExp(const std::vector< T > &v1)
Definition: VectorTools.h:710
Number exception: doubles.
Definition: Exceptions.h:95
static std::vector< T > pow(const std::vector< T > &v1, T &b)
Definition: VectorTools.h:841
virtual ~VectorTools()
Definition: VectorTools.h:312
static std::vector< T > unique(const std::vector< T > &v)
Send a new std::vector with unique elements.
Definition: VectorTools.h:446
static bool containsAll(std::vector< T > &v1, std::vector< T > &v2)
Definition: VectorTools.h:1792
static std::vector< T > vectorIntersection(const std::vector< std::vector< T >> &vecElementL)
Definition: VectorTools.h:1876
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:1133
static OutputType cor(const std::vector< InputType > &v1, const std::vector< InputType > &v2)
Definition: VectorTools.h:1515
static OutputType shannonDiscrete(const std::vector< InputType > &v, double base=2.7182818)
Definition: VectorTools.h:1586
static std::vector< double > log(const std::vector< T > &v1, double base)
Definition: VectorTools.h:779
static T prod(const std::vector< T > &v1)
Definition: VectorTools.h:557
static std::vector< T > vectorUnion(const std::vector< T > &vec1, const std::vector< T > &vec2)
Definition: VectorTools.h:1813
T to(const std::string &s)
Template to string conversion.
Definition: TextTools.h:170
static T max(const std::vector< T > &v)
Template function to get the maximum value of a std::vector.
Definition: VectorTools.h:1111
order_Cmp_(const std::vector< T > &v)
Definition: VectorTools.h:1259
static OutputType miContinuous(const std::vector< InputType > &v1, const std::vector< InputType > &v2, double base=2.7182818)
Definition: VectorTools.h:1697
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:1160
std::vector< Vuint > VVuint
Definition: VectorTools.h:50
std::vector< T > operator+(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:59
static std::vector< double > log10(const std::vector< T > &v1)
Definition: VectorTools.h:817
static void resize4(VVVVdouble &vvvv, size_t n1, size_t n2, size_t n3, size_t n4)
Definition: VectorTools.h:339
static std::vector< T > log(const std::vector< T > &v1)
Definition: VectorTools.h:762
static T sum(const std::vector< T > &v1)
Definition: VectorTools.h:587
std::vector< long double > Vldouble
Definition: VectorTools.h:39
static std::vector< T > vectorIntersection(const std::vector< T > &vec1, const std::vector< T > &vec2)
Definition: VectorTools.h:1850
Exception thrown when a given element was not found in the vector.
static OutputType norm(const std::vector< InputType > &v1)
Definition: VectorTools.h:1015
static std::vector< T > cumProd(const std::vector< T > &v1)
Definition: VectorTools.h:573
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:1388
static bool contains(const std::vector< T > &vec, T el)
Definition: VectorTools.h:1764
static void resize2(VVdouble &vv, size_t n1, size_t n2)
Definition: VectorTools.h:320
std::vector< VVlcomplex > VVVlcomplex
Definition: VectorTools.h:32
static T sumProd(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:601
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:1298
Matrix storage in one vector.
Definition: Matrix.h:318
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:1274
static std::string paste(const std::vector< T > &v, const std::string &delim=" ")
Concatenate a std::vector after converting to string.
Definition: VectorTools.h:856
bool operator()(size_t a, size_t b)
Definition: VectorTools.h:1260
std::vector< VVldouble > VVVldouble
Definition: VectorTools.h:41
std::vector< VVcomplex > VVVcomplex
Definition: VectorTools.h:28
static double log(const T &x)
Definition: VectorTools.h:773
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:402
static std::vector< OutputType > center(const std::vector< InputType > &v1)
Set the mean of a std::vector to be 0.
Definition: VectorTools.h:1369
static OutputType var(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true)
Definition: VectorTools.h:1478
static OutputType sd(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true)
Definition: VectorTools.h:1503
static std::vector< T > vectorUnion(const std::vector< std::vector< T >> &vecElementL)
Definition: VectorTools.h:1830
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:1899
static OutputType cov(const std::vector< InputType > &v1, const std::vector< InputType > &v2, bool unbiased=true)
Definition: VectorTools.h:1407
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:1977
static OutputType cos(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w)
Definition: VectorTools.h:1068
void operator+=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:224
std::vector< T > operator-(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:79
static std::vector< double > sin(const std::vector< T > &v1)
Definition: VectorTools.h:809
static OutputType scalar(const std::vector< InputType > &v1, const std::vector< InputType > &v2)
Definition: VectorTools.h:939
std::vector< VVint > VVVint
Definition: VectorTools.h:46
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:1429
std::vector< std::complex< double > > Vcomplex
Definition: VectorTools.h:26
std::vector< VVVint > VVVVint
Definition: VectorTools.h:47
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:422
static std::vector< double > cos(const std::vector< T > &v1)
Definition: VectorTools.h:801
static double exp(const T &x)
Definition: VectorTools.h:787
std::vector< Vcomplex > VVcomplex
Definition: VectorTools.h:27
static std::vector< T > fact(const std::vector< T > &v1)
Definition: VectorTools.h:825
static T logSumExp(const std::vector< T > &v1)
Definition: VectorTools.h:649
static OutputType var(const std::vector< InputType > &v1, bool unbiased=true)
Definition: VectorTools.h:1465
static OutputType scalar(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w)
Definition: VectorTools.h:969
std::vector< unsigned int > Vuint
Definition: VectorTools.h:49
static OutputType sd(const std::vector< InputType > &v1, bool unbiased=true)
Definition: VectorTools.h:1489
std::vector< int > Vint
Definition: VectorTools.h:44
static std::vector< T > rep(const std::vector< T > &vec, size_t n)
Definition: VectorTools.h:1954
void operator &=(std::vector< T > &v1, const C &c)
Definition: VectorTools.h:260
static OutputType cos(const std::vector< InputType > &v1, const std::vector< InputType > &v2)
Definition: VectorTools.h:1054
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:1090
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:1939
static std::map< T, size_t > countValues(const std::vector< T > &v)
Count each element of a std::vector.
Definition: VectorTools.h:510
static std::vector< T > exp(const std::vector< T > &v1)
Definition: VectorTools.h:793
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:18
std::vector< Vint > VVint
Definition: VectorTools.h:45
static T logSumExp(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:673
std::vector< double > Vdouble
Definition: VectorTools.h:34
static std::shared_ptr< OutputStream > message
The output stream where messages have to be displayed.
static size_t nclassScott(const std::vector< T > &v)
Get the optimal class number following Scott&#39;s method.
Definition: VectorTools.h:543
static std::vector< T > sqr(const std::vector< T > &v1)
Definition: VectorTools.h:833
static std::vector< T > cumSum(const std::vector< T > &v1)
Definition: VectorTools.h:624
static T sumExp(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:733
std::vector< T > operator/(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:119
OutputStream interface.
Definition: OutputStream.h:29
static void fill(std::vector< T > &v, T value)
Definition: VectorTools.h:359
static void logNorm(std::vector< T > &v)
Definition: VectorTools.h:638
static OutputType shannonContinuous(const std::vector< InputType > &v, double base=2.7182818)
Definition: VectorTools.h:1659
std::vector< T > operator*(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:99
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:919
std::vector< std::complex< long double > > Vlcomplex
Definition: VectorTools.h:30
const std::vector< T > & values_
Definition: VectorTools.h:1256
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:1187
static bool contains(const std::vector< T > &vec, U el)
Definition: VectorTools.h:1774
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:1213
std::vector< VVVdouble > VVVVdouble
Definition: VectorTools.h:37
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:1910
static bool isUnique(const std::vector< T > &v)
Tell if the std::vector as unique elements.
Definition: VectorTools.h:471
static T logMeanExp(const std::vector< T > &v1)
Definition: VectorTools.h:698
static std::vector< T > append(const std::vector< std::vector< T >> &vecElementL)
Definition: VectorTools.h:1921
static bool test()
Test function.
Definition: VectorTools.cpp:33
static OutputType mean(const std::vector< InputType > &v1)
Definition: VectorTools.h:1313
double kDensity(const std::vector< double > &x)
std::vector< VVVuint > VVVVuint
Definition: VectorTools.h:52
static std::vector< T > extract(const std::vector< T > &v1, const std::vector< size_t > &v2)
Takes elements of a vector given a vector of positions.
Definition: VectorTools.h:493
static bool haveSameElements(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:1731
static std::vector< T > vectorIntersection(const std::vector< T > &vec1, const std::vector< U > &vec2)
Definition: VectorTools.h:1861
static OutputType norm(const std::vector< InputType > &v1, const std::vector< InputType > &w)
Definition: VectorTools.h:1033
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:876
std::vector< Vldouble > VVldouble
Definition: VectorTools.h:40
static OutputType mean(const std::vector< InputType > &v1, const std::vector< InputType > &w, bool normalizeWeights=true)
Definition: VectorTools.h:1324
static std::vector< T > range(const std::vector< T > &v)
Template function to get both extrema of a std::vector.
Definition: VectorTools.h:1239
static OutputType shannon(const std::vector< InputType > &v, double base=2.7182818)
Definition: VectorTools.h:1560
static OutputType cor(const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w, bool normalizeWeights=true)
Definition: VectorTools.h:1530
static bool haveSameElements(std::vector< T > &v1, std::vector< T > &v2)
Definition: VectorTools.h:1750
static std::vector< T > kroneckerMult(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:994
std::vector< VVuint > VVVuint
Definition: VectorTools.h:51
static std::vector< T > seq(T from, T to, T by)
Build a sequence std::vector.
Definition: VectorTools.h:377
static void printRange(const std::vector< T > &v1, OutputStream &out= *ApplicationTools::message, const std::string &delim=",", const std::string &rangeDelim="-")
Definition: VectorTools.h:888
void operator-=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:233
Exception thrown when a dimension problem occured.
std::vector< VVVldouble > VVVVldouble
Definition: VectorTools.h:42
std::vector< VVdouble > VVVdouble
Definition: VectorTools.h:36
std::vector< Vdouble > VVdouble
Definition: VectorTools.h:35
static InputType median(std::vector< InputType > &v1)
Definition: VectorTools.h:1342
static void resize3(VVVdouble &vvv, size_t n1, size_t n2, size_t n3)
Definition: VectorTools.h:326
static OutputType miDiscrete(const std::vector< InputType > &v1, const std::vector< InputType > &v2, double base=2.7182818)
Definition: VectorTools.h:1618
void operator*=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorTools.h:242
std::vector< Vlcomplex > VVlcomplex
Definition: VectorTools.h:31