Loading...
Searching...
No Matches
ParameterMatrix.h
Go to the documentation of this file.
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
#pragma once
#include <gtsam/base/Matrix.h>
#include <gtsam/base/Testable.h>
#include <gtsam/base/VectorSpace.h>
#include <iostream>
namespace gtsam {
template <int M>
class ParameterMatrix {
using MatrixType = Eigen::Matrix<double, M, -1>;
private:
MatrixType matrix_;
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
enum { dimension = Eigen::Dynamic };
ParameterMatrix(const size_t N) : matrix_(M, N) { matrix_.setZero(); }
ParameterMatrix(const MatrixType& matrix) : matrix_(matrix) {}
size_t rows() const { return matrix_.rows(); }
size_t cols() const { return matrix_.cols(); }
MatrixType matrix() const { return matrix_; }
Eigen::Matrix<double, -1, M> transpose() const { return matrix_.transpose(); }
Eigen::Matrix<double, 1, -1> row(size_t index) const {
return matrix_.row(index);
}
auto row(size_t index) -> Eigen::Block<MatrixType, 1, -1, false> {
return matrix_.row(index);
}
Eigen::Matrix<double, M, 1> col(size_t index) const {
return matrix_.col(index);
}
auto col(size_t index) -> Eigen::Block<MatrixType, M, 1, true> {
return matrix_.col(index);
}
void setZero() { matrix_.setZero(); }
ParameterMatrix<M> operator+(const ParameterMatrix<M>& other) const {
return ParameterMatrix<M>(matrix_ + other.matrix());
}
ParameterMatrix<M> operator+(
const Eigen::Matrix<double, -1, 1>& other) const {
// This form avoids a deep copy and instead typecasts `other`.
Eigen::Map<const MatrixType> other_(other.data(), M, cols());
return ParameterMatrix<M>(matrix_ + other_);
}
ParameterMatrix<M> operator-(const ParameterMatrix<M>& other) const {
return ParameterMatrix<M>(matrix_ - other.matrix());
}
ParameterMatrix<M> operator-(
const Eigen::Matrix<double, -1, 1>& other) const {
Eigen::Map<const MatrixType> other_(other.data(), M, cols());
return ParameterMatrix<M>(matrix_ - other_);
}
MatrixType operator*(const Eigen::Matrix<double, -1, -1>& other) const {
return matrix_ * other;
}
void print(const std::string& s = "") const {
std::cout << (s == "" ? s : s + " ") << matrix_ << std::endl;
}
bool equals(const ParameterMatrix<M>& other, double tol = 1e-8) const {
return gtsam::equal_with_abs_tol(matrix_, other.matrix(), tol);
}
inline size_t dim() const { return matrix_.size(); }
inline Vector vector() const {
using RowMajor = Eigen::Matrix<double, -1, -1, Eigen::RowMajor>;
Vector result(matrix_.size());
Eigen::Map<RowMajor>(&result(0), rows(), cols()) = matrix_;
return result;
}
inline static ParameterMatrix Identity() {
// throw std::runtime_error(
// "ParameterMatrix::Identity(): Don't use this function");
return ParameterMatrix(0);
}
};
// traits for ParameterMatrix
template <int M>
struct traits<ParameterMatrix<M>>
: public internal::VectorSpace<ParameterMatrix<M>> {};
/* ************************************************************************* */
// Stream operator that takes a ParameterMatrix. Used for printing.
template <int M>
inline std::ostream& operator<<(std::ostream& os,
const ParameterMatrix<M>& parameterMatrix) {
os << parameterMatrix.matrix();
return os;
}
} // namespace gtsam
Testable.h
Concept check for values that can be used in unit tests.
Matrix.h
typedef and functions to augment Eigen's MatrixXd
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::equal_with_abs_tol
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:108
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
gtsam::internal::VectorSpace
VectorSpace provides both Testable and VectorSpaceTraits.
Definition VectorSpace.h:207
gtsam::ParameterMatrix
A matrix abstraction of MxN values at the Basis points.
Definition ParameterMatrix.h:38
gtsam::ParameterMatrix::operator*
MatrixType operator*(const Eigen::Matrix< double, -1, -1 > &other) const
Multiply ParameterMatrix with an Eigen matrix.
Definition ParameterMatrix.h:152
gtsam::ParameterMatrix::operator-
ParameterMatrix< M > operator-(const Eigen::Matrix< double, -1, 1 > &other) const
Subtract a MxN-sized vector from the ParameterMatrix.
Definition ParameterMatrix.h:141
gtsam::ParameterMatrix::row
Eigen::Matrix< double, 1, -1 > row(size_t index) const
Get the matrix row specified by index.
Definition ParameterMatrix.h:77
gtsam::ParameterMatrix::ParameterMatrix
ParameterMatrix(const size_t N)
Create ParameterMatrix using the number of basis points.
Definition ParameterMatrix.h:53
gtsam::ParameterMatrix::Identity
static ParameterMatrix Identity()
Identity function to satisfy VectorSpace traits.
Definition ParameterMatrix.h:192
gtsam::ParameterMatrix::setZero
void setZero()
Set all matrix coefficients to zero.
Definition ParameterMatrix.h:108
gtsam::ParameterMatrix::matrix
MatrixType matrix() const
Get the underlying matrix.
Definition ParameterMatrix.h:68
gtsam::ParameterMatrix::ParameterMatrix
ParameterMatrix(const MatrixType &matrix)
Create ParameterMatrix from an MxN Eigen Matrix.
Definition ParameterMatrix.h:59
gtsam::ParameterMatrix::col
Eigen::Matrix< double, M, 1 > col(size_t index) const
Get the matrix column specified by index.
Definition ParameterMatrix.h:93
gtsam::ParameterMatrix::vector
Vector vector() const
Convert to vector form, is done row-wise.
Definition ParameterMatrix.h:180
gtsam::ParameterMatrix::equals
bool equals(const ParameterMatrix< M > &other, double tol=1e-8) const
Check for equality up to absolute tolerance.
Definition ParameterMatrix.h:172
gtsam::ParameterMatrix::operator+
ParameterMatrix< M > operator+(const ParameterMatrix< M > &other) const
Add a ParameterMatrix to another.
Definition ParameterMatrix.h:114
gtsam::ParameterMatrix::rows
size_t rows() const
Get the number of rows.
Definition ParameterMatrix.h:62
gtsam::ParameterMatrix::row
auto row(size_t index) -> Eigen::Block< MatrixType, 1, -1, false >
Set the matrix row specified by index.
Definition ParameterMatrix.h:85
gtsam::ParameterMatrix::print
void print(const std::string &s="") const
Print the ParameterMatrix.
Definition ParameterMatrix.h:163
gtsam::ParameterMatrix::col
auto col(size_t index) -> Eigen::Block< MatrixType, M, 1, true >
Set the matrix column specified by index.
Definition ParameterMatrix.h:101
gtsam::ParameterMatrix::operator+
ParameterMatrix< M > operator+(const Eigen::Matrix< double, -1, 1 > &other) const
Add a MxN-sized vector to the ParameterMatrix.
Definition ParameterMatrix.h:122
gtsam::ParameterMatrix::operator-
ParameterMatrix< M > operator-(const ParameterMatrix< M > &other) const
Subtract a ParameterMatrix from another.
Definition ParameterMatrix.h:133
gtsam::ParameterMatrix::dim
size_t dim() const
Returns dimensionality of the tangent space.
Definition ParameterMatrix.h:177
gtsam::ParameterMatrix::cols
size_t cols() const
Get the number of columns.
Definition ParameterMatrix.h:65
gtsam::ParameterMatrix::transpose
Eigen::Matrix< double, -1, M > transpose() const
Return the tranpose of the underlying matrix.
Definition ParameterMatrix.h:71