blob: f8a651a73be9c06d1b50cd64b7088be4084a7ee8 [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//
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{
32 typedef typename MatrixType::Scalar Scalar;
Benoit Jacob84934ea2008-01-06 16:35:21 +000033 typedef Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> VectorType;
Benoit Jacobdad245a2007-12-25 17:20:58 +000034
Benoit Jacobe445f502007-10-13 16:56:24 +000035 int rows = m.rows();
36 int cols = m.cols();
37
Benoit Jacob5309ef52007-11-26 08:47:07 +000038 // this test relies a lot on Random.h, and there's not much more that we can do
39 // to test it, hence I consider that we will have tested Random.h
Benoit Jacobe445f502007-10-13 16:56:24 +000040 MatrixType m1 = MatrixType::random(rows, cols),
41 m2 = MatrixType::random(rows, cols),
Benoit Jacob5309ef52007-11-26 08:47:07 +000042 m3(rows, cols),
Benoit Jacob6c8f1592007-10-14 09:18:18 +000043 mzero = MatrixType::zero(rows, cols),
Benoit Jacob84934ea2008-01-06 16:35:21 +000044 identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacobbcf7b292008-01-11 15:56:21 +000045 ::identity(rows, rows),
Benoit Jacob84934ea2008-01-06 16:35:21 +000046 square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
Benoit Jacob6c8f1592007-10-14 09:18:18 +000047 ::random(rows, rows);
48 VectorType v1 = VectorType::random(rows),
49 v2 = VectorType::random(rows),
50 vzero = VectorType::zero(rows);
Benoit Jacobe445f502007-10-13 16:56:24 +000051
Benoit Jacobfc7b2b52007-12-12 17:48:20 +000052 int r = random<int>(0, rows-1),
53 c = random<int>(0, cols-1);
54
Benoit Jacob346c00f2007-12-03 10:23:08 +000055 VERIFY_IS_APPROX( v1, v1);
56 VERIFY_IS_NOT_APPROX( v1, 2*v1);
57 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1);
Benoit Jacobe05f2912007-12-02 18:32:59 +000058 if(NumTraits<Scalar>::HasFloatingPoint)
Benoit Jacob346c00f2007-12-03 10:23:08 +000059 VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.norm());
60 VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1, v1);
61 VERIFY_IS_APPROX( vzero, v1-v1);
62 VERIFY_IS_APPROX( m1, m1);
63 VERIFY_IS_NOT_APPROX( m1, 2*m1);
64 VERIFY_IS_MUCH_SMALLER_THAN( mzero, m1);
65 VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1, m1);
66 VERIFY_IS_APPROX( mzero, m1-m1);
Benoit Jacobe445f502007-10-13 16:56:24 +000067
Benoit Jacobfc7b2b52007-12-12 17:48:20 +000068 // always test operator() on each read-only expression class,
69 // in order to check const-qualifiers.
70 // indeed, if an expression class (here Zero) is meant to be read-only,
71 // hence has no _write() method, the corresponding MatrixBase method (here zero())
72 // should return a const-qualified object so that it is the const-qualified
73 // operator() that gets called, which in turn calls _read().
Benoit Jacob7c384752007-12-15 18:16:30 +000074 VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::zero(rows,cols)(r,c), static_cast<Scalar>(1));
Benoit Jacobfc7b2b52007-12-12 17:48:20 +000075
Benoit Jacobdad245a2007-12-25 17:20:58 +000076 // now test copying a row-vector into a (column-)vector and conversely.
77 square.col(r) = square.row(r).eval();
Benoit Jacob84934ea2008-01-06 16:35:21 +000078 Matrix<Scalar, 1, MatrixType::Traits::RowsAtCompileTime> rv(rows);
79 Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> cv(rows);
Benoit Jacobdad245a2007-12-25 17:20:58 +000080 rv = square.col(r);
81 cv = square.row(r);
82 VERIFY_IS_APPROX(rv, cv.transpose());
Benoit Jacobe445f502007-10-13 16:56:24 +000083}
84
85void EigenTest::testBasicStuff()
86{
Benoit Jacob2b208142007-12-11 10:07:18 +000087 for(int i = 0; i < m_repeat; i++) {
Benoit Jacob5abaaf92007-12-03 08:35:23 +000088 basicStuff(Matrix<float, 1, 1>());
Benoit Jacob04502cc2007-12-05 07:22:22 +000089 basicStuff(Matrix4d());
Benoit Jacob5abaaf92007-12-03 08:35:23 +000090 basicStuff(MatrixXcf(3, 3));
91 basicStuff(MatrixXi(8, 12));
Benoit Jacob04502cc2007-12-05 07:22:22 +000092 basicStuff(MatrixXcd(20, 20));
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