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 |
| 8 | // License as published by the Free Software Foundation; either |
| 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 |
| 13 | // published by the Free Software Foundation; either version 2 of |
| 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 | // |
Benoit Jacob | 3698d8c | 2008-02-28 15:44:45 +0000 | [diff] [blame^] | 21 | // You should have received a copy of the GNU Lesser General Public |
| 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 | |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 27 | namespace Eigen { |
| 28 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 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 | */ |
| 34 | |
| 35 | typedef typename MatrixType::Scalar Scalar; |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame] | 36 | typedef Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> VectorType; |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 37 | int rows = m.rows(); |
| 38 | int cols = m.cols(); |
| 39 | |
| 40 | MatrixType m1 = MatrixType::random(rows, cols), |
| 41 | m2 = MatrixType::random(rows, cols), |
| 42 | m3(rows, cols), |
| 43 | mzero = MatrixType::zero(rows, cols), |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame] | 44 | identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime> |
Benoit Jacob | bcf7b29 | 2008-01-11 15:56:21 +0000 | [diff] [blame] | 45 | ::identity(rows, rows), |
Benoit Jacob | 84934ea | 2008-01-06 16:35:21 +0000 | [diff] [blame] | 46 | square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime> |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 47 | ::random(rows, rows); |
| 48 | VectorType v1 = VectorType::random(rows), |
| 49 | v2 = VectorType::random(rows), |
| 50 | v3 = VectorType::random(rows), |
| 51 | vzero = VectorType::zero(rows); |
| 52 | |
Benoit Jacob | 6907886 | 2008-02-28 12:38:12 +0000 | [diff] [blame] | 53 | Scalar s1 = ei_random<Scalar>(), |
| 54 | s2 = ei_random<Scalar>(); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 55 | |
| 56 | // check involutivity of adjoint, transpose, conjugate |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 57 | VERIFY_IS_APPROX(m1.transpose().transpose(), m1); |
| 58 | VERIFY_IS_APPROX(m1.conjugate().conjugate(), m1); |
| 59 | VERIFY_IS_APPROX(m1.adjoint().adjoint(), m1); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 60 | |
| 61 | // check basic compatibility of adjoint, transpose, conjugate |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 62 | VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1); |
| 63 | VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1); |
| 64 | if(!NumTraits<Scalar>::IsComplex) |
| 65 | VERIFY_IS_APPROX(m1.adjoint().transpose(), m1); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 66 | |
| 67 | // check multiplicative behavior |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 68 | VERIFY_IS_APPROX((m1.transpose() * m2).transpose(), m2.transpose() * m1); |
| 69 | VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1); |
| 70 | VERIFY_IS_APPROX((m1.transpose() * m2).conjugate(), m1.adjoint() * m2.conjugate()); |
| 71 | VERIFY_IS_APPROX((s1 * m1).transpose(), s1 * m1.transpose()); |
Benoit Jacob | 6907886 | 2008-02-28 12:38:12 +0000 | [diff] [blame] | 72 | VERIFY_IS_APPROX((s1 * m1).conjugate(), ei_conj(s1) * m1.conjugate()); |
| 73 | VERIFY_IS_APPROX((s1 * m1).adjoint(), ei_conj(s1) * m1.adjoint()); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 74 | |
| 75 | // check basic properties of dot, norm, norm2 |
| 76 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 77 | VERIFY_IS_APPROX((s1 * v1 + s2 * v2).dot(v3), s1 * v1.dot(v3) + s2 * v2.dot(v3)); |
Benoit Jacob | 6907886 | 2008-02-28 12:38:12 +0000 | [diff] [blame] | 78 | VERIFY_IS_APPROX(v3.dot(s1 * v1 + s2 * v2), ei_conj(s1)*v3.dot(v1)+ei_conj(s2)*v3.dot(v2)); |
| 79 | VERIFY_IS_APPROX(ei_conj(v1.dot(v2)), v2.dot(v1)); |
| 80 | VERIFY_IS_APPROX(ei_abs(v1.dot(v1)), v1.norm2()); |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 81 | if(NumTraits<Scalar>::HasFloatingPoint) |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 82 | VERIFY_IS_APPROX(v1.norm2(), v1.norm() * v1.norm()); |
Benoit Jacob | 6907886 | 2008-02-28 12:38:12 +0000 | [diff] [blame] | 83 | 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] | 84 | if(NumTraits<Scalar>::HasFloatingPoint) |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 85 | VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1)); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 86 | |
| 87 | // check compatibility of dot and adjoint |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 88 | VERIFY_IS_APPROX(v1.dot(square * v2), (square.adjoint() * v1).dot(v2)); |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame] | 89 | |
| 90 | // like in testBasicStuff, test operator() to check const-qualification |
Benoit Jacob | 6907886 | 2008-02-28 12:38:12 +0000 | [diff] [blame] | 91 | int r = ei_random<int>(0, rows-1), |
| 92 | c = ei_random<int>(0, cols-1); |
| 93 | VERIFY_IS_APPROX(m1.conjugate()(r,c), ei_conj(m1(r,c))); |
| 94 | VERIFY_IS_APPROX(m1.adjoint()(c,r), ei_conj(m1(r,c))); |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame] | 95 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void EigenTest::testAdjoint() |
| 99 | { |
Benoit Jacob | 2b20814 | 2007-12-11 10:07:18 +0000 | [diff] [blame] | 100 | for(int i = 0; i < m_repeat; i++) { |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 101 | adjoint(Matrix<float, 1, 1>()); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 102 | adjoint(Matrix4d()); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 103 | adjoint(MatrixXcf(3, 3)); |
| 104 | adjoint(MatrixXi(8, 12)); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 105 | adjoint(MatrixXcd(20, 20)); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 106 | } |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 107 | } |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 108 | |
| 109 | } // namespace Eigen |