blob: 4fb4b991a6c40849836ab8334d0e0d810f80b67c [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
Benoit Jacobe05f2912007-12-02 18:32:59 +000028namespace Eigen {
29
Benoit Jacobe445f502007-10-13 16:56:24 +000030template<typename MatrixType> void basicStuff(const MatrixType& m)
31{
Benoit Jacob5309ef52007-11-26 08:47:07 +000032 /* this test covers the following files:
33 1) Explicitly (see comments below):
34 Random.h Zero.h Identity.h Fuzzy.h Sum.h Difference.h
35 Opposite.h Product.h ScalarMultiple.h FromArray.h
36
37 2) Implicitly (the core stuff):
Benoit Jacob39f17762007-11-27 13:57:51 +000038 MatrixBase.h Matrix.h MatrixStorage.h CopyHelper.h MatrixRef.h
Benoit Jacob5309ef52007-11-26 08:47:07 +000039 NumTraits.h Util.h
40 */
41
Benoit Jacobe445f502007-10-13 16:56:24 +000042 typedef typename MatrixType::Scalar Scalar;
43 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
44 int rows = m.rows();
45 int cols = m.cols();
46
Benoit Jacob5309ef52007-11-26 08:47:07 +000047 // this test relies a lot on Random.h, and there's not much more that we can do
48 // to test it, hence I consider that we will have tested Random.h
Benoit Jacobe445f502007-10-13 16:56:24 +000049 MatrixType m1 = MatrixType::random(rows, cols),
50 m2 = MatrixType::random(rows, cols),
Benoit Jacob5309ef52007-11-26 08:47:07 +000051 m3(rows, cols),
Benoit Jacob6c8f1592007-10-14 09:18:18 +000052 mzero = MatrixType::zero(rows, cols),
53 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Benoit Jacob3f979182007-10-14 10:01:25 +000054 ::identity(rows),
Benoit Jacob6c8f1592007-10-14 09:18:18 +000055 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
56 ::random(rows, rows);
57 VectorType v1 = VectorType::random(rows),
58 v2 = VectorType::random(rows),
59 vzero = VectorType::zero(rows);
Benoit Jacobe445f502007-10-13 16:56:24 +000060
Benoit Jacobe05f2912007-12-02 18:32:59 +000061 Scalar s1 = random<Scalar>(),
62 s2 = random<Scalar>();
Benoit Jacobe445f502007-10-13 16:56:24 +000063
Benoit Jacob5309ef52007-11-26 08:47:07 +000064 // test Fuzzy.h and Zero.h.
Benoit Jacob346c00f2007-12-03 10:23:08 +000065 VERIFY_IS_APPROX( v1, v1);
66 VERIFY_IS_NOT_APPROX( v1, 2*v1);
67 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1);
Benoit Jacobe05f2912007-12-02 18:32:59 +000068 if(NumTraits<Scalar>::HasFloatingPoint)
Benoit Jacob346c00f2007-12-03 10:23:08 +000069 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.norm());
70 VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1);
71 VERIFY_IS_APPROX( vzero, v1-v1);
72 VERIFY_IS_APPROX( m1, m1);
73 VERIFY_IS_NOT_APPROX( m1, 2*m1);
74 VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1);
75 VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1);
76 VERIFY_IS_APPROX( mzero, m1-m1);
Benoit Jacobe445f502007-10-13 16:56:24 +000077
Benoit Jacob5309ef52007-11-26 08:47:07 +000078 // test the linear structure, i.e. the following files:
79 // Sum.h Difference.h Opposite.h ScalarMultiple.h
Benoit Jacob346c00f2007-12-03 10:23:08 +000080 VERIFY_IS_APPROX(-(-m1), m1);
81 VERIFY_IS_APPROX(m1+m1, 2*m1);
82 VERIFY_IS_APPROX(m1+m2-m1, m2);
83 VERIFY_IS_APPROX(-m2+m1+m2, m1);
84 VERIFY_IS_APPROX(m1*s1, s1*m1);
85 VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
86 VERIFY_IS_APPROX((s1+s2)*m1, m1*s1+m1*s2);
87 VERIFY_IS_APPROX((m1-m2)*s1, s1*m1-s1*m2);
88 VERIFY_IS_APPROX((s1-s2)*m1, m1*s1-m1*s2);
89 VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2);
90 VERIFY_IS_APPROX((-s1+s2)*m1, -m1*s1+m1*s2);
91 m3 = m2; m3 += m1;
92 VERIFY_IS_APPROX(m3, m1+m2);
93 m3 = m2; m3 -= m1;
94 VERIFY_IS_APPROX(m3, m2-m1);
95 m3 = m2; m3 *= s1;
96 VERIFY_IS_APPROX(m3, s1*m2);
97 if(NumTraits<Scalar>::HasFloatingPoint)
98 {
99 m3 = m2; m3 /= s1;
100 VERIFY_IS_APPROX(m3, m2/s1);
101 }
Benoit Jacobe445f502007-10-13 16:56:24 +0000102
Benoit Jacob5309ef52007-11-26 08:47:07 +0000103 // begin testing Product.h: only associativity for now
104 // (we use Transpose.h but this doesn't count as a test for it)
Benoit Jacob346c00f2007-12-03 10:23:08 +0000105 VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
Benoit Jacobe445f502007-10-13 16:56:24 +0000106 m3 = m1;
107 m3 *= (m1.transpose() * m2);
Benoit Jacob346c00f2007-12-03 10:23:08 +0000108 VERIFY_IS_APPROX(m3, m1*(m1.transpose()*m2));
109 VERIFY_IS_APPROX(m3, m1.lazyProduct(m1.transpose()*m2));
Benoit Jacob5309ef52007-11-26 08:47:07 +0000110
111 // continue testing Product.h: distributivity
Benoit Jacob346c00f2007-12-03 10:23:08 +0000112 VERIFY_IS_APPROX(square*(m1 + m2), square*m1+square*m2);
113 VERIFY_IS_APPROX(square*(m1 - m2), square*m1-square*m2);
Benoit Jacob5309ef52007-11-26 08:47:07 +0000114
115 // continue testing Product.h: compatibility with ScalarMultiple.h
Benoit Jacob346c00f2007-12-03 10:23:08 +0000116 VERIFY_IS_APPROX(s1*(square*m1), (s1*square)*m1);
117 VERIFY_IS_APPROX(s1*(square*m1), square*(m1*s1));
Benoit Jacob5309ef52007-11-26 08:47:07 +0000118
Benoit Jacob2fdd0672007-11-28 15:34:40 +0000119 // continue testing Product.h: lazyProduct
Benoit Jacob346c00f2007-12-03 10:23:08 +0000120 VERIFY_IS_APPROX(square.lazyProduct(m1), square*m1);
Benoit Jacob2fdd0672007-11-28 15:34:40 +0000121
Benoit Jacob5309ef52007-11-26 08:47:07 +0000122 // test Product.h together with Identity.h. This does test Identity.h.
Benoit Jacob346c00f2007-12-03 10:23:08 +0000123 VERIFY_IS_APPROX(m1, identity*m1);
124 VERIFY_IS_APPROX(v1, identity*v1);
Benoit Jacob6c8f1592007-10-14 09:18:18 +0000125
Benoit Jacob5309ef52007-11-26 08:47:07 +0000126 // test FromArray.h
Benoit Jacobf355ef22007-10-14 18:02:16 +0000127 Scalar* array1 = new Scalar[rows];
128 Scalar* array2 = new Scalar[rows];
129 Matrix<Scalar, Dynamic, 1>::fromArray(array1, rows) = Matrix<Scalar, Dynamic, 1>::random(rows);
Benoit Jacob5309ef52007-11-26 08:47:07 +0000130 Matrix<Scalar, Dynamic, 1>::fromArray(array2, rows)
131 = Matrix<Scalar, Dynamic, 1>::fromArray(array1, rows);
Benoit Jacob346c00f2007-12-03 10:23:08 +0000132 Matrix<Scalar, Dynamic, 1> ma1 = Matrix<Scalar, Dynamic, 1>::fromArray(array1, rows);
133 Matrix<Scalar, Dynamic, 1> ma2 = Matrix<Scalar, Dynamic, 1>::fromArray(array2, rows);
134 VERIFY_IS_APPROX(ma1, ma2);
Benoit Jacob5309ef52007-11-26 08:47:07 +0000135 delete[] array1;
136 delete[] array2;
Benoit Jacobe445f502007-10-13 16:56:24 +0000137}
138
139void EigenTest::testBasicStuff()
140{
Benoit Jacob5abaaf92007-12-03 08:35:23 +0000141 REPEAT {
142 basicStuff(Matrix<float, 1, 1>());
Benoit Jacob04502cc2007-12-05 07:22:22 +0000143 basicStuff(Matrix4d());
Benoit Jacob5abaaf92007-12-03 08:35:23 +0000144 basicStuff(MatrixXcf(3, 3));
145 basicStuff(MatrixXi(8, 12));
Benoit Jacob04502cc2007-12-05 07:22:22 +0000146 basicStuff(MatrixXcd(20, 20));
Benoit Jacob5abaaf92007-12-03 08:35:23 +0000147 }
Benoit Jacobe445f502007-10-13 16:56:24 +0000148}
Benoit Jacobe05f2912007-12-02 18:32:59 +0000149
150} // namespace Eigen