blob: 02ebb2343f2ee53812f42106801712e0fdf01544 [file] [log] [blame]
Benoit Jacobe445f502007-10-13 16:56:24 +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
28template<typename MatrixType> void basicStuff(const MatrixType& m)
29{
30 typedef typename MatrixType::Scalar Scalar;
31 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
32 int rows = m.rows();
33 int cols = m.cols();
34
35 MatrixType m1 = MatrixType::random(rows, cols),
36 m2 = MatrixType::random(rows, cols),
Benoit Jacob6c8f1592007-10-14 09:18:18 +000037 m3,
38 mzero = MatrixType::zero(rows, cols),
39 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Benoit Jacob3f979182007-10-14 10:01:25 +000040 ::identity(rows),
Benoit Jacob6c8f1592007-10-14 09:18:18 +000041 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
42 ::random(rows, rows);
43 VectorType v1 = VectorType::random(rows),
44 v2 = VectorType::random(rows),
45 vzero = VectorType::zero(rows);
Benoit Jacobe445f502007-10-13 16:56:24 +000046
47 Scalar s1 = NumTraits<Scalar>::random(),
48 s2 = NumTraits<Scalar>::random();
49
50 QVERIFY(v1.isApprox(v1));
Benoit Jacob6c8f1592007-10-14 09:18:18 +000051 QVERIFY(!v1.isApprox(2*v1));
52
53 QVERIFY(vzero.isMuchSmallerThan(v1));
54 QVERIFY(vzero.isMuchSmallerThan(v1.norm()));
55 QVERIFY(!v1.isMuchSmallerThan(v1));
Benoit Jacobe445f502007-10-13 16:56:24 +000056 QVERIFY(m1.isApprox(m1));
Benoit Jacob6c8f1592007-10-14 09:18:18 +000057 QVERIFY(!m1.isApprox(2*m1));
58 QVERIFY(mzero.isMuchSmallerThan(m1));
59 QVERIFY(!m1.isMuchSmallerThan(m1));
60
61 QVERIFY(vzero.isApprox(v1-v1));
62 QVERIFY(mzero.isApprox(m1-m1));
Benoit Jacobe445f502007-10-13 16:56:24 +000063
64 QVERIFY((m1+m1).isApprox(2 * m1));
65 QVERIFY((m1 * s1).isApprox(s1 * m1));
66 QVERIFY(((m1 + m2) * s1).isApprox(s1 * m1 + s1 * m2));
67 QVERIFY(((s1 + s2) * m1).isApprox(m1 * s1 + m1 * s2));
68
Benoit Jacobe445f502007-10-13 16:56:24 +000069 m3 = m2;
70 QVERIFY((m3 += m1).isApprox(m1 + m2));
71 m3 = m2;
72 QVERIFY((m3 -= m1).isApprox(-m1 + m2));
73 m3 = m2;
74 QVERIFY((m3 *= s1).isApprox(s1 * m2));
75 m3 = m2;
76 if(NumTraits<Scalar>::HasFloatingPoint
77 && s1 != static_cast<Scalar>(0))
78 QVERIFY((m3 /= s1).isApprox(m2 / s1));
79
80 QVERIFY(((m1 * m1.transpose()) * m2).isApprox(m1 * (m1.transpose() * m2)));
81 m3 = m1;
82 m3 *= (m1.transpose() * m2);
83 QVERIFY(m3.isApprox(m1 * (m1.transpose() * m2)));
84 QVERIFY(m3.isApprox(m1.lazyProduct(m1.transpose() * m2)));
Benoit Jacob6c8f1592007-10-14 09:18:18 +000085
86 QVERIFY(m1.isApprox(identity * m1));
87 QVERIFY(v1.isApprox(identity * v1));
88
89 QVERIFY((square * (m1 + m2)).isApprox(square * m1 + square * m2));
Benoit Jacobf355ef22007-10-14 18:02:16 +000090
91 Scalar* array1 = new Scalar[rows];
92 Scalar* array2 = new Scalar[rows];
93 Matrix<Scalar, Dynamic, 1>::fromArray(array1, rows) = Matrix<Scalar, Dynamic, 1>::random(rows);
94 Matrix<Scalar, Dynamic, 1>::fromArray(array2, rows) = Matrix<Scalar, Dynamic, 1>::fromArray(array1, rows);
95 bool b = Matrix<Scalar, Dynamic, 1>::fromArray(array1, rows).isApprox(Matrix<Scalar, Dynamic, 1>::fromArray(array2, rows));
96 QVERIFY(b);
Benoit Jacobe445f502007-10-13 16:56:24 +000097}
98
99void EigenTest::testBasicStuff()
100{
101 basicStuff(Matrix<float, 1, 1>());
Benoit Jacobe445f502007-10-13 16:56:24 +0000102 basicStuff(Matrix<complex<double>, 4, 4>());
103 basicStuff(MatrixXcf(3, 3));
104 basicStuff(MatrixXi(8, 12));
105 basicStuff(MatrixXd(20, 20));
106}