blob: 835af5e6472804244b39a2e5851b95bd54eb54c5 [file] [log] [blame]
Benoit Jacob2fdd0672007-11-28 15:34:40 +00001// 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 Jacob8ba30552008-01-07 09:34:21 +00004// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
Benoit Jacob2fdd0672007-11-28 15:34:40 +00005//
Benoit Jacob3698d8c2008-02-28 15:44:45 +00006// Eigen is free software; you can redistribute it and/or
7// modify it under the terms of the GNU Lesser General Public
Gael Guennebaud8e0d5482008-03-05 13:18:19 +00008// License as published by the Free Software Foundation; either
Benoit Jacob3698d8c2008-02-28 15:44:45 +00009// 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 Guennebaud8e0d5482008-03-05 13:18:19 +000013// published by the Free Software Foundation; either version 2 of
Benoit Jacob3698d8c2008-02-28 15:44:45 +000014// the License, or (at your option) any later version.
Benoit Jacob2fdd0672007-11-28 15:34:40 +000015//
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 Jacob3698d8c2008-02-28 15:44:45 +000018// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19// GNU General Public License for more details.
Benoit Jacob2fdd0672007-11-28 15:34:40 +000020//
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000021// You should have received a copy of the GNU Lesser General Public
Benoit Jacob3698d8c2008-02-28 15:44:45 +000022// License and a copy of the GNU General Public License along with
23// Eigen. If not, see <http://www.gnu.org/licenses/>.
Benoit Jacob2fdd0672007-11-28 15:34:40 +000024
25#include "main.h"
26
27template<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;
Benoit Jacob2ee68a02008-03-12 17:17:36 +000034 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
Benoit Jacob2fdd0672007-11-28 15:34:40 +000035 int rows = m.rows();
36 int cols = m.cols();
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000037
Gael Guennebaudc10f0692008-07-21 00:34:46 +000038 MatrixType m1 = MatrixType::Random(rows, cols),
39 m2 = MatrixType::Random(rows, cols),
Benoit Jacob2fdd0672007-11-28 15:34:40 +000040 m3(rows, cols),
Gael Guennebaudc10f0692008-07-21 00:34:46 +000041 mzero = MatrixType::Zero(rows, cols),
Benoit Jacob2ee68a02008-03-12 17:17:36 +000042 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Gael Guennebaudc10f0692008-07-21 00:34:46 +000043 ::Identity(rows, rows),
Benoit Jacob2ee68a02008-03-12 17:17:36 +000044 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Gael Guennebaudc10f0692008-07-21 00:34:46 +000045 ::Random(rows, rows);
46 VectorType v1 = VectorType::Random(rows),
47 v2 = VectorType::Random(rows),
48 v3 = VectorType::Random(rows),
49 vzero = VectorType::Zero(rows);
Benoit Jacob2fdd0672007-11-28 15:34:40 +000050
Benoit Jacob69078862008-02-28 12:38:12 +000051 Scalar s1 = ei_random<Scalar>(),
52 s2 = ei_random<Scalar>();
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000053
Benoit Jacob2fdd0672007-11-28 15:34:40 +000054 // check basic compatibility of adjoint, transpose, conjugate
Benoit Jacob346c00f2007-12-03 10:23:08 +000055 VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1);
56 VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1);
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000057
Benoit Jacob2fdd0672007-11-28 15:34:40 +000058 // check multiplicative behavior
Benoit Jacob346c00f2007-12-03 10:23:08 +000059 VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
Benoit Jacob69078862008-02-28 12:38:12 +000060 VERIFY_IS_APPROX((s1 * m1).adjoint(), ei_conj(s1) * m1.adjoint());
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000061
Benoit Jacob2fdd0672007-11-28 15:34:40 +000062 // check basic properties of dot, norm, norm2
63 typedef typename NumTraits<Scalar>::Real RealScalar;
Benoit Jacob346c00f2007-12-03 10:23:08 +000064 VERIFY_IS_APPROX((s1 * v1 + s2 * v2).dot(v3), s1 * v1.dot(v3) + s2 * v2.dot(v3));
Benoit Jacob69078862008-02-28 12:38:12 +000065 VERIFY_IS_APPROX(v3.dot(s1 * v1 + s2 * v2), ei_conj(s1)*v3.dot(v1)+ei_conj(s2)*v3.dot(v2));
66 VERIFY_IS_APPROX(ei_conj(v1.dot(v2)), v2.dot(v1));
67 VERIFY_IS_APPROX(ei_abs(v1.dot(v1)), v1.norm2());
Benoit Jacobe05f2912007-12-02 18:32:59 +000068 if(NumTraits<Scalar>::HasFloatingPoint)
Benoit Jacob346c00f2007-12-03 10:23:08 +000069 VERIFY_IS_APPROX(v1.norm2(), v1.norm() * v1.norm());
Benoit Jacob69078862008-02-28 12:38:12 +000070 VERIFY_IS_MUCH_SMALLER_THAN(ei_abs(vzero.dot(v1)), static_cast<RealScalar>(1));
Benoit Jacobe05f2912007-12-02 18:32:59 +000071 if(NumTraits<Scalar>::HasFloatingPoint)
Benoit Jacob346c00f2007-12-03 10:23:08 +000072 VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1));
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000073
Benoit Jacob2fdd0672007-11-28 15:34:40 +000074 // check compatibility of dot and adjoint
Benoit Jacob346c00f2007-12-03 10:23:08 +000075 VERIFY_IS_APPROX(v1.dot(square * v2), (square.adjoint() * v1).dot(v2));
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000076
Benoit Jacobfc7b2b52007-12-12 17:48:20 +000077 // like in testBasicStuff, test operator() to check const-qualification
Benoit Jacob69078862008-02-28 12:38:12 +000078 int r = ei_random<int>(0, rows-1),
79 c = ei_random<int>(0, cols-1);
80 VERIFY_IS_APPROX(m1.conjugate()(r,c), ei_conj(m1(r,c)));
81 VERIFY_IS_APPROX(m1.adjoint()(c,r), ei_conj(m1(r,c)));
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000082
Benoit Jacob2fdd0672007-11-28 15:34:40 +000083}
84
Gael Guennebaud522e24f2008-05-22 12:18:55 +000085void test_adjoint()
Benoit Jacob2fdd0672007-11-28 15:34:40 +000086{
Gael Guennebaud522e24f2008-05-22 12:18:55 +000087 for(int i = 0; i < g_repeat; i++) {
88 CALL_SUBTEST( adjoint(Matrix<float, 1, 1>()) );
89 CALL_SUBTEST( adjoint(Matrix4d()) );
90 CALL_SUBTEST( adjoint(MatrixXcf(3, 3)) );
91 CALL_SUBTEST( adjoint(MatrixXi(8, 12)) );
92 CALL_SUBTEST( adjoint(MatrixXcd(20, 20)) );
Benoit Jacob5abaaf92007-12-03 08:35:23 +000093 }
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000094 // test a large matrix only once
Gael Guennebaud522e24f2008-05-22 12:18:55 +000095 CALL_SUBTEST( adjoint(Matrix<float, 100, 100>()) );
Benoit Jacob2fdd0672007-11-28 15:34:40 +000096}
Benoit Jacobe05f2912007-12-02 18:32:59 +000097