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