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
30#include <vector>
31
37namespace gtsam {
38
39typedef Eigen::MatrixXd Matrix;
40typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatrixRowMajor;
43 Eigen::Ref<const Matrix, 0, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>>;
44
45// Create handy typedefs and constants for square-size matrices
46// MatrixMN, MatrixN = MatrixNN, I_NxN, and Z_NxN, for M,N=1..9
47#define GTSAM_MAKE_MATRIX_DEFS(N) \
48using Matrix##N = Eigen::Matrix<double, N, N>; \
49using Matrix1##N = Eigen::Matrix<double, 1, N>; \
50using Matrix2##N = Eigen::Matrix<double, 2, N>; \
51using Matrix3##N = Eigen::Matrix<double, 3, N>; \
52using Matrix4##N = Eigen::Matrix<double, 4, N>; \
53using Matrix5##N = Eigen::Matrix<double, 5, N>; \
54using Matrix6##N = Eigen::Matrix<double, 6, N>; \
55using Matrix7##N = Eigen::Matrix<double, 7, N>; \
56using Matrix8##N = Eigen::Matrix<double, 8, N>; \
57using Matrix9##N = Eigen::Matrix<double, 9, N>; \
58
59GTSAM_MAKE_MATRIX_DEFS(1)
60GTSAM_MAKE_MATRIX_DEFS(2)
61GTSAM_MAKE_MATRIX_DEFS(3)
62GTSAM_MAKE_MATRIX_DEFS(4)
63GTSAM_MAKE_MATRIX_DEFS(5)
64GTSAM_MAKE_MATRIX_DEFS(6)
65GTSAM_MAKE_MATRIX_DEFS(7)
66GTSAM_MAKE_MATRIX_DEFS(8)
67GTSAM_MAKE_MATRIX_DEFS(9)
68
69// Matrix expressions for accessing parts of matrices
70typedef Eigen::Block<Matrix> SubMatrix;
71typedef Eigen::Block<const Matrix> ConstSubMatrix;
72
73// Matrix formatting arguments when printing.
74// Akin to Matlab style.
75const Eigen::IOFormat& matlabFormat();
76
80template <class MATRIX>
81bool equal_with_abs_tol(const Eigen::DenseBase<MATRIX>& A, const Eigen::DenseBase<MATRIX>& B, double tol = 1e-9) {
82
83 const size_t n1 = A.cols(), m1 = A.rows();
84 const size_t n2 = B.cols(), m2 = B.rows();
85
86 if(m1!=m2 || n1!=n2) return false;
87
88 for(size_t i=0; i<m1; i++)
89 for(size_t j=0; j<n1; j++) {
90 if(!fpEqual(A(i,j), B(i,j), tol, false)) {
91 return false;
92 }
93 }
94 return true;
95}
96
100inline bool operator==(const Matrix& A, const Matrix& B) {
101 return equal_with_abs_tol(A,B,1e-9);
102}
103
107inline bool operator!=(const Matrix& A, const Matrix& B) {
108 return !(A==B);
109 }
110
114GTSAM_EXPORT bool assert_equal(const Matrix& A, const Matrix& B, double tol = 1e-9);
115
119GTSAM_EXPORT bool assert_inequal(const Matrix& A, const Matrix& B, double tol = 1e-9);
120
124GTSAM_EXPORT bool assert_equal(const std::list<Matrix>& As, const std::list<Matrix>& Bs, double tol = 1e-9);
125
129GTSAM_EXPORT bool linear_independent(const Matrix& A, const Matrix& B, double tol = 1e-9);
130
134GTSAM_EXPORT bool linear_dependent(const Matrix& A, const Matrix& B, double tol = 1e-9);
135
139GTSAM_EXPORT void print(const Matrix& A, const std::string& s, std::ostream& stream);
140
144GTSAM_EXPORT void print(const Matrix& A, const std::string& s = "");
145
149GTSAM_EXPORT void save(const Matrix& A, const std::string &s, const std::string& filename);
150
156GTSAM_EXPORT std::istream& operator>>(std::istream& inputStream, Matrix& destinationMatrix);
157
158#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
169template <typename Derived1, typename Derived2>
170void insertSub(Eigen::MatrixBase<Derived1>& fullMatrix,
171 const Eigen::MatrixBase<Derived2>& subMatrix, size_t i,
172 size_t j) {
173 fullMatrix.block(i, j, subMatrix.rows(), subMatrix.cols()) = subMatrix;
174}
175#endif
176
180GTSAM_EXPORT Matrix diag(const std::vector<Matrix>& Hs);
181
182#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
187inline Matrix trans(const Matrix& A) { return A.transpose(); }
188#endif
189
196GTSAM_EXPORT std::pair<Matrix,Matrix> qr(const Matrix& A);
197
203GTSAM_EXPORT void inplace_QR(Matrix& A);
204
213GTSAM_EXPORT std::list<std::tuple<Vector, double, double> >
214weighted_eliminate(Matrix& A, Vector& b, const Vector& sigmas);
215
223GTSAM_EXPORT void householder_(Matrix& A, size_t k, bool copy_vectors=true);
224
231GTSAM_EXPORT void householder(Matrix& A, size_t k);
232
233#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
244GTSAM_EXPORT Vector backSubstituteUpper(const Matrix& U, const Vector& b,
245 bool unit = false);
246
257GTSAM_EXPORT Vector backSubstituteUpper(const Vector& b, const Matrix& U,
258 bool unit = false);
259
270GTSAM_EXPORT Vector backSubstituteLower(const Matrix& L, const Vector& b,
271 bool unit = false);
272#endif
273
274namespace internal {
275
277template <class RDerived, class SDerived, class DDerived, class ParentsDerived>
278void solveUpperConditional(const Eigen::MatrixBase<RDerived>& R,
279 const Eigen::MatrixBase<SDerived>& S,
280 const Eigen::MatrixBase<DDerived>& d,
281 const Eigen::MatrixBase<ParentsDerived>& parents,
282 Vector* result) {
283 result->resize(d.rows());
284 result->noalias() = d - S * parents;
285 R.derived().template triangularView<Eigen::Upper>().solveInPlace(*result);
286}
287
288} // namespace internal
289
290#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
298GTSAM_EXPORT Matrix stack(size_t nrMatrices, ...);
299#endif
300GTSAM_EXPORT Matrix stack(const std::vector<Matrix>& blocks);
301
312GTSAM_EXPORT Matrix collect(const std::vector<const Matrix*>& matrices,
313 size_t m = 0, size_t n = 0);
314#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
316GTSAM_EXPORT Matrix collect(size_t nrMatrices, ...);
317#endif
318
319#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
328GTSAM_EXPORT void vector_scale_inplace(const Vector& v, Matrix& A,
329 bool inf_mask = false);
330
339GTSAM_EXPORT Matrix vector_scale(const Vector& v, const Matrix& A,
340 bool inf_mask = false);
341
350GTSAM_EXPORT Matrix vector_scale(const Matrix& A, const Vector& v,
351 bool inf_mask = false);
352#endif
353
364
365inline Matrix3 skewSymmetric(double wx, double wy, double wz) {
366 return Matrix3{{0.0, -wz, +wy}, {+wz, 0.0, -wx}, {-wy, +wx, 0.0}};
367}
368
369template <class Derived>
370inline Matrix3 skewSymmetric(const Eigen::MatrixBase<Derived>& w) {
371 return skewSymmetric(w(0), w(1), w(2));
372}
373
375GTSAM_EXPORT Matrix inverse_square_root(const Matrix& A);
376
377#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
383GTSAM_EXPORT Matrix cholesky_inverse(const Matrix &A);
384#endif
385
398GTSAM_EXPORT void svd(const Matrix& A, Matrix& U, Vector& S, Matrix& V);
399
407GTSAM_EXPORT std::tuple<int, double, Vector>
408DLT(const Matrix& A, double rank_tol = 1e-9);
409
415GTSAM_EXPORT Matrix expm(const Matrix& A, size_t K=7);
416
417std::string formatMatrixIndented(const std::string& label, const Matrix& matrix, bool makeVectorHorizontal = false);
418
425template <int N>
427 typedef Eigen::Matrix<double, N, 1> VectorN;
428 typedef Eigen::Matrix<double, N, N> MatrixN;
429
431 VectorN operator()(const MatrixN& A, const VectorN& b,
433 OptionalJacobian<N, N> H2 = {}) const {
434 const MatrixN invA = A.inverse();
435 const VectorN c = invA * b;
436 // The derivative in A is just -[c[0]*invA c[1]*invA ... c[N-1]*invA]
437 if (H1)
438 for (size_t j = 0; j < N; j++)
439 H1->template middleCols<N>(N * j) = -c[j] * invA;
440 // The derivative in b is easy, as invA*b is just a linear map:
441 if (H2) *H2 = invA;
442 return c;
443 }
444};
445
451template <typename T, int N>
453 inline constexpr static auto M = traits<T>::dimension;
454 typedef Eigen::Matrix<double, N, 1> VectorN;
455 typedef Eigen::Matrix<double, N, N> MatrixN;
456
457 // The function phi should calculate f(a)*b, with derivatives in a and b.
458 // Naturally, the derivative in b is f(a).
459 typedef std::function<VectorN(
460 const T&, const VectorN&, OptionalJacobian<N, M>, OptionalJacobian<N, N>)>
461 Operator;
462
464 MultiplyWithInverseFunction(const Operator& phi) : phi_(phi) {}
465
467 VectorN operator()(const T& a, const VectorN& b,
469 OptionalJacobian<N, N> H2 = {}) const {
470 MatrixN A;
471 phi_(a, b, {}, A); // get A = f(a) by calling f once
472 const MatrixN invA = A.inverse();
473 const VectorN c = invA * b;
474
475 if (H1) {
476 Eigen::Matrix<double, N, M> H;
477 phi_(a, c, H, {}); // get derivative H of forward mapping
478 *H1 = -invA* H;
479 }
480 if (H2) *H2 = invA;
481 return c;
482 }
483
484 private:
485 const Operator phi_;
486};
487
488#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
490GTSAM_EXPORT Matrix LLt(const Matrix& A);
491
493GTSAM_EXPORT Matrix RtR(const Matrix& A);
494
499GTSAM_EXPORT Vector columnNormSquare(const Matrix &A);
500#endif
501} // namespace gtsam
void solveUpperConditional(const Eigen::MatrixBase< RDerived > &R, const Eigen::MatrixBase< SDerived > &S, const Eigen::MatrixBase< DDerived > &d, const Eigen::MatrixBase< ParentsDerived > &parents, Vector *result)
Solve the block upper-triangular system R*x = d - S*parents.
Definition Matrix.h:278
Special class for optional Jacobian arguments.
typedef and functions to augment Eigen's VectorXd
Global functions in a separate testing namespace.
Definition chartTesting.h:28
list< std::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:261
void householder(Matrix &A, size_t k)
Householder tranformation, zeros below diagonal.
Definition Matrix.cpp:342
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:557
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:365
Matrix expm(const Matrix &A, size_t K)
Numerical exponential map, naive approach, not industrial strength !
Definition Matrix.cpp:587
bool operator!=(const Matrix &A, const Matrix &B)
inequality
Definition Matrix.h:107
std::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:565
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:315
Eigen::Ref< const Matrix, 0, Eigen::Stride< Eigen::Dynamic, Eigen::Dynamic > > ConstMatrixView
Dynamic-stride const Matrix view for accepting NumPy arrays without copies.
Definition Matrix.h:42
pair< Matrix, Matrix > qr(const Matrix &A)
Householder QR factorization, Golub & Van Loan p 224, explicit version.
Definition Matrix.cpp:224
Matrix diag(const std::vector< Matrix > &Hs)
Create a matrix with submatrices along its diagonal.
Definition Matrix.cpp:194
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:81
bool operator==(const Matrix &A, const Matrix &B)
equality is just equal_with_abs_tol 1e-9
Definition Matrix.h:100
Matrix inverse_square_root(const Matrix &A)
Use Cholesky to calculate inverse square root of a matrix.
Definition Matrix.cpp:549
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Functor that implements multiplication of a vector b with the inverse of a matrix A.
Definition Matrix.h:426
VectorN operator()(const MatrixN &A, const VectorN &b, OptionalJacobian< N, N *N > H1={}, OptionalJacobian< N, N > H2={}) const
A.inverse() * b, with optional derivatives.
Definition Matrix.h:431
VectorN operator()(const T &a, const VectorN &b, OptionalJacobian< N, M > H1={}, OptionalJacobian< N, N > H2={}) const
f(a).inverse() * b, with optional derivatives
Definition Matrix.h:467
MultiplyWithInverseFunction(const Operator &phi)
Construct with function as explained above.
Definition Matrix.h:464
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40