gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
21 // \callgraph
22 
23 #pragma once
24 
25 #include <gtsam/base/Vector.h>
26 #include <boost/format.hpp>
27 #include <boost/tuple/tuple.hpp>
28 #include <boost/math/special_functions/fpclassify.hpp>
29 
35 namespace gtsam {
36 
37 typedef Eigen::MatrixXd Matrix;
38 typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatrixRowMajor;
39 
40 typedef Eigen::Matrix3d Matrix3;
41 typedef Eigen::Matrix4d Matrix4;
42 typedef Eigen::Matrix<double,6,6> Matrix6;
43 
44 // Matrix expressions for accessing parts of matrices
45 typedef Eigen::Block<Matrix> SubMatrix;
46 typedef Eigen::Block<const Matrix> ConstSubMatrix;
47 
48 // Matlab-like syntax
49 
56 GTSAM_EXPORT Matrix zeros(size_t m, size_t n);
57 
61 GTSAM_EXPORT Matrix ones(size_t m, size_t n);
62 
69 GTSAM_EXPORT Matrix eye(size_t m, size_t n);
70 
77 inline Matrix eye( size_t m ) { return eye(m,m); }
78 GTSAM_EXPORT Matrix diag(const Vector& v);
79 
83 template <class MATRIX>
84 bool equal_with_abs_tol(const Eigen::DenseBase<MATRIX>& A, const Eigen::DenseBase<MATRIX>& B, double tol = 1e-9) {
85 
86  const size_t n1 = A.cols(), m1 = A.rows();
87  const size_t n2 = B.cols(), m2 = B.rows();
88 
89  if(m1!=m2 || n1!=n2) return false;
90 
91  for(size_t i=0; i<m1; i++)
92  for(size_t j=0; j<n1; j++) {
93  if(boost::math::isnan(A(i,j)) ^ boost::math::isnan(B(i,j)))
94  return false;
95  else if(fabs(A(i,j) - B(i,j)) > tol)
96  return false;
97  }
98  return true;
99 }
100 
104 inline bool operator==(const Matrix& A, const Matrix& B) {
105  return equal_with_abs_tol(A,B,1e-9);
106 }
107 
111 inline bool operator!=(const Matrix& A, const Matrix& B) {
112  return !(A==B);
113  }
114 
118 GTSAM_EXPORT bool assert_equal(const Matrix& A, const Matrix& B, double tol = 1e-9);
119 
123 GTSAM_EXPORT bool assert_inequal(const Matrix& A, const Matrix& B, double tol = 1e-9);
124 
128 GTSAM_EXPORT bool assert_equal(const std::list<Matrix>& As, const std::list<Matrix>& Bs, double tol = 1e-9);
129 
133 GTSAM_EXPORT bool linear_independent(const Matrix& A, const Matrix& B, double tol = 1e-9);
134 
138 GTSAM_EXPORT bool linear_dependent(const Matrix& A, const Matrix& B, double tol = 1e-9);
139 
143 GTSAM_EXPORT void multiplyAdd(double alpha, const Matrix& A, const Vector& x, Vector& e);
144 
148 GTSAM_EXPORT void multiplyAdd(const Matrix& A, const Vector& x, Vector& e);
149 
154 GTSAM_EXPORT Vector operator^(const Matrix& A, const Vector & v);
155 
159 GTSAM_EXPORT void transposeMultiplyAdd(double alpha, const Matrix& A, const Vector& e, Vector& x);
160 
164 GTSAM_EXPORT void transposeMultiplyAdd(const Matrix& A, const Vector& e, Vector& x);
165 
169 GTSAM_EXPORT void transposeMultiplyAdd(double alpha, const Matrix& A, const Vector& e, SubVector x);
170 
172 template<class MATRIX>
173 inline MATRIX prod(const MATRIX& A, const MATRIX&B) {
174  MATRIX result = A * B;
175  return result;
176 }
177 
181 GTSAM_EXPORT void print(const Matrix& A, const std::string& s = "", std::ostream& stream = std::cout);
182 
186 GTSAM_EXPORT void save(const Matrix& A, const std::string &s, const std::string& filename);
187 
193 GTSAM_EXPORT std::istream& operator>>(std::istream& inputStream, Matrix& destinationMatrix);
194 
204 template<class MATRIX>
205 Eigen::Block<const MATRIX> sub(const MATRIX& A, size_t i1, size_t i2, size_t j1, size_t j2) {
206  size_t m=i2-i1, n=j2-j1;
207  return A.block(i1,j1,m,n);
208 }
209 
218 template <typename Derived1, typename Derived2>
219 void insertSub(Eigen::MatrixBase<Derived1>& fullMatrix, const Eigen::MatrixBase<Derived2>& subMatrix, size_t i, size_t j) {
220  fullMatrix.block(i, j, subMatrix.rows(), subMatrix.cols()) = subMatrix;
221 }
222 
226 GTSAM_EXPORT Matrix diag(const std::vector<Matrix>& Hs);
227 
234 template<class MATRIX>
235 const typename MATRIX::ConstColXpr column(const MATRIX& A, size_t j) {
236  return A.col(j);
237 }
238 
245 template<class MATRIX>
246 const typename MATRIX::ConstRowXpr row(const MATRIX& A, size_t j) {
247  return A.row(j);
248 }
249 
258 GTSAM_EXPORT void insertColumn(Matrix& A, const Vector& col, size_t j);
259 GTSAM_EXPORT void insertColumn(Matrix& A, const Vector& col, size_t i, size_t j);
260 
261 GTSAM_EXPORT Vector columnNormSquare(const Matrix &A);
262 
268 template<class MATRIX>
269 void zeroBelowDiagonal(MATRIX& A, size_t cols=0) {
270  const size_t m = A.rows(), n = A.cols();
271  const size_t k = (cols) ? std::min(cols, std::min(m,n)) : std::min(m,n);
272  for (size_t j=0; j<k; ++j)
273  A.col(j).segment(j+1, m-(j+1)).setZero();
274 }
275 
279 inline Matrix trans(const Matrix& A) { return A.transpose(); }
280 
285 GTSAM_EXPORT void solve(Matrix& A, Matrix& B);
286 
290 GTSAM_EXPORT Matrix inverse(const Matrix& A);
291 
298 GTSAM_EXPORT std::pair<Matrix,Matrix> qr(const Matrix& A);
299 
305 template <class MATRIX>
306 void inplace_QR(MATRIX& A) {
307  size_t rows = A.rows();
308  size_t cols = A.cols();
309  size_t size = std::min(rows,cols);
310 
311  typedef Eigen::internal::plain_diag_type<Matrix>::type HCoeffsType;
312  typedef Eigen::internal::plain_row_type<Matrix>::type RowVectorType;
313  HCoeffsType hCoeffs(size);
314  RowVectorType temp(cols);
315 
316  Eigen::internal::householder_qr_inplace_blocked<MATRIX, HCoeffsType>::run(A, hCoeffs, 48, temp.data());
317 
319 }
320 
329 GTSAM_EXPORT std::list<boost::tuple<Vector, double, double> >
330 weighted_eliminate(Matrix& A, Vector& b, const Vector& sigmas);
331 
339 GTSAM_EXPORT void householder_(Matrix& A, size_t k, bool copy_vectors=true);
340 
347 GTSAM_EXPORT void householder(Matrix& A, size_t k);
348 
356 GTSAM_EXPORT Vector backSubstituteUpper(const Matrix& U, const Vector& b, bool unit=false);
357 
365 //TODO: is this function necessary? it isn't used
366 GTSAM_EXPORT Vector backSubstituteUpper(const Vector& b, const Matrix& U, bool unit=false);
367 
375 GTSAM_EXPORT Vector backSubstituteLower(const Matrix& L, const Vector& b, bool unit=false);
376 
383 GTSAM_EXPORT Matrix stack(size_t nrMatrices, ...);
384 GTSAM_EXPORT Matrix stack(const std::vector<Matrix>& blocks);
385 
396 GTSAM_EXPORT Matrix collect(const std::vector<const Matrix *>& matrices, size_t m = 0, size_t n = 0);
397 GTSAM_EXPORT Matrix collect(size_t nrMatrices, ...);
398 
405 GTSAM_EXPORT void vector_scale_inplace(const Vector& v, Matrix& A, bool inf_mask = false); // row
406 GTSAM_EXPORT Matrix vector_scale(const Vector& v, const Matrix& A, bool inf_mask = false); // row
407 GTSAM_EXPORT Matrix vector_scale(const Matrix& A, const Vector& v, bool inf_mask = false); // column
408 
419 GTSAM_EXPORT Matrix3 skewSymmetric(double wx, double wy, double wz);
420 template<class Derived>
421 inline Matrix3 skewSymmetric(const Eigen::MatrixBase<Derived>& w) { return skewSymmetric(w(0),w(1),w(2));}
422 
424 GTSAM_EXPORT Matrix inverse_square_root(const Matrix& A);
425 
427 GTSAM_EXPORT Matrix LLt(const Matrix& A);
428 
430 GTSAM_EXPORT Matrix RtR(const Matrix& A);
431 
433 GTSAM_EXPORT Matrix cholesky_inverse(const Matrix &A);
434 
447 GTSAM_EXPORT void svd(const Matrix& A, Matrix& U, Vector& S, Matrix& V);
448 
456 GTSAM_EXPORT boost::tuple<int, double, Vector>
457 DLT(const Matrix& A, double rank_tol = 1e-9);
458 
464 GTSAM_EXPORT Matrix expm(const Matrix& A, size_t K=7);
465 
467 GTSAM_EXPORT Matrix Cayley(const Matrix& A);
468 
471 template<int N>
472 Eigen::Matrix<double, N, N> CayleyFixed(const Eigen::Matrix<double, N, N>& A) {
473  typedef Eigen::Matrix<double, N, N> FMat;
474  return (FMat::Identity() - A)*(FMat::Identity() + A).inverse();
475 }
476 
477 std::string formatMatrixIndented(const std::string& label, const Matrix& matrix, bool makeVectorHorizontal = false);
478 
479 } // namespace gtsam
480 
481 #include <boost/serialization/nvp.hpp>
482 #include <boost/serialization/array.hpp>
483 #include <boost/serialization/split_free.hpp>
484 
485 namespace boost {
486  namespace serialization {
487 
488  // split version - sends sizes ahead
489  template<class Archive>
490  void save(Archive & ar, const gtsam::Matrix & m, unsigned int version) {
491  const size_t rows = m.rows(), cols = m.cols();
492  ar << BOOST_SERIALIZATION_NVP(rows);
493  ar << BOOST_SERIALIZATION_NVP(cols);
494  ar << make_nvp("data", make_array(m.data(), m.size()));
495  }
496 
497  template<class Archive>
498  void load(Archive & ar, gtsam::Matrix & m, unsigned int version) {
499  size_t rows, cols;
500  ar >> BOOST_SERIALIZATION_NVP(rows);
501  ar >> BOOST_SERIALIZATION_NVP(cols);
502  m.resize(rows, cols);
503  ar >> make_nvp("data", make_array(m.data(), m.size()));
504  }
505 
506  } // namespace serialization
507 } // namespace boost
508 
509 BOOST_SERIALIZATION_SPLIT_FREE(gtsam::Matrix)
void transposeMultiplyAdd(double alpha, const Matrix &A, const Vector &e, Vector &x)
BLAS Level-2 style x <- x + alpha*A'*e.
Definition: Matrix.cpp:168
Vector backSubstituteLower(const Matrix &L, const Vector &b, bool unit)
backSubstitute L*x=b
Definition: Matrix.cpp:428
Matrix stack(size_t nrMatrices,...)
create a matrix by stacking other matrices Given a set of matrices: A1, A2, A3... ...
Definition: Matrix.cpp:458
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:543
bool assert_inequal(const Matrix &A, const Matrix &B, double tol)
inequals with an tolerance, prints out message if within tolerance
Definition: Matrix.cpp:80
typedef and functions to augment Eigen's VectorXd
Matrix inverse(const Matrix &A)
invert A
Definition: Matrix.cpp:289
Matrix RtR(const Matrix &A)
Calculate the R^tR decomposition of a S.P.D matrix.
Definition: Matrix.cpp:600
pair< Matrix, Matrix > qr(const Matrix &A)
Householder QR factorization, Golub & Van Loan p 224, explicit version.
Definition: Matrix.cpp:296
Matrix inverse_square_root(const Matrix &A)
Use Cholesky to calculate inverse square root of a matrix.
Definition: Matrix.cpp:622
void solve(Matrix &A, Matrix &B)
solve AX=B via in-place Lu factorization and backsubstitution After calling, A contains LU...
Definition: Matrix.cpp:283
void zeroBelowDiagonal(MATRIX &A, size_t cols=0)
Zeros all of the elements below the diagonal of a matrix, in place.
Definition: Matrix.h:269
Matrix eye(size_t m, size_t n)
Creates an identity matrix, with matlab-like syntax.
Definition: Matrix.cpp:50
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:202
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:134
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:205
void householder(Matrix &A, size_t k)
Householder tranformation, zeros below diagonal.
Definition: Matrix.cpp:415
Matrix LLt(const Matrix &A)
Calculate the LL^t decomposition of a S.P.D matrix.
Definition: Matrix.cpp:593
bool linear_dependent(const Matrix &A, const Matrix &B, double tol)
check whether the rows of two matrices are linear dependent
Definition: Matrix.cpp:134
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Extracts a row view from a matrix that avoids a copy.
Definition: Matrix.h:246
MATRIX prod(const MATRIX &A, const MATRIX &B)
products using old-style format to improve compatibility
Definition: Matrix.h:173
Matrix trans(const Matrix &A)
static transpose function, just calls Eigen transpose member function
Definition: Matrix.h:279
bool operator==(const Matrix &A, const Matrix &B)
equality is just equal_with_abs_tol 1e-9
Definition: Matrix.h:104
void inplace_QR(MATRIX &A)
QR factorization using Eigen's internal block QR algorithm.
Definition: Matrix.h:306
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
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:388
Matrix3 skewSymmetric(double wx, double wy, double wz)
skew symmetric matrix returns this: 0 -wz wy wz 0 -wx -wy wx 0
Definition: Matrix.cpp:584
void multiplyAdd(double alpha, const Matrix &A, const Vector &x, Vector &e)
BLAS Level-2 style e <- e + alpha*A*x.
Definition: Matrix.cpp:148
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:334
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
equals with an tolerance, prints out message if unequal
Definition: Matrix.cpp:60
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...
Definition: Matrix.cpp:504
void svd(const Matrix &A, Matrix &U, Vector &S, Matrix &V)
SVD computes economy SVD A=U*S*V'.
Definition: Matrix.cpp:630
Matrix ones(size_t m, size_t n)
Creates an ones matrix, with matlab-like syntax.
Definition: Matrix.cpp:45
bool operator!=(const Matrix &A, const Matrix &B)
inequality
Definition: Matrix.h:111
Vector operator^(const Matrix &A, const Vector &v)
overload ^ for trans(A)*v We transpose the vectors for speed.
Definition: Matrix.cpp:158
istream & operator>>(istream &inputStream, Matrix &destinationMatrix)
Read a matrix from an input stream, such as a file.
Definition: Matrix.cpp:209
void insertColumn(Matrix &A, const Vector &col, size_t j)
inserts a column into a matrix IN PLACE NOTE: there is no size checking Alternate form allows for vec...
Definition: Matrix.cpp:264
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:638
bool linear_independent(const Matrix &A, const Matrix &B, double tol)
check whether the rows of two matrices are linear independent
Definition: Matrix.cpp:120
Matrix cholesky_inverse(const Matrix &A)
Return the inverse of a S.P.D.
Definition: Matrix.cpp:609
Vector backSubstituteUpper(const Matrix &U, const Vector &b, bool unit)
backSubstitute U*x=b
Definition: Matrix.cpp:438
Matrix Cayley(const Matrix &A)
Cayley transform.
Definition: Matrix.cpp:669
Eigen::Matrix< double, N, N > CayleyFixed(const Eigen::Matrix< double, N, N > &A)
Implementation of Cayley transform using fixed size matrices to let Eigen do more optimization...
Definition: Matrix.h:472
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
equals with an tolerance
Definition: Matrix.h:84
Matrix zeros(size_t m, size_t n)
Creates an zeros matrix, with matlab-like syntax.
Definition: Matrix.cpp:40
const MATRIX::ConstColXpr column(const MATRIX &A, size_t j)
Extracts a column view from a matrix that avoids a copy.
Definition: Matrix.h:235