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