Home | History | Annotate | Download | only in test
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      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      CwiseUnaryOp.h, CwiseBinaryOp.h, SelfCwiseBinaryOp.h
     16   */
     17   typedef typename MatrixType::Index Index;
     18   typedef typename MatrixType::Scalar Scalar;
     19 
     20   Index rows = m.rows();
     21   Index cols = m.cols();
     22 
     23   // this test relies a lot on Random.h, and there's not much more that we can do
     24   // to test it, hence I consider that we will have tested Random.h
     25   MatrixType m1 = MatrixType::Random(rows, cols),
     26              m2 = MatrixType::Random(rows, cols),
     27              m3(rows, cols);
     28 
     29   Scalar s1 = internal::random<Scalar>();
     30   while (internal::abs(s1)<1e-3) s1 = internal::random<Scalar>();
     31 
     32   Index r = internal::random<Index>(0, rows-1),
     33         c = internal::random<Index>(0, cols-1);
     34 
     35   VERIFY_IS_APPROX(-(-m1),                  m1);
     36   VERIFY_IS_APPROX(m1+m1,                   2*m1);
     37   VERIFY_IS_APPROX(m1+m2-m1,                m2);
     38   VERIFY_IS_APPROX(-m2+m1+m2,               m1);
     39   VERIFY_IS_APPROX(m1*s1,                   s1*m1);
     40   VERIFY_IS_APPROX((m1+m2)*s1,              s1*m1+s1*m2);
     41   VERIFY_IS_APPROX((-m1+m2)*s1,             -s1*m1+s1*m2);
     42   m3 = m2; m3 += m1;
     43   VERIFY_IS_APPROX(m3,                      m1+m2);
     44   m3 = m2; m3 -= m1;
     45   VERIFY_IS_APPROX(m3,                      m2-m1);
     46   m3 = m2; m3 *= s1;
     47   VERIFY_IS_APPROX(m3,                      s1*m2);
     48   if(!NumTraits<Scalar>::IsInteger)
     49   {
     50     m3 = m2; m3 /= s1;
     51     VERIFY_IS_APPROX(m3,                    m2/s1);
     52   }
     53 
     54   // again, test operator() to check const-qualification
     55   VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
     56   VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
     57   VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
     58   VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
     59   VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
     60   if(!NumTraits<Scalar>::IsInteger)
     61     VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
     62 
     63   // use .block to disable vectorization and compare to the vectorized version
     64   VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1);
     65   VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), m1.cwiseProduct(m1));
     66   VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1);
     67   VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1);
     68 }
     69 
     70 void test_linearstructure()
     71 {
     72   for(int i = 0; i < g_repeat; i++) {
     73     CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) );
     74     CALL_SUBTEST_2( linearStructure(Matrix2f()) );
     75     CALL_SUBTEST_3( linearStructure(Vector3d()) );
     76     CALL_SUBTEST_4( linearStructure(Matrix4d()) );
     77     CALL_SUBTEST_5( linearStructure(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
     78     CALL_SUBTEST_6( linearStructure(MatrixXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
     79     CALL_SUBTEST_7( linearStructure(MatrixXi (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
     80     CALL_SUBTEST_8( linearStructure(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
     81     CALL_SUBTEST_9( linearStructure(ArrayXXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
     82   }
     83 }
     84