blob: 5e79ec21cd63d781220e625b5feed5d3c68d1854 [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//
Benoit Jacob8ba30552008-01-07 09:34:21 +00004// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
Benoit Jacobe445f502007-10-13 16:56:24 +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
Gael Guennebaud46885d32008-03-03 11:02:52 +00008// License as published by the Free Software Foundation; either
Benoit Jacob3698d8c2008-02-28 15:44:45 +00009// 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
Gael Guennebaud46885d32008-03-03 11:02:52 +000013// published by the Free Software Foundation; either version 2 of
Benoit Jacob3698d8c2008-02-28 15:44:45 +000014// the License, or (at your option) any later version.
Benoit Jacobe445f502007-10-13 16:56:24 +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 Jacobe445f502007-10-13 16:56:24 +000020//
Gael Guennebaud46885d32008-03-03 11:02:52 +000021// You should have received a copy of the GNU Lesser General Public
Benoit Jacob3698d8c2008-02-28 15:44:45 +000022// License and a copy of the GNU General Public License along with
23// Eigen. If not, see <http://www.gnu.org/licenses/>.
Benoit Jacobe445f502007-10-13 16:56:24 +000024
25#include "main.h"
26
Benoit Jacobe05f2912007-12-02 18:32:59 +000027namespace Eigen {
28
Benoit Jacobe445f502007-10-13 16:56:24 +000029template<typename MatrixType> void basicStuff(const MatrixType& m)
30{
31 typedef typename MatrixType::Scalar Scalar;
Benoit Jacob2ee68a02008-03-12 17:17:36 +000032 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
Gael Guennebaud46885d32008-03-03 11:02:52 +000033
Benoit Jacobe445f502007-10-13 16:56:24 +000034 int rows = m.rows();
35 int cols = m.cols();
Gael Guennebaud46885d32008-03-03 11:02:52 +000036
Benoit Jacob5309ef52007-11-26 08:47:07 +000037 // this test relies a lot on Random.h, and there's not much more that we can do
38 // to test it, hence I consider that we will have tested Random.h
Benoit Jacobe445f502007-10-13 16:56:24 +000039 MatrixType m1 = MatrixType::random(rows, cols),
40 m2 = MatrixType::random(rows, cols),
Benoit Jacob5309ef52007-11-26 08:47:07 +000041 m3(rows, cols),
Benoit Jacob6c8f1592007-10-14 09:18:18 +000042 mzero = MatrixType::zero(rows, cols),
Benoit Jacob2ee68a02008-03-12 17:17:36 +000043 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Benoit Jacobbcf7b292008-01-11 15:56:21 +000044 ::identity(rows, rows),
Benoit Jacob2ee68a02008-03-12 17:17:36 +000045 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Benoit Jacob6c8f1592007-10-14 09:18:18 +000046 ::random(rows, rows);
47 VectorType v1 = VectorType::random(rows),
48 v2 = VectorType::random(rows),
49 vzero = VectorType::zero(rows);
Benoit Jacobe445f502007-10-13 16:56:24 +000050
Benoit Jacob69078862008-02-28 12:38:12 +000051 int r = ei_random<int>(0, rows-1),
52 c = ei_random<int>(0, cols-1);
Gael Guennebaud46885d32008-03-03 11:02:52 +000053
Benoit Jacob346c00f2007-12-03 10:23:08 +000054 VERIFY_IS_APPROX( v1, v1);
55 VERIFY_IS_NOT_APPROX( v1, 2*v1);
56 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1);
Benoit Jacobe05f2912007-12-02 18:32:59 +000057 if(NumTraits<Scalar>::HasFloatingPoint)
Benoit Jacob346c00f2007-12-03 10:23:08 +000058 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.norm());
59 VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1);
60 VERIFY_IS_APPROX( vzero, v1-v1);
61 VERIFY_IS_APPROX( m1, m1);
62 VERIFY_IS_NOT_APPROX( m1, 2*m1);
63 VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1);
64 VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1);
65 VERIFY_IS_APPROX( mzero, m1-m1);
Gael Guennebaud46885d32008-03-03 11:02:52 +000066
Benoit Jacobfc7b2b52007-12-12 17:48:20 +000067 // always test operator() on each read-only expression class,
68 // in order to check const-qualifiers.
69 // indeed, if an expression class (here Zero) is meant to be read-only,
70 // hence has no _write() method, the corresponding MatrixBase method (here zero())
71 // should return a const-qualified object so that it is the const-qualified
72 // operator() that gets called, which in turn calls _read().
Benoit Jacob7c384752007-12-15 18:16:30 +000073 VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::zero(rows,cols)(r,c), static_cast<Scalar>(1));
Gael Guennebaud46885d32008-03-03 11:02:52 +000074
Benoit Jacobdad245a2007-12-25 17:20:58 +000075 // now test copying a row-vector into a (column-)vector and conversely.
76 square.col(r) = square.row(r).eval();
Benoit Jacob2ee68a02008-03-12 17:17:36 +000077 Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
78 Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
Benoit Jacobdad245a2007-12-25 17:20:58 +000079 rv = square.col(r);
80 cv = square.row(r);
81 VERIFY_IS_APPROX(rv, cv.transpose());
Gael Guennebaud721626d2008-03-08 19:02:24 +000082
Benoit Jacob2ee68a02008-03-12 17:17:36 +000083 if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
Gael Guennebaud721626d2008-03-08 19:02:24 +000084 {
85 VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
86 }
Benoit Jacobe445f502007-10-13 16:56:24 +000087}
88
89void EigenTest::testBasicStuff()
90{
Benoit Jacob2b208142007-12-11 10:07:18 +000091 for(int i = 0; i < m_repeat; i++) {
Benoit Jacob5abaaf92007-12-03 08:35:23 +000092 basicStuff(Matrix<float, 1, 1>());
Benoit Jacob04502cc2007-12-05 07:22:22 +000093 basicStuff(Matrix4d());
Benoit Jacob5abaaf92007-12-03 08:35:23 +000094 basicStuff(MatrixXcf(3, 3));
95 basicStuff(MatrixXi(8, 12));
Benoit Jacob04502cc2007-12-05 07:22:22 +000096 basicStuff(MatrixXcd(20, 20));
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000097 basicStuff(Matrix<float, 100, 100>());
Benoit Jacob5abaaf92007-12-03 08:35:23 +000098 }
Gael Guennebaud721626d2008-03-08 19:02:24 +000099
100 // some additional basic tests
101 {
102 Matrix3d m3;
103 Matrix4d m4;
104 VERIFY_RAISES_ASSERT(m4 = m3);
105
106 VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8) );
107 VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) );
Gael Guennebaudf64311e2008-03-08 19:46:06 +0000108
Gael Guennebaud721626d2008-03-08 19:02:24 +0000109 double data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Gael Guennebaudf64311e2008-03-08 19:46:06 +0000110
111 m3 = Matrix3d::random();
112 m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
113 VERIFY_IS_APPROX(m3, (Matrix<double,3,3,RowMajor>::map(data)) );
114
115 Vector3d vec[3];
116 vec[0] << 1, 4, 7;
117 vec[1] << 2, 5, 8;
118 vec[2] << 3, 6, 9;
119 m3 = Matrix3d::random();
120 m3 << vec[0], vec[1], vec[2];
121 VERIFY_IS_APPROX(m3, (Matrix<double,3,3,RowMajor>::map(data)) );
122
123 vec[0] << 1, 2, 3;
124 vec[1] << 4, 5, 6;
125 vec[2] << 7, 8, 9;
126 m3 = Matrix3d::random();
127 m3 << vec[0].transpose(),
128 4, 5, 6,
129 vec[2].transpose();
Gael Guennebaud721626d2008-03-08 19:02:24 +0000130 VERIFY_IS_APPROX(m3, (Matrix<double,3,3,RowMajor>::map(data)) );
131 }
Benoit Jacobe445f502007-10-13 16:56:24 +0000132}
Benoit Jacobe05f2912007-12-02 18:32:59 +0000133
134} // namespace Eigen