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) 20013 Gael Guennebaud <gael.guennebaud@inria.fr> |
| 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 | // This unit test cannot be easily written to work with EIGEN_DEFAULT_TO_ROW_MAJOR |
| 11 | #ifdef EIGEN_DEFAULT_TO_ROW_MAJOR |
| 12 | #undef EIGEN_DEFAULT_TO_ROW_MAJOR |
| 13 | #endif |
| 14 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 15 | #define TEST_ENABLE_TEMPORARY_TRACKING |
| 16 | #define TEST_CHECK_STATIC_ASSERTIONS |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 17 | #include "main.h" |
| 18 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 19 | // test Ref.h |
| 20 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 21 | // Deal with i387 extended precision |
| 22 | #if EIGEN_ARCH_i386 && !(EIGEN_ARCH_x86_64) |
| 23 | |
| 24 | #if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_AT_LEAST(4,4) |
| 25 | #pragma GCC optimize ("-ffloat-store") |
| 26 | #else |
| 27 | #undef VERIFY_IS_EQUAL |
| 28 | #define VERIFY_IS_EQUAL(X,Y) VERIFY_IS_APPROX(X,Y) |
| 29 | #endif |
| 30 | |
| 31 | #endif |
| 32 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 33 | template<typename MatrixType> void ref_matrix(const MatrixType& m) |
| 34 | { |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 35 | typedef typename MatrixType::Scalar Scalar; |
| 36 | typedef typename MatrixType::RealScalar RealScalar; |
| 37 | typedef Matrix<Scalar,Dynamic,Dynamic,MatrixType::Options> DynMatrixType; |
| 38 | typedef Matrix<RealScalar,Dynamic,Dynamic,MatrixType::Options> RealDynMatrixType; |
| 39 | |
| 40 | typedef Ref<MatrixType> RefMat; |
| 41 | typedef Ref<DynMatrixType> RefDynMat; |
| 42 | typedef Ref<const DynMatrixType> ConstRefDynMat; |
| 43 | typedef Ref<RealDynMatrixType , 0, Stride<Dynamic,Dynamic> > RefRealMatWithStride; |
| 44 | |
| 45 | Index rows = m.rows(), cols = m.cols(); |
| 46 | |
| 47 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 48 | m2 = m1; |
| 49 | |
| 50 | Index i = internal::random<Index>(0,rows-1); |
| 51 | Index j = internal::random<Index>(0,cols-1); |
| 52 | Index brows = internal::random<Index>(1,rows-i); |
| 53 | Index bcols = internal::random<Index>(1,cols-j); |
| 54 | |
| 55 | RefMat rm0 = m1; |
| 56 | VERIFY_IS_EQUAL(rm0, m1); |
| 57 | RefDynMat rm1 = m1; |
| 58 | VERIFY_IS_EQUAL(rm1, m1); |
| 59 | RefDynMat rm2 = m1.block(i,j,brows,bcols); |
| 60 | VERIFY_IS_EQUAL(rm2, m1.block(i,j,brows,bcols)); |
| 61 | rm2.setOnes(); |
| 62 | m2.block(i,j,brows,bcols).setOnes(); |
| 63 | VERIFY_IS_EQUAL(m1, m2); |
| 64 | |
| 65 | m2.block(i,j,brows,bcols).setRandom(); |
| 66 | rm2 = m2.block(i,j,brows,bcols); |
| 67 | VERIFY_IS_EQUAL(m1, m2); |
| 68 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 69 | ConstRefDynMat rm3 = m1.block(i,j,brows,bcols); |
| 70 | m1.block(i,j,brows,bcols) *= 2; |
| 71 | m2.block(i,j,brows,bcols) *= 2; |
| 72 | VERIFY_IS_EQUAL(rm3, m2.block(i,j,brows,bcols)); |
| 73 | RefRealMatWithStride rm4 = m1.real(); |
| 74 | VERIFY_IS_EQUAL(rm4, m2.real()); |
| 75 | rm4.array() += 1; |
| 76 | m2.real().array() += 1; |
| 77 | VERIFY_IS_EQUAL(m1, m2); |
| 78 | } |
| 79 | |
| 80 | template<typename VectorType> void ref_vector(const VectorType& m) |
| 81 | { |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 82 | typedef typename VectorType::Scalar Scalar; |
| 83 | typedef typename VectorType::RealScalar RealScalar; |
| 84 | typedef Matrix<Scalar,Dynamic,1,VectorType::Options> DynMatrixType; |
| 85 | typedef Matrix<Scalar,Dynamic,Dynamic,ColMajor> MatrixType; |
| 86 | typedef Matrix<RealScalar,Dynamic,1,VectorType::Options> RealDynMatrixType; |
| 87 | |
| 88 | typedef Ref<VectorType> RefMat; |
| 89 | typedef Ref<DynMatrixType> RefDynMat; |
| 90 | typedef Ref<const DynMatrixType> ConstRefDynMat; |
| 91 | typedef Ref<RealDynMatrixType , 0, InnerStride<> > RefRealMatWithStride; |
| 92 | typedef Ref<DynMatrixType , 0, InnerStride<> > RefMatWithStride; |
| 93 | |
| 94 | Index size = m.size(); |
| 95 | |
| 96 | VectorType v1 = VectorType::Random(size), |
| 97 | v2 = v1; |
| 98 | MatrixType mat1 = MatrixType::Random(size,size), |
| 99 | mat2 = mat1, |
| 100 | mat3 = MatrixType::Random(size,size); |
| 101 | |
| 102 | Index i = internal::random<Index>(0,size-1); |
| 103 | Index bsize = internal::random<Index>(1,size-i); |
| 104 | |
| 105 | RefMat rm0 = v1; |
| 106 | VERIFY_IS_EQUAL(rm0, v1); |
| 107 | RefDynMat rv1 = v1; |
| 108 | VERIFY_IS_EQUAL(rv1, v1); |
| 109 | RefDynMat rv2 = v1.segment(i,bsize); |
| 110 | VERIFY_IS_EQUAL(rv2, v1.segment(i,bsize)); |
| 111 | rv2.setOnes(); |
| 112 | v2.segment(i,bsize).setOnes(); |
| 113 | VERIFY_IS_EQUAL(v1, v2); |
| 114 | |
| 115 | v2.segment(i,bsize).setRandom(); |
| 116 | rv2 = v2.segment(i,bsize); |
| 117 | VERIFY_IS_EQUAL(v1, v2); |
| 118 | |
| 119 | ConstRefDynMat rm3 = v1.segment(i,bsize); |
| 120 | v1.segment(i,bsize) *= 2; |
| 121 | v2.segment(i,bsize) *= 2; |
| 122 | VERIFY_IS_EQUAL(rm3, v2.segment(i,bsize)); |
| 123 | |
| 124 | RefRealMatWithStride rm4 = v1.real(); |
| 125 | VERIFY_IS_EQUAL(rm4, v2.real()); |
| 126 | rm4.array() += 1; |
| 127 | v2.real().array() += 1; |
| 128 | VERIFY_IS_EQUAL(v1, v2); |
| 129 | |
| 130 | RefMatWithStride rm5 = mat1.row(i).transpose(); |
| 131 | VERIFY_IS_EQUAL(rm5, mat1.row(i).transpose()); |
| 132 | rm5.array() += 1; |
| 133 | mat2.row(i).array() += 1; |
| 134 | VERIFY_IS_EQUAL(mat1, mat2); |
| 135 | rm5.noalias() = rm4.transpose() * mat3; |
| 136 | mat2.row(i) = v2.real().transpose() * mat3; |
| 137 | VERIFY_IS_APPROX(mat1, mat2); |
| 138 | } |
| 139 | |
| 140 | template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&) |
| 141 | { |
| 142 | // verify that ref-to-const don't have LvalueBit |
| 143 | typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType; |
| 144 | VERIFY( !(internal::traits<Ref<ConstPlainObjectType> >::Flags & LvalueBit) ); |
| 145 | VERIFY( !(internal::traits<Ref<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) ); |
| 146 | VERIFY( !(Ref<ConstPlainObjectType>::Flags & LvalueBit) ); |
| 147 | VERIFY( !(Ref<ConstPlainObjectType, Aligned>::Flags & LvalueBit) ); |
| 148 | } |
| 149 | |
| 150 | template<typename B> |
| 151 | EIGEN_DONT_INLINE void call_ref_1(Ref<VectorXf> a, const B &b) { VERIFY_IS_EQUAL(a,b); } |
| 152 | template<typename B> |
| 153 | EIGEN_DONT_INLINE void call_ref_2(const Ref<const VectorXf>& a, const B &b) { VERIFY_IS_EQUAL(a,b); } |
| 154 | template<typename B> |
| 155 | EIGEN_DONT_INLINE void call_ref_3(Ref<VectorXf,0,InnerStride<> > a, const B &b) { VERIFY_IS_EQUAL(a,b); } |
| 156 | template<typename B> |
| 157 | EIGEN_DONT_INLINE void call_ref_4(const Ref<const VectorXf,0,InnerStride<> >& a, const B &b) { VERIFY_IS_EQUAL(a,b); } |
| 158 | template<typename B> |
| 159 | EIGEN_DONT_INLINE void call_ref_5(Ref<MatrixXf,0,OuterStride<> > a, const B &b) { VERIFY_IS_EQUAL(a,b); } |
| 160 | template<typename B> |
| 161 | EIGEN_DONT_INLINE void call_ref_6(const Ref<const MatrixXf,0,OuterStride<> >& a, const B &b) { VERIFY_IS_EQUAL(a,b); } |
| 162 | template<typename B> |
| 163 | EIGEN_DONT_INLINE void call_ref_7(Ref<Matrix<float,Dynamic,3> > a, const B &b) { VERIFY_IS_EQUAL(a,b); } |
| 164 | |
| 165 | void call_ref() |
| 166 | { |
| 167 | VectorXcf ca = VectorXcf::Random(10); |
| 168 | VectorXf a = VectorXf::Random(10); |
| 169 | RowVectorXf b = RowVectorXf::Random(10); |
| 170 | MatrixXf A = MatrixXf::Random(10,10); |
| 171 | RowVector3f c = RowVector3f::Random(); |
| 172 | const VectorXf& ac(a); |
| 173 | VectorBlock<VectorXf> ab(a,0,3); |
| 174 | const VectorBlock<VectorXf> abc(a,0,3); |
| 175 | |
| 176 | |
| 177 | VERIFY_EVALUATION_COUNT( call_ref_1(a,a), 0); |
| 178 | VERIFY_EVALUATION_COUNT( call_ref_1(b,b.transpose()), 0); |
| 179 | // call_ref_1(ac,a<c); // does not compile because ac is const |
| 180 | VERIFY_EVALUATION_COUNT( call_ref_1(ab,ab), 0); |
| 181 | VERIFY_EVALUATION_COUNT( call_ref_1(a.head(4),a.head(4)), 0); |
| 182 | VERIFY_EVALUATION_COUNT( call_ref_1(abc,abc), 0); |
| 183 | VERIFY_EVALUATION_COUNT( call_ref_1(A.col(3),A.col(3)), 0); |
| 184 | // call_ref_1(A.row(3),A.row(3)); // does not compile because innerstride!=1 |
| 185 | VERIFY_EVALUATION_COUNT( call_ref_3(A.row(3),A.row(3).transpose()), 0); |
| 186 | VERIFY_EVALUATION_COUNT( call_ref_4(A.row(3),A.row(3).transpose()), 0); |
| 187 | // call_ref_1(a+a, a+a); // does not compile for obvious reason |
| 188 | |
| 189 | MatrixXf tmp = A*A.col(1); |
| 190 | VERIFY_EVALUATION_COUNT( call_ref_2(A*A.col(1), tmp), 1); // evaluated into a temp |
| 191 | VERIFY_EVALUATION_COUNT( call_ref_2(ac.head(5),ac.head(5)), 0); |
| 192 | VERIFY_EVALUATION_COUNT( call_ref_2(ac,ac), 0); |
| 193 | VERIFY_EVALUATION_COUNT( call_ref_2(a,a), 0); |
| 194 | VERIFY_EVALUATION_COUNT( call_ref_2(ab,ab), 0); |
| 195 | VERIFY_EVALUATION_COUNT( call_ref_2(a.head(4),a.head(4)), 0); |
| 196 | tmp = a+a; |
| 197 | VERIFY_EVALUATION_COUNT( call_ref_2(a+a,tmp), 1); // evaluated into a temp |
| 198 | VERIFY_EVALUATION_COUNT( call_ref_2(ca.imag(),ca.imag()), 1); // evaluated into a temp |
| 199 | |
| 200 | VERIFY_EVALUATION_COUNT( call_ref_4(ac.head(5),ac.head(5)), 0); |
| 201 | tmp = a+a; |
| 202 | VERIFY_EVALUATION_COUNT( call_ref_4(a+a,tmp), 1); // evaluated into a temp |
| 203 | VERIFY_EVALUATION_COUNT( call_ref_4(ca.imag(),ca.imag()), 0); |
| 204 | |
| 205 | VERIFY_EVALUATION_COUNT( call_ref_5(a,a), 0); |
| 206 | VERIFY_EVALUATION_COUNT( call_ref_5(a.head(3),a.head(3)), 0); |
| 207 | VERIFY_EVALUATION_COUNT( call_ref_5(A,A), 0); |
| 208 | // call_ref_5(A.transpose(),A.transpose()); // does not compile because storage order does not match |
| 209 | VERIFY_EVALUATION_COUNT( call_ref_5(A.block(1,1,2,2),A.block(1,1,2,2)), 0); |
| 210 | VERIFY_EVALUATION_COUNT( call_ref_5(b,b), 0); // storage order do not match, but this is a degenerate case that should work |
| 211 | VERIFY_EVALUATION_COUNT( call_ref_5(a.row(3),a.row(3)), 0); |
| 212 | |
| 213 | VERIFY_EVALUATION_COUNT( call_ref_6(a,a), 0); |
| 214 | VERIFY_EVALUATION_COUNT( call_ref_6(a.head(3),a.head(3)), 0); |
| 215 | VERIFY_EVALUATION_COUNT( call_ref_6(A.row(3),A.row(3)), 1); // evaluated into a temp thouth it could be avoided by viewing it as a 1xn matrix |
| 216 | tmp = A+A; |
| 217 | VERIFY_EVALUATION_COUNT( call_ref_6(A+A,tmp), 1); // evaluated into a temp |
| 218 | VERIFY_EVALUATION_COUNT( call_ref_6(A,A), 0); |
| 219 | VERIFY_EVALUATION_COUNT( call_ref_6(A.transpose(),A.transpose()), 1); // evaluated into a temp because the storage orders do not match |
| 220 | VERIFY_EVALUATION_COUNT( call_ref_6(A.block(1,1,2,2),A.block(1,1,2,2)), 0); |
| 221 | |
| 222 | VERIFY_EVALUATION_COUNT( call_ref_7(c,c), 0); |
| 223 | } |
| 224 | |
| 225 | typedef Matrix<double,Dynamic,Dynamic,RowMajor> RowMatrixXd; |
| 226 | int test_ref_overload_fun1(Ref<MatrixXd> ) { return 1; } |
| 227 | int test_ref_overload_fun1(Ref<RowMatrixXd> ) { return 2; } |
| 228 | int test_ref_overload_fun1(Ref<MatrixXf> ) { return 3; } |
| 229 | |
| 230 | int test_ref_overload_fun2(Ref<const MatrixXd> ) { return 4; } |
| 231 | int test_ref_overload_fun2(Ref<const MatrixXf> ) { return 5; } |
| 232 | |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 233 | void test_ref_ambiguous(const Ref<const ArrayXd> &A, Ref<ArrayXd> B) |
| 234 | { |
| 235 | B = A; |
| 236 | B = A - A; |
| 237 | } |
| 238 | |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 239 | // See also bug 969 |
| 240 | void test_ref_overloads() |
| 241 | { |
| 242 | MatrixXd Ad, Bd; |
| 243 | RowMatrixXd rAd, rBd; |
| 244 | VERIFY( test_ref_overload_fun1(Ad)==1 ); |
| 245 | VERIFY( test_ref_overload_fun1(rAd)==2 ); |
| 246 | |
| 247 | MatrixXf Af, Bf; |
| 248 | VERIFY( test_ref_overload_fun2(Ad)==4 ); |
| 249 | VERIFY( test_ref_overload_fun2(Ad+Bd)==4 ); |
| 250 | VERIFY( test_ref_overload_fun2(Af+Bf)==5 ); |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 251 | |
| 252 | ArrayXd A, B; |
| 253 | test_ref_ambiguous(A, B); |
| 254 | } |
| 255 | |
| 256 | void test_ref_fixed_size_assert() |
| 257 | { |
| 258 | Vector4f v4; |
| 259 | VectorXf vx(10); |
| 260 | VERIFY_RAISES_STATIC_ASSERT( Ref<Vector3f> y = v4; (void)y; ); |
| 261 | VERIFY_RAISES_STATIC_ASSERT( Ref<Vector3f> y = vx.head<4>(); (void)y; ); |
| 262 | VERIFY_RAISES_STATIC_ASSERT( Ref<const Vector3f> y = v4; (void)y; ); |
| 263 | VERIFY_RAISES_STATIC_ASSERT( Ref<const Vector3f> y = vx.head<4>(); (void)y; ); |
| 264 | VERIFY_RAISES_STATIC_ASSERT( Ref<const Vector3f> y = 2*v4; (void)y; ); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void test_ref() |
| 268 | { |
| 269 | for(int i = 0; i < g_repeat; i++) { |
| 270 | CALL_SUBTEST_1( ref_vector(Matrix<float, 1, 1>()) ); |
| 271 | CALL_SUBTEST_1( check_const_correctness(Matrix<float, 1, 1>()) ); |
| 272 | CALL_SUBTEST_2( ref_vector(Vector4d()) ); |
| 273 | CALL_SUBTEST_2( check_const_correctness(Matrix4d()) ); |
| 274 | CALL_SUBTEST_3( ref_vector(Vector4cf()) ); |
| 275 | CALL_SUBTEST_4( ref_vector(VectorXcf(8)) ); |
| 276 | CALL_SUBTEST_5( ref_vector(VectorXi(12)) ); |
| 277 | CALL_SUBTEST_5( check_const_correctness(VectorXi(12)) ); |
| 278 | |
| 279 | CALL_SUBTEST_1( ref_matrix(Matrix<float, 1, 1>()) ); |
| 280 | CALL_SUBTEST_2( ref_matrix(Matrix4d()) ); |
| 281 | CALL_SUBTEST_1( ref_matrix(Matrix<float,3,5>()) ); |
| 282 | CALL_SUBTEST_4( ref_matrix(MatrixXcf(internal::random<int>(1,10),internal::random<int>(1,10))) ); |
| 283 | CALL_SUBTEST_4( ref_matrix(Matrix<std::complex<double>,10,15>()) ); |
| 284 | CALL_SUBTEST_5( ref_matrix(MatrixXi(internal::random<int>(1,10),internal::random<int>(1,10))) ); |
| 285 | CALL_SUBTEST_6( call_ref() ); |
| 286 | } |
| 287 | |
| 288 | CALL_SUBTEST_7( test_ref_overloads() ); |
Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 289 | CALL_SUBTEST_7( test_ref_fixed_size_assert() ); |
Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame] | 290 | } |