• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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@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 
linearStructure(const MatrixType & m)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              mzero = MatrixType::Zero(rows, cols);
30 
31   Scalar s1 = ei_random<Scalar>();
32   while (ei_abs(s1)<1e-3) s1 = ei_random<Scalar>();
33 
34   int r = ei_random<int>(0, rows-1),
35       c = ei_random<int>(0, cols-1);
36 
37   VERIFY_IS_APPROX(-(-m1),                  m1);
38   VERIFY_IS_APPROX(m1+m1,                   2*m1);
39   VERIFY_IS_APPROX(m1+m2-m1,                m2);
40   VERIFY_IS_APPROX(-m2+m1+m2,               m1);
41   VERIFY_IS_APPROX(m1*s1,                   s1*m1);
42   VERIFY_IS_APPROX((m1+m2)*s1,              s1*m1+s1*m2);
43   VERIFY_IS_APPROX((-m1+m2)*s1,             -s1*m1+s1*m2);
44   m3 = m2; m3 += m1;
45   VERIFY_IS_APPROX(m3,                      m1+m2);
46   m3 = m2; m3 -= m1;
47   VERIFY_IS_APPROX(m3,                      m2-m1);
48   m3 = m2; m3 *= s1;
49   VERIFY_IS_APPROX(m3,                      s1*m2);
50   if(NumTraits<Scalar>::HasFloatingPoint)
51   {
52     m3 = m2; m3 /= s1;
53     VERIFY_IS_APPROX(m3,                    m2/s1);
54   }
55 
56   // again, test operator() to check const-qualification
57   VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c)));
58   VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c)));
59   VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
60   VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c)));
61   VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1);
62   if(NumTraits<Scalar>::HasFloatingPoint)
63     VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1);
64 
65   // use .block to disable vectorization and compare to the vectorized version
66   VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1);
67   VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
68   VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1);
69   VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1);
70 }
71 
test_eigen2_linearstructure()72 void test_eigen2_linearstructure()
73 {
74   for(int i = 0; i < g_repeat; i++) {
75     CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) );
76     CALL_SUBTEST_2( linearStructure(Matrix2f()) );
77     CALL_SUBTEST_3( linearStructure(Vector3d()) );
78     CALL_SUBTEST_4( linearStructure(Matrix4d()) );
79     CALL_SUBTEST_5( linearStructure(MatrixXcf(3, 3)) );
80     CALL_SUBTEST_6( linearStructure(MatrixXf(8, 12)) );
81     CALL_SUBTEST_7( linearStructure(MatrixXi(8, 12)) );
82     CALL_SUBTEST_8( linearStructure(MatrixXcd(20, 20)) );
83   }
84 }
85