gtsam 4.1.1
gtsam
ParameterMatrix.h
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
28namespace gtsam {
29
37template <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
202template <int M>
204 : public internal::VectorSpace<ParameterMatrix<M>> {};
205
206/* ************************************************************************* */
207// Stream operator that takes a ParameterMatrix. Used for printing.
208template <int M>
209inline std::ostream& operator<<(std::ostream& os,
210 const ParameterMatrix<M>& parameterMatrix) {
211 os << parameterMatrix.matrix();
212 return os;
213}
214
215} // namespace gtsam
typedef and functions to augment Eigen's MatrixXd
Concept check for values that can be used in unit tests.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
equals with a tolerance
Definition: Matrix.h:84
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
VectorSpace provides both Testable and VectorSpaceTraits.
Definition: VectorSpace.h:207
A matrix abstraction of MxN values at the Basis points.
Definition: ParameterMatrix.h:38
MatrixType operator*(const Eigen::Matrix< double, -1, -1 > &other) const
Multiply ParameterMatrix with an Eigen matrix.
Definition: ParameterMatrix.h:152
ParameterMatrix< M > operator-(const Eigen::Matrix< double, -1, 1 > &other) const
Subtract a MxN-sized vector from the ParameterMatrix.
Definition: ParameterMatrix.h:141
Eigen::Matrix< double, 1, -1 > row(size_t index) const
Get the matrix row specified by index.
Definition: ParameterMatrix.h:77
ParameterMatrix(const size_t N)
Create ParameterMatrix using the number of basis points.
Definition: ParameterMatrix.h:53
void setZero()
Set all matrix coefficients to zero.
Definition: ParameterMatrix.h:108
MatrixType matrix() const
Get the underlying matrix.
Definition: ParameterMatrix.h:68
ParameterMatrix(const MatrixType &matrix)
Create ParameterMatrix from an MxN Eigen Matrix.
Definition: ParameterMatrix.h:59
Eigen::Matrix< double, M, 1 > col(size_t index) const
Get the matrix column specified by index.
Definition: ParameterMatrix.h:93
Vector vector() const
Convert to vector form, is done row-wise.
Definition: ParameterMatrix.h:180
bool equals(const ParameterMatrix< M > &other, double tol=1e-8) const
Check for equality up to absolute tolerance.
Definition: ParameterMatrix.h:172
ParameterMatrix< M > operator+(const ParameterMatrix< M > &other) const
Add a ParameterMatrix to another.
Definition: ParameterMatrix.h:114
static ParameterMatrix identity()
Identity function to satisfy VectorSpace traits.
Definition: ParameterMatrix.h:192
size_t rows() const
Get the number of rows.
Definition: ParameterMatrix.h:62
auto row(size_t index) -> Eigen::Block< MatrixType, 1, -1, false >
Set the matrix row specified by index.
Definition: ParameterMatrix.h:85
void print(const std::string &s="") const
Print the ParameterMatrix.
Definition: ParameterMatrix.h:163
auto col(size_t index) -> Eigen::Block< MatrixType, M, 1, true >
Set the matrix column specified by index.
Definition: ParameterMatrix.h:101
ParameterMatrix< M > operator+(const Eigen::Matrix< double, -1, 1 > &other) const
Add a MxN-sized vector to the ParameterMatrix.
Definition: ParameterMatrix.h:122
ParameterMatrix< M > operator-(const ParameterMatrix< M > &other) const
Subtract a ParameterMatrix from another.
Definition: ParameterMatrix.h:133
size_t dim() const
Returns dimensionality of the tangent space.
Definition: ParameterMatrix.h:177
size_t cols() const
Get the number of columns.
Definition: ParameterMatrix.h:65
Eigen::Matrix< double, -1, M > transpose() const
Return the tranpose of the underlying matrix.
Definition: ParameterMatrix.h:71