inverse.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #include "main.h"
12 #include <Eigen/LU>
13 
14 template<typename MatrixType>
16 {
17 }
18 
19 template<typename MatrixType>
21 {
22  using std::abs;
23 
24  MatrixType m2, identity = MatrixType::Identity();
25 
26  typedef typename MatrixType::Scalar Scalar;
27  typedef typename NumTraits<Scalar>::Real RealScalar;
29 
30  //computeInverseAndDetWithCheck tests
31  //First: an invertible matrix
32  bool invertible;
33  Scalar det;
34 
35  m2.setZero();
36  m1.computeInverseAndDetWithCheck(m2, det, invertible);
37  VERIFY(invertible);
38  VERIFY_IS_APPROX(identity, m1*m2);
39  VERIFY_IS_APPROX(det, m1.determinant());
40 
41  m2.setZero();
42  m1.computeInverseWithCheck(m2, invertible);
43  VERIFY(invertible);
44  VERIFY_IS_APPROX(identity, m1*m2);
45 
46  //Second: a rank one matrix (not invertible, except for 1x1 matrices)
47  VectorType v3 = VectorType::Random();
48  MatrixType m3 = v3*v3.transpose(), m4;
49  m3.computeInverseAndDetWithCheck(m4, det, invertible);
50  VERIFY( m1.rows()==1 ? invertible : !invertible );
51  VERIFY_IS_MUCH_SMALLER_THAN(abs(det-m3.determinant()), RealScalar(1));
52  m3.computeInverseWithCheck(m4, invertible);
53  VERIFY( m1.rows()==1 ? invertible : !invertible );
54 
55  // check with submatrices
56  {
58  m5.setRandom();
59  m5.topLeftCorner(m1.rows(),m1.rows()) = m1;
60  m2 = m5.template topLeftCorner<MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime>().inverse();
61  VERIFY_IS_APPROX( (m5.template topLeftCorner<MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime>()), m2.inverse() );
62  }
63 }
64 
65 template<typename MatrixType> void inverse(const MatrixType& m)
66 {
67  /* this test covers the following files:
68  Inverse.h
69  */
70  Index rows = m.rows();
71  Index cols = m.cols();
72 
73  typedef typename MatrixType::Scalar Scalar;
74 
75  MatrixType m1(rows, cols),
76  m2(rows, cols),
77  identity = MatrixType::Identity(rows, rows);
78  createRandomPIMatrixOfRank(rows,rows,rows,m1);
79  m2 = m1.inverse();
80  VERIFY_IS_APPROX(m1, m2.inverse() );
81 
82  VERIFY_IS_APPROX((Scalar(2)*m2).inverse(), m2.inverse()*Scalar(0.5));
83 
84  VERIFY_IS_APPROX(identity, m1.inverse() * m1 );
85  VERIFY_IS_APPROX(identity, m1 * m1.inverse() );
86 
87  VERIFY_IS_APPROX(m1, m1.inverse().inverse() );
88 
89  // since for the general case we implement separately row-major and col-major, test that
90  VERIFY_IS_APPROX(MatrixType(m1.transpose().inverse()), MatrixType(m1.inverse().transpose()));
91 
93 
94  // check in-place inversion
95  if(MatrixType::RowsAtCompileTime>=2 && MatrixType::RowsAtCompileTime<=4)
96  {
97  // in-place is forbidden
98  VERIFY_RAISES_ASSERT(m1 = m1.inverse());
99  }
100  else
101  {
102  m2 = m1.inverse();
103  m1 = m1.inverse();
105  }
106 }
107 
108 template<typename Scalar>
110 {
112  {
114  x = A.inverse() * b;
115  }
116  {
118  x = A.inverse() * b;
119  VERIFY_IS_EQUAL(x.rows(), 0);
120  VERIFY_IS_EQUAL(x.cols(), 1);
121  }
122 }
123 
125 {
126  int s = 0;
127  for(int i = 0; i < g_repeat; i++) {
129  CALL_SUBTEST_2( inverse(Matrix2d()) );
130  CALL_SUBTEST_3( inverse(Matrix3f()) );
131  CALL_SUBTEST_4( inverse(Matrix4f()) );
133 
134  s = internal::random<int>(50,320);
135  CALL_SUBTEST_5( inverse(MatrixXf(s,s)) );
137  CALL_SUBTEST_5( inverse_zerosized<float>() );
138  CALL_SUBTEST_5( inverse(MatrixXf(0, 0)) );
139  CALL_SUBTEST_5( inverse(MatrixXf(1, 1)) );
140 
141  s = internal::random<int>(25,100);
142  CALL_SUBTEST_6( inverse(MatrixXcd(s,s)) );
144 
145  CALL_SUBTEST_7( inverse(Matrix4d()) );
147 
148  CALL_SUBTEST_8( inverse(Matrix4cd()) );
149  }
150 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
Scalar * b
Definition: benchVecAdd.cpp:17
#define CALL_SUBTEST_3(FUNC)
MatrixType m2(n_dims)
#define CALL_SUBTEST_7(FUNC)
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
void inverse_zerosized()
Definition: inverse.cpp:109
#define VERIFY_IS_APPROX(a, b)
Vector v3
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
#define CALL_SUBTEST_1(FUNC)
Matrix3d m1
Definition: IOFormat.cpp:2
void inverse_for_fixed_size(const MatrixType &, typename internal::enable_if< MatrixType::SizeAtCompileTime==Dynamic >::type *=0)
Definition: inverse.cpp:15
static int g_repeat
Definition: main.h:169
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
#define CALL_SUBTEST_8(FUNC)
RealScalar s
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
void inverse(const MatrixType &m)
Definition: inverse.cpp:65
EIGEN_DECLARE_TEST(inverse)
Definition: inverse.cpp:124
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
#define CALL_SUBTEST_5(FUNC)
#define VERIFY(a)
Definition: main.h:380
void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType &m)
Definition: main.h:653
#define CALL_SUBTEST_2(FUNC)
The matrix class, also used for vectors and row-vectors.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
#define abs(x)
Definition: datatypes.h:17
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > & setRandom(Index size)
Definition: Random.h:151


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:23