determinant.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 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
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 determinant(const MatrixType& m)
00030 {
00031   /* this test covers the following files:
00032      Determinant.h
00033   */
00034   typedef typename MatrixType::Index Index;
00035   Index size = m.rows();
00036 
00037   MatrixType m1(size, size), m2(size, size);
00038   m1.setRandom();
00039   m2.setRandom();
00040   typedef typename MatrixType::Scalar Scalar;
00041   Scalar x = internal::random<Scalar>();
00042   VERIFY_IS_APPROX(MatrixType::Identity(size, size).determinant(), Scalar(1));
00043   VERIFY_IS_APPROX((m1*m2).eval().determinant(), m1.determinant() * m2.determinant());
00044   if(size==1) return;
00045   Index i = internal::random<Index>(0, size-1);
00046   Index j;
00047   do {
00048     j = internal::random<Index>(0, size-1);
00049   } while(j==i);
00050   m2 = m1;
00051   m2.row(i).swap(m2.row(j));
00052   VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
00053   m2 = m1;
00054   m2.col(i).swap(m2.col(j));
00055   VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
00056   VERIFY_IS_APPROX(m2.determinant(), m2.transpose().determinant());
00057   VERIFY_IS_APPROX(internal::conj(m2.determinant()), m2.adjoint().determinant());
00058   m2 = m1;
00059   m2.row(i) += x*m2.row(j);
00060   VERIFY_IS_APPROX(m2.determinant(), m1.determinant());
00061   m2 = m1;
00062   m2.row(i) *= x;
00063   VERIFY_IS_APPROX(m2.determinant(), m1.determinant() * x);
00064   
00065   // check empty matrix
00066   VERIFY_IS_APPROX(m2.block(0,0,0,0).determinant(), Scalar(1));
00067 }
00068 
00069 void test_determinant()
00070 {
00071   for(int i = 0; i < g_repeat; i++) {
00072     CALL_SUBTEST_1( determinant(Matrix<float, 1, 1>()) );
00073     CALL_SUBTEST_2( determinant(Matrix<double, 2, 2>()) );
00074     CALL_SUBTEST_3( determinant(Matrix<double, 3, 3>()) );
00075     CALL_SUBTEST_4( determinant(Matrix<double, 4, 4>()) );
00076     CALL_SUBTEST_5( determinant(Matrix<std::complex<double>, 10, 10>()) );
00077     CALL_SUBTEST_6( determinant(MatrixXd(20, 20)) );
00078   }
00079   CALL_SUBTEST_6( determinant(MatrixXd(200, 200)) );
00080 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:37