Home | History | Annotate | Download | only in eigen2
      1 // 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-2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #include "main.h"
     11 
     12 template<typename MatrixType> void basicStuff(const MatrixType& m)
     13 {
     14   typedef typename MatrixType::Scalar Scalar;
     15   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
     16 
     17   int rows = m.rows();
     18   int cols = m.cols();
     19 
     20   // this test relies a lot on Random.h, and there's not much more that we can do
     21   // to test it, hence I consider that we will have tested Random.h
     22   MatrixType m1 = MatrixType::Random(rows, cols),
     23              m2 = MatrixType::Random(rows, cols),
     24              m3(rows, cols),
     25              mzero = MatrixType::Zero(rows, cols),
     26              identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
     27                               ::Identity(rows, rows),
     28              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>::Random(rows, rows);
     29   VectorType v1 = VectorType::Random(rows),
     30              v2 = VectorType::Random(rows),
     31              vzero = VectorType::Zero(rows);
     32 
     33   Scalar x = ei_random<Scalar>();
     34 
     35   int r = ei_random<int>(0, rows-1),
     36       c = ei_random<int>(0, cols-1);
     37 
     38   m1.coeffRef(r,c) = x;
     39   VERIFY_IS_APPROX(x, m1.coeff(r,c));
     40   m1(r,c) = x;
     41   VERIFY_IS_APPROX(x, m1(r,c));
     42   v1.coeffRef(r) = x;
     43   VERIFY_IS_APPROX(x, v1.coeff(r));
     44   v1(r) = x;
     45   VERIFY_IS_APPROX(x, v1(r));
     46   v1[r] = x;
     47   VERIFY_IS_APPROX(x, v1[r]);
     48 
     49   VERIFY_IS_APPROX(               v1,    v1);
     50   VERIFY_IS_NOT_APPROX(           v1,    2*v1);
     51   VERIFY_IS_MUCH_SMALLER_THAN(    vzero, v1);
     52   if(NumTraits<Scalar>::HasFloatingPoint)
     53     VERIFY_IS_MUCH_SMALLER_THAN(  vzero, v1.norm());
     54   VERIFY_IS_NOT_MUCH_SMALLER_THAN(v1,    v1);
     55   VERIFY_IS_APPROX(               vzero, v1-v1);
     56   VERIFY_IS_APPROX(               m1,    m1);
     57   VERIFY_IS_NOT_APPROX(           m1,    2*m1);
     58   VERIFY_IS_MUCH_SMALLER_THAN(    mzero, m1);
     59   VERIFY_IS_NOT_MUCH_SMALLER_THAN(m1,    m1);
     60   VERIFY_IS_APPROX(               mzero, m1-m1);
     61 
     62   // always test operator() on each read-only expression class,
     63   // in order to check const-qualifiers.
     64   // indeed, if an expression class (here Zero) is meant to be read-only,
     65   // hence has no _write() method, the corresponding MatrixBase method (here zero())
     66   // should return a const-qualified object so that it is the const-qualified
     67   // operator() that gets called, which in turn calls _read().
     68   VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
     69 
     70   // now test copying a row-vector into a (column-)vector and conversely.
     71   square.col(r) = square.row(r).eval();
     72   Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> rv(rows);
     73   Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> cv(rows);
     74   rv = square.row(r);
     75   cv = square.col(r);
     76   VERIFY_IS_APPROX(rv, cv.transpose());
     77 
     78   if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
     79   {
     80     VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
     81   }
     82 
     83   VERIFY_IS_APPROX(m3 = m1,m1);
     84   MatrixType m4;
     85   VERIFY_IS_APPROX(m4 = m1,m1);
     86 
     87   // test swap
     88   m3 = m1;
     89   m1.swap(m2);
     90   VERIFY_IS_APPROX(m3, m2);
     91   if(rows*cols>=3)
     92   {
     93     VERIFY_IS_NOT_APPROX(m3, m1);
     94   }
     95 }
     96 
     97 void test_eigen2_basicstuff()
     98 {
     99   for(int i = 0; i < g_repeat; i++) {
    100     CALL_SUBTEST_1( basicStuff(Matrix<float, 1, 1>()) );
    101     CALL_SUBTEST_2( basicStuff(Matrix4d()) );
    102     CALL_SUBTEST_3( basicStuff(MatrixXcf(3, 3)) );
    103     CALL_SUBTEST_4( basicStuff(MatrixXi(8, 12)) );
    104     CALL_SUBTEST_5( basicStuff(MatrixXcd(20, 20)) );
    105     CALL_SUBTEST_6( basicStuff(Matrix<float, 100, 100>()) );
    106     CALL_SUBTEST_7( basicStuff(Matrix<long double,Dynamic,Dynamic>(10,10)) );
    107   }
    108 }
    109