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 | // |
| 4 | // Copyright (C) 2006-2007 Benoit Jacob <jacob@math.jussieu.fr> |
| 5 | // |
| 6 | // Eigen is free software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the Free Software |
| 8 | // Foundation; either version 2 or (at your option) any later version. |
| 9 | // |
| 10 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 | // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 13 | // details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License along |
| 16 | // with Eigen; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | // Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | // |
| 19 | // As a special exception, if other files instantiate templates or use macros |
| 20 | // or functions from this file, or you compile this file and link it |
| 21 | // with other works to produce a work based on this file, this file does not |
| 22 | // by itself cause the resulting work to be covered by the GNU General Public |
| 23 | // License. This exception does not invalidate any other reasons why a work |
| 24 | // based on this file might be covered by the GNU General Public License. |
| 25 | |
| 26 | #include "main.h" |
| 27 | |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 28 | namespace Eigen { |
| 29 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 30 | template<typename MatrixType> void adjoint(const MatrixType& m) |
| 31 | { |
| 32 | /* this test covers the following files: |
| 33 | Transpose.h Conjugate.h Dot.h |
| 34 | */ |
| 35 | |
| 36 | typedef typename MatrixType::Scalar Scalar; |
| 37 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 38 | int rows = m.rows(); |
| 39 | int cols = m.cols(); |
| 40 | |
| 41 | MatrixType m1 = MatrixType::random(rows, cols), |
| 42 | m2 = MatrixType::random(rows, cols), |
| 43 | m3(rows, cols), |
| 44 | mzero = MatrixType::zero(rows, cols), |
| 45 | identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
| 46 | ::identity(rows), |
| 47 | square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
| 48 | ::random(rows, rows); |
| 49 | VectorType v1 = VectorType::random(rows), |
| 50 | v2 = VectorType::random(rows), |
| 51 | v3 = VectorType::random(rows), |
| 52 | vzero = VectorType::zero(rows); |
| 53 | |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 54 | Scalar s1 = random<Scalar>(), |
| 55 | s2 = random<Scalar>(); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 56 | |
| 57 | // check involutivity of adjoint, transpose, conjugate |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 58 | VERIFY_IS_APPROX(m1.transpose().transpose(), m1); |
| 59 | VERIFY_IS_APPROX(m1.conjugate().conjugate(), m1); |
| 60 | VERIFY_IS_APPROX(m1.adjoint().adjoint(), m1); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 61 | |
| 62 | // check basic compatibility of adjoint, transpose, conjugate |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 63 | VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1); |
| 64 | VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1); |
| 65 | if(!NumTraits<Scalar>::IsComplex) |
| 66 | VERIFY_IS_APPROX(m1.adjoint().transpose(), m1); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 67 | |
| 68 | // check multiplicative behavior |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 69 | VERIFY_IS_APPROX((m1.transpose() * m2).transpose(), m2.transpose() * m1); |
| 70 | VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1); |
| 71 | VERIFY_IS_APPROX((m1.transpose() * m2).conjugate(), m1.adjoint() * m2.conjugate()); |
| 72 | VERIFY_IS_APPROX((s1 * m1).transpose(), s1 * m1.transpose()); |
| 73 | VERIFY_IS_APPROX((s1 * m1).conjugate(), conj(s1) * m1.conjugate()); |
| 74 | VERIFY_IS_APPROX((s1 * m1).adjoint(), conj(s1) * m1.adjoint()); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 75 | |
| 76 | // check basic properties of dot, norm, norm2 |
| 77 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 78 | VERIFY_IS_APPROX((s1 * v1 + s2 * v2).dot(v3), s1 * v1.dot(v3) + s2 * v2.dot(v3)); |
| 79 | VERIFY_IS_APPROX(v3.dot(s1 * v1 + s2 * v2), conj(s1)*v3.dot(v1)+conj(s2)*v3.dot(v2)); |
| 80 | VERIFY_IS_APPROX(conj(v1.dot(v2)), v2.dot(v1)); |
| 81 | VERIFY_IS_APPROX(abs(v1.dot(v1)), v1.norm2()); |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 82 | if(NumTraits<Scalar>::HasFloatingPoint) |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 83 | VERIFY_IS_APPROX(v1.norm2(), v1.norm() * v1.norm()); |
| 84 | VERIFY_IS_MUCH_SMALLER_THAN(abs(vzero.dot(v1)), static_cast<RealScalar>(1)); |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 85 | if(NumTraits<Scalar>::HasFloatingPoint) |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 86 | VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1)); |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 87 | |
| 88 | // check compatibility of dot and adjoint |
Benoit Jacob | 346c00f | 2007-12-03 10:23:08 +0000 | [diff] [blame] | 89 | VERIFY_IS_APPROX(v1.dot(square * v2), (square.adjoint() * v1).dot(v2)); |
Benoit Jacob | fc7b2b5 | 2007-12-12 17:48:20 +0000 | [diff] [blame^] | 90 | |
| 91 | // like in testBasicStuff, test operator() to check const-qualification |
| 92 | int r = random<int>(0, rows-1), |
| 93 | c = random<int>(0, cols-1); |
| 94 | VERIFY_IS_APPROX(m1.conjugate()(r,c), conj(m1(r,c))); |
| 95 | VERIFY_IS_APPROX(m1.adjoint()(c,r), conj(m1(r,c))); |
| 96 | |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void EigenTest::testAdjoint() |
| 100 | { |
Benoit Jacob | 2b20814 | 2007-12-11 10:07:18 +0000 | [diff] [blame] | 101 | for(int i = 0; i < m_repeat; i++) { |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 102 | adjoint(Matrix<float, 1, 1>()); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 103 | adjoint(Matrix4d()); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 104 | adjoint(MatrixXcf(3, 3)); |
| 105 | adjoint(MatrixXi(8, 12)); |
Benoit Jacob | 04502cc | 2007-12-05 07:22:22 +0000 | [diff] [blame] | 106 | adjoint(MatrixXcd(20, 20)); |
Benoit Jacob | 5abaaf9 | 2007-12-03 08:35:23 +0000 | [diff] [blame] | 107 | } |
Benoit Jacob | 2fdd067 | 2007-11-28 15:34:40 +0000 | [diff] [blame] | 108 | } |
Benoit Jacob | e05f291 | 2007-12-02 18:32:59 +0000 | [diff] [blame] | 109 | |
| 110 | } // namespace Eigen |