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