1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra. Eigen itself is part of the KDE project.
3//
4// Copyright (C) 2006-2008 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
12template<typename MatrixType> void adjoint(const MatrixType& m)
13{
14  /* this test covers the following files:
15     Transpose.h Conjugate.h Dot.h
16  */
17
18  typedef typename MatrixType::Scalar Scalar;
19  typedef typename NumTraits<Scalar>::Real RealScalar;
20  typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
21  typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
22  int rows = m.rows();
23  int cols = m.cols();
24
25  RealScalar largerEps = test_precision<RealScalar>();
26  if (ei_is_same_type<RealScalar,float>::ret)
27    largerEps = RealScalar(1e-3f);
28
29  MatrixType m1 = MatrixType::Random(rows, cols),
30             m2 = MatrixType::Random(rows, cols),
31             m3(rows, cols),
32             square = SquareMatrixType::Random(rows, rows);
33  VectorType v1 = VectorType::Random(rows),
34             v2 = VectorType::Random(rows),
35             v3 = VectorType::Random(rows),
36             vzero = VectorType::Zero(rows);
37
38  Scalar s1 = ei_random<Scalar>(),
39         s2 = ei_random<Scalar>();
40
41  // check basic compatibility of adjoint, transpose, conjugate
42  VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(),    m1);
43  VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(),    m1);
44
45  // check multiplicative behavior
46  VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(),           m2.adjoint() * m1);
47  VERIFY_IS_APPROX((s1 * m1).adjoint(),                     ei_conj(s1) * m1.adjoint());
48
49  // check basic properties of dot, norm, norm2
50  typedef typename NumTraits<Scalar>::Real RealScalar;
51  VERIFY(ei_isApprox((s1 * v1 + s2 * v2).eigen2_dot(v3),   s1 * v1.eigen2_dot(v3) + s2 * v2.eigen2_dot(v3), largerEps));
52  VERIFY(ei_isApprox(v3.eigen2_dot(s1 * v1 + s2 * v2),     ei_conj(s1)*v3.eigen2_dot(v1)+ei_conj(s2)*v3.eigen2_dot(v2), largerEps));
53  VERIFY_IS_APPROX(ei_conj(v1.eigen2_dot(v2)),               v2.eigen2_dot(v1));
54  VERIFY_IS_APPROX(ei_real(v1.eigen2_dot(v1)),               v1.squaredNorm());
55  if(NumTraits<Scalar>::HasFloatingPoint)
56    VERIFY_IS_APPROX(v1.squaredNorm(),                      v1.norm() * v1.norm());
57  VERIFY_IS_MUCH_SMALLER_THAN(ei_abs(vzero.eigen2_dot(v1)),  static_cast<RealScalar>(1));
58  if(NumTraits<Scalar>::HasFloatingPoint)
59    VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(),         static_cast<RealScalar>(1));
60
61  // check compatibility of dot and adjoint
62  VERIFY(ei_isApprox(v1.eigen2_dot(square * v2), (square.adjoint() * v1).eigen2_dot(v2), largerEps));
63
64  // like in testBasicStuff, test operator() to check const-qualification
65  int r = ei_random<int>(0, rows-1),
66      c = ei_random<int>(0, cols-1);
67  VERIFY_IS_APPROX(m1.conjugate()(r,c), ei_conj(m1(r,c)));
68  VERIFY_IS_APPROX(m1.adjoint()(c,r), ei_conj(m1(r,c)));
69
70  if(NumTraits<Scalar>::HasFloatingPoint)
71  {
72    // check that Random().normalized() works: tricky as the random xpr must be evaluated by
73    // normalized() in order to produce a consistent result.
74    VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1));
75  }
76
77  // check inplace transpose
78  m3 = m1;
79  m3.transposeInPlace();
80  VERIFY_IS_APPROX(m3,m1.transpose());
81  m3.transposeInPlace();
82  VERIFY_IS_APPROX(m3,m1);
83
84}
85
86void test_eigen2_adjoint()
87{
88  for(int i = 0; i < g_repeat; i++) {
89    CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
90    CALL_SUBTEST_2( adjoint(Matrix3d()) );
91    CALL_SUBTEST_3( adjoint(Matrix4f()) );
92    CALL_SUBTEST_4( adjoint(MatrixXcf(4, 4)) );
93    CALL_SUBTEST_5( adjoint(MatrixXi(8, 12)) );
94    CALL_SUBTEST_6( adjoint(MatrixXf(21, 21)) );
95  }
96  // test a large matrix only once
97  CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
98}
99
100