1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
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#define EIGEN_NO_STATIC_ASSERT
11
12#include "main.h"
13
14template<typename MatrixType> void adjoint(const MatrixType& m)
15{
16  /* this test covers the following files:
17     Transpose.h Conjugate.h Dot.h
18  */
19  typedef typename MatrixType::Index Index;
20  typedef typename MatrixType::Scalar Scalar;
21  typedef typename NumTraits<Scalar>::Real RealScalar;
22  typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
23  typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
24
25  Index rows = m.rows();
26  Index cols = m.cols();
27
28  MatrixType m1 = MatrixType::Random(rows, cols),
29             m2 = MatrixType::Random(rows, cols),
30             m3(rows, cols),
31             square = SquareMatrixType::Random(rows, rows);
32  VectorType v1 = VectorType::Random(rows),
33             v2 = VectorType::Random(rows),
34             v3 = VectorType::Random(rows),
35             vzero = VectorType::Zero(rows);
36
37  Scalar s1 = internal::random<Scalar>(),
38         s2 = internal::random<Scalar>();
39
40  // check basic compatibility of adjoint, transpose, conjugate
41  VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(),    m1);
42  VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(),    m1);
43
44  // check multiplicative behavior
45  VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(),           m2.adjoint() * m1);
46  VERIFY_IS_APPROX((s1 * m1).adjoint(),                     internal::conj(s1) * m1.adjoint());
47
48  // check basic properties of dot, norm, norm2
49  typedef typename NumTraits<Scalar>::Real RealScalar;
50
51  RealScalar ref = NumTraits<Scalar>::IsInteger ? RealScalar(0) : (std::max)((s1 * v1 + s2 * v2).norm(),v3.norm());
52  VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3),     internal::conj(s1) * v1.dot(v3) + internal::conj(s2) * v2.dot(v3), ref));
53  VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2),       s1*v3.dot(v1)+s2*v3.dot(v2), ref));
54  VERIFY_IS_APPROX(internal::conj(v1.dot(v2)),               v2.dot(v1));
55  VERIFY_IS_APPROX(internal::real(v1.dot(v1)),                v1.squaredNorm());
56  if(!NumTraits<Scalar>::IsInteger) {
57    VERIFY_IS_APPROX(v1.squaredNorm(),                v1.norm() * v1.norm());
58    // check normalized() and normalize()
59    VERIFY_IS_APPROX(v1, v1.norm() * v1.normalized());
60    v3 = v1;
61    v3.normalize();
62    VERIFY_IS_APPROX(v1, v1.norm() * v3);
63    VERIFY_IS_APPROX(v3, v1.normalized());
64    VERIFY_IS_APPROX(v3.norm(), RealScalar(1));
65  }
66  VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(vzero.dot(v1)),  static_cast<RealScalar>(1));
67
68  // check compatibility of dot and adjoint
69
70  ref = NumTraits<Scalar>::IsInteger ? 0 : (std::max)((std::max)(v1.norm(),v2.norm()),(std::max)((square * v2).norm(),(square.adjoint() * v1).norm()));
71  VERIFY(test_isApproxWithRef(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), ref));
72
73  // like in testBasicStuff, test operator() to check const-qualification
74  Index r = internal::random<Index>(0, rows-1),
75      c = internal::random<Index>(0, cols-1);
76  VERIFY_IS_APPROX(m1.conjugate()(r,c), internal::conj(m1(r,c)));
77  VERIFY_IS_APPROX(m1.adjoint()(c,r), internal::conj(m1(r,c)));
78
79  if(!NumTraits<Scalar>::IsInteger)
80  {
81    // check that Random().normalized() works: tricky as the random xpr must be evaluated by
82    // normalized() in order to produce a consistent result.
83    VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1));
84  }
85
86  // check inplace transpose
87  m3 = m1;
88  m3.transposeInPlace();
89  VERIFY_IS_APPROX(m3,m1.transpose());
90  m3.transposeInPlace();
91  VERIFY_IS_APPROX(m3,m1);
92
93  // check inplace adjoint
94  m3 = m1;
95  m3.adjointInPlace();
96  VERIFY_IS_APPROX(m3,m1.adjoint());
97  m3.transposeInPlace();
98  VERIFY_IS_APPROX(m3,m1.conjugate());
99
100  // check mixed dot product
101  typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
102  RealVectorType rv1 = RealVectorType::Random(rows);
103  VERIFY_IS_APPROX(v1.dot(rv1.template cast<Scalar>()), v1.dot(rv1));
104  VERIFY_IS_APPROX(rv1.template cast<Scalar>().dot(v1), rv1.dot(v1));
105}
106
107void test_adjoint()
108{
109  for(int i = 0; i < g_repeat; i++) {
110    CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
111    CALL_SUBTEST_2( adjoint(Matrix3d()) );
112    CALL_SUBTEST_3( adjoint(Matrix4f()) );
113    CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
114    CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
115    CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
116  }
117  // test a large static matrix only once
118  CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
119
120#ifdef EIGEN_TEST_PART_4
121  {
122    MatrixXcf a(10,10), b(10,10);
123    VERIFY_RAISES_ASSERT(a = a.transpose());
124    VERIFY_RAISES_ASSERT(a = a.transpose() + b);
125    VERIFY_RAISES_ASSERT(a = b + a.transpose());
126    VERIFY_RAISES_ASSERT(a = a.conjugate().transpose());
127    VERIFY_RAISES_ASSERT(a = a.adjoint());
128    VERIFY_RAISES_ASSERT(a = a.adjoint() + b);
129    VERIFY_RAISES_ASSERT(a = b + a.adjoint());
130
131    // no assertion should be triggered for these cases:
132    a.transpose() = a.transpose();
133    a.transpose() += a.transpose();
134    a.transpose() += a.transpose() + b;
135    a.transpose() = a.adjoint();
136    a.transpose() += a.adjoint();
137    a.transpose() += a.adjoint() + b;
138  }
139#endif
140}
141
142