blob: a8fb8db332fa026da4104bf42ff7d37349d1ba76 [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 Jacob84934ea2008-01-06 16:35:21 +000032 typedef Matrix<Scalar, MatrixType::Traits::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 Jacob84934ea2008-01-06 16:35:21 +000043 identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacobbcf7b292008-01-11 15:56:21 +000044 ::identity(rows, rows),
Benoit Jacob84934ea2008-01-06 16:35:21 +000045 square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::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 Jacob84934ea2008-01-06 16:35:21 +000077 Matrix<Scalar, 1, MatrixType::Traits::RowsAtCompileTime> rv(rows);
78 Matrix<Scalar, MatrixType::Traits::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());
Benoit Jacobe445f502007-10-13 16:56:24 +000082}
83
84void EigenTest::testBasicStuff()
85{
Benoit Jacob2b208142007-12-11 10:07:18 +000086 for(int i = 0; i < m_repeat; i++) {
Benoit Jacob5abaaf92007-12-03 08:35:23 +000087 basicStuff(Matrix<float, 1, 1>());
Benoit Jacob04502cc2007-12-05 07:22:22 +000088 basicStuff(Matrix4d());
Benoit Jacob5abaaf92007-12-03 08:35:23 +000089 basicStuff(MatrixXcf(3, 3));
90 basicStuff(MatrixXi(8, 12));
Benoit Jacob04502cc2007-12-05 07:22:22 +000091 basicStuff(MatrixXcd(20, 20));
Gael Guennebaud8e0d5482008-03-05 13:18:19 +000092 basicStuff(Matrix<float, 100, 100>());
Benoit Jacob5abaaf92007-12-03 08:35:23 +000093 }
Benoit Jacobe445f502007-10-13 16:56:24 +000094}
Benoit Jacobe05f2912007-12-02 18:32:59 +000095
96} // namespace Eigen