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
adjoint(const MatrixType & m)12 template<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 mzero = MatrixType::Zero(rows, cols),
33 identity = SquareMatrixType::Identity(rows, rows),
34 square = SquareMatrixType::Random(rows, rows);
35 VectorType v1 = VectorType::Random(rows),
36 v2 = VectorType::Random(rows),
37 v3 = VectorType::Random(rows),
38 vzero = VectorType::Zero(rows);
39
40 Scalar s1 = ei_random<Scalar>(),
41 s2 = ei_random<Scalar>();
42
43 // check basic compatibility of adjoint, transpose, conjugate
44 VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1);
45 VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1);
46
47 // check multiplicative behavior
48 VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
49 VERIFY_IS_APPROX((s1 * m1).adjoint(), ei_conj(s1) * m1.adjoint());
50
51 // check basic properties of dot, norm, norm2
52 typedef typename NumTraits<Scalar>::Real RealScalar;
53 VERIFY(ei_isApprox((s1 * v1 + s2 * v2).eigen2_dot(v3), s1 * v1.eigen2_dot(v3) + s2 * v2.eigen2_dot(v3), largerEps));
54 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));
55 VERIFY_IS_APPROX(ei_conj(v1.eigen2_dot(v2)), v2.eigen2_dot(v1));
56 VERIFY_IS_APPROX(ei_real(v1.eigen2_dot(v1)), v1.squaredNorm());
57 if(NumTraits<Scalar>::HasFloatingPoint)
58 VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm());
59 VERIFY_IS_MUCH_SMALLER_THAN(ei_abs(vzero.eigen2_dot(v1)), static_cast<RealScalar>(1));
60 if(NumTraits<Scalar>::HasFloatingPoint)
61 VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1));
62
63 // check compatibility of dot and adjoint
64 VERIFY(ei_isApprox(v1.eigen2_dot(square * v2), (square.adjoint() * v1).eigen2_dot(v2), largerEps));
65
66 // like in testBasicStuff, test operator() to check const-qualification
67 int r = ei_random<int>(0, rows-1),
68 c = ei_random<int>(0, cols-1);
69 VERIFY_IS_APPROX(m1.conjugate()(r,c), ei_conj(m1(r,c)));
70 VERIFY_IS_APPROX(m1.adjoint()(c,r), ei_conj(m1(r,c)));
71
72 if(NumTraits<Scalar>::HasFloatingPoint)
73 {
74 // check that Random().normalized() works: tricky as the random xpr must be evaluated by
75 // normalized() in order to produce a consistent result.
76 VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1));
77 }
78
79 // check inplace transpose
80 m3 = m1;
81 m3.transposeInPlace();
82 VERIFY_IS_APPROX(m3,m1.transpose());
83 m3.transposeInPlace();
84 VERIFY_IS_APPROX(m3,m1);
85
86 }
87
test_eigen2_adjoint()88 void test_eigen2_adjoint()
89 {
90 for(int i = 0; i < g_repeat; i++) {
91 CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
92 CALL_SUBTEST_2( adjoint(Matrix3d()) );
93 CALL_SUBTEST_3( adjoint(Matrix4f()) );
94 CALL_SUBTEST_4( adjoint(MatrixXcf(4, 4)) );
95 CALL_SUBTEST_5( adjoint(MatrixXi(8, 12)) );
96 CALL_SUBTEST_6( adjoint(MatrixXf(21, 21)) );
97 }
98 // test a large matrix only once
99 CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
100 }
101
102