GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
ParameterMatrix.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 #pragma once
21 
22 #include <gtsam/base/Matrix.h>
23 #include <gtsam/base/Testable.h>
24 #include <gtsam/base/VectorSpace.h>
25 
26 #include <iostream>
27 
28 namespace gtsam {
29 
37 template <int M>
39  using MatrixType = Eigen::Matrix<double, M, -1>;
40 
41  private:
42  MatrixType matrix_;
43 
44  public:
45  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
46 
47  enum { dimension = Eigen::Dynamic };
48 
53  ParameterMatrix(const size_t N) : matrix_(M, N) { matrix_.setZero(); }
54 
59  ParameterMatrix(const MatrixType& matrix) : matrix_(matrix) {}
60 
62  size_t rows() const { return matrix_.rows(); }
63 
65  size_t cols() const { return matrix_.cols(); }
66 
68  MatrixType matrix() const { return matrix_; }
69 
71  Eigen::Matrix<double, -1, M> transpose() const { return matrix_.transpose(); }
72 
77  Eigen::Matrix<double, 1, -1> row(size_t index) const {
78  return matrix_.row(index);
79  }
80 
85  auto row(size_t index) -> Eigen::Block<MatrixType, 1, -1, false> {
86  return matrix_.row(index);
87  }
88 
93  Eigen::Matrix<double, M, 1> col(size_t index) const {
94  return matrix_.col(index);
95  }
96 
101  auto col(size_t index) -> Eigen::Block<MatrixType, M, 1, true> {
102  return matrix_.col(index);
103  }
104 
108  void setZero() { matrix_.setZero(); }
109 
115  return ParameterMatrix<M>(matrix_ + other.matrix());
116  }
117 
123  const Eigen::Matrix<double, -1, 1>& other) const {
124  // This form avoids a deep copy and instead typecasts `other`.
125  Eigen::Map<const MatrixType> other_(other.data(), M, cols());
126  return ParameterMatrix<M>(matrix_ + other_);
127  }
128 
134  return ParameterMatrix<M>(matrix_ - other.matrix());
135  }
136 
142  const Eigen::Matrix<double, -1, 1>& other) const {
143  Eigen::Map<const MatrixType> other_(other.data(), M, cols());
144  return ParameterMatrix<M>(matrix_ - other_);
145  }
146 
152  MatrixType operator*(const Eigen::Matrix<double, -1, -1>& other) const {
153  return matrix_ * other;
154  }
155 
158 
163  void print(const std::string& s = "") const {
164  std::cout << (s == "" ? s : s + " ") << matrix_ << std::endl;
165  }
166 
172  bool equals(const ParameterMatrix<M>& other, double tol = 1e-8) const {
173  return gtsam::equal_with_abs_tol(matrix_, other.matrix(), tol);
174  }
175 
177  inline size_t dim() const { return matrix_.size(); }
178 
180  inline Vector vector() const {
181  using RowMajor = Eigen::Matrix<double, -1, -1, Eigen::RowMajor>;
182  Vector result(matrix_.size());
183  Eigen::Map<RowMajor>(&result(0), rows(), cols()) = matrix_;
184  return result;
185  }
186 
192  inline static ParameterMatrix Identity() {
193  // throw std::runtime_error(
194  // "ParameterMatrix::Identity(): Don't use this function");
195  return ParameterMatrix(0);
196  }
197 
199 };
200 
201 // traits for ParameterMatrix
202 template <int M>
204  : public internal::VectorSpace<ParameterMatrix<M>> {};
205 
206 /* ************************************************************************* */
207 // Stream operator that takes a ParameterMatrix. Used for printing.
208 template <int M>
209 inline std::ostream& operator<<(std::ostream& os,
210  const ParameterMatrix<M>& parameterMatrix) {
211  os << parameterMatrix.matrix();
212  return os;
213 }
214 
215 } // namespace gtsam
ParameterMatrix< M > operator-(const Eigen::Matrix< double, -1, 1 > &other) const
Definition: ParameterMatrix.h:141
MatrixType operator*(const Eigen::Matrix< double, -1, -1 > &other) const
Definition: ParameterMatrix.h:152
bool equals(const ParameterMatrix< M > &other, double tol=1e-8) const
Definition: ParameterMatrix.h:172
Concept check for values that can be used in unit tests.
VectorSpace provides both Testable and VectorSpaceTraits.
Definition: VectorSpace.h:207
Definition: Group.h:43
size_t rows() const
Get the number of rows.
Definition: ParameterMatrix.h:62
auto col(size_t index) -> Eigen::Block< MatrixType, M, 1, true >
Definition: ParameterMatrix.h:101
Eigen::Matrix< double, -1, M > transpose() const
Return the tranpose of the underlying matrix.
Definition: ParameterMatrix.h:71
Eigen::Matrix< double, 1, -1 > row(size_t index) const
Definition: ParameterMatrix.h:77
ParameterMatrix(const size_t N)
Definition: ParameterMatrix.h:53
size_t dim() const
Returns dimensionality of the tangent space.
Definition: ParameterMatrix.h:177
ParameterMatrix< M > operator+(const ParameterMatrix< M > &other) const
Definition: ParameterMatrix.h:114
void setZero()
Definition: ParameterMatrix.h:108
Vector vector() const
Convert to vector form, is done row-wise.
Definition: ParameterMatrix.h:180
auto row(size_t index) -> Eigen::Block< MatrixType, 1, -1, false >
Definition: ParameterMatrix.h:85
typedef and functions to augment Eigen&#39;s MatrixXd
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
Definition: Matrix.h:80
ParameterMatrix< M > operator+(const Eigen::Matrix< double, -1, 1 > &other) const
Definition: ParameterMatrix.h:122
Definition: chartTesting.h:28
size_t cols() const
Get the number of columns.
Definition: ParameterMatrix.h:65
void print(const std::string &s="") const
Definition: ParameterMatrix.h:163
Eigen::Matrix< double, M, 1 > col(size_t index) const
Definition: ParameterMatrix.h:93
Definition: ParameterMatrix.h:38
MatrixType matrix() const
Get the underlying matrix.
Definition: ParameterMatrix.h:68
ParameterMatrix(const MatrixType &matrix)
Definition: ParameterMatrix.h:59
static ParameterMatrix Identity()
Definition: ParameterMatrix.h:192
ParameterMatrix< M > operator-(const ParameterMatrix< M > &other) const
Definition: ParameterMatrix.h:133