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 
inverse(const MatrixType & m)14 template<typename MatrixType> void inverse(const MatrixType& m)
15 {
16   using std::abs;
17   typedef typename MatrixType::Index Index;
18   /* this test covers the following files:
19      Inverse.h
20   */
21   Index rows = m.rows();
22   Index cols = m.cols();
23 
24   typedef typename MatrixType::Scalar Scalar;
25 
26   MatrixType m1(rows, cols),
27              m2(rows, cols),
28              identity = MatrixType::Identity(rows, rows);
29   createRandomPIMatrixOfRank(rows,rows,rows,m1);
30   m2 = m1.inverse();
31   VERIFY_IS_APPROX(m1, m2.inverse() );
32 
33   VERIFY_IS_APPROX((Scalar(2)*m2).inverse(), m2.inverse()*Scalar(0.5));
34 
35   VERIFY_IS_APPROX(identity, m1.inverse() * m1 );
36   VERIFY_IS_APPROX(identity, m1 * m1.inverse() );
37 
38   VERIFY_IS_APPROX(m1, m1.inverse().inverse() );
39 
40   // since for the general case we implement separately row-major and col-major, test that
41   VERIFY_IS_APPROX(MatrixType(m1.transpose().inverse()), MatrixType(m1.inverse().transpose()));
42 
43 #if !defined(EIGEN_TEST_PART_5) && !defined(EIGEN_TEST_PART_6)
44   typedef typename NumTraits<Scalar>::Real RealScalar;
45   typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
46 
47   //computeInverseAndDetWithCheck tests
48   //First: an invertible matrix
49   bool invertible;
50   RealScalar det;
51 
52   m2.setZero();
53   m1.computeInverseAndDetWithCheck(m2, det, invertible);
54   VERIFY(invertible);
55   VERIFY_IS_APPROX(identity, m1*m2);
56   VERIFY_IS_APPROX(det, m1.determinant());
57 
58   m2.setZero();
59   m1.computeInverseWithCheck(m2, invertible);
60   VERIFY(invertible);
61   VERIFY_IS_APPROX(identity, m1*m2);
62 
63   //Second: a rank one matrix (not invertible, except for 1x1 matrices)
64   VectorType v3 = VectorType::Random(rows);
65   MatrixType m3 = v3*v3.transpose(), m4(rows,cols);
66   m3.computeInverseAndDetWithCheck(m4, det, invertible);
67   VERIFY( rows==1 ? invertible : !invertible );
68   VERIFY_IS_MUCH_SMALLER_THAN(abs(det-m3.determinant()), RealScalar(1));
69   m3.computeInverseWithCheck(m4, invertible);
70   VERIFY( rows==1 ? invertible : !invertible );
71 
72   // check with submatrices
73   {
74     Matrix<Scalar, MatrixType::RowsAtCompileTime+1, MatrixType::RowsAtCompileTime+1, MatrixType::Options> m5;
75     m5.setRandom();
76     m5.topLeftCorner(rows,rows) = m1;
77     m2 = m5.template topLeftCorner<MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime>().inverse();
78     VERIFY_IS_APPROX( (m5.template topLeftCorner<MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime>()), m2.inverse() );
79   }
80 #endif
81 
82   // check in-place inversion
83   if(MatrixType::RowsAtCompileTime>=2 && MatrixType::RowsAtCompileTime<=4)
84   {
85     // in-place is forbidden
86     VERIFY_RAISES_ASSERT(m1 = m1.inverse());
87   }
88   else
89   {
90     m2 = m1.inverse();
91     m1 = m1.inverse();
92     VERIFY_IS_APPROX(m1,m2);
93   }
94 }
95 
test_inverse()96 void test_inverse()
97 {
98   int s = 0;
99   for(int i = 0; i < g_repeat; i++) {
100     CALL_SUBTEST_1( inverse(Matrix<double,1,1>()) );
101     CALL_SUBTEST_2( inverse(Matrix2d()) );
102     CALL_SUBTEST_3( inverse(Matrix3f()) );
103     CALL_SUBTEST_4( inverse(Matrix4f()) );
104     CALL_SUBTEST_4( inverse(Matrix<float,4,4,DontAlign>()) );
105 
106     s = internal::random<int>(50,320);
107     CALL_SUBTEST_5( inverse(MatrixXf(s,s)) );
108     TEST_SET_BUT_UNUSED_VARIABLE(s)
109 
110     s = internal::random<int>(25,100);
111     CALL_SUBTEST_6( inverse(MatrixXcd(s,s)) );
112     TEST_SET_BUT_UNUSED_VARIABLE(s)
113 
114     CALL_SUBTEST_7( inverse(Matrix4d()) );
115     CALL_SUBTEST_7( inverse(Matrix<double,4,4,DontAlign>()) );
116   }
117 }
118