eigen2_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. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008 Gael Guennebaud <g.gael@free.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   int size = m.rows();
00035 
00036   MatrixType m1(size, size), m2(size, size);
00037   m1.setRandom();
00038   m2.setRandom();
00039   typedef typename MatrixType::Scalar Scalar;
00040   Scalar x = ei_random<Scalar>();
00041   VERIFY_IS_APPROX(MatrixType::Identity(size, size).determinant(), Scalar(1));
00042   VERIFY_IS_APPROX((m1*m2).determinant(), m1.determinant() * m2.determinant());
00043   if(size==1) return;
00044   int i = ei_random<int>(0, size-1);
00045   int j;
00046   do {
00047     j = ei_random<int>(0, size-1);
00048   } while(j==i);
00049   m2 = m1;
00050   m2.row(i).swap(m2.row(j));
00051   VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
00052   m2 = m1;
00053   m2.col(i).swap(m2.col(j));
00054   VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
00055   VERIFY_IS_APPROX(m2.determinant(), m2.transpose().determinant());
00056   VERIFY_IS_APPROX(ei_conj(m2.determinant()), m2.adjoint().determinant());
00057   m2 = m1;
00058   m2.row(i) += x*m2.row(j);
00059   VERIFY_IS_APPROX(m2.determinant(), m1.determinant());
00060   m2 = m1;
00061   m2.row(i) *= x;
00062   VERIFY_IS_APPROX(m2.determinant(), m1.determinant() * x);
00063 }
00064 
00065 void test_eigen2_determinant()
00066 {
00067   for(int i = 0; i < g_repeat; i++) {
00068     CALL_SUBTEST_1( determinant(Matrix<float, 1, 1>()) );
00069     CALL_SUBTEST_2( determinant(Matrix<double, 2, 2>()) );
00070     CALL_SUBTEST_3( determinant(Matrix<double, 3, 3>()) );
00071     CALL_SUBTEST_4( determinant(Matrix<double, 4, 4>()) );
00072     CALL_SUBTEST_5( determinant(Matrix<std::complex<double>, 10, 10>()) );
00073     CALL_SUBTEST_6( determinant(MatrixXd(20, 20)) );
00074   }
00075   CALL_SUBTEST_6( determinant(MatrixXd(200, 200)) );
00076 }


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