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 }
Matrix3f m
void diagonal_assert(const MatrixType &m)
Definition: diagonal.cpp:73
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
MatrixType m2(n_dims)
void diagonal(const MatrixType &m)
Definition: diagonal.cpp:12
MatrixXf MatrixType
#define VERIFY_IS_APPROX(a, b)
#define CALL_SUBTEST_1(FUNC)
Matrix3d m1
Definition: IOFormat.cpp:2
static int g_repeat
Definition: main.h:169
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
#define VERIFY(a)
Definition: main.h:380
#define EIGEN_TEST_MAX_SIZE
EIGEN_DECLARE_TEST(diagonal)
Definition: diagonal.cpp:91
#define CALL_SUBTEST_2(FUNC)
const int Dynamic
Definition: Constants.h:22
The matrix class, also used for vectors and row-vectors.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:10