blob: d34137c10a29fa532fc34665ae6f1fbb9e78a988 [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//
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 Jacobe05f2912007-12-02 18:32:59 +000028namespace Eigen {
29
Benoit Jacob2fdd0672007-11-28 15:34:40 +000030template<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 Jacobe05f2912007-12-02 18:32:59 +000054 Scalar s1 = random<Scalar>(),
55 s2 = random<Scalar>();
Benoit Jacob2fdd0672007-11-28 15:34:40 +000056
57 // check involutivity of adjoint, transpose, conjugate
58 QVERIFY(m1.transpose().transpose().isApprox(m1));
59 QVERIFY(m1.conjugate().conjugate().isApprox(m1));
60 QVERIFY(m1.adjoint().adjoint().isApprox(m1));
61
62 // check basic compatibility of adjoint, transpose, conjugate
63 QVERIFY(m1.transpose().conjugate().adjoint().isApprox(m1));
64 QVERIFY(m1.adjoint().conjugate().transpose().isApprox(m1));
65 if(!NumTraits<Scalar>::IsComplex) QVERIFY(m1.adjoint().transpose().isApprox(m1));
66
67 // check multiplicative behavior
68 QVERIFY((m1.transpose() * m2).transpose().isApprox(m2.transpose() * m1));
69 QVERIFY((m1.adjoint() * m2).adjoint().isApprox(m2.adjoint() * m1));
70 QVERIFY((m1.transpose() * m2).conjugate().isApprox(m1.adjoint() * m2.conjugate()));
71 QVERIFY((s1 * m1).transpose().isApprox(s1 * m1.transpose()));
Benoit Jacobe05f2912007-12-02 18:32:59 +000072 QVERIFY((s1 * m1).conjugate().isApprox(conj(s1) * m1.conjugate()));
73 QVERIFY((s1 * m1).adjoint().isApprox(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 Jacobe05f2912007-12-02 18:32:59 +000077 QVERIFY(isApprox((s1 * v1 + s2 * v2).dot(v3), s1 * v1.dot(v3) + s2 * v2.dot(v3)));
78 QVERIFY(isApprox(v3.dot(s1 * v1 + s2 * v2), conj(s1) * v3.dot(v1) + conj(s2) * v3.dot(v2)));
79 QVERIFY(isApprox(conj(v1.dot(v2)), v2.dot(v1)));
80 QVERIFY(isApprox(abs(v1.dot(v1)), v1.norm2()));
81 if(NumTraits<Scalar>::HasFloatingPoint)
82 QVERIFY(isApprox(v1.norm2(), v1.norm() * v1.norm()));
83 QVERIFY(isMuchSmallerThan(abs(vzero.dot(v1)), static_cast<RealScalar>(1)));
84 if(NumTraits<Scalar>::HasFloatingPoint)
85 QVERIFY(isMuchSmallerThan(vzero.norm(), static_cast<RealScalar>(1)));
Benoit Jacob2fdd0672007-11-28 15:34:40 +000086
87 // check compatibility of dot and adjoint
Benoit Jacobe05f2912007-12-02 18:32:59 +000088 QVERIFY(isApprox(v1.dot(square * v2), (square.adjoint() * v1).dot(v2)));
Benoit Jacob2fdd0672007-11-28 15:34:40 +000089}
90
91void EigenTest::testAdjoint()
92{
Benoit Jacob5abaaf92007-12-03 08:35:23 +000093 REPEAT {
94 adjoint(Matrix<float, 1, 1>());
95 adjoint(Matrix4cd());
96 adjoint(MatrixXcf(3, 3));
97 adjoint(MatrixXi(8, 12));
98 adjoint(MatrixXd(20, 20));
99 }
Benoit Jacob2fdd0672007-11-28 15:34:40 +0000100}
Benoit Jacobe05f2912007-12-02 18:32:59 +0000101
102} // namespace Eigen