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 
76  m2(rows, cols),
77  identity = MatrixType::Identity(rows, rows);
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 }
VERIFY_IS_MUCH_SMALLER_THAN
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(inverse)
Definition: inverse.cpp:124
Eigen::PlainObjectBase< Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::setRandom
Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > & setRandom(Index size)
Definition: Random.h:151
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
s
RealScalar s
Definition: level1_cplx_impl.h:126
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
b
Scalar * b
Definition: benchVecAdd.cpp:17
x
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
Definition: gnuplot_common_settings.hh:12
m1
Matrix3d m1
Definition: IOFormat.cpp:2
inverse
void inverse(const MatrixType &m)
Definition: inverse.cpp:65
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
Eigen::createRandomPIMatrixOfRank
void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType &m)
Definition: main.h:653
inverse_zerosized
void inverse_zerosized()
Definition: inverse.cpp:109
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
CALL_SUBTEST_6
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
m3
static const DiscreteKey m3(M(3), 2)
main.h
inverse_for_fixed_size
void inverse_for_fixed_size(const MatrixType &, typename internal::enable_if< MatrixType::SizeAtCompileTime==Dynamic >::type *=0)
Definition: inverse.cpp:15
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
abs
#define abs(x)
Definition: datatypes.h:17
VectorType
Definition: FFTW.cpp:65
v3
Vector v3
Definition: testSerializationBase.cpp:40
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
CALL_SUBTEST_7
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
CALL_SUBTEST_8
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
TEST_SET_BUT_UNUSED_VARIABLE
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:42