diagonal.cpp
Go to the documentation of this file.
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::Scalar Scalar;
15 
16  Index rows = m.rows();
17  Index cols = m.cols();
18 
19  MatrixType m1 = MatrixType::Random(rows, cols),
20  m2 = MatrixType::Random(rows, cols);
21 
22  Scalar s1 = internal::random<Scalar>();
23 
24  //check diagonal()
25  VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal());
26  m2.diagonal() = 2 * m1.diagonal();
27  m2.diagonal()[0] *= 3;
28 
29  if (rows>2)
30  {
31  enum {
32  N1 = MatrixType::RowsAtCompileTime>2 ? 2 : 0,
33  N2 = MatrixType::RowsAtCompileTime>1 ? -1 : 0
34  };
35 
36  // check sub/super diagonal
37  if(MatrixType::SizeAtCompileTime!=Dynamic)
38  {
39  VERIFY(m1.template diagonal<N1>().RowsAtCompileTime == m1.diagonal(N1).size());
40  VERIFY(m1.template diagonal<N2>().RowsAtCompileTime == m1.diagonal(N2).size());
41  }
42 
43  m2.template diagonal<N1>() = 2 * m1.template diagonal<N1>();
44  VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
45  m2.template diagonal<N1>()[0] *= 3;
46  VERIFY_IS_APPROX(m2.template diagonal<N1>()[0], static_cast<Scalar>(6) * m1.template diagonal<N1>()[0]);
47 
48 
49  m2.template diagonal<N2>() = 2 * m1.template diagonal<N2>();
50  m2.template diagonal<N2>()[0] *= 3;
51  VERIFY_IS_APPROX(m2.template diagonal<N2>()[0], static_cast<Scalar>(6) * m1.template diagonal<N2>()[0]);
52 
53  m2.diagonal(N1) = 2 * m1.diagonal(N1);
54  VERIFY_IS_APPROX(m2.template diagonal<N1>(), static_cast<Scalar>(2) * m1.diagonal(N1));
55  m2.diagonal(N1)[0] *= 3;
56  VERIFY_IS_APPROX(m2.diagonal(N1)[0], static_cast<Scalar>(6) * m1.diagonal(N1)[0]);
57 
58  m2.diagonal(N2) = 2 * m1.diagonal(N2);
59  VERIFY_IS_APPROX(m2.template diagonal<N2>(), static_cast<Scalar>(2) * m1.diagonal(N2));
60  m2.diagonal(N2)[0] *= 3;
61  VERIFY_IS_APPROX(m2.diagonal(N2)[0], static_cast<Scalar>(6) * m1.diagonal(N2)[0]);
62 
63  m2.diagonal(N2).x() = s1;
64  VERIFY_IS_APPROX(m2.diagonal(N2).x(), s1);
65  m2.diagonal(N2).coeffRef(0) = Scalar(2)*s1;
66  VERIFY_IS_APPROX(m2.diagonal(N2).coeff(0), Scalar(2)*s1);
67  }
68 
69  VERIFY( m1.diagonal( cols).size()==0 );
70  VERIFY( m1.diagonal(-rows).size()==0 );
71 }
72 
73 template<typename MatrixType> void diagonal_assert(const MatrixType& m) {
74  Index rows = m.rows();
75  Index cols = m.cols();
76 
77  MatrixType m1 = MatrixType::Random(rows, cols);
78 
79  if (rows>=2 && cols>=2)
80  {
81  VERIFY_RAISES_ASSERT( m1 += m1.diagonal() );
82  VERIFY_RAISES_ASSERT( m1 -= m1.diagonal() );
83  VERIFY_RAISES_ASSERT( m1.array() *= m1.diagonal().array() );
84  VERIFY_RAISES_ASSERT( m1.array() /= m1.diagonal().array() );
85  }
86 
87  VERIFY_RAISES_ASSERT( m1.diagonal(cols+1) );
88  VERIFY_RAISES_ASSERT( m1.diagonal(-(rows+1)) );
89 }
90 
92 {
93  for(int i = 0; i < g_repeat; i++) {
97  CALL_SUBTEST_2( diagonal(Matrix4d()) );
98  CALL_SUBTEST_2( diagonal(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
99  CALL_SUBTEST_2( diagonal(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
100  CALL_SUBTEST_2( diagonal(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
101  CALL_SUBTEST_1( diagonal(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
103  CALL_SUBTEST_1( diagonal_assert(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
104  }
105 }
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
m1
Matrix3d m1
Definition: IOFormat.cpp:2
diagonal
void diagonal(const MatrixType &m)
Definition: diagonal.cpp:12
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
m2
MatrixType m2(n_dims)
diagonal_assert
void diagonal_assert(const MatrixType &m)
Definition: diagonal.cpp:73
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(diagonal)
Definition: diagonal.cpp:91
main.h
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:01:02