1 | // This file is part of Eigen, a lightweight C++ template library
|
---|
2 | // for linear algebra.
|
---|
3 | //
|
---|
4 | // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
|
---|
5 | // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
|
---|
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> void determinant(const MatrixType& m)
|
---|
15 | {
|
---|
16 | /* this test covers the following files:
|
---|
17 | Determinant.h
|
---|
18 | */
|
---|
19 | typedef typename MatrixType::Index Index;
|
---|
20 | Index size = m.rows();
|
---|
21 |
|
---|
22 | MatrixType m1(size, size), m2(size, size);
|
---|
23 | m1.setRandom();
|
---|
24 | m2.setRandom();
|
---|
25 | typedef typename MatrixType::Scalar Scalar;
|
---|
26 | Scalar x = internal::random<Scalar>();
|
---|
27 | VERIFY_IS_APPROX(MatrixType::Identity(size, size).determinant(), Scalar(1));
|
---|
28 | VERIFY_IS_APPROX((m1*m2).eval().determinant(), m1.determinant() * m2.determinant());
|
---|
29 | if(size==1) return;
|
---|
30 | Index i = internal::random<Index>(0, size-1);
|
---|
31 | Index j;
|
---|
32 | do {
|
---|
33 | j = internal::random<Index>(0, size-1);
|
---|
34 | } while(j==i);
|
---|
35 | m2 = m1;
|
---|
36 | m2.row(i).swap(m2.row(j));
|
---|
37 | VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
|
---|
38 | m2 = m1;
|
---|
39 | m2.col(i).swap(m2.col(j));
|
---|
40 | VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
|
---|
41 | VERIFY_IS_APPROX(m2.determinant(), m2.transpose().determinant());
|
---|
42 | VERIFY_IS_APPROX(numext::conj(m2.determinant()), m2.adjoint().determinant());
|
---|
43 | m2 = m1;
|
---|
44 | m2.row(i) += x*m2.row(j);
|
---|
45 | VERIFY_IS_APPROX(m2.determinant(), m1.determinant());
|
---|
46 | m2 = m1;
|
---|
47 | m2.row(i) *= x;
|
---|
48 | VERIFY_IS_APPROX(m2.determinant(), m1.determinant() * x);
|
---|
49 |
|
---|
50 | // check empty matrix
|
---|
51 | VERIFY_IS_APPROX(m2.block(0,0,0,0).determinant(), Scalar(1));
|
---|
52 | }
|
---|
53 |
|
---|
54 | void test_determinant()
|
---|
55 | {
|
---|
56 | for(int i = 0; i < g_repeat; i++) {
|
---|
57 | int s = 0;
|
---|
58 | CALL_SUBTEST_1( determinant(Matrix<float, 1, 1>()) );
|
---|
59 | CALL_SUBTEST_2( determinant(Matrix<double, 2, 2>()) );
|
---|
60 | CALL_SUBTEST_3( determinant(Matrix<double, 3, 3>()) );
|
---|
61 | CALL_SUBTEST_4( determinant(Matrix<double, 4, 4>()) );
|
---|
62 | CALL_SUBTEST_5( determinant(Matrix<std::complex<double>, 10, 10>()) );
|
---|
63 | s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
|
---|
64 | CALL_SUBTEST_6( determinant(MatrixXd(s, s)) );
|
---|
65 | TEST_SET_BUT_UNUSED_VARIABLE(s)
|
---|
66 | }
|
---|
67 | }
|
---|