blob: 98d7aabfb9efb89cfa18c07e3976c1a7a0ebfcd9 [file] [log] [blame]
Benoit Jacob2fdd0672007-11-28 15:34:40 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Benoit Jacob2fdd0672007-11-28 15:34:40 +00003//
Benoit Jacob00f89a82008-11-24 13:40:43 +00004// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
Benoit Jacob2fdd0672007-11-28 15:34:40 +00005//
Benoit Jacob3698d8c2008-02-28 15:44:45 +00006// Eigen is free software; you can redistribute it and/or
7// modify it under the terms of the GNU Lesser General Public
Gael Guennebaud8e0d5482008-03-05 13:18:19 +00008// License as published by the Free Software Foundation; either
Benoit Jacob3698d8c2008-02-28 15:44:45 +00009// version 3 of the License, or (at your option) any later version.
10//
11// Alternatively, you can redistribute it and/or
12// modify it under the terms of the GNU General Public License as
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000013// published by the Free Software Foundation; either version 2 of
Benoit Jacob3698d8c2008-02-28 15:44:45 +000014// the License, or (at your option) any later version.
Benoit Jacob2fdd0672007-11-28 15:34:40 +000015//
16// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Benoit Jacob3698d8c2008-02-28 15:44:45 +000018// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19// GNU General Public License for more details.
Benoit Jacob2fdd0672007-11-28 15:34:40 +000020//
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000021// You should have received a copy of the GNU Lesser General Public
Benoit Jacob3698d8c2008-02-28 15:44:45 +000022// License and a copy of the GNU General Public License along with
23// Eigen. If not, see <http://www.gnu.org/licenses/>.
Gael Guennebaud239ada92009-08-15 22:19:29 +020024
Benoit Jacobe2775862010-04-28 18:51:38 -040025#define EIGEN_NO_STATIC_ASSERT
26
Benoit Jacob2fdd0672007-11-28 15:34:40 +000027#include "main.h"
28
29template<typename MatrixType> void adjoint(const MatrixType& m)
30{
31 /* this test covers the following files:
32 Transpose.h Conjugate.h Dot.h
33 */
Hauke Heibelf1679c72010-06-20 17:37:56 +020034 typedef typename MatrixType::Index Index;
Benoit Jacob2fdd0672007-11-28 15:34:40 +000035 typedef typename MatrixType::Scalar Scalar;
Gael Guennebaud2120fed2008-08-23 15:14:20 +000036 typedef typename NumTraits<Scalar>::Real RealScalar;
Benoit Jacob2ee68a02008-03-12 17:17:36 +000037 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
Gael Guennebaud2120fed2008-08-23 15:14:20 +000038 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
Hauke Heibelf1679c72010-06-20 17:37:56 +020039
40 Index rows = m.rows();
41 Index cols = m.cols();
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000042
Benoit Jacob46fe7a32008-09-01 17:31:21 +000043 MatrixType m1 = MatrixType::Random(rows, cols),
44 m2 = MatrixType::Random(rows, cols),
Benoit Jacob2fdd0672007-11-28 15:34:40 +000045 m3(rows, cols),
Gael Guennebaudc10f0692008-07-21 00:34:46 +000046 mzero = MatrixType::Zero(rows, cols),
Gael Guennebaud2120fed2008-08-23 15:14:20 +000047 identity = SquareMatrixType::Identity(rows, rows),
Benoit Jacob46fe7a32008-09-01 17:31:21 +000048 square = SquareMatrixType::Random(rows, rows);
49 VectorType v1 = VectorType::Random(rows),
50 v2 = VectorType::Random(rows),
51 v3 = VectorType::Random(rows),
Gael Guennebaudc10f0692008-07-21 00:34:46 +000052 vzero = VectorType::Zero(rows);
Benoit Jacob2fdd0672007-11-28 15:34:40 +000053
Benoit Jacob47160402010-10-25 10:15:22 -040054 Scalar s1 = internal::random<Scalar>(),
55 s2 = internal::random<Scalar>();
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000056
Benoit Jacob2fdd0672007-11-28 15:34:40 +000057 // check basic compatibility of adjoint, transpose, conjugate
Benoit Jacob346c00f2007-12-03 10:23:08 +000058 VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1);
59 VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1);
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000060
Benoit Jacob2fdd0672007-11-28 15:34:40 +000061 // check multiplicative behavior
Benoit Jacob346c00f2007-12-03 10:23:08 +000062 VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
Benoit Jacob47160402010-10-25 10:15:22 -040063 VERIFY_IS_APPROX((s1 * m1).adjoint(), internal::conj(s1) * m1.adjoint());
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000064
Benoit Jacob2fdd0672007-11-28 15:34:40 +000065 // check basic properties of dot, norm, norm2
66 typedef typename NumTraits<Scalar>::Real RealScalar;
Gael Guennebaud86ca05b2011-02-18 17:39:04 +010067
Benoit Jacobd2673d82011-06-15 00:30:46 -040068 RealScalar ref = NumTraits<Scalar>::IsInteger ? RealScalar(0) : std::max((s1 * v1 + s2 * v2).norm(),v3.norm());
Gael Guennebaud86ca05b2011-02-18 17:39:04 +010069 VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), internal::conj(s1) * v1.dot(v3) + internal::conj(s2) * v2.dot(v3), ref));
70 VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), ref));
Benoit Jacob47160402010-10-25 10:15:22 -040071 VERIFY_IS_APPROX(internal::conj(v1.dot(v2)), v2.dot(v1));
Benoit Jacob3386a942011-02-07 10:54:50 -050072 VERIFY_IS_APPROX(internal::real(v1.dot(v1)), v1.squaredNorm());
Benoit Jacobd2673d82011-06-15 00:30:46 -040073 if(!NumTraits<Scalar>::IsInteger) {
Gael Guennebaud36c8a642009-01-28 22:11:56 +000074 VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm());
Benoit Jacobd2673d82011-06-15 00:30:46 -040075 // check normalized() and normalize()
76 VERIFY_IS_APPROX(v1, v1.norm() * v1.normalized());
77 // normalize() returns a reference to *this
78 v3 = v1;
79 VERIFY_IS_APPROX(v1, v1.norm() * v3.normalize());
80 VERIFY_IS_APPROX(v3, v1.normalized());
81 VERIFY_IS_APPROX(v3.norm(), RealScalar(1));
82 }
Benoit Jacob47160402010-10-25 10:15:22 -040083 VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(vzero.dot(v1)), static_cast<RealScalar>(1));
Benoit Jacobd2673d82011-06-15 00:30:46 -040084
Benoit Jacob2fdd0672007-11-28 15:34:40 +000085 // check compatibility of dot and adjoint
Gael Guennebaud86ca05b2011-02-18 17:39:04 +010086
87 ref = NumTraits<Scalar>::IsInteger ? 0 : std::max(std::max(v1.norm(),v2.norm()),std::max((square * v2).norm(),(square.adjoint() * v1).norm()));
88 VERIFY(test_isApproxWithRef(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), ref));
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000089
Benoit Jacobfc7b2b52007-12-12 17:48:20 +000090 // like in testBasicStuff, test operator() to check const-qualification
Benoit Jacob47160402010-10-25 10:15:22 -040091 Index r = internal::random<Index>(0, rows-1),
92 c = internal::random<Index>(0, cols-1);
93 VERIFY_IS_APPROX(m1.conjugate()(r,c), internal::conj(m1(r,c)));
94 VERIFY_IS_APPROX(m1.adjoint()(c,r), internal::conj(m1(r,c)));
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000095
Benoit Jacobe2775862010-04-28 18:51:38 -040096 if(!NumTraits<Scalar>::IsInteger)
Benoit Jacob13ad8872008-08-12 02:14:02 +000097 {
98 // check that Random().normalized() works: tricky as the random xpr must be evaluated by
99 // normalized() in order to produce a consistent result.
100 VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1));
101 }
Gael Guennebaudebe14aa2008-10-29 15:24:08 +0000102
103 // check inplace transpose
104 m3 = m1;
105 m3.transposeInPlace();
106 VERIFY_IS_APPROX(m3,m1.transpose());
107 m3.transposeInPlace();
108 VERIFY_IS_APPROX(m3,m1);
Benoit Jacobbf596d02009-03-31 13:55:40 +0000109
110 // check inplace adjoint
111 m3 = m1;
112 m3.adjointInPlace();
113 VERIFY_IS_APPROX(m3,m1.adjoint());
114 m3.transposeInPlace();
115 VERIFY_IS_APPROX(m3,m1.conjugate());
116
Gael Guennebaud0bfb78c2011-01-27 09:59:19 +0100117 // check mixed dot product
118 typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
119 RealVectorType rv1 = RealVectorType::Random(rows);
120 VERIFY_IS_APPROX(v1.dot(rv1.template cast<Scalar>()), v1.dot(rv1));
121 VERIFY_IS_APPROX(rv1.template cast<Scalar>().dot(v1), rv1.dot(v1));
Benoit Jacob2fdd0672007-11-28 15:34:40 +0000122}
123
Gael Guennebaud522e24f2008-05-22 12:18:55 +0000124void test_adjoint()
Benoit Jacob2fdd0672007-11-28 15:34:40 +0000125{
Gael Guennebaud7a9519a2009-07-14 23:00:53 +0200126 for(int i = 0; i < g_repeat; i++) {
Benoit Jacob2840ac72009-10-28 18:19:29 -0400127 CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
128 CALL_SUBTEST_2( adjoint(Matrix3d()) );
129 CALL_SUBTEST_3( adjoint(Matrix4f()) );
Gael Guennebaud86ca05b2011-02-18 17:39:04 +0100130 CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,50), internal::random<int>(1,50))) );
131 CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,50), internal::random<int>(1,50))) );
132 CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,50), internal::random<int>(1,50))) );
Gael Guennebaudf5d23172009-07-13 21:14:47 +0200133 }
Gael Guennebaud7a9519a2009-07-14 23:00:53 +0200134 // test a large matrix only once
Benoit Jacob2840ac72009-10-28 18:19:29 -0400135 CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
Gael Guennebaudb56bb442009-09-07 12:46:16 +0200136
Benoit Jacob2840ac72009-10-28 18:19:29 -0400137#ifdef EIGEN_TEST_PART_4
Gael Guennebaud239ada92009-08-15 22:19:29 +0200138 {
139 MatrixXcf a(10,10), b(10,10);
140 VERIFY_RAISES_ASSERT(a = a.transpose());
141 VERIFY_RAISES_ASSERT(a = a.transpose() + b);
142 VERIFY_RAISES_ASSERT(a = b + a.transpose());
143 VERIFY_RAISES_ASSERT(a = a.conjugate().transpose());
144 VERIFY_RAISES_ASSERT(a = a.adjoint());
145 VERIFY_RAISES_ASSERT(a = a.adjoint() + b);
146 VERIFY_RAISES_ASSERT(a = b + a.adjoint());
Gael Guennebaud6db67742009-12-16 11:41:16 +0100147
148 // no assertion should be triggered for these cases:
149 a.transpose() = a.transpose();
150 a.transpose() += a.transpose();
151 a.transpose() += a.transpose() + b;
152 a.transpose() = a.adjoint();
153 a.transpose() += a.adjoint();
154 a.transpose() += a.adjoint() + b;
Gael Guennebaud239ada92009-08-15 22:19:29 +0200155 }
Benoit Jacob2840ac72009-10-28 18:19:29 -0400156#endif
Benoit Jacob2fdd0672007-11-28 15:34:40 +0000157}
Benoit Jacobe05f2912007-12-02 18:32:59 +0000158