1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2008 Benoit Jacob <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2008 Gael Guennebaud <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li
11*bf2c3715SXin Li #include "main.h"
12*bf2c3715SXin Li #include <Eigen/LU>
13*bf2c3715SXin Li
determinant(const MatrixType & m)14*bf2c3715SXin Li template<typename MatrixType> void determinant(const MatrixType& m)
15*bf2c3715SXin Li {
16*bf2c3715SXin Li /* this test covers the following files:
17*bf2c3715SXin Li Determinant.h
18*bf2c3715SXin Li */
19*bf2c3715SXin Li Index size = m.rows();
20*bf2c3715SXin Li
21*bf2c3715SXin Li MatrixType m1(size, size), m2(size, size);
22*bf2c3715SXin Li m1.setRandom();
23*bf2c3715SXin Li m2.setRandom();
24*bf2c3715SXin Li typedef typename MatrixType::Scalar Scalar;
25*bf2c3715SXin Li Scalar x = internal::random<Scalar>();
26*bf2c3715SXin Li VERIFY_IS_APPROX(MatrixType::Identity(size, size).determinant(), Scalar(1));
27*bf2c3715SXin Li VERIFY_IS_APPROX((m1*m2).eval().determinant(), m1.determinant() * m2.determinant());
28*bf2c3715SXin Li if(size==1) return;
29*bf2c3715SXin Li Index i = internal::random<Index>(0, size-1);
30*bf2c3715SXin Li Index j;
31*bf2c3715SXin Li do {
32*bf2c3715SXin Li j = internal::random<Index>(0, size-1);
33*bf2c3715SXin Li } while(j==i);
34*bf2c3715SXin Li m2 = m1;
35*bf2c3715SXin Li m2.row(i).swap(m2.row(j));
36*bf2c3715SXin Li VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
37*bf2c3715SXin Li m2 = m1;
38*bf2c3715SXin Li m2.col(i).swap(m2.col(j));
39*bf2c3715SXin Li VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
40*bf2c3715SXin Li VERIFY_IS_APPROX(m2.determinant(), m2.transpose().determinant());
41*bf2c3715SXin Li VERIFY_IS_APPROX(numext::conj(m2.determinant()), m2.adjoint().determinant());
42*bf2c3715SXin Li m2 = m1;
43*bf2c3715SXin Li m2.row(i) += x*m2.row(j);
44*bf2c3715SXin Li VERIFY_IS_APPROX(m2.determinant(), m1.determinant());
45*bf2c3715SXin Li m2 = m1;
46*bf2c3715SXin Li m2.row(i) *= x;
47*bf2c3715SXin Li VERIFY_IS_APPROX(m2.determinant(), m1.determinant() * x);
48*bf2c3715SXin Li
49*bf2c3715SXin Li // check empty matrix
50*bf2c3715SXin Li VERIFY_IS_APPROX(m2.block(0,0,0,0).determinant(), Scalar(1));
51*bf2c3715SXin Li }
52*bf2c3715SXin Li
EIGEN_DECLARE_TEST(determinant)53*bf2c3715SXin Li EIGEN_DECLARE_TEST(determinant)
54*bf2c3715SXin Li {
55*bf2c3715SXin Li for(int i = 0; i < g_repeat; i++) {
56*bf2c3715SXin Li int s = 0;
57*bf2c3715SXin Li CALL_SUBTEST_1( determinant(Matrix<float, 1, 1>()) );
58*bf2c3715SXin Li CALL_SUBTEST_2( determinant(Matrix<double, 2, 2>()) );
59*bf2c3715SXin Li CALL_SUBTEST_3( determinant(Matrix<double, 3, 3>()) );
60*bf2c3715SXin Li CALL_SUBTEST_4( determinant(Matrix<double, 4, 4>()) );
61*bf2c3715SXin Li CALL_SUBTEST_5( determinant(Matrix<std::complex<double>, 10, 10>()) );
62*bf2c3715SXin Li s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
63*bf2c3715SXin Li CALL_SUBTEST_6( determinant(MatrixXd(s, s)) );
64*bf2c3715SXin Li TEST_SET_BUT_UNUSED_VARIABLE(s)
65*bf2c3715SXin Li }
66*bf2c3715SXin Li }
67