Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 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@gmail.com> |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 5 | // Copyright (C) 2014 Gael Guennebaud <gael.guennebaud@inria.fr> |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 6 | // |
| 7 | // This Source Code Form is subject to the terms of the Mozilla |
| 8 | // Public License v. 2.0. If a copy of the MPL was not distributed |
| 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 10 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 11 | static bool g_called; |
| 12 | #define EIGEN_SCALAR_BINARY_OP_PLUGIN { g_called |= (!internal::is_same<LhsScalar,RhsScalar>::value); } |
| 13 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 14 | #include "main.h" |
| 15 | |
| 16 | template<typename MatrixType> void linearStructure(const MatrixType& m) |
| 17 | { |
| 18 | using std::abs; |
| 19 | /* this test covers the following files: |
| 20 | CwiseUnaryOp.h, CwiseBinaryOp.h, SelfCwiseBinaryOp.h |
| 21 | */ |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 22 | typedef typename MatrixType::Scalar Scalar; |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 23 | typedef typename MatrixType::RealScalar RealScalar; |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 24 | |
| 25 | Index rows = m.rows(); |
| 26 | Index cols = m.cols(); |
| 27 | |
| 28 | // this test relies a lot on Random.h, and there's not much more that we can do |
| 29 | // to test it, hence I consider that we will have tested Random.h |
| 30 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 31 | m2 = MatrixType::Random(rows, cols), |
| 32 | m3(rows, cols); |
| 33 | |
| 34 | Scalar s1 = internal::random<Scalar>(); |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 35 | while (abs(s1)<RealScalar(1e-3)) s1 = internal::random<Scalar>(); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 36 | |
| 37 | Index r = internal::random<Index>(0, rows-1), |
| 38 | c = internal::random<Index>(0, cols-1); |
| 39 | |
| 40 | VERIFY_IS_APPROX(-(-m1), m1); |
| 41 | VERIFY_IS_APPROX(m1+m1, 2*m1); |
| 42 | VERIFY_IS_APPROX(m1+m2-m1, m2); |
| 43 | VERIFY_IS_APPROX(-m2+m1+m2, m1); |
| 44 | VERIFY_IS_APPROX(m1*s1, s1*m1); |
| 45 | VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2); |
| 46 | VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2); |
| 47 | m3 = m2; m3 += m1; |
| 48 | VERIFY_IS_APPROX(m3, m1+m2); |
| 49 | m3 = m2; m3 -= m1; |
| 50 | VERIFY_IS_APPROX(m3, m2-m1); |
| 51 | m3 = m2; m3 *= s1; |
| 52 | VERIFY_IS_APPROX(m3, s1*m2); |
| 53 | if(!NumTraits<Scalar>::IsInteger) |
| 54 | { |
| 55 | m3 = m2; m3 /= s1; |
| 56 | VERIFY_IS_APPROX(m3, m2/s1); |
| 57 | } |
| 58 | |
| 59 | // again, test operator() to check const-qualification |
| 60 | VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c))); |
| 61 | VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c))); |
| 62 | VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c))); |
| 63 | VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c))); |
| 64 | VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1); |
| 65 | if(!NumTraits<Scalar>::IsInteger) |
| 66 | VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1); |
| 67 | |
| 68 | // use .block to disable vectorization and compare to the vectorized version |
| 69 | VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1); |
| 70 | VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), m1.cwiseProduct(m1)); |
| 71 | VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1); |
| 72 | VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1); |
| 73 | } |
| 74 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 75 | // Make sure that complex * real and real * complex are properly optimized |
| 76 | template<typename MatrixType> void real_complex(DenseIndex rows = MatrixType::RowsAtCompileTime, DenseIndex cols = MatrixType::ColsAtCompileTime) |
| 77 | { |
| 78 | typedef typename MatrixType::Scalar Scalar; |
| 79 | typedef typename MatrixType::RealScalar RealScalar; |
| 80 | |
| 81 | RealScalar s = internal::random<RealScalar>(); |
| 82 | MatrixType m1 = MatrixType::Random(rows, cols); |
| 83 | |
| 84 | g_called = false; |
| 85 | VERIFY_IS_APPROX(s*m1, Scalar(s)*m1); |
| 86 | VERIFY(g_called && "real * matrix<complex> not properly optimized"); |
| 87 | |
| 88 | g_called = false; |
| 89 | VERIFY_IS_APPROX(m1*s, m1*Scalar(s)); |
| 90 | VERIFY(g_called && "matrix<complex> * real not properly optimized"); |
| 91 | |
| 92 | g_called = false; |
| 93 | VERIFY_IS_APPROX(m1/s, m1/Scalar(s)); |
| 94 | VERIFY(g_called && "matrix<complex> / real not properly optimized"); |
| 95 | |
| 96 | g_called = false; |
| 97 | VERIFY_IS_APPROX(s+m1.array(), Scalar(s)+m1.array()); |
| 98 | VERIFY(g_called && "real + matrix<complex> not properly optimized"); |
| 99 | |
| 100 | g_called = false; |
| 101 | VERIFY_IS_APPROX(m1.array()+s, m1.array()+Scalar(s)); |
| 102 | VERIFY(g_called && "matrix<complex> + real not properly optimized"); |
| 103 | |
| 104 | g_called = false; |
| 105 | VERIFY_IS_APPROX(s-m1.array(), Scalar(s)-m1.array()); |
| 106 | VERIFY(g_called && "real - matrix<complex> not properly optimized"); |
| 107 | |
| 108 | g_called = false; |
| 109 | VERIFY_IS_APPROX(m1.array()-s, m1.array()-Scalar(s)); |
| 110 | VERIFY(g_called && "matrix<complex> - real not properly optimized"); |
| 111 | } |
| 112 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 113 | void test_linearstructure() |
| 114 | { |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 115 | g_called = true; |
| 116 | VERIFY(g_called); // avoid `unneeded-internal-declaration` warning. |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 117 | for(int i = 0; i < g_repeat; i++) { |
| 118 | CALL_SUBTEST_1( linearStructure(Matrix<float, 1, 1>()) ); |
| 119 | CALL_SUBTEST_2( linearStructure(Matrix2f()) ); |
| 120 | CALL_SUBTEST_3( linearStructure(Vector3d()) ); |
| 121 | CALL_SUBTEST_4( linearStructure(Matrix4d()) ); |
| 122 | CALL_SUBTEST_5( linearStructure(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) ); |
| 123 | CALL_SUBTEST_6( linearStructure(MatrixXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 124 | CALL_SUBTEST_7( linearStructure(MatrixXi (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 125 | CALL_SUBTEST_8( linearStructure(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) ); |
| 126 | CALL_SUBTEST_9( linearStructure(ArrayXXf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 127 | CALL_SUBTEST_10( linearStructure(ArrayXXcf (internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); |
| 128 | |
| 129 | CALL_SUBTEST_11( real_complex<Matrix4cd>() ); |
| 130 | CALL_SUBTEST_11( real_complex<MatrixXcf>(10,10) ); |
| 131 | CALL_SUBTEST_11( real_complex<ArrayXXcf>(10,10) ); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 132 | } |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 133 | |
| 134 | #ifdef EIGEN_TEST_PART_4 |
| 135 | { |
| 136 | // make sure that /=scalar and /scalar do not overflow |
| 137 | // rational: 1.0/4.94e-320 overflow, but m/4.94e-320 should not |
| 138 | Matrix4d m2, m3; |
| 139 | m3 = m2 = Matrix4d::Random()*1e-20; |
| 140 | m2 = m2 / 4.9e-320; |
| 141 | VERIFY_IS_APPROX(m2.cwiseQuotient(m2), Matrix4d::Ones()); |
| 142 | m3 /= 4.9e-320; |
| 143 | VERIFY_IS_APPROX(m3.cwiseQuotient(m3), Matrix4d::Ones()); |
| 144 | |
| 145 | |
| 146 | } |
| 147 | #endif |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 148 | } |