inverse.cpp File Reference
#include "main.h"
#include <Eigen/LU>

Functions

template<typename MatrixType >
void inverse_for_fixed_size (const MatrixType &, std::enable_if_t< MatrixType::SizeAtCompileTime==Dynamic > *=0)
 
template<typename MatrixType >
void inverse_for_fixed_size (const MatrixType &m1, std::enable_if_t< MatrixType::SizeAtCompileTime !=Dynamic > *=0)
 
template<typename MatrixType >
void inverse (const MatrixType &m)
 
template<typename Scalar >
void inverse_zerosized ()
 
 EIGEN_DECLARE_TEST (inverse)
 

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( inverse  )
116  {
117  int s = 0;
118  for (int i = 0; i < g_repeat; i++) {
120  CALL_SUBTEST_2(inverse(Matrix2d()));
121  CALL_SUBTEST_3(inverse(Matrix3f()));
122  CALL_SUBTEST_4(inverse(Matrix4f()));
124 
125  s = internal::random<int>(50, 320);
126  CALL_SUBTEST_5(inverse(MatrixXf(s, s)));
128  CALL_SUBTEST_5(inverse_zerosized<float>());
129  CALL_SUBTEST_5(inverse(MatrixXf(0, 0)));
130  CALL_SUBTEST_5(inverse(MatrixXf(1, 1)));
131 
132  s = internal::random<int>(25, 100);
133  CALL_SUBTEST_6(inverse(MatrixXcd(s, s)));
135 
136  CALL_SUBTEST_7(inverse(Matrix4d()));
138 
139  CALL_SUBTEST_8(inverse(Matrix4cd()));
140  }
141 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
void inverse(const MatrixType &m)
Definition: inverse.cpp:64
RealScalar s
Definition: level1_cplx_impl.h:130
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:139
static int g_repeat
Definition: main.h:191
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22

References CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, CALL_SUBTEST_8, Eigen::g_repeat, i, inverse(), s, and TEST_SET_BUT_UNUSED_VARIABLE.

◆ inverse()

template<typename MatrixType >
void inverse ( const MatrixType m)
64  {
65  /* this test covers the following files:
66  Inverse.h
67  */
68  Index rows = m.rows();
69  Index cols = m.cols();
70 
71  typedef typename MatrixType::Scalar Scalar;
72 
73  MatrixType m1(rows, cols), m2(rows, cols), identity = MatrixType::Identity(rows, rows);
75  m2 = m1.inverse();
76  VERIFY_IS_APPROX(m1, m2.inverse());
77 
78  VERIFY_IS_APPROX((Scalar(2) * m2).inverse(), m2.inverse() * Scalar(0.5));
79 
80  VERIFY_IS_APPROX(identity, m1.inverse() * m1);
81  VERIFY_IS_APPROX(identity, m1 * m1.inverse());
82 
83  VERIFY_IS_APPROX(m1, m1.inverse().inverse());
84 
85  // since for the general case we implement separately row-major and col-major, test that
86  VERIFY_IS_APPROX(MatrixType(m1.transpose().inverse()), MatrixType(m1.inverse().transpose()));
87 
89 
90  // check in-place inversion
91  if (MatrixType::RowsAtCompileTime >= 2 && MatrixType::RowsAtCompileTime <= 4) {
92  // in-place is forbidden
93  VERIFY_RAISES_ASSERT(m1 = m1.inverse());
94  } else {
95  m2 = m1.inverse();
96  m1 = m1.inverse();
98  }
99 }
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
void inverse_for_fixed_size(const MatrixType &, std::enable_if_t< MatrixType::SizeAtCompileTime==Dynamic > *=0)
Definition: inverse.cpp:15
int * m
Definition: level2_cplx_impl.h:294
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:329
void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType &m)
Definition: random_matrix_helper.h:60
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83

References cols, Eigen::createRandomPIMatrixOfRank(), inverse_for_fixed_size(), m, m1, m2(), rows, VERIFY_IS_APPROX, and VERIFY_RAISES_ASSERT.

Referenced by array_complex(), array_real(), Eigen::MatrixBase< Derived >::computeInverseAndDetWithCheck(), EIGEN_DECLARE_TEST(), Eigen::internal::kissfft_impl< Scalar_ >::get_plan(), Eigen::internal::fftw_impl< Scalar_ >::get_plan(), Eigen::internal::kiss_cpx_fft< Scalar_ >::make_twiddles(), Eigen::internal::compute_inverse_and_det_with_check< MatrixType, ResultType, 2 >::run(), Eigen::internal::compute_inverse_and_det_with_check< MatrixType, ResultType, 3 >::run(), Eigen::internal::compute_inverse_and_det_with_check< MatrixType, ResultType, 4 >::run(), test_3d(), and test_unary_builtins_for_assignement().

◆ inverse_for_fixed_size() [1/2]

template<typename MatrixType >
void inverse_for_fixed_size ( const MatrixType ,
std::enable_if_t< MatrixType::SizeAtCompileTime==Dynamic > *  = 0 
)
15 {}

Referenced by inverse().

◆ inverse_for_fixed_size() [2/2]

template<typename MatrixType >
void inverse_for_fixed_size ( const MatrixType m1,
std::enable_if_t< MatrixType::SizeAtCompileTime !=Dynamic > *  = 0 
)
18  {
19  using std::abs;
20 
21  MatrixType m2, identity = MatrixType::Identity();
22 
23  typedef typename MatrixType::Scalar Scalar;
24  typedef typename NumTraits<Scalar>::Real RealScalar;
26 
27  // computeInverseAndDetWithCheck tests
28  // First: an invertible matrix
29  bool invertible;
30  Scalar det;
31 
32  m2.setZero();
33  m1.computeInverseAndDetWithCheck(m2, det, invertible);
34  VERIFY(invertible);
35  VERIFY_IS_APPROX(identity, m1 * m2);
36  VERIFY_IS_APPROX(det, m1.determinant());
37 
38  m2.setZero();
39  m1.computeInverseWithCheck(m2, invertible);
40  VERIFY(invertible);
41  VERIFY_IS_APPROX(identity, m1 * m2);
42 
43  // Second: a rank one matrix (not invertible, except for 1x1 matrices)
44  VectorType v3 = VectorType::Random();
45  MatrixType m3 = v3 * v3.transpose(), m4;
46  m3.computeInverseAndDetWithCheck(m4, det, invertible);
47  VERIFY(m1.rows() == 1 ? invertible : !invertible);
48  VERIFY_IS_MUCH_SMALLER_THAN(abs(det - m3.determinant()), RealScalar(1));
49  m3.computeInverseWithCheck(m4, invertible);
50  VERIFY(m1.rows() == 1 ? invertible : !invertible);
51 
52  // check with submatrices
53  {
55  m5.setRandom();
56  m5.topLeftCorner(m1.rows(), m1.rows()) = m1;
57  m2 = m5.template topLeftCorner<MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime>().inverse();
58  VERIFY_IS_APPROX((m5.template topLeftCorner<MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime>()),
59  m2.inverse());
60  }
61 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
Derived & setRandom(Index size)
Definition: Random.h:147
#define VERIFY(a)
Definition: main.h:362
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:371
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: fft_test_shared.h:66

References abs(), m1, m2(), Eigen::PlainObjectBase< Derived >::setRandom(), VERIFY, VERIFY_IS_APPROX, and VERIFY_IS_MUCH_SMALLER_THAN.

◆ inverse_zerosized()

template<typename Scalar >
void inverse_zerosized ( )
102  {
104  {
106  x = A.inverse() * b;
107  }
108  {
110  x = A.inverse() * b;
111  VERIFY_IS_EQUAL(x.rows(), 0);
112  VERIFY_IS_EQUAL(x.cols(), 1);
113  }
114 }
Scalar * b
Definition: benchVecAdd.cpp:17
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
list x
Definition: plotDoE.py:28

References b, VERIFY_IS_EQUAL, and plotDoE::x.