MultidimensionalGaussian.h
Go to the documentation of this file.
1 
18 #pragma once
19 
20 #include <boost/shared_ptr.hpp>
21 #include <iostream>
22 #include <iomanip>
23 #include <string>
24 #include <map>
25 #include <random>
26 #include <cmath>
27 #include <Eigen/Cholesky>
28 #include <Eigen/Core>
29 #include "Gaussian.h"
30 
31 using namespace Eigen;
32 using namespace std;
33 
34 namespace ASR
35 {
36 
42  {
43  private:
44  std::vector<float> means;
45  MatrixXf covariance;
46  MatrixXf L;
48  int dimension;
49 
50  public:
57  MultidimensionalGaussian(std::vector<float> means, MatrixXf covariance)
58  {
59  this->means = means;
60  this->covariance = covariance;
61  this->dimension = means.size();
62 
63  normal_dist = GaussianPtr(new ASR::Gaussian(0,1));
64 
65  // Calculate Cholesky decomposition
66  LLT<MatrixXf> lltOfA(covariance); // compute the Cholesky decomposition of A
67  this->L = lltOfA.matrixL(); // retrieve factor L in the decomposition
68 
69  // For debugging only
70  //test();
71  }
72 
78  void sampleRandomValues(std::vector<float> &x)
79  {
80  //ROS_ASSERT(x != nullptr);
81 
82  // 1. A = L * L.transpose()
83  // 2. Erzeuge N-dim Vector mit standartnormalverteilten Zufallszahlen
84  std::vector<float> z;
85  normal_dist->sampleRandomValues(z, dimension);
86  // 3. transform x = means + L * random
87  VectorXf Z = initVectorXf(z);
88  VectorXf mu = initVectorXf(means);
89  VectorXf X = mu + L * Z;
90 
91  // Get the valus of the VectorXf and convert them to a std::vector
92  float *t1 = X.data();
93  for(int i=0; i < dimension; i++)
94  {
95  x.push_back(*(t1 + i));
96  }
97  }
98 
103  std::vector<float> getMean() {return means;}
104 
105  private:
111  VectorXf initVectorXf(std::vector<float> values)
112  {
113  VectorXf x(values.size());
114  for(unsigned int i=0; i<values.size();i++)
115  {
116  x(i) = values.at(i);
117  }
118  return x;
119  }
120 
121 
127  MatrixXf initMatrixXf(std::vector<float> values, unsigned int rows, unsigned int cols)
128  {
129  assert (rows > 0);
130  assert (cols > 0);
131 
132  MatrixXf x(rows, cols);
133  for(unsigned int i=0; i < rows;i++)
134  {
135  for(unsigned int j=0; j < cols; j++)
136  {
137  x(i,j) = values.at(i*cols + j);
138  }
139  }
140  return x;
141  }
142 
149  void initVectorFromCSVString(std::vector<float> &x, std::string csv)
150  {
151  std::stringstream ss(csv);
152  float i;
153  while (ss >> i)
154  {
155  x.push_back(i);
156 
157  if (ss.peek() == ',' || ss.peek() == ' ')
158  ss.ignore();
159  }
160  }
161 
162 
166  void test()
167  {
168  cout << "The means are " << endl << initVectorXf(this->means) << endl;
169  cout << "The matrix A is" << endl << covariance << endl;
170  cout << "The Cholesky factor L is" << endl << L << endl;
171  cout << "To check this, let us compute L * L.transpose()" << endl;
172  cout << L * L.transpose() << endl;
173  cout << "This should equal the matrix A" << endl;
174  }
175 
176  };
177 
179 
180 }
MultidimensionalGaussian(std::vector< float > means, MatrixXf covariance)
VectorXf initVectorXf(std::vector< float > values)
MatrixXf initMatrixXf(std::vector< float > values, unsigned int rows, unsigned int cols)
void sampleRandomValues(std::vector< float > &x)
boost::shared_ptr< Gaussian > GaussianPtr
Definition: Gaussian.h:120
void initVectorFromCSVString(std::vector< float > &x, std::string csv)
TFSIMD_FORCE_INLINE const tfScalar & x() const
TFSIMD_FORCE_INLINE const tfScalar & z() const
boost::shared_ptr< MultidimensionalGaussian > MultidimensionalGaussianPtr


asr_recognizer_prediction_psm
Author(s): Braun Kai, Meißner Pascal
autogenerated on Wed Feb 19 2020 03:31:30