1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 20015 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li
10*bf2c3715SXin Li // This unit test cannot be easily written to work with EIGEN_DEFAULT_TO_ROW_MAJOR
11*bf2c3715SXin Li #ifdef EIGEN_DEFAULT_TO_ROW_MAJOR
12*bf2c3715SXin Li #undef EIGEN_DEFAULT_TO_ROW_MAJOR
13*bf2c3715SXin Li #endif
14*bf2c3715SXin Li
15*bf2c3715SXin Li static long int nb_temporaries;
16*bf2c3715SXin Li
on_temporary_creation()17*bf2c3715SXin Li inline void on_temporary_creation() {
18*bf2c3715SXin Li // here's a great place to set a breakpoint when debugging failures in this test!
19*bf2c3715SXin Li nb_temporaries++;
20*bf2c3715SXin Li }
21*bf2c3715SXin Li
22*bf2c3715SXin Li #define EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN { on_temporary_creation(); }
23*bf2c3715SXin Li
24*bf2c3715SXin Li #include "main.h"
25*bf2c3715SXin Li #include <Eigen/SparseCore>
26*bf2c3715SXin Li
27*bf2c3715SXin Li #define VERIFY_EVALUATION_COUNT(XPR,N) {\
28*bf2c3715SXin Li nb_temporaries = 0; \
29*bf2c3715SXin Li CALL_SUBTEST( XPR ); \
30*bf2c3715SXin Li if(nb_temporaries!=N) std::cerr << "nb_temporaries == " << nb_temporaries << "\n"; \
31*bf2c3715SXin Li VERIFY( (#XPR) && nb_temporaries==N ); \
32*bf2c3715SXin Li }
33*bf2c3715SXin Li
check_const_correctness(const PlainObjectType &)34*bf2c3715SXin Li template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
35*bf2c3715SXin Li {
36*bf2c3715SXin Li // verify that ref-to-const don't have LvalueBit
37*bf2c3715SXin Li typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
38*bf2c3715SXin Li VERIFY( !(internal::traits<Ref<ConstPlainObjectType> >::Flags & LvalueBit) );
39*bf2c3715SXin Li VERIFY( !(internal::traits<Ref<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) );
40*bf2c3715SXin Li VERIFY( !(Ref<ConstPlainObjectType>::Flags & LvalueBit) );
41*bf2c3715SXin Li VERIFY( !(Ref<ConstPlainObjectType, Aligned>::Flags & LvalueBit) );
42*bf2c3715SXin Li }
43*bf2c3715SXin Li
44*bf2c3715SXin Li template<typename B>
call_ref_1(Ref<SparseMatrix<float>> a,const B & b)45*bf2c3715SXin Li EIGEN_DONT_INLINE void call_ref_1(Ref<SparseMatrix<float> > a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
46*bf2c3715SXin Li
47*bf2c3715SXin Li template<typename B>
call_ref_2(const Ref<const SparseMatrix<float>> & a,const B & b)48*bf2c3715SXin Li EIGEN_DONT_INLINE void call_ref_2(const Ref<const SparseMatrix<float> >& a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
49*bf2c3715SXin Li
50*bf2c3715SXin Li template<typename B>
call_ref_3(const Ref<const SparseMatrix<float>,StandardCompressedFormat> & a,const B & b)51*bf2c3715SXin Li EIGEN_DONT_INLINE void call_ref_3(const Ref<const SparseMatrix<float>, StandardCompressedFormat>& a, const B &b) {
52*bf2c3715SXin Li VERIFY(a.isCompressed());
53*bf2c3715SXin Li VERIFY_IS_EQUAL(a.toDense(),b.toDense());
54*bf2c3715SXin Li }
55*bf2c3715SXin Li
56*bf2c3715SXin Li template<typename B>
call_ref_4(Ref<SparseVector<float>> a,const B & b)57*bf2c3715SXin Li EIGEN_DONT_INLINE void call_ref_4(Ref<SparseVector<float> > a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
58*bf2c3715SXin Li
59*bf2c3715SXin Li template<typename B>
call_ref_5(const Ref<const SparseVector<float>> & a,const B & b)60*bf2c3715SXin Li EIGEN_DONT_INLINE void call_ref_5(const Ref<const SparseVector<float> >& a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
61*bf2c3715SXin Li
call_ref()62*bf2c3715SXin Li void call_ref()
63*bf2c3715SXin Li {
64*bf2c3715SXin Li SparseMatrix<float> A = MatrixXf::Random(10,10).sparseView(0.5,1);
65*bf2c3715SXin Li SparseMatrix<float,RowMajor> B = MatrixXf::Random(10,10).sparseView(0.5,1);
66*bf2c3715SXin Li SparseMatrix<float> C = MatrixXf::Random(10,10).sparseView(0.5,1);
67*bf2c3715SXin Li C.reserve(VectorXi::Constant(C.outerSize(), 2));
68*bf2c3715SXin Li const SparseMatrix<float>& Ac(A);
69*bf2c3715SXin Li Block<SparseMatrix<float> > Ab(A,0,1, 3,3);
70*bf2c3715SXin Li const Block<SparseMatrix<float> > Abc(A,0,1,3,3);
71*bf2c3715SXin Li SparseVector<float> vc = VectorXf::Random(10).sparseView(0.5,1);
72*bf2c3715SXin Li SparseVector<float,RowMajor> vr = VectorXf::Random(10).sparseView(0.5,1);
73*bf2c3715SXin Li SparseMatrix<float> AA = A*A;
74*bf2c3715SXin Li
75*bf2c3715SXin Li
76*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_1(A, A), 0);
77*bf2c3715SXin Li // VERIFY_EVALUATION_COUNT( call_ref_1(Ac, Ac), 0); // does not compile on purpose
78*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(A, A), 0);
79*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_3(A, A), 0);
80*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(A.transpose(), A.transpose()), 1);
81*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_3(A.transpose(), A.transpose()), 1);
82*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(Ac,Ac), 0);
83*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_3(Ac,Ac), 0);
84*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(A+A,2*Ac), 1);
85*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_3(A+A,2*Ac), 1);
86*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(B, B), 1);
87*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_3(B, B), 1);
88*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(B.transpose(), B.transpose()), 0);
89*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_3(B.transpose(), B.transpose()), 0);
90*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(A*A, AA), 3);
91*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_3(A*A, AA), 3);
92*bf2c3715SXin Li
93*bf2c3715SXin Li VERIFY(!C.isCompressed());
94*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_3(C, C), 1);
95*bf2c3715SXin Li
96*bf2c3715SXin Li Ref<SparseMatrix<float> > Ar(A);
97*bf2c3715SXin Li VERIFY_IS_APPROX(Ar+Ar, A+A);
98*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_1(Ar, A), 0);
99*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(Ar, A), 0);
100*bf2c3715SXin Li
101*bf2c3715SXin Li Ref<SparseMatrix<float,RowMajor> > Br(B);
102*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_1(Br.transpose(), Br.transpose()), 0);
103*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(Br, Br), 1);
104*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(Br.transpose(), Br.transpose()), 0);
105*bf2c3715SXin Li
106*bf2c3715SXin Li Ref<const SparseMatrix<float> > Arc(A);
107*bf2c3715SXin Li // VERIFY_EVALUATION_COUNT( call_ref_1(Arc, Arc), 0); // does not compile on purpose
108*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(Arc, Arc), 0);
109*bf2c3715SXin Li
110*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(A.middleCols(1,3), A.middleCols(1,3)), 0);
111*bf2c3715SXin Li
112*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(A.col(2), A.col(2)), 0);
113*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(vc, vc), 0);
114*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(vr.transpose(), vr.transpose()), 0);
115*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(vr, vr.transpose()), 0);
116*bf2c3715SXin Li
117*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_2(A.block(1,1,3,3), A.block(1,1,3,3)), 1); // should be 0 (allocate starts/nnz only)
118*bf2c3715SXin Li
119*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_4(vc, vc), 0);
120*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_4(vr, vr.transpose()), 0);
121*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_5(vc, vc), 0);
122*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_5(vr, vr.transpose()), 0);
123*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_4(A.col(2), A.col(2)), 0);
124*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_5(A.col(2), A.col(2)), 0);
125*bf2c3715SXin Li // VERIFY_EVALUATION_COUNT( call_ref_4(A.row(2), A.row(2).transpose()), 1); // does not compile on purpose
126*bf2c3715SXin Li VERIFY_EVALUATION_COUNT( call_ref_5(A.row(2), A.row(2).transpose()), 1);
127*bf2c3715SXin Li }
128*bf2c3715SXin Li
EIGEN_DECLARE_TEST(sparse_ref)129*bf2c3715SXin Li EIGEN_DECLARE_TEST(sparse_ref)
130*bf2c3715SXin Li {
131*bf2c3715SXin Li for(int i = 0; i < g_repeat; i++) {
132*bf2c3715SXin Li CALL_SUBTEST_1( check_const_correctness(SparseMatrix<float>()) );
133*bf2c3715SXin Li CALL_SUBTEST_1( check_const_correctness(SparseMatrix<double,RowMajor>()) );
134*bf2c3715SXin Li CALL_SUBTEST_2( call_ref() );
135*bf2c3715SXin Li
136*bf2c3715SXin Li CALL_SUBTEST_3( check_const_correctness(SparseVector<float>()) );
137*bf2c3715SXin Li CALL_SUBTEST_3( check_const_correctness(SparseVector<double,RowMajor>()) );
138*bf2c3715SXin Li }
139*bf2c3715SXin Li }
140