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