1 | // This file is part of Eigen, a lightweight C++ template library
|
---|
2 | // for linear algebra.
|
---|
3 | //
|
---|
4 | // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
|
---|
5 | //
|
---|
6 | // This Source Code Form is subject to the terms of the Mozilla
|
---|
7 | // Public License v. 2.0. If a copy of the MPL was not distributed
|
---|
8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
---|
9 |
|
---|
10 | #include "main.h"
|
---|
11 |
|
---|
12 | template<typename MatrixType> void diagonal(const MatrixType& m)
|
---|
13 | {
|
---|
14 | typedef typename MatrixType::Index Index;
|
---|
15 | typedef typename MatrixType::Scalar Scalar;
|
---|
16 |
|
---|
17 | Index rows = m.rows();
|
---|
18 | Index cols = m.cols();
|
---|
19 |
|
---|
20 | MatrixType m1 = MatrixType::Random(rows, cols),
|
---|
21 | m2 = MatrixType::Random(rows, cols);
|
---|
22 |
|
---|
23 | //check diagonal()
|
---|
24 | VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal());
|
---|
25 | m2.diagonal() = 2 * m1.diagonal();
|
---|
26 | m2.diagonal()[0] *= 3;
|
---|
27 |
|
---|
28 | if (rows>2)
|
---|
29 | {
|
---|
30 | enum {
|
---|
31 | N1 = MatrixType::RowsAtCompileTime>2 ? 2 : 0,
|
---|
32 | N2 = MatrixType::RowsAtCompileTime>1 ? -1 : 0
|
---|
33 | };
|
---|
34 |
|
---|
35 | // check sub/super diagonal
|
---|
36 | if(MatrixType::SizeAtCompileTime!=Dynamic)
|
---|
37 | {
|
---|
38 | VERIFY(m1.template diagonal<N1>().RowsAtCompileTime == m1.diagonal(N1).size());
|
---|
39 | VERIFY(m1.template diagonal<N2>().RowsAtCompileTime == m1.diagonal(N2).size());
|
---|
40 | }
|
---|
41 |
|
---|
42 | m2.template diagonal<N1>() = 2 * m1.template diagonal<N1>();
|
---|
43 | VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
|
---|
44 | m2.template diagonal<N1>()[0] *= 3;
|
---|
45 | VERIFY_IS_APPROX(m2.template diagonal<N1>()[0], static_cast<Scalar>(6) * m1.template diagonal<N1>()[0]);
|
---|
46 |
|
---|
47 |
|
---|
48 | m2.template diagonal<N2>() = 2 * m1.template diagonal<N2>();
|
---|
49 | m2.template diagonal<N2>()[0] *= 3;
|
---|
50 | VERIFY_IS_APPROX(m2.template diagonal<N2>()[0], static_cast<Scalar>(6) * m1.template diagonal<N2>()[0]);
|
---|
51 |
|
---|
52 | m2.diagonal(N1) = 2 * m1.diagonal(N1);
|
---|
53 | VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
|
---|
54 | m2.diagonal(N1)[0] *= 3;
|
---|
55 | VERIFY_IS_APPROX(m2.diagonal(N1)[0], static_cast<Scalar>(6) * m1.diagonal(N1)[0]);
|
---|
56 |
|
---|
57 | m2.diagonal(N2) = 2 * m1.diagonal(N2);
|
---|
58 | VERIFY_IS_APPROX(m2.template diagonal<N2>(), static_cast<Scalar>(2) * m1.diagonal(N2));
|
---|
59 | m2.diagonal(N2)[0] *= 3;
|
---|
60 | VERIFY_IS_APPROX(m2.diagonal(N2)[0], static_cast<Scalar>(6) * m1.diagonal(N2)[0]);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | void test_diagonal()
|
---|
65 | {
|
---|
66 | for(int i = 0; i < g_repeat; i++) {
|
---|
67 | CALL_SUBTEST_1( diagonal(Matrix<float, 1, 1>()) );
|
---|
68 | CALL_SUBTEST_1( diagonal(Matrix<float, 4, 9>()) );
|
---|
69 | CALL_SUBTEST_1( diagonal(Matrix<float, 7, 3>()) );
|
---|
70 | CALL_SUBTEST_2( diagonal(Matrix4d()) );
|
---|
71 | CALL_SUBTEST_2( diagonal(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
|
---|
72 | CALL_SUBTEST_2( diagonal(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
|
---|
73 | CALL_SUBTEST_2( diagonal(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
|
---|
74 | CALL_SUBTEST_1( diagonal(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
|
---|
75 | CALL_SUBTEST_1( diagonal(Matrix<float,Dynamic,4>(3, 4)) );
|
---|
76 | }
|
---|
77 | }
|
---|