blob: 690f0424af422f24f3b78dd234d9050308842bdb [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;
Benoit Jacob84934ea2008-01-06 16:35:21 +000037 typedef Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> VectorType;
Benoit Jacob2fdd0672007-11-28 15:34:40 +000038 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),
Benoit Jacob84934ea2008-01-06 16:35:21 +000045 identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacob2fdd0672007-11-28 15:34:40 +000046 ::identity(rows),
Benoit Jacob84934ea2008-01-06 16:35:21 +000047 square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacob2fdd0672007-11-28 15:34:40 +000048 ::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
Benoit Jacob346c00f2007-12-03 10:23:08 +000058 VERIFY_IS_APPROX(m1.transpose().transpose(), m1);
59 VERIFY_IS_APPROX(m1.conjugate().conjugate(), m1);
60 VERIFY_IS_APPROX(m1.adjoint().adjoint(), m1);
Benoit Jacob2fdd0672007-11-28 15:34:40 +000061
62 // check basic compatibility of adjoint, transpose, conjugate
Benoit Jacob346c00f2007-12-03 10:23:08 +000063 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 Jacob2fdd0672007-11-28 15:34:40 +000067
68 // check multiplicative behavior
Benoit Jacob346c00f2007-12-03 10:23:08 +000069 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 Jacob2fdd0672007-11-28 15:34:40 +000075
76 // check basic properties of dot, norm, norm2
77 typedef typename NumTraits<Scalar>::Real RealScalar;
Benoit Jacob346c00f2007-12-03 10:23:08 +000078 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 Jacobe05f2912007-12-02 18:32:59 +000082 if(NumTraits<Scalar>::HasFloatingPoint)
Benoit Jacob346c00f2007-12-03 10:23:08 +000083 VERIFY_IS_APPROX(v1.norm2(), v1.norm() * v1.norm());
84 VERIFY_IS_MUCH_SMALLER_THAN(abs(vzero.dot(v1)), static_cast<RealScalar>(1));
Benoit Jacobe05f2912007-12-02 18:32:59 +000085 if(NumTraits<Scalar>::HasFloatingPoint)
Benoit Jacob346c00f2007-12-03 10:23:08 +000086 VERIFY_IS_MUCH_SMALLER_THAN(vzero.norm(), static_cast<RealScalar>(1));
Benoit Jacob2fdd0672007-11-28 15:34:40 +000087
88 // check compatibility of dot and adjoint
Benoit Jacob346c00f2007-12-03 10:23:08 +000089 VERIFY_IS_APPROX(v1.dot(square * v2), (square.adjoint() * v1).dot(v2));
Benoit Jacobfc7b2b52007-12-12 17:48:20 +000090
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 Jacob2fdd0672007-11-28 15:34:40 +000097}
98
99void EigenTest::testAdjoint()
100{
Benoit Jacob2b208142007-12-11 10:07:18 +0000101 for(int i = 0; i < m_repeat; i++) {
Benoit Jacob5abaaf92007-12-03 08:35:23 +0000102 adjoint(Matrix<float, 1, 1>());
Benoit Jacob04502cc2007-12-05 07:22:22 +0000103 adjoint(Matrix4d());
Benoit Jacob5abaaf92007-12-03 08:35:23 +0000104 adjoint(MatrixXcf(3, 3));
105 adjoint(MatrixXi(8, 12));
Benoit Jacob04502cc2007-12-05 07:22:22 +0000106 adjoint(MatrixXcd(20, 20));
Benoit Jacob5abaaf92007-12-03 08:35:23 +0000107 }
Benoit Jacob2fdd0672007-11-28 15:34:40 +0000108}
Benoit Jacobe05f2912007-12-02 18:32:59 +0000109
110} // namespace Eigen