1// This file is triangularView of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
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// This file tests the basic selfadjointView API,
13// the related products and decompositions are tested in specific files.
14
15template<typename MatrixType> void selfadjoint(const MatrixType& m)
16{
17  typedef typename MatrixType::Index Index;
18  typedef typename MatrixType::Scalar Scalar;
19  typedef typename NumTraits<Scalar>::Real RealScalar;
20
21  Index rows = m.rows();
22  Index cols = m.cols();
23
24  MatrixType m1 = MatrixType::Random(rows, cols),
25             m3(rows, cols);
26
27  m1.diagonal() = m1.diagonal().real().template cast<Scalar>();
28
29  // check selfadjoint to dense
30  m3 = m1.template selfadjointView<Upper>();
31  VERIFY_IS_APPROX(MatrixType(m3.template triangularView<Upper>()), MatrixType(m1.template triangularView<Upper>()));
32  VERIFY_IS_APPROX(m3, m3.adjoint());
33
34
35  m3 = m1.template selfadjointView<Lower>();
36  VERIFY_IS_APPROX(MatrixType(m3.template triangularView<Lower>()), MatrixType(m1.template triangularView<Lower>()));
37  VERIFY_IS_APPROX(m3, m3.adjoint());
38}
39
40void bug_159()
41{
42  Matrix3d m = Matrix3d::Random().selfadjointView<Lower>();
43  EIGEN_UNUSED_VARIABLE(m)
44}
45
46void test_selfadjoint()
47{
48  for(int i = 0; i < g_repeat ; i++)
49  {
50    int s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE); EIGEN_UNUSED_VARIABLE(s);
51
52    CALL_SUBTEST_1( selfadjoint(Matrix<float, 1, 1>()) );
53    CALL_SUBTEST_2( selfadjoint(Matrix<float, 2, 2>()) );
54    CALL_SUBTEST_3( selfadjoint(Matrix3cf()) );
55    CALL_SUBTEST_4( selfadjoint(MatrixXcd(s,s)) );
56    CALL_SUBTEST_5( selfadjoint(Matrix<float,Dynamic,Dynamic,RowMajor>(s, s)) );
57
58    EIGEN_UNUSED_VARIABLE(s)
59  }
60
61  CALL_SUBTEST_1( bug_159() );
62}
63