blob: 7218c43fae52813e870f1deeecb93d60bb165ba8 [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
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 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//
Benoit Jacob3698d8c2008-02-28 15:44:45 +000021// 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 Jacob2fdd0672007-11-28 15:34:40 +000024
25#include "main.h"
26
Benoit Jacobe05f2912007-12-02 18:32:59 +000027namespace Eigen {
28
Benoit Jacob2fdd0672007-11-28 15:34:40 +000029template<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 Jacob84934ea2008-01-06 16:35:21 +000036 typedef Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> VectorType;
Benoit Jacob2fdd0672007-11-28 15:34:40 +000037 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 Jacob84934ea2008-01-06 16:35:21 +000044 identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacobbcf7b292008-01-11 15:56:21 +000045 ::identity(rows, rows),
Benoit Jacob84934ea2008-01-06 16:35:21 +000046 square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacob2fdd0672007-11-28 15:34:40 +000047 ::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 Jacob69078862008-02-28 12:38:12 +000053 Scalar s1 = ei_random<Scalar>(),
54 s2 = ei_random<Scalar>();
Benoit Jacob2fdd0672007-11-28 15:34:40 +000055
56 // check involutivity of adjoint, transpose, conjugate
Benoit Jacob346c00f2007-12-03 10:23:08 +000057 VERIFY_IS_APPROX(m1.transpose().transpose(), m1);
58 VERIFY_IS_APPROX(m1.conjugate().conjugate(), m1);
59 VERIFY_IS_APPROX(m1.adjoint().adjoint(), m1);
Benoit Jacob2fdd0672007-11-28 15:34:40 +000060
61 // check basic compatibility of adjoint, transpose, conjugate
Benoit Jacob346c00f2007-12-03 10:23:08 +000062 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 Jacob2fdd0672007-11-28 15:34:40 +000066
67 // check multiplicative behavior
Benoit Jacob346c00f2007-12-03 10:23:08 +000068 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 Jacob69078862008-02-28 12:38:12 +000072 VERIFY_IS_APPROX((s1 * m1).conjugate(), ei_conj(s1) * m1.conjugate());
73 VERIFY_IS_APPROX((s1 * m1).adjoint(), ei_conj(s1) * m1.adjoint());
Benoit Jacob2fdd0672007-11-28 15:34:40 +000074
75 // check basic properties of dot, norm, norm2
76 typedef typename NumTraits<Scalar>::Real RealScalar;
Benoit Jacob346c00f2007-12-03 10:23:08 +000077 VERIFY_IS_APPROX((s1 * v1 + s2 * v2).dot(v3), s1 * v1.dot(v3) + s2 * v2.dot(v3));
Benoit Jacob69078862008-02-28 12:38:12 +000078 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 Jacobe05f2912007-12-02 18:32:59 +000081 if(NumTraits<Scalar>::HasFloatingPoint)
Benoit Jacob346c00f2007-12-03 10:23:08 +000082 VERIFY_IS_APPROX(v1.norm2(), v1.norm() * v1.norm());
Benoit Jacob69078862008-02-28 12:38:12 +000083 VERIFY_IS_MUCH_SMALLER_THAN(ei_abs(vzero.dot(v1)), static_cast<RealScalar>(1));
Benoit Jacobe05f2912007-12-02 18:32:59 +000084 if(NumTraits<Scalar>::HasFloatingPoint)
Benoit Jacob346c00f2007-12-03 10:23:08 +000085 VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1));
Benoit Jacob2fdd0672007-11-28 15:34:40 +000086
87 // check compatibility of dot and adjoint
Benoit Jacob346c00f2007-12-03 10:23:08 +000088 VERIFY_IS_APPROX(v1.dot(square * v2), (square.adjoint() * v1).dot(v2));
Benoit Jacobfc7b2b52007-12-12 17:48:20 +000089
90 // like in testBasicStuff, test operator() to check const-qualification
Benoit Jacob69078862008-02-28 12:38:12 +000091 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 Jacobfc7b2b52007-12-12 17:48:20 +000095
Benoit Jacob2fdd0672007-11-28 15:34:40 +000096}
97
98void EigenTest::testAdjoint()
99{
Benoit Jacob2b208142007-12-11 10:07:18 +0000100 for(int i = 0; i < m_repeat; i++) {
Benoit Jacob5abaaf92007-12-03 08:35:23 +0000101 adjoint(Matrix<float, 1, 1>());
Benoit Jacob04502cc2007-12-05 07:22:22 +0000102 adjoint(Matrix4d());
Benoit Jacob5abaaf92007-12-03 08:35:23 +0000103 adjoint(MatrixXcf(3, 3));
104 adjoint(MatrixXi(8, 12));
Benoit Jacob04502cc2007-12-05 07:22:22 +0000105 adjoint(MatrixXcd(20, 20));
Benoit Jacob5abaaf92007-12-03 08:35:23 +0000106 }
Benoit Jacob2fdd0672007-11-28 15:34:40 +0000107}
Benoit Jacobe05f2912007-12-02 18:32:59 +0000108
109} // namespace Eigen