Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 3 | // |
Benoit Jacob | 00f89a8 | 2008-11-24 13:40:43 +0000 | [diff] [blame] | 4 | // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 5 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 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/. |
Gael Guennebaud | 239ada9 | 2009-08-15 22:19:29 +0200 | [diff] [blame] | 9 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 10 | #define EIGEN_NO_STATIC_ASSERT |
| 11 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 12 | #include "main.h" |
| 13 | |
| 14 | template<typename MatrixType> void adjoint(const MatrixType& m) |
| 15 | { |
| 16 | /* this test covers the following files: |
| 17 | Transpose.h Conjugate.h Dot.h |
| 18 | */ |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame^] | 19 | using std::abs; |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 20 | typedef typename MatrixType::Index Index; |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 21 | typedef typename MatrixType::Scalar Scalar; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 22 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Benoit Jacob | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame] | 23 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 24 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType; |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 25 | |
| 26 | Index rows = m.rows(); |
| 27 | Index cols = m.cols(); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 28 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 29 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 30 | m2 = MatrixType::Random(rows, cols), |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 31 | m3(rows, cols), |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 32 | square = SquareMatrixType::Random(rows, rows); |
| 33 | VectorType v1 = VectorType::Random(rows), |
| 34 | v2 = VectorType::Random(rows), |
| 35 | v3 = VectorType::Random(rows), |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 36 | vzero = VectorType::Zero(rows); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 37 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 38 | Scalar s1 = internal::random<Scalar>(), |
| 39 | s2 = internal::random<Scalar>(); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 40 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 41 | // check basic compatibility of adjoint, transpose, conjugate |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 42 | VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1); |
| 43 | VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 44 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 45 | // check multiplicative behavior |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 46 | VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1); |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 47 | VERIFY_IS_APPROX((s1 * m1).adjoint(), internal::conj(s1) * m1.adjoint()); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 48 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 49 | // check basic properties of dot, norm, norm2 |
| 50 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Gael Guennebaud | 86ca05b | 2011-02-18 17:39:04 +0100 | [diff] [blame] | 51 | |
Gael Guennebaud | 22bff94 | 2011-07-21 11:19:36 +0200 | [diff] [blame] | 52 | RealScalar ref = NumTraits<Scalar>::IsInteger ? RealScalar(0) : (std::max)((s1 * v1 + s2 * v2).norm(),v3.norm()); |
Gael Guennebaud | 86ca05b | 2011-02-18 17:39:04 +0100 | [diff] [blame] | 53 | VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), internal::conj(s1) * v1.dot(v3) + internal::conj(s2) * v2.dot(v3), ref)); |
| 54 | VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), ref)); |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 55 | VERIFY_IS_APPROX(internal::conj(v1.dot(v2)), v2.dot(v1)); |
Benoit Jacob | 3386a94 | 2011-02-07 10:54:50 -0500 | [diff] [blame] | 56 | VERIFY_IS_APPROX(internal::real(v1.dot(v1)), v1.squaredNorm()); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 57 | if(!NumTraits<Scalar>::IsInteger) { |
Gael Guennebaud | 36c8a64 | 2009-01-28 22:11:56 +0000 | [diff] [blame] | 58 | VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm()); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 59 | // check normalized() and normalize() |
| 60 | VERIFY_IS_APPROX(v1, v1.norm() * v1.normalized()); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 61 | v3 = v1; |
Benoit Jacob | a871f3c | 2011-06-15 10:00:43 -0400 | [diff] [blame] | 62 | v3.normalize(); |
| 63 | VERIFY_IS_APPROX(v1, v1.norm() * v3); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 64 | VERIFY_IS_APPROX(v3, v1.normalized()); |
| 65 | VERIFY_IS_APPROX(v3.norm(), RealScalar(1)); |
| 66 | } |
Gael Guennebaud | a76fbbf | 2012-11-06 15:25:50 +0100 | [diff] [blame^] | 67 | VERIFY_IS_MUCH_SMALLER_THAN(abs(vzero.dot(v1)), static_cast<RealScalar>(1)); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 68 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 69 | // check compatibility of dot and adjoint |
Gael Guennebaud | 86ca05b | 2011-02-18 17:39:04 +0100 | [diff] [blame] | 70 | |
Gael Guennebaud | 22bff94 | 2011-07-21 11:19:36 +0200 | [diff] [blame] | 71 | ref = NumTraits<Scalar>::IsInteger ? 0 : (std::max)((std::max)(v1.norm(),v2.norm()),(std::max)((square * v2).norm(),(square.adjoint() * v1).norm())); |
Gael Guennebaud | 86ca05b | 2011-02-18 17:39:04 +0100 | [diff] [blame] | 72 | VERIFY(test_isApproxWithRef(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), ref)); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 73 | |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame] | 74 | // like in testBasicStuff, test operator() to check const-qualification |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 75 | Index r = internal::random<Index>(0, rows-1), |
| 76 | c = internal::random<Index>(0, cols-1); |
| 77 | VERIFY_IS_APPROX(m1.conjugate()(r,c), internal::conj(m1(r,c))); |
| 78 | VERIFY_IS_APPROX(m1.adjoint()(c,r), internal::conj(m1(r,c))); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 79 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 80 | if(!NumTraits<Scalar>::IsInteger) |
Benoit Jacob | 13ad887 | 2008-08-12 02:14:02 +0000 | [diff] [blame] | 81 | { |
| 82 | // check that Random().normalized() works: tricky as the random xpr must be evaluated by |
| 83 | // normalized() in order to produce a consistent result. |
| 84 | VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1)); |
| 85 | } |
Gael Guennebaud | ebe14aa | 2008-10-29 15:24:08 +0000 | [diff] [blame] | 86 | |
| 87 | // check inplace transpose |
| 88 | m3 = m1; |
| 89 | m3.transposeInPlace(); |
| 90 | VERIFY_IS_APPROX(m3,m1.transpose()); |
| 91 | m3.transposeInPlace(); |
| 92 | VERIFY_IS_APPROX(m3,m1); |
Benoit Jacob | bf596d0 | 2009-03-31 13:55:40 +0000 | [diff] [blame] | 93 | |
| 94 | // check inplace adjoint |
| 95 | m3 = m1; |
| 96 | m3.adjointInPlace(); |
| 97 | VERIFY_IS_APPROX(m3,m1.adjoint()); |
| 98 | m3.transposeInPlace(); |
| 99 | VERIFY_IS_APPROX(m3,m1.conjugate()); |
| 100 | |
Gael Guennebaud | 0bfb78c | 2011-01-27 09:59:19 +0100 | [diff] [blame] | 101 | // check mixed dot product |
| 102 | typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType; |
| 103 | RealVectorType rv1 = RealVectorType::Random(rows); |
| 104 | VERIFY_IS_APPROX(v1.dot(rv1.template cast<Scalar>()), v1.dot(rv1)); |
| 105 | VERIFY_IS_APPROX(rv1.template cast<Scalar>().dot(v1), rv1.dot(v1)); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Gael Guennebaud | 522e24f | 2008-05-22 12:18:55 +0000 | [diff] [blame] | 108 | void test_adjoint() |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 109 | { |
Gael Guennebaud | 7a9519a | 2009-07-14 23:00:53 +0200 | [diff] [blame] | 110 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 111 | CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) ); |
| 112 | CALL_SUBTEST_2( adjoint(Matrix3d()) ); |
| 113 | CALL_SUBTEST_3( adjoint(Matrix4f()) ); |
Gael Guennebaud | a8f66fe | 2011-07-12 14:41:00 +0200 | [diff] [blame] | 114 | CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) ); |
| 115 | CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 116 | CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
Gael Guennebaud | f5d2317 | 2009-07-13 21:14:47 +0200 | [diff] [blame] | 117 | } |
Gael Guennebaud | a8f66fe | 2011-07-12 14:41:00 +0200 | [diff] [blame] | 118 | // test a large static matrix only once |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 119 | CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) ); |
Gael Guennebaud | b56bb44 | 2009-09-07 12:46:16 +0200 | [diff] [blame] | 120 | |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 121 | #ifdef EIGEN_TEST_PART_4 |
Gael Guennebaud | 239ada9 | 2009-08-15 22:19:29 +0200 | [diff] [blame] | 122 | { |
| 123 | MatrixXcf a(10,10), b(10,10); |
| 124 | VERIFY_RAISES_ASSERT(a = a.transpose()); |
| 125 | VERIFY_RAISES_ASSERT(a = a.transpose() + b); |
| 126 | VERIFY_RAISES_ASSERT(a = b + a.transpose()); |
| 127 | VERIFY_RAISES_ASSERT(a = a.conjugate().transpose()); |
| 128 | VERIFY_RAISES_ASSERT(a = a.adjoint()); |
| 129 | VERIFY_RAISES_ASSERT(a = a.adjoint() + b); |
| 130 | VERIFY_RAISES_ASSERT(a = b + a.adjoint()); |
Gael Guennebaud | 6db6774 | 2009-12-16 11:41:16 +0100 | [diff] [blame] | 131 | |
| 132 | // no assertion should be triggered for these cases: |
| 133 | a.transpose() = a.transpose(); |
| 134 | a.transpose() += a.transpose(); |
| 135 | a.transpose() += a.transpose() + b; |
| 136 | a.transpose() = a.adjoint(); |
| 137 | a.transpose() += a.adjoint(); |
| 138 | a.transpose() += a.adjoint() + b; |
Gael Guennebaud | 239ada9 | 2009-08-15 22:19:29 +0200 | [diff] [blame] | 139 | } |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 140 | #endif |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 141 | } |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 142 | |