gtsam 4.2.2
gtsam
Loading...
Searching...
No Matches
Matrix.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
22
23// \callgraph
24
25#pragma once
26
28#include <gtsam/base/Vector.h>
29#include <boost/tuple/tuple.hpp>
30
31#include <vector>
32
38namespace gtsam {
39
40typedef Eigen::MatrixXd Matrix;
41typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatrixRowMajor;
42
43// Create handy typedefs and constants for square-size matrices
44// MatrixMN, MatrixN = MatrixNN, I_NxN, and Z_NxN, for M,N=1..9
45#define GTSAM_MAKE_MATRIX_DEFS(N) \
46using Matrix##N = Eigen::Matrix<double, N, N>; \
47using Matrix1##N = Eigen::Matrix<double, 1, N>; \
48using Matrix2##N = Eigen::Matrix<double, 2, N>; \
49using Matrix3##N = Eigen::Matrix<double, 3, N>; \
50using Matrix4##N = Eigen::Matrix<double, 4, N>; \
51using Matrix5##N = Eigen::Matrix<double, 5, N>; \
52using Matrix6##N = Eigen::Matrix<double, 6, N>; \
53using Matrix7##N = Eigen::Matrix<double, 7, N>; \
54using Matrix8##N = Eigen::Matrix<double, 8, N>; \
55using Matrix9##N = Eigen::Matrix<double, 9, N>;
56
57GTSAM_MAKE_MATRIX_DEFS(1)
58GTSAM_MAKE_MATRIX_DEFS(2)
59GTSAM_MAKE_MATRIX_DEFS(3)
60GTSAM_MAKE_MATRIX_DEFS(4)
61GTSAM_MAKE_MATRIX_DEFS(5)
62GTSAM_MAKE_MATRIX_DEFS(6)
63GTSAM_MAKE_MATRIX_DEFS(7)
64GTSAM_MAKE_MATRIX_DEFS(8)
65GTSAM_MAKE_MATRIX_DEFS(9)
66
67#define I_1x1 Matrix1::Identity()
68#define Z_1x1 Matrix1::Constant(0.0)
69
70#define I_2x2 Matrix2::Identity()
71#define Z_2x2 Matrix2::Constant(0.0)
72
73#define I_3x3 Matrix3::Identity()
74#define Z_3x3 Matrix3::Constant(0.0)
75
76#define I_4x4 Matrix4::Identity()
77#define Z_4x4 Matrix4::Constant(0.0)
78
79#define I_5x5 Matrix5::Identity()
80#define Z_5x5 Matrix5::Constant(0.0)
81
82#define I_6x6 Matrix6::Identity()
83#define Z_6x6 Matrix6::Constant(0.0)
84
85#define I_7x7 Matrix7::Identity()
86#define Z_7x7 Matrix7::Constant(0.0)
87
88#define I_8x8 Matrix8::Identity()
89#define Z_8x8 Matrix8::Constant(0.0)
90
91#define I_9x9 Matrix9::Identity()
92#define Z_9x9 Matrix9::Constant(0.0)
93
94
95
96// Matrix expressions for accessing parts of matrices
97typedef Eigen::Block<Matrix> SubMatrix;
98typedef Eigen::Block<const Matrix> ConstSubMatrix;
99
100// Matrix formatting arguments when printing.
101// Akin to Matlab style.
102const Eigen::IOFormat& matlabFormat();
103
107template <class MATRIX>
108bool equal_with_abs_tol(const Eigen::DenseBase<MATRIX>& A, const Eigen::DenseBase<MATRIX>& B, double tol = 1e-9) {
109
110 const size_t n1 = A.cols(), m1 = A.rows();
111 const size_t n2 = B.cols(), m2 = B.rows();
112
113 if(m1!=m2 || n1!=n2) return false;
114
115 for(size_t i=0; i<m1; i++)
116 for(size_t j=0; j<n1; j++) {
117 if(!fpEqual(A(i,j), B(i,j), tol, false)) {
118 return false;
119 }
120 }
121 return true;
122}
123
127inline bool operator==(const Matrix& A, const Matrix& B) {
128 return equal_with_abs_tol(A,B,1e-9);
129}
130
134inline bool operator!=(const Matrix& A, const Matrix& B) {
135 return !(A==B);
136 }
137
141GTSAM_EXPORT bool assert_equal(const Matrix& A, const Matrix& B, double tol = 1e-9);
142
146GTSAM_EXPORT bool assert_inequal(const Matrix& A, const Matrix& B, double tol = 1e-9);
147
151GTSAM_EXPORT bool assert_equal(const std::list<Matrix>& As, const std::list<Matrix>& Bs, double tol = 1e-9);
152
156GTSAM_EXPORT bool linear_independent(const Matrix& A, const Matrix& B, double tol = 1e-9);
157
161GTSAM_EXPORT bool linear_dependent(const Matrix& A, const Matrix& B, double tol = 1e-9);
162
167GTSAM_EXPORT Vector operator^(const Matrix& A, const Vector & v);
168
170template<class MATRIX>
171inline MATRIX prod(const MATRIX& A, const MATRIX&B) {
172 MATRIX result = A * B;
173 return result;
174}
175
179GTSAM_EXPORT void print(const Matrix& A, const std::string& s, std::ostream& stream);
180
184GTSAM_EXPORT void print(const Matrix& A, const std::string& s = "");
185
189GTSAM_EXPORT void save(const Matrix& A, const std::string &s, const std::string& filename);
190
196GTSAM_EXPORT std::istream& operator>>(std::istream& inputStream, Matrix& destinationMatrix);
197
207template<class MATRIX>
208Eigen::Block<const MATRIX> sub(const MATRIX& A, size_t i1, size_t i2, size_t j1, size_t j2) {
209 size_t m=i2-i1, n=j2-j1;
210 return A.block(i1,j1,m,n);
211}
212
221template <typename Derived1, typename Derived2>
222void insertSub(Eigen::MatrixBase<Derived1>& fullMatrix, const Eigen::MatrixBase<Derived2>& subMatrix, size_t i, size_t j) {
223 fullMatrix.block(i, j, subMatrix.rows(), subMatrix.cols()) = subMatrix;
224}
225
229GTSAM_EXPORT Matrix diag(const std::vector<Matrix>& Hs);
230
237template<class MATRIX>
238const typename MATRIX::ConstColXpr column(const MATRIX& A, size_t j) {
239 return A.col(j);
240}
241
248template<class MATRIX>
249const typename MATRIX::ConstRowXpr row(const MATRIX& A, size_t j) {
250 return A.row(j);
251}
252
258template<class MATRIX>
259void zeroBelowDiagonal(MATRIX& A, size_t cols=0) {
260 const size_t m = A.rows(), n = A.cols();
261 const size_t k = (cols) ? std::min(cols, std::min(m,n)) : std::min(m,n);
262 for (size_t j=0; j<k; ++j)
263 A.col(j).segment(j+1, m-(j+1)).setZero();
264}
265
269inline Matrix trans(const Matrix& A) { return A.transpose(); }
270
272template <int OutM, int OutN, int OutOptions, int InM, int InN, int InOptions>
273struct Reshape {
274 //TODO replace this with Eigen's reshape function as soon as available. (There is a PR already pending : https://bitbucket.org/eigen/eigen/pull-request/41/reshape/diff)
275 typedef Eigen::Map<const Eigen::Matrix<double, OutM, OutN, OutOptions> > ReshapedType;
276 static inline ReshapedType reshape(const Eigen::Matrix<double, InM, InN, InOptions> & in) {
277 return in.data();
278 }
279};
280
282template <int M, int InOptions>
283struct Reshape<M, M, InOptions, M, M, InOptions> {
284 typedef const Eigen::Matrix<double, M, M, InOptions> & ReshapedType;
285 static inline ReshapedType reshape(const Eigen::Matrix<double, M, M, InOptions> & in) {
286 return in;
287 }
288};
289
291template <int M, int N, int InOptions>
292struct Reshape<M, N, InOptions, M, N, InOptions> {
293 typedef const Eigen::Matrix<double, M, N, InOptions> & ReshapedType;
294 static inline ReshapedType reshape(const Eigen::Matrix<double, M, N, InOptions> & in) {
295 return in;
296 }
297};
298
300template <int M, int N, int InOptions>
301struct Reshape<N, M, InOptions, M, N, InOptions> {
302 typedef typename Eigen::Matrix<double, M, N, InOptions>::ConstTransposeReturnType ReshapedType;
303 static inline ReshapedType reshape(const Eigen::Matrix<double, M, N, InOptions> & in) {
304 return in.transpose();
305 }
306};
307
308template <int OutM, int OutN, int OutOptions, int InM, int InN, int InOptions>
309inline typename Reshape<OutM, OutN, OutOptions, InM, InN, InOptions>::ReshapedType reshape(const Eigen::Matrix<double, InM, InN, InOptions> & m){
310 BOOST_STATIC_ASSERT(InM * InN == OutM * OutN);
311 return Reshape<OutM, OutN, OutOptions, InM, InN, InOptions>::reshape(m);
312}
313
320GTSAM_EXPORT std::pair<Matrix,Matrix> qr(const Matrix& A);
321
327GTSAM_EXPORT void inplace_QR(Matrix& A);
328
337GTSAM_EXPORT std::list<boost::tuple<Vector, double, double> >
338weighted_eliminate(Matrix& A, Vector& b, const Vector& sigmas);
339
347GTSAM_EXPORT void householder_(Matrix& A, size_t k, bool copy_vectors=true);
348
355GTSAM_EXPORT void householder(Matrix& A, size_t k);
356
364GTSAM_EXPORT Vector backSubstituteUpper(const Matrix& U, const Vector& b, bool unit=false);
365
373//TODO: is this function necessary? it isn't used
374GTSAM_EXPORT Vector backSubstituteUpper(const Vector& b, const Matrix& U, bool unit=false);
375
383GTSAM_EXPORT Vector backSubstituteLower(const Matrix& L, const Vector& b, bool unit=false);
384
391GTSAM_EXPORT Matrix stack(size_t nrMatrices, ...);
392GTSAM_EXPORT Matrix stack(const std::vector<Matrix>& blocks);
393
404GTSAM_EXPORT Matrix collect(const std::vector<const Matrix *>& matrices, size_t m = 0, size_t n = 0);
405GTSAM_EXPORT Matrix collect(size_t nrMatrices, ...);
406
413GTSAM_EXPORT void vector_scale_inplace(const Vector& v, Matrix& A, bool inf_mask = false); // row
414GTSAM_EXPORT Matrix vector_scale(const Vector& v, const Matrix& A, bool inf_mask = false); // row
415GTSAM_EXPORT Matrix vector_scale(const Matrix& A, const Vector& v, bool inf_mask = false); // column
416
427
428inline Matrix3 skewSymmetric(double wx, double wy, double wz) {
429 return (Matrix3() << 0.0, -wz, +wy, +wz, 0.0, -wx, -wy, +wx, 0.0).finished();
430}
431
432template <class Derived>
433inline Matrix3 skewSymmetric(const Eigen::MatrixBase<Derived>& w) {
434 return skewSymmetric(w(0), w(1), w(2));
435}
436
438GTSAM_EXPORT Matrix inverse_square_root(const Matrix& A);
439
441GTSAM_EXPORT Matrix cholesky_inverse(const Matrix &A);
442
455GTSAM_EXPORT void svd(const Matrix& A, Matrix& U, Vector& S, Matrix& V);
456
464GTSAM_EXPORT boost::tuple<int, double, Vector>
465DLT(const Matrix& A, double rank_tol = 1e-9);
466
472GTSAM_EXPORT Matrix expm(const Matrix& A, size_t K=7);
473
474std::string formatMatrixIndented(const std::string& label, const Matrix& matrix, bool makeVectorHorizontal = false);
475
482template <int N>
484 typedef Eigen::Matrix<double, N, 1> VectorN;
485 typedef Eigen::Matrix<double, N, N> MatrixN;
486
488 VectorN operator()(const MatrixN& A, const VectorN& b,
489 OptionalJacobian<N, N* N> H1 = boost::none,
490 OptionalJacobian<N, N> H2 = boost::none) const {
491 const MatrixN invA = A.inverse();
492 const VectorN c = invA * b;
493 // The derivative in A is just -[c[0]*invA c[1]*invA ... c[N-1]*invA]
494 if (H1)
495 for (size_t j = 0; j < N; j++)
496 H1->template middleCols<N>(N * j) = -c[j] * invA;
497 // The derivative in b is easy, as invA*b is just a linear map:
498 if (H2) *H2 = invA;
499 return c;
500 }
501};
502
508template <typename T, int N>
510 enum { M = traits<T>::dimension };
511 typedef Eigen::Matrix<double, N, 1> VectorN;
512 typedef Eigen::Matrix<double, N, N> MatrixN;
513
514 // The function phi should calculate f(a)*b, with derivatives in a and b.
515 // Naturally, the derivative in b is f(a).
516 typedef std::function<VectorN(
517 const T&, const VectorN&, OptionalJacobian<N, M>, OptionalJacobian<N, N>)>
518 Operator;
519
521 MultiplyWithInverseFunction(const Operator& phi) : phi_(phi) {}
522
524 VectorN operator()(const T& a, const VectorN& b,
525 OptionalJacobian<N, M> H1 = boost::none,
526 OptionalJacobian<N, N> H2 = boost::none) const {
527 MatrixN A;
528 phi_(a, b, boost::none, A); // get A = f(a) by calling f once
529 const MatrixN invA = A.inverse();
530 const VectorN c = invA * b;
531
532 if (H1) {
533 Eigen::Matrix<double, N, M> H;
534 phi_(a, c, H, boost::none); // get derivative H of forward mapping
535 *H1 = -invA* H;
536 }
537 if (H2) *H2 = invA;
538 return c;
539 }
540
541 private:
542 const Operator phi_;
543};
544
545GTSAM_EXPORT Matrix LLt(const Matrix& A);
546
547GTSAM_EXPORT Matrix RtR(const Matrix& A);
548
549GTSAM_EXPORT Vector columnNormSquare(const Matrix &A);
550} // namespace gtsam
typedef and functions to augment Eigen's VectorXd
Special class for optional Jacobian arguments.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
Vector backSubstituteLower(const Matrix &L, const Vector &b, bool unit)
backSubstitute L*x=b
Definition Matrix.cpp:367
Vector operator^(const Matrix &A, const Vector &v)
overload ^ for trans(A)*v We transpose the vectors for speed.
Definition Matrix.cpp:131
void vector_scale_inplace(const Vector &v, Matrix &A, bool inf_mask)
scales a matrix row or column by the values in a vector Arguments (Matrix, Vector) scales the columns...
Definition Matrix.cpp:482
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Extracts a row view from a matrix that avoids a copy.
Definition Matrix.h:249
T expm(const Vector &x, int K=7)
Exponential map given exponential coordinates class T needs a wedge<> function and a constructor from...
Definition Lie.h:317
void save(const Matrix &A, const string &s, const string &filename)
save a matrix to file, which can be loaded by matlab
Definition Matrix.cpp:167
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition Matrix.cpp:43
bool linear_dependent(const Matrix &A, const Matrix &B, double tol)
check whether the rows of two matrices are linear dependent
Definition Matrix.cpp:117
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:156
const MATRIX::ConstColXpr column(const MATRIX &A, size_t j)
Extracts a column view from a matrix that avoids a copy.
Definition Matrix.h:238
void zeroBelowDiagonal(MATRIX &A, size_t cols=0)
Zeros all of the elements below the diagonal of a matrix, in place.
Definition Matrix.h:259
Matrix stack(size_t nrMatrices,...)
create a matrix by stacking other matrices Given a set of matrices: A1, A2, A3...
Definition Matrix.cpp:397
list< boost::tuple< Vector, double, double > > weighted_eliminate(Matrix &A, Vector &b, const Vector &sigmas)
Imperative algorithm for in-place full elimination with weights and constraint handling.
Definition Matrix.cpp:273
Vector backSubstituteUpper(const Matrix &U, const Vector &b, bool unit)
backSubstitute U*x=b
Definition Matrix.cpp:377
bool assert_inequal(const Matrix &A, const Matrix &B, double tol)
inequals with an tolerance, prints out message if within tolerance
Definition Matrix.cpp:63
void householder(Matrix &A, size_t k)
Householder tranformation, zeros below diagonal.
Definition Matrix.cpp:354
istream & operator>>(istream &inputStream, Matrix &destinationMatrix)
Read a matrix from an input stream, such as a file.
Definition Matrix.cpp:174
void inplace_QR(Matrix &A)
QR factorization using Eigen's internal block QR algorithm.
Definition Matrix.cpp:636
void svd(const Matrix &A, Matrix &U, Vector &S, Matrix &V)
SVD computes economy SVD A=U*S*V'.
Definition Matrix.cpp:560
Matrix3 skewSymmetric(double wx, double wy, double wz)
skew symmetric matrix returns this: 0 -wz wy wz 0 -wx -wy wx 0
Definition Matrix.h:428
Eigen::Block< const MATRIX > sub(const MATRIX &A, size_t i1, size_t i2, size_t j1, size_t j2)
extract submatrix, slice semantics, i.e.
Definition Matrix.h:208
Matrix trans(const Matrix &A)
static transpose function, just calls Eigen transpose member function
Definition Matrix.h:269
bool operator!=(const Matrix &A, const Matrix &B)
inequality
Definition Matrix.h:134
boost::tuple< int, double, Vector > DLT(const Matrix &A, double rank_tol)
Direct linear transform algorithm that calls svd to find a vector v that minimizes the algebraic erro...
Definition Matrix.cpp:568
Matrix cholesky_inverse(const Matrix &A)
Return the inverse of a S.P.D.
Definition Matrix.cpp:539
MATRIX prod(const MATRIX &A, const MATRIX &B)
products using old-style format to improve compatibility
Definition Matrix.h:171
void householder_(Matrix &A, size_t k, bool copy_vectors)
Imperative version of Householder QR factorization, Golub & Van Loan p 224 version with Householder v...
Definition Matrix.cpp:327
void insertSub(Eigen::MatrixBase< Derived1 > &fullMatrix, const Eigen::MatrixBase< Derived2 > &subMatrix, size_t i, size_t j)
insert a submatrix IN PLACE at a specified location in a larger matrix NOTE: there is no size checkin...
Definition Matrix.h:222
Matrix collect(const std::vector< const Matrix * > &matrices, size_t m, size_t n)
create a matrix by concatenating Given a set of matrices: A1, A2, A3... If all matrices have the same...
Definition Matrix.cpp:443
bool linear_independent(const Matrix &A, const Matrix &B, double tol)
check whether the rows of two matrices are linear independent
Definition Matrix.cpp:103
bool fpEqual(double a, double b, double tol, bool check_relative_also)
Ensure we are not including a different version of Eigen in user code than while compiling gtsam,...
Definition Vector.cpp:42
pair< Matrix, Matrix > qr(const Matrix &A)
Householder QR factorization, Golub & Van Loan p 224, explicit version.
Definition Matrix.cpp:235
Matrix diag(const std::vector< Matrix > &Hs)
Create a matrix with submatrices along its diagonal.
Definition Matrix.cpp:207
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
bool operator==(const Matrix &A, const Matrix &B)
equality is just equal_with_abs_tol 1e-9
Definition Matrix.h:127
Matrix inverse_square_root(const Matrix &A)
Use Cholesky to calculate inverse square root of a matrix.
Definition Matrix.cpp:552
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
Reshape functor.
Definition Matrix.h:273
Functor that implements multiplication of a vector b with the inverse of a matrix A.
Definition Matrix.h:483
VectorN operator()(const MatrixN &A, const VectorN &b, OptionalJacobian< N, N *N > H1=boost::none, OptionalJacobian< N, N > H2=boost::none) const
A.inverse() * b, with optional derivatives.
Definition Matrix.h:488
VectorN operator()(const T &a, const VectorN &b, OptionalJacobian< N, M > H1=boost::none, OptionalJacobian< N, N > H2=boost::none) const
f(a).inverse() * b, with optional derivatives
Definition Matrix.h:524
MultiplyWithInverseFunction(const Operator &phi)
Construct with function as explained above.
Definition Matrix.h:521
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:41