• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
diagonal(const MatrixType & m)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   Scalar s1 = internal::random<Scalar>();
24 
25   //check diagonal()
26   VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal());
27   m2.diagonal() = 2 * m1.diagonal();
28   m2.diagonal()[0] *= 3;
29 
30   if (rows>2)
31   {
32     enum {
33       N1 = MatrixType::RowsAtCompileTime>2 ?  2 : 0,
34       N2 = MatrixType::RowsAtCompileTime>1 ? -1 : 0
35     };
36 
37     // check sub/super diagonal
38     if(MatrixType::SizeAtCompileTime!=Dynamic)
39     {
40       VERIFY(m1.template diagonal<N1>().RowsAtCompileTime == m1.diagonal(N1).size());
41       VERIFY(m1.template diagonal<N2>().RowsAtCompileTime == m1.diagonal(N2).size());
42     }
43 
44     m2.template diagonal<N1>() = 2 * m1.template diagonal<N1>();
45     VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
46     m2.template diagonal<N1>()[0] *= 3;
47     VERIFY_IS_APPROX(m2.template diagonal<N1>()[0], static_cast<Scalar>(6) * m1.template diagonal<N1>()[0]);
48 
49 
50     m2.template diagonal<N2>() = 2 * m1.template diagonal<N2>();
51     m2.template diagonal<N2>()[0] *= 3;
52     VERIFY_IS_APPROX(m2.template diagonal<N2>()[0], static_cast<Scalar>(6) * m1.template diagonal<N2>()[0]);
53 
54     m2.diagonal(N1) = 2 * m1.diagonal(N1);
55     VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
56     m2.diagonal(N1)[0] *= 3;
57     VERIFY_IS_APPROX(m2.diagonal(N1)[0], static_cast<Scalar>(6) * m1.diagonal(N1)[0]);
58 
59     m2.diagonal(N2) = 2 * m1.diagonal(N2);
60     VERIFY_IS_APPROX(m2.template diagonal<N2>(), static_cast<Scalar>(2) * m1.diagonal(N2));
61     m2.diagonal(N2)[0] *= 3;
62     VERIFY_IS_APPROX(m2.diagonal(N2)[0], static_cast<Scalar>(6) * m1.diagonal(N2)[0]);
63 
64     m2.diagonal(N2).x() = s1;
65     VERIFY_IS_APPROX(m2.diagonal(N2).x(), s1);
66     m2.diagonal(N2).coeffRef(0) = Scalar(2)*s1;
67     VERIFY_IS_APPROX(m2.diagonal(N2).coeff(0), Scalar(2)*s1);
68   }
69 }
70 
diagonal_assert(const MatrixType & m)71 template<typename MatrixType> void diagonal_assert(const MatrixType& m) {
72   Index rows = m.rows();
73   Index cols = m.cols();
74 
75   MatrixType m1 = MatrixType::Random(rows, cols);
76 
77   if (rows>=2 && cols>=2)
78   {
79     VERIFY_RAISES_ASSERT( m1 += m1.diagonal() );
80     VERIFY_RAISES_ASSERT( m1 -= m1.diagonal() );
81     VERIFY_RAISES_ASSERT( m1.array() *= m1.diagonal().array() );
82     VERIFY_RAISES_ASSERT( m1.array() /= m1.diagonal().array() );
83   }
84 }
85 
test_diagonal()86 void test_diagonal()
87 {
88   for(int i = 0; i < g_repeat; i++) {
89     CALL_SUBTEST_1( diagonal(Matrix<float, 1, 1>()) );
90     CALL_SUBTEST_1( diagonal(Matrix<float, 4, 9>()) );
91     CALL_SUBTEST_1( diagonal(Matrix<float, 7, 3>()) );
92     CALL_SUBTEST_2( diagonal(Matrix4d()) );
93     CALL_SUBTEST_2( diagonal(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
94     CALL_SUBTEST_2( diagonal(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
95     CALL_SUBTEST_2( diagonal(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
96     CALL_SUBTEST_1( diagonal(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
97     CALL_SUBTEST_1( diagonal(Matrix<float,Dynamic,4>(3, 4)) );
98   }
99 
100   CALL_SUBTEST_1( diagonal_assert(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
101 }
102