Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. Eigen itself is part of the KDE project. |
| 3 | // |
Benoit Jacob | 8ba3055 | 2008-01-07 09:34:21 +0000 | [diff] [blame] | 4 | // Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr> |
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/>. |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 24 | |
| 25 | #include "main.h" |
| 26 | |
| 27 | template<typename MatrixType> void adjoint(const MatrixType& m) |
| 28 | { |
| 29 | /* this test covers the following files: |
| 30 | Transpose.h Conjugate.h Dot.h |
| 31 | */ |
| 32 | |
| 33 | typedef typename MatrixType::Scalar Scalar; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 34 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Benoit Jacob | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame] | 35 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 36 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType; |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 37 | int rows = m.rows(); |
| 38 | int cols = m.cols(); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 39 | |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 40 | RealScalar largerEps = test_precision<RealScalar>(); |
| 41 | if (ei_is_same_type<RealScalar,float>::ret) |
| 42 | largerEps = 1e-3f; |
| 43 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 44 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 45 | m2 = MatrixType::Random(rows, cols), |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 46 | m3(rows, cols), |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 47 | mzero = MatrixType::Zero(rows, cols), |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 48 | identity = SquareMatrixType::Identity(rows, rows), |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 49 | square = SquareMatrixType::Random(rows, rows); |
| 50 | VectorType v1 = VectorType::Random(rows), |
| 51 | v2 = VectorType::Random(rows), |
| 52 | v3 = VectorType::Random(rows), |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 53 | vzero = VectorType::Zero(rows); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 54 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 55 | Scalar s1 = ei_random<Scalar>(), |
| 56 | s2 = ei_random<Scalar>(); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 57 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 58 | // check basic compatibility of adjoint, transpose, conjugate |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 59 | VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1); |
| 60 | VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 61 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 62 | // check multiplicative behavior |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 63 | VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1); |
Benoit Jacob | 6907886 | 2008-02-28 12:38:12 +0000 | [diff] [blame] | 64 | VERIFY_IS_APPROX((s1 * m1).adjoint(), ei_conj(s1) * m1.adjoint()); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 65 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 66 | // check basic properties of dot, norm, norm2 |
| 67 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 68 | VERIFY(ei_isApprox((s1 * v1 + s2 * v2).dot(v3), s1 * v1.dot(v3) + s2 * v2.dot(v3), largerEps)); |
| 69 | VERIFY(ei_isApprox(v3.dot(s1 * v1 + s2 * v2), ei_conj(s1)*v3.dot(v1)+ei_conj(s2)*v3.dot(v2), largerEps)); |
| 70 | VERIFY_IS_APPROX(ei_conj(v1.dot(v2)), v2.dot(v1)); |
Benoit Jacob | 3d90c13 | 2008-11-03 19:14:17 +0000 | [diff] [blame] | 71 | VERIFY_IS_APPROX(ei_abs(v1.dot(v1)), v1.squaredNorm()); |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 72 | if(NumTraits<Scalar>::HasFloatingPoint) |
Benoit Jacob | 3d90c13 | 2008-11-03 19:14:17 +0000 | [diff] [blame] | 73 | VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm()); |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 74 | VERIFY_IS_MUCH_SMALLER_THAN(ei_abs(vzero.dot(v1)), static_cast<RealScalar>(1)); |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 75 | if(NumTraits<Scalar>::HasFloatingPoint) |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 76 | VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1)); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 77 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 78 | // check compatibility of dot and adjoint |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 79 | VERIFY(ei_isApprox(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), largerEps)); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 80 | |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame] | 81 | // like in testBasicStuff, test operator() to check const-qualification |
Benoit Jacob | 6907886 | 2008-02-28 12:38:12 +0000 | [diff] [blame] | 82 | int r = ei_random<int>(0, rows-1), |
| 83 | c = ei_random<int>(0, cols-1); |
| 84 | VERIFY_IS_APPROX(m1.conjugate()(r,c), ei_conj(m1(r,c))); |
| 85 | VERIFY_IS_APPROX(m1.adjoint()(c,r), ei_conj(m1(r,c))); |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 86 | |
Benoit Jacob | 13ad887 | 2008-08-12 02:14:02 +0000 | [diff] [blame] | 87 | if(NumTraits<Scalar>::HasFloatingPoint) |
| 88 | { |
| 89 | // check that Random().normalized() works: tricky as the random xpr must be evaluated by |
| 90 | // normalized() in order to produce a consistent result. |
| 91 | VERIFY_IS_APPROX(VectorType::Random(rows).normalized().norm(), RealScalar(1)); |
| 92 | } |
Gael Guennebaud | ebe14aa | 2008-10-29 15:24:08 +0000 | [diff] [blame] | 93 | |
| 94 | // check inplace transpose |
| 95 | m3 = m1; |
| 96 | m3.transposeInPlace(); |
| 97 | VERIFY_IS_APPROX(m3,m1.transpose()); |
| 98 | m3.transposeInPlace(); |
| 99 | VERIFY_IS_APPROX(m3,m1); |
| 100 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Gael Guennebaud | 522e24f | 2008-05-22 12:18:55 +0000 | [diff] [blame] | 103 | void test_adjoint() |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 104 | { |
Gael Guennebaud | 522e24f | 2008-05-22 12:18:55 +0000 | [diff] [blame] | 105 | for(int i = 0; i < g_repeat; i++) { |
| 106 | CALL_SUBTEST( adjoint(Matrix<float, 1, 1>()) ); |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 107 | CALL_SUBTEST( adjoint(Matrix3d()) ); |
| 108 | CALL_SUBTEST( adjoint(Matrix4f()) ); |
| 109 | CALL_SUBTEST( adjoint(MatrixXcf(4, 4)) ); |
Gael Guennebaud | 522e24f | 2008-05-22 12:18:55 +0000 | [diff] [blame] | 110 | CALL_SUBTEST( adjoint(MatrixXi(8, 12)) ); |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 111 | CALL_SUBTEST( adjoint(MatrixXf(21, 21)) ); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 112 | } |
Gael Guennebaud | 8e0d548 | 2008-03-05 13:18:19 +0000 | [diff] [blame] | 113 | // test a large matrix only once |
Gael Guennebaud | 522e24f | 2008-05-22 12:18:55 +0000 | [diff] [blame] | 114 | CALL_SUBTEST( adjoint(Matrix<float, 100, 100>()) ); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 115 | } |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 116 | |