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 | |
| 28 | template<typename MatrixType> void adjoint(const MatrixType& m) |
| 29 | { |
| 30 | /* this test covers the following files: |
| 31 | Transpose.h Conjugate.h Dot.h |
| 32 | */ |
| 33 | |
| 34 | typedef typename MatrixType::Scalar Scalar; |
| 35 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 36 | int rows = m.rows(); |
| 37 | int cols = m.cols(); |
| 38 | |
| 39 | MatrixType m1 = MatrixType::random(rows, cols), |
| 40 | m2 = MatrixType::random(rows, cols), |
| 41 | m3(rows, cols), |
| 42 | mzero = MatrixType::zero(rows, cols), |
| 43 | identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
| 44 | ::identity(rows), |
| 45 | square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
| 46 | ::random(rows, rows); |
| 47 | VectorType v1 = VectorType::random(rows), |
| 48 | v2 = VectorType::random(rows), |
| 49 | v3 = VectorType::random(rows), |
| 50 | vzero = VectorType::zero(rows); |
| 51 | |
| 52 | Scalar s1 = NumTraits<Scalar>::random(), |
| 53 | s2 = NumTraits<Scalar>::random(); |
| 54 | |
| 55 | // check involutivity of adjoint, transpose, conjugate |
| 56 | QVERIFY(m1.transpose().transpose().isApprox(m1)); |
| 57 | QVERIFY(m1.conjugate().conjugate().isApprox(m1)); |
| 58 | QVERIFY(m1.adjoint().adjoint().isApprox(m1)); |
| 59 | |
| 60 | // check basic compatibility of adjoint, transpose, conjugate |
| 61 | QVERIFY(m1.transpose().conjugate().adjoint().isApprox(m1)); |
| 62 | QVERIFY(m1.adjoint().conjugate().transpose().isApprox(m1)); |
| 63 | if(!NumTraits<Scalar>::IsComplex) QVERIFY(m1.adjoint().transpose().isApprox(m1)); |
| 64 | |
| 65 | // check multiplicative behavior |
| 66 | QVERIFY((m1.transpose() * m2).transpose().isApprox(m2.transpose() * m1)); |
| 67 | QVERIFY((m1.adjoint() * m2).adjoint().isApprox(m2.adjoint() * m1)); |
| 68 | QVERIFY((m1.transpose() * m2).conjugate().isApprox(m1.adjoint() * m2.conjugate())); |
| 69 | QVERIFY((s1 * m1).transpose().isApprox(s1 * m1.transpose())); |
| 70 | QVERIFY((s1 * m1).conjugate().isApprox(NumTraits<Scalar>::conj(s1) * m1.conjugate())); |
| 71 | QVERIFY((s1 * m1).adjoint().isApprox(NumTraits<Scalar>::conj(s1) * m1.adjoint())); |
| 72 | |
| 73 | // check basic properties of dot, norm, norm2 |
| 74 | typedef typename NumTraits<Scalar>::Real RealScalar; |
| 75 | QVERIFY(NumTraits<Scalar>::isApprox((s1 * v1 + s2 * v2).dot(v3), s1 * v1.dot(v3) + s2 * v2.dot(v3))); |
| 76 | QVERIFY(NumTraits<Scalar>::isApprox(v3.dot(s1 * v1 + s2 * v2), NumTraits<Scalar>::conj(s1) * v3.dot(v1) + NumTraits<Scalar>::conj(s2) * v3.dot(v2))); |
| 77 | QVERIFY(NumTraits<Scalar>::isApprox(NumTraits<Scalar>::conj(v1.dot(v2)), v2.dot(v1))); |
| 78 | QVERIFY(NumTraits<RealScalar>::isApprox(abs(v1.dot(v1)), v1.norm2())); |
| 79 | if(NumTraits<Scalar>::HasFloatingPoint) QVERIFY(NumTraits<RealScalar>::isApprox(v1.norm2(), v1.norm() * v1.norm())); |
| 80 | QVERIFY(NumTraits<RealScalar>::isMuchSmallerThan(abs(vzero.dot(v1)), 1)); |
| 81 | QVERIFY(NumTraits<RealScalar>::isMuchSmallerThan(vzero.norm(), 1)); |
| 82 | |
| 83 | // check compatibility of dot and adjoint |
| 84 | QVERIFY(NumTraits<Scalar>::isApprox(v1.dot(square * v2), (square.adjoint() * v1).dot(v2))); |
| 85 | } |
| 86 | |
| 87 | void EigenTest::testAdjoint() |
| 88 | { |
| 89 | adjoint(Matrix<float, 1, 1>()); |
| 90 | adjoint(Matrix<complex<double>, 4, 4>()); |
| 91 | adjoint(MatrixXcf(3, 3)); |
| 92 | adjoint(MatrixXi(8, 12)); |
| 93 | adjoint(MatrixXd(20, 20)); |
| 94 | } |