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 | 3698d8c | 2008-02-28 15:44:45 +0000 | [diff] [blame] | 6 | // Eigen is free software; you can redistribute it and/or |
| 7 | // modify it under the terms of the GNU Lesser General Public |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 8 | // License as published by the Free Software Foundation; either |
Benoit Jacob | 3698d8c | 2008-02-28 15:44:45 +0000 | [diff] [blame] | 9 | // 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 Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 13 | // published by the Free Software Foundation; either version 2 of |
Benoit Jacob | 3698d8c | 2008-02-28 15:44:45 +0000 | [diff] [blame] | 14 | // the License, or (at your option) any later version. |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 15 | // |
| 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 Jacob | 3698d8c | 2008-02-28 15:44:45 +0000 | [diff] [blame] | 18 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the |
| 19 | // GNU General Public License for more details. |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 20 | // |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 21 | // You should have received a copy of the GNU Lesser General Public |
Benoit Jacob | 3698d8c | 2008-02-28 15:44:45 +0000 | [diff] [blame] | 22 | // License and a copy of the GNU General Public License along with |
| 23 | // Eigen. If not, see <http://www.gnu.org/licenses/>. |
Gael Guennebaud | 239ada9 | 2009-08-15 22:19:29 +0200 | [diff] [blame] | 24 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 25 | #define EIGEN_NO_STATIC_ASSERT |
| 26 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 27 | #include "main.h" |
| 28 | |
| 29 | template<typename MatrixType> void adjoint(const MatrixType& m) |
| 30 | { |
| 31 | /* this test covers the following files: |
| 32 | Transpose.h Conjugate.h Dot.h |
| 33 | */ |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 34 | typedef typename MatrixType::Index Index; |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 35 | typedef typename MatrixType::Scalar Scalar; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 36 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Benoit Jacob | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame] | 37 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 38 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType; |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 39 | |
| 40 | Index rows = m.rows(); |
| 41 | Index cols = m.cols(); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 42 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 43 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 44 | m2 = MatrixType::Random(rows, cols), |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 45 | m3(rows, cols), |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 46 | mzero = MatrixType::Zero(rows, cols), |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 47 | identity = SquareMatrixType::Identity(rows, rows), |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 48 | square = SquareMatrixType::Random(rows, rows); |
| 49 | VectorType v1 = VectorType::Random(rows), |
| 50 | v2 = VectorType::Random(rows), |
| 51 | v3 = VectorType::Random(rows), |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 52 | vzero = VectorType::Zero(rows); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 53 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 54 | Scalar s1 = internal::random<Scalar>(), |
| 55 | s2 = internal::random<Scalar>(); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 56 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 57 | // check basic compatibility of adjoint, transpose, conjugate |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 58 | VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1); |
| 59 | VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 60 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 61 | // check multiplicative behavior |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 62 | VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1); |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 63 | VERIFY_IS_APPROX((s1 * m1).adjoint(), internal::conj(s1) * m1.adjoint()); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 64 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 65 | // check basic properties of dot, norm, norm2 |
| 66 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Gael Guennebaud | 86ca05b | 2011-02-18 17:39:04 +0100 | [diff] [blame] | 67 | |
Gael Guennebaud | 22bff94 | 2011-07-21 11:19:36 +0200 | [diff] [blame^] | 68 | 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] | 69 | 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 Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 71 | VERIFY_IS_APPROX(internal::conj(v1.dot(v2)), v2.dot(v1)); |
Benoit Jacob | 3386a94 | 2011-02-07 10:54:50 -0500 | [diff] [blame] | 72 | VERIFY_IS_APPROX(internal::real(v1.dot(v1)), v1.squaredNorm()); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 73 | if(!NumTraits<Scalar>::IsInteger) { |
Gael Guennebaud | 36c8a64 | 2009-01-28 22:11:56 +0000 | [diff] [blame] | 74 | VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm()); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 75 | // check normalized() and normalize() |
| 76 | VERIFY_IS_APPROX(v1, v1.norm() * v1.normalized()); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 77 | v3 = v1; |
Benoit Jacob | a871f3c | 2011-06-15 10:00:43 -0400 | [diff] [blame] | 78 | v3.normalize(); |
| 79 | VERIFY_IS_APPROX(v1, v1.norm() * v3); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 80 | VERIFY_IS_APPROX(v3, v1.normalized()); |
| 81 | VERIFY_IS_APPROX(v3.norm(), RealScalar(1)); |
| 82 | } |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 83 | VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(vzero.dot(v1)), static_cast<RealScalar>(1)); |
Benoit Jacob | d2673d8 | 2011-06-15 00:30:46 -0400 | [diff] [blame] | 84 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 85 | // check compatibility of dot and adjoint |
Gael Guennebaud | 86ca05b | 2011-02-18 17:39:04 +0100 | [diff] [blame] | 86 | |
Gael Guennebaud | 22bff94 | 2011-07-21 11:19:36 +0200 | [diff] [blame^] | 87 | 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] | 88 | 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] | 89 | |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame] | 90 | // like in testBasicStuff, test operator() to check const-qualification |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 91 | 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 Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 95 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 96 | if(!NumTraits<Scalar>::IsInteger) |
Benoit Jacob | 13ad887 | 2008-08-12 02:14:02 +0000 | [diff] [blame] | 97 | { |
| 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 Guennebaud | ebe14aa | 2008-10-29 15:24:08 +0000 | [diff] [blame] | 102 | |
| 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 Jacob | bf596d0 | 2009-03-31 13:55:40 +0000 | [diff] [blame] | 109 | |
| 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 Guennebaud | 0bfb78c | 2011-01-27 09:59:19 +0100 | [diff] [blame] | 117 | // 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 Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Gael Guennebaud | 522e24f | 2008-05-22 12:18:55 +0000 | [diff] [blame] | 124 | void test_adjoint() |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 125 | { |
Gael Guennebaud | 7a9519a | 2009-07-14 23:00:53 +0200 | [diff] [blame] | 126 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 127 | CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) ); |
| 128 | CALL_SUBTEST_2( adjoint(Matrix3d()) ); |
| 129 | CALL_SUBTEST_3( adjoint(Matrix4f()) ); |
Gael Guennebaud | a8f66fe | 2011-07-12 14:41:00 +0200 | [diff] [blame] | 130 | CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) ); |
| 131 | CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 132 | 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] | 133 | } |
Gael Guennebaud | a8f66fe | 2011-07-12 14:41:00 +0200 | [diff] [blame] | 134 | // test a large static matrix only once |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 135 | CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) ); |
Gael Guennebaud | b56bb44 | 2009-09-07 12:46:16 +0200 | [diff] [blame] | 136 | |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 137 | #ifdef EIGEN_TEST_PART_4 |
Gael Guennebaud | 239ada9 | 2009-08-15 22:19:29 +0200 | [diff] [blame] | 138 | { |
| 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 Guennebaud | 6db6774 | 2009-12-16 11:41:16 +0100 | [diff] [blame] | 147 | |
| 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 Guennebaud | 239ada9 | 2009-08-15 22:19:29 +0200 | [diff] [blame] | 155 | } |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 156 | #endif |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 157 | } |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 158 | |