26 #include <boost/format.hpp>
27 #include <boost/tuple/tuple.hpp>
28 #include <boost/math/special_functions/fpclassify.hpp>
37 typedef Eigen::MatrixXd Matrix;
38 typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatrixRowMajor;
40 typedef Eigen::Matrix3d Matrix3;
41 typedef Eigen::Matrix4d Matrix4;
42 typedef Eigen::Matrix<double,6,6> Matrix6;
45 typedef Eigen::Block<Matrix> SubMatrix;
46 typedef Eigen::Block<const Matrix> ConstSubMatrix;
56 GTSAM_EXPORT Matrix
zeros(
size_t m,
size_t n);
61 GTSAM_EXPORT Matrix
ones(
size_t m,
size_t n);
69 GTSAM_EXPORT Matrix
eye(
size_t m,
size_t n);
77 inline Matrix
eye(
size_t m ) {
return eye(m,m); }
78 GTSAM_EXPORT Matrix diag(
const Vector& v);
83 template <
class MATRIX>
84 bool equal_with_abs_tol(
const Eigen::DenseBase<MATRIX>& A,
const Eigen::DenseBase<MATRIX>& B,
double tol = 1e-9) {
86 const size_t n1 = A.cols(), m1 = A.rows();
87 const size_t n2 = B.cols(), m2 = B.rows();
89 if(m1!=m2 || n1!=n2)
return false;
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)))
95 else if(fabs(A(i,j) - B(i,j)) > tol)
118 GTSAM_EXPORT
bool assert_equal(
const Matrix& A,
const Matrix& B,
double tol = 1e-9);
123 GTSAM_EXPORT
bool assert_inequal(
const Matrix& A,
const Matrix& B,
double tol = 1e-9);
128 GTSAM_EXPORT
bool assert_equal(
const std::list<Matrix>& As,
const std::list<Matrix>& Bs,
double tol = 1e-9);
133 GTSAM_EXPORT
bool linear_independent(
const Matrix& A,
const Matrix& B,
double tol = 1e-9);
138 GTSAM_EXPORT
bool linear_dependent(
const Matrix& A,
const Matrix& B,
double tol = 1e-9);
143 GTSAM_EXPORT
void multiplyAdd(
double alpha,
const Matrix& A,
const Vector& x, Vector& e);
148 GTSAM_EXPORT
void multiplyAdd(
const Matrix& A,
const Vector& x, Vector& e);
154 GTSAM_EXPORT Vector
operator^(
const Matrix& A,
const Vector & v);
159 GTSAM_EXPORT
void transposeMultiplyAdd(
double alpha,
const Matrix& A,
const Vector& e, Vector& x);
169 GTSAM_EXPORT
void transposeMultiplyAdd(
double alpha,
const Matrix& A,
const Vector& e, SubVector x);
172 template<
class MATRIX>
173 inline MATRIX
prod(
const MATRIX& A,
const MATRIX&B) {
174 MATRIX result = A * B;
181 GTSAM_EXPORT
void print(
const Matrix& A,
const std::string& s =
"", std::ostream& stream = std::cout);
186 GTSAM_EXPORT
void save(
const Matrix& A,
const std::string &s,
const std::string& filename);
193 GTSAM_EXPORT std::istream&
operator>>(std::istream& inputStream, Matrix& destinationMatrix);
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);
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;
226 GTSAM_EXPORT Matrix diag(
const std::vector<Matrix>& Hs);
234 template<
class MATRIX>
235 const typename MATRIX::ConstColXpr
column(
const MATRIX& A,
size_t j) {
245 template<
class MATRIX>
246 const typename MATRIX::ConstRowXpr
row(
const MATRIX& A,
size_t j) {
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);
261 GTSAM_EXPORT Vector columnNormSquare(
const Matrix &A);
268 template<
class MATRIX>
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();
279 inline Matrix
trans(
const Matrix& A) {
return A.transpose(); }
285 GTSAM_EXPORT
void solve(Matrix& A, Matrix& B);
290 GTSAM_EXPORT Matrix
inverse(
const Matrix& A);
298 GTSAM_EXPORT std::pair<Matrix,Matrix>
qr(
const Matrix& A);
305 template <
class MATRIX>
307 size_t rows = A.rows();
308 size_t cols = A.cols();
309 size_t size = std::min(rows,cols);
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);
316 Eigen::internal::householder_qr_inplace_blocked<MATRIX, HCoeffsType>::run(A, hCoeffs, 48, temp.data());
329 GTSAM_EXPORT std::list<boost::tuple<Vector, double, double> >
339 GTSAM_EXPORT
void householder_(Matrix& A,
size_t k,
bool copy_vectors=
true);
347 GTSAM_EXPORT
void householder(Matrix& A,
size_t k);
356 GTSAM_EXPORT Vector
backSubstituteUpper(
const Matrix& U,
const Vector& b,
bool unit=
false);
366 GTSAM_EXPORT Vector
backSubstituteUpper(
const Vector& b,
const Matrix& U,
bool unit=
false);
375 GTSAM_EXPORT Vector
backSubstituteLower(
const Matrix& L,
const Vector& b,
bool unit=
false);
383 GTSAM_EXPORT Matrix
stack(
size_t nrMatrices, ...);
384 GTSAM_EXPORT Matrix
stack(
const std::vector<Matrix>& blocks);
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, ...);
406 GTSAM_EXPORT Matrix vector_scale(
const Vector& v,
const Matrix& A,
bool inf_mask =
false);
407 GTSAM_EXPORT Matrix vector_scale(
const Matrix& A,
const Vector& v,
bool inf_mask =
false);
419 GTSAM_EXPORT Matrix3
skewSymmetric(
double wx,
double wy,
double wz);
420 template<
class Derived>
427 GTSAM_EXPORT Matrix
LLt(
const Matrix& A);
430 GTSAM_EXPORT Matrix
RtR(
const Matrix& A);
447 GTSAM_EXPORT
void svd(
const Matrix& A, Matrix& U, Vector& S, Matrix& V);
456 GTSAM_EXPORT boost::tuple<int, double, Vector>
457 DLT(
const Matrix& A,
double rank_tol = 1e-9);
464 GTSAM_EXPORT Matrix
expm(
const Matrix& A,
size_t K=7);
467 GTSAM_EXPORT Matrix
Cayley(
const Matrix& A);
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();
477 std::string formatMatrixIndented(
const std::string& label,
const Matrix& matrix,
bool makeVectorHorizontal =
false);
481 #include <boost/serialization/nvp.hpp>
482 #include <boost/serialization/array.hpp>
483 #include <boost/serialization/split_free.hpp>
486 namespace serialization {
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()));
497 template<
class Archive>
498 void load(Archive & ar, gtsam::Matrix & m,
unsigned int version) {
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()));
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