inverse.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #include "main.h"
00027 #include <Eigen/LU>
00028 
00029 template<typename MatrixType> void inverse(const MatrixType& m)
00030 {
00031   typedef typename MatrixType::Index Index;
00032   /* this test covers the following files:
00033      Inverse.h
00034   */
00035   Index rows = m.rows();
00036   Index cols = m.cols();
00037 
00038   typedef typename MatrixType::Scalar Scalar;
00039   typedef typename NumTraits<Scalar>::Real RealScalar;
00040   typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
00041 
00042   MatrixType m1(rows, cols),
00043              m2(rows, cols),
00044              mzero = MatrixType::Zero(rows, cols),
00045              identity = MatrixType::Identity(rows, rows);
00046   createRandomPIMatrixOfRank(rows,rows,rows,m1);
00047   m2 = m1.inverse();
00048   VERIFY_IS_APPROX(m1, m2.inverse() );
00049 
00050   VERIFY_IS_APPROX((Scalar(2)*m2).inverse(), m2.inverse()*Scalar(0.5));
00051 
00052   VERIFY_IS_APPROX(identity, m1.inverse() * m1 );
00053   VERIFY_IS_APPROX(identity, m1 * m1.inverse() );
00054 
00055   VERIFY_IS_APPROX(m1, m1.inverse().inverse() );
00056 
00057   // since for the general case we implement separately row-major and col-major, test that
00058   VERIFY_IS_APPROX(MatrixType(m1.transpose().inverse()), MatrixType(m1.inverse().transpose()));
00059 
00060 #if !defined(EIGEN_TEST_PART_5) && !defined(EIGEN_TEST_PART_6)
00061   //computeInverseAndDetWithCheck tests
00062   //First: an invertible matrix
00063   bool invertible;
00064   RealScalar det;
00065 
00066   m2.setZero();
00067   m1.computeInverseAndDetWithCheck(m2, det, invertible);
00068   VERIFY(invertible);
00069   VERIFY_IS_APPROX(identity, m1*m2);
00070   VERIFY_IS_APPROX(det, m1.determinant());
00071 
00072   m2.setZero();
00073   m1.computeInverseWithCheck(m2, invertible);
00074   VERIFY(invertible);
00075   VERIFY_IS_APPROX(identity, m1*m2);
00076 
00077   //Second: a rank one matrix (not invertible, except for 1x1 matrices)
00078   VectorType v3 = VectorType::Random(rows);
00079   MatrixType m3 = v3*v3.transpose(), m4(rows,cols);
00080   m3.computeInverseAndDetWithCheck(m4, det, invertible);
00081   VERIFY( rows==1 ? invertible : !invertible );
00082   VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(det-m3.determinant()), RealScalar(1));
00083   m3.computeInverseWithCheck(m4, invertible);
00084   VERIFY( rows==1 ? invertible : !invertible );
00085 #endif
00086 
00087   // check in-place inversion
00088   if(MatrixType::RowsAtCompileTime>=2 && MatrixType::RowsAtCompileTime<=4)
00089   {
00090     // in-place is forbidden
00091     VERIFY_RAISES_ASSERT(m1 = m1.inverse());
00092   }
00093   else
00094   {
00095     m2 = m1.inverse();
00096     m1 = m1.inverse();
00097     VERIFY_IS_APPROX(m1,m2);
00098   }
00099 }
00100 
00101 void test_inverse()
00102 {
00103   int s;
00104   for(int i = 0; i < g_repeat; i++) {
00105     CALL_SUBTEST_1( inverse(Matrix<double,1,1>()) );
00106     CALL_SUBTEST_2( inverse(Matrix2d()) );
00107     CALL_SUBTEST_3( inverse(Matrix3f()) );
00108     CALL_SUBTEST_4( inverse(Matrix4f()) );
00109     CALL_SUBTEST_4( inverse(Matrix<float,4,4,DontAlign>()) );
00110     s = internal::random<int>(50,320);
00111     CALL_SUBTEST_5( inverse(MatrixXf(s,s)) );
00112     s = internal::random<int>(25,100);
00113     CALL_SUBTEST_6( inverse(MatrixXcd(s,s)) );
00114     CALL_SUBTEST_7( inverse(Matrix4d()) );
00115     CALL_SUBTEST_7( inverse(Matrix<double,4,4,DontAlign>()) );
00116   }
00117 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:26