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 linearStructure(const MatrixType& m)
     13 {
     14   /* this test covers the following files:
     15      Sum.h Difference.h Opposite.h ScalarMultiple.h
     16   */
     17 
     18   typedef typename MatrixType::Scalar Scalar;
     19   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
     20 
     21   int rows = m.rows();
     22   int cols = m.cols();
     23 
     24   // this test relies a lot on Random.h, and there's not much more that we can do
     25   // to test it, hence I consider that we will have tested Random.h
     26   MatrixType m1 = MatrixType::Random(rows, cols),
     27              m2 = MatrixType::Random(rows, cols),
     28              m3(rows, cols);
     29 
     30   Scalar s1 = ei_random<Scalar>();
     31   while (ei_abs(s1)<1e-3) s1 = ei_random<Scalar>();
     32 
     33   int r = ei_random<int>(0, rows-1),
     34       c = ei_random<int>(0, cols-1);
     35 
     36   VERIFY_IS_APPROX(-(-m1),                  m1);
     37   VERIFY_IS_APPROX(m1+m1,                   2*m1);
     38   VERIFY_IS_APPROX(m1+m2-m1,                m2);
     39   VERIFY_IS_APPROX(-m2+m1+m2,               m1);
     40   VERIFY_IS_APPROX(m1*s1,                   s1*m1);
     41   VERIFY_IS_APPROX((m1+m2)*s1,              s1*m1+s1*m2);
     42   VERIFY_IS_APPROX((-m1+m2)*s1,             -s1*m1+s1*m2);
     43   m3 = m2; m3 += m1;
     44   VERIFY_IS_APPROX(m3,                      m1+m2);
     45   m3 = m2; m3 -= m1;
     46   VERIFY_IS_APPROX(m3,                      m2-m1);
     47   m3 = m2; m3 *= s1;
     48   VERIFY_IS_APPROX(m3,                      s1*m2);
     49   if(NumTraits<Scalar>::HasFloatingPoint)
     50   {
     51     m3 = m2; m3 /= s1;
     52     VERIFY_IS_APPROX(m3,                    m2/s1);
     53   }
     54 
     55   // again, test operator() to check const-qualification
     56   VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
     57   VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
     58   VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
     59   VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
     60   VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
     61   if(NumTraits<Scalar>::HasFloatingPoint)
     62     VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
     63 
     64   // use .block to disable vectorization and compare to the vectorized version
     65   VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1);
     66   VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
     67   VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1);
     68   VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1);
     69 }
     70 
     71 void test_eigen2_linearstructure()
     72 {
     73   for(int i = 0; i < g_repeat; i++) {
     74     CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) );
     75     CALL_SUBTEST_2( linearStructure(Matrix2f()) );
     76     CALL_SUBTEST_3( linearStructure(Vector3d()) );
     77     CALL_SUBTEST_4( linearStructure(Matrix4d()) );
     78     CALL_SUBTEST_5( linearStructure(MatrixXcf(3, 3)) );
     79     CALL_SUBTEST_6( linearStructure(MatrixXf(8, 12)) );
     80     CALL_SUBTEST_7( linearStructure(MatrixXi(8, 12)) );
     81     CALL_SUBTEST_8( linearStructure(MatrixXcd(20, 20)) );
     82   }
     83 }
     84