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