Bayes Filters Library
LinearModel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
9 
10 #include <cmath>
11 #include <iostream>
12 #include <utility>
13 
14 #include <Eigen/Cholesky>
15 
16 using namespace bfl;
17 using namespace Eigen;
18 
19 
21 (
22  const LinearMatrixComponent& linear_matrix_component,
23  const Ref<const MatrixXd>& noise_covariance_matrix,
24  const unsigned int seed
25 ) :
26  LTIMeasurementModel(MatrixXd::Zero(linear_matrix_component.second.size(), linear_matrix_component.first), noise_covariance_matrix),
27  generator_(std::mt19937_64(seed)),
28  distribution_(std::normal_distribution<double>(0.0, 1.0)),
29  gauss_rnd_sample_([&] { return (distribution_)(generator_); })
30 {
31  for (std::size_t i = 0; i < linear_matrix_component.second.size(); ++i)
32  {
33  const std::size_t& component_index = linear_matrix_component.second[i];
34 
35  if (component_index < linear_matrix_component.first)
36  H_(i, component_index) = 1.0;
37  else
38  throw std::runtime_error(std::string("ERROR::LINEARMODEL::CTOR\nERROR:\n\tIndex component out of bound.\nLOG:\n\tProvided: ") + std::to_string(component_index) + ". Index bound: " + std::to_string(linear_matrix_component.first) + ".");
39  }
40 
41  LDLT<MatrixXd> chol_ldlt(R_);
42  sqrt_R_ = (chol_ldlt.transpositionsP() * MatrixXd::Identity(R_.rows(), R_.cols())).transpose() * chol_ldlt.matrixL() * chol_ldlt.vectorD().real().cwiseSqrt().asDiagonal();
43 }
44 
45 
46 LinearModel::LinearModel(const LinearMatrixComponent& linear_matrix_component, const Ref<const MatrixXd>& noise_covariance_matrix) :
47  LinearModel(linear_matrix_component, noise_covariance_matrix, 1)
48 { }
49 
50 
51 std::pair<bool, MatrixXd> LinearModel::getNoiseSample(const int num) const
52 {
53  MatrixXd rand_vectors(2, num);
54  for (int i = 0; i < rand_vectors.size(); i++)
55  *(rand_vectors.data() + i) = gauss_rnd_sample_();
56 
57  MatrixXd noise_sample = sqrt_R_ * rand_vectors;
58 
59  return std::make_pair(true, std::move(noise_sample));
60 }
61 
62 
63 std::pair<bool, MatrixXd> LinearModel::getNoiseCovarianceMatrix() const
64 {
65  return std::make_pair(true, R_);
66 }
67 
68 
69 Eigen::MatrixXd LinearModel::getMeasurementMatrix() const
70 {
71  return H_;
72 }
bfl::LinearModel
Definition: LinearModel.h:24
bfl::LTIMeasurementModel
This class represent an linear measurement model Hx + w, where H is a time-invariant measurement matr...
Definition: LTIMeasurementModel.h:23
bfl::LinearModel::LinearModel
LinearModel(const LinearMatrixComponent &linear_matrix_component, const Eigen::Ref< const Eigen::MatrixXd > &noise_covariance_matrix, const unsigned int seed)
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
bfl::LinearModel::gauss_rnd_sample_
std::function< double()> gauss_rnd_sample_
Random number generator function from a Normal distribution.
Definition: LinearModel.h:67
bfl::LinearModel::sqrt_R_
Eigen::MatrixXd sqrt_R_
Square root matrix of R_.
Definition: LinearModel.h:61
bfl::LinearModel::getNoiseCovarianceMatrix
std::pair< bool, Eigen::MatrixXd > getNoiseCovarianceMatrix() const override
Definition: LinearModel.cpp:63
bfl::LTIMeasurementModel::H_
Eigen::MatrixXd H_
Measurement matrix.
Definition: LTIMeasurementModel.h:39
bfl::LTIMeasurementModel::R_
Eigen::MatrixXd R_
Convariance matrix of the additive white noise of the measurements.
Definition: LTIMeasurementModel.h:44
bfl::LinearModel::getNoiseSample
std::pair< bool, Eigen::MatrixXd > getNoiseSample(const int num) const
Definition: LinearModel.cpp:51
LinearModel.h
bfl::LinearModel::getMeasurementMatrix
Eigen::MatrixXd getMeasurementMatrix() const override
Definition: LinearModel.cpp:69