1 // This file is part of Eigen, a lightweight C++ template library
4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
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/.
12 template<typename MatrixType
> void linearStructure(const MatrixType
& m
)
15 /* this test covers the following files:
16 CwiseUnaryOp.h, CwiseBinaryOp.h, SelfCwiseBinaryOp.h
18 typedef typename
MatrixType::Index Index
;
19 typedef typename
MatrixType::Scalar Scalar
;
21 Index rows
= m
.rows();
22 Index cols
= m
.cols();
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
),
30 Scalar s1
= internal::random
<Scalar
>();
31 while (abs(s1
)<1e-3) s1
= internal::random
<Scalar
>();
33 Index r
= internal::random
<Index
>(0, rows
-1),
34 c
= internal::random
<Index
>(0, cols
-1);
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
);
44 VERIFY_IS_APPROX(m3
, m1
+m2
);
46 VERIFY_IS_APPROX(m3
, m2
-m1
);
48 VERIFY_IS_APPROX(m3
, s1
*m2
);
49 if(!NumTraits
<Scalar
>::IsInteger
)
52 VERIFY_IS_APPROX(m3
, m2
/s1
);
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
>::IsInteger
)
62 VERIFY_IS_APPROX((m1
/s1
)(r
,c
), (m1(r
,c
))/s1
);
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
.cwiseProduct(m1
.block(0,0,rows
,cols
)), m1
.cwiseProduct(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
);
71 void test_linearstructure()
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(internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
/2), internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
/2))) );
79 CALL_SUBTEST_6( linearStructure(MatrixXf (internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
), internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
))) );
80 CALL_SUBTEST_7( linearStructure(MatrixXi (internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
), internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
))) );
81 CALL_SUBTEST_8( linearStructure(MatrixXcd(internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
/2), internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
/2))) );
82 CALL_SUBTEST_9( linearStructure(ArrayXXf (internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
), internal::random
<int>(1,EIGEN_TEST_MAX_SIZE
))) );