bpp-core3  3.0.0
VectorTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 // From Utils:
6 #include "../Text/TextTools.h"
7 
8 #include "VectorTools.h"
9 using namespace bpp;
10 
11 // From the STL:
12 #include <cmath>
13 #include <iostream>
14 using namespace std;
15 
16 /******************************************************************************/
17 
18 vector<double> VectorTools::breaks(const vector<double>& v, unsigned int n)
19 {
20  vector<double> out;
21  vector<double> r = VectorTools::range(v);
22  double part = (r[1] - r[0]) / n;
23  for (unsigned int i = 0; i < n; ++i)
24  {
25  out.push_back(r[0] + (part * i));
26  }
27  out.push_back(r[1]);
28  return out;
29 }
30 
31 /******************************************************************************/
32 
34 {
35  vector<double> x1(5);
36  vector<double> x2(5);
37  x1[0] = -3.4;
38  x1[1] = 1.8;
39  x1[2] = -2.1;
40  x1[3] = -2.5;
41  x1[4] = 1.0;
42 
43  x2[0] = -5.3;
44  x2[1] = -4.8;
45  x2[2] = 2.7;
46  x2[3] = 7.2;
47  x2[4] = 0.4;
48 
49  print(x1);
50  print(x2);
51  double m1 = mean<double, double>(x1);
52  double m2 = mean<double, double>(x2);
53  double v1 = var<double, double>(x1);
54  double v2 = var<double, double>(x2);
55  cout << "Mean x1 = " << m1 << "\tVar x1 = " << v1 << endl;
56  cout << "Mean x2 = " << m2 << "\tVar x2 = " << v2 << endl;
57  cov<double, double>(x1, x2);
58  cor<double, double>(x1, x2);
59  cos<double, double>(x1, x2);
60  shannon<double, double>(x1);
61  return m1 == -0.2 && m2 == 0.04 && v1 == 6.565 && v2 == 27.603;
62 }
63 
64 /******************************************************************************/
STL namespace.
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
static bool test()
Test function.
Definition: VectorTools.cpp:33
static std::vector< T > range(const std::vector< T > &v)
Template function to get both extrema of a std::vector.
Definition: VectorTools.h:1239