cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

inverse.cpp (4701B)


      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>
     15 void inverse_for_fixed_size(const MatrixType&, typename internal::enable_if<MatrixType::SizeAtCompileTime==Dynamic>::type* = 0)
     16 {
     17 }
     18 
     19 template<typename MatrixType>
     20 void inverse_for_fixed_size(const MatrixType& m1, typename internal::enable_if<MatrixType::SizeAtCompileTime!=Dynamic>::type* = 0)
     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;
     28   typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
     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   {
     57     Matrix<Scalar, MatrixType::RowsAtCompileTime+1, MatrixType::RowsAtCompileTime+1, MatrixType::Options> m5;
     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 
     75   MatrixType m1(rows, cols),
     76              m2(rows, cols),
     77              identity = MatrixType::Identity(rows, rows);
     78   createRandomPIMatrixOfRank(rows,rows,rows,m1);
     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 
     92   inverse_for_fixed_size(m1);
     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();
    104     VERIFY_IS_APPROX(m1,m2);
    105   }
    106 }
    107 
    108 template<typename Scalar>
    109 void inverse_zerosized()
    110 {
    111   Matrix<Scalar,Dynamic,Dynamic> A(0,0);
    112   {
    113     Matrix<Scalar,0,1> b, x;
    114     x = A.inverse() * b;
    115   }
    116   {
    117     Matrix<Scalar,Dynamic,Dynamic> b(0,1), x;
    118     x = A.inverse() * b;
    119     VERIFY_IS_EQUAL(x.rows(), 0);
    120     VERIFY_IS_EQUAL(x.cols(), 1);
    121   }
    122 }
    123 
    124 EIGEN_DECLARE_TEST(inverse)
    125 {
    126   int s = 0;
    127   for(int i = 0; i < g_repeat; i++) {
    128     CALL_SUBTEST_1( inverse(Matrix<double,1,1>()) );
    129     CALL_SUBTEST_2( inverse(Matrix2d()) );
    130     CALL_SUBTEST_3( inverse(Matrix3f()) );
    131     CALL_SUBTEST_4( inverse(Matrix4f()) );
    132     CALL_SUBTEST_4( inverse(Matrix<float,4,4,DontAlign>()) );
    133     
    134     s = internal::random<int>(50,320); 
    135     CALL_SUBTEST_5( inverse(MatrixXf(s,s)) );
    136     TEST_SET_BUT_UNUSED_VARIABLE(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)) );
    143     TEST_SET_BUT_UNUSED_VARIABLE(s)
    144     
    145     CALL_SUBTEST_7( inverse(Matrix4d()) );
    146     CALL_SUBTEST_7( inverse(Matrix<double,4,4,DontAlign>()) );
    147 
    148     CALL_SUBTEST_8( inverse(Matrix4cd()) );
    149   }
    150 }