blob: 593f9e7b96a322ec2dfff51b2dbd286260877476 [file] [log] [blame]
Benoit Jacobdad245a2007-12-25 17:20:58 +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 Jacobdad245a2007-12-25 17:20:58 +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 Jacobdad245a2007-12-25 17:20:58 +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 Jacobdad245a2007-12-25 17:20:58 +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 Jacobdad245a2007-12-25 17:20:58 +000024
25#include "main.h"
26
27namespace Eigen {
28
29template<typename MatrixType> void linearStructure(const MatrixType& m)
30{
31 /* this test covers the following files:
32 Sum.h Difference.h Opposite.h ScalarMultiple.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 Jacobdad245a2007-12-25 17:20:58 +000037
38 int rows = m.rows();
39 int cols = m.cols();
40
41 // this test relies a lot on Random.h, and there's not much more that we can do
42 // to test it, hence I consider that we will have tested Random.h
43 MatrixType m1 = MatrixType::random(rows, cols),
44 m2 = MatrixType::random(rows, cols),
45 m3(rows, cols),
46 mzero = MatrixType::zero(rows, cols),
Benoit Jacob84934ea2008-01-06 16:35:21 +000047 identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacobbcf7b292008-01-11 15:56:21 +000048 ::identity(rows, rows),
Benoit Jacob84934ea2008-01-06 16:35:21 +000049 square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacobdad245a2007-12-25 17:20:58 +000050 ::random(rows, rows);
51 VectorType v1 = VectorType::random(rows),
52 v2 = VectorType::random(rows),
53 vzero = VectorType::zero(rows);
54
Benoit Jacob69078862008-02-28 12:38:12 +000055 Scalar s1 = ei_random<Scalar>(),
56 s2 = ei_random<Scalar>();
Benoit Jacobdad245a2007-12-25 17:20:58 +000057
Benoit Jacob69078862008-02-28 12:38:12 +000058 int r = ei_random<int>(0, rows-1),
59 c = ei_random<int>(0, cols-1);
Benoit Jacobdad245a2007-12-25 17:20:58 +000060
61 VERIFY_IS_APPROX(-(-m1), m1);
62 VERIFY_IS_APPROX(m1+m1, 2*m1);
63 VERIFY_IS_APPROX(m1+m2-m1, m2);
64 VERIFY_IS_APPROX(-m2+m1+m2, m1);
65 VERIFY_IS_APPROX(m1*s1, s1*m1);
66 VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
67 VERIFY_IS_APPROX((s1+s2)*m1, m1*s1+m1*s2);
68 VERIFY_IS_APPROX((m1-m2)*s1, s1*m1-s1*m2);
69 VERIFY_IS_APPROX((s1-s2)*m1, m1*s1-m1*s2);
70 VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2);
71 VERIFY_IS_APPROX((-s1+s2)*m1, -m1*s1+m1*s2);
72 m3 = m2; m3 += m1;
73 VERIFY_IS_APPROX(m3, m1+m2);
74 m3 = m2; m3 -= m1;
75 VERIFY_IS_APPROX(m3, m2-m1);
76 m3 = m2; m3 *= s1;
77 VERIFY_IS_APPROX(m3, s1*m2);
78 if(NumTraits<Scalar>::HasFloatingPoint)
79 {
80 m3 = m2; m3 /= s1;
81 VERIFY_IS_APPROX(m3, m2/s1);
82 }
83
84 // again, test operator() to check const-qualification
85 VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
86 VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
87 VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
88 VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
89 VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
90 if(NumTraits<Scalar>::HasFloatingPoint)
91 VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
92}
93
94void EigenTest::testLinearStructure()
95{
96 for(int i = 0; i < m_repeat; i++) {
97 linearStructure(Matrix<float, 1, 1>());
98 linearStructure(Matrix4d());
99 linearStructure(MatrixXcf(3, 3));
100 linearStructure(MatrixXi(8, 12));
101 linearStructure(MatrixXcd(20, 20));
102 }
103}
104
105} // namespace Eigen