blob: d2d47561605e19a0e89e4c04e80a723e0d3e4b70 [file] [log] [blame]
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +01001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 20015 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
15static long int nb_temporaries;
16
17inline void on_temporary_creation() {
18 // here's a great place to set a breakpoint when debugging failures in this test!
19 nb_temporaries++;
20}
21
22#define EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN { on_temporary_creation(); }
23
24#include "main.h"
25#include <Eigen/SparseCore>
26
27#define VERIFY_EVALUATION_COUNT(XPR,N) {\
28 nb_temporaries = 0; \
Gael Guennebaud27a94292015-10-06 17:23:11 +020029 CALL_SUBTEST( XPR ); \
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010030 if(nb_temporaries!=N) std::cerr << "nb_temporaries == " << nb_temporaries << "\n"; \
31 VERIFY( (#XPR) && nb_temporaries==N ); \
32 }
33
34template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
35{
36 // verify that ref-to-const don't have LvalueBit
37 typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
38 VERIFY( !(internal::traits<Ref<ConstPlainObjectType> >::Flags & LvalueBit) );
39 VERIFY( !(internal::traits<Ref<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) );
40 VERIFY( !(Ref<ConstPlainObjectType>::Flags & LvalueBit) );
41 VERIFY( !(Ref<ConstPlainObjectType, Aligned>::Flags & LvalueBit) );
42}
43
44template<typename B>
45EIGEN_DONT_INLINE void call_ref_1(Ref<SparseMatrix<float> > a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
46
47template<typename B>
48EIGEN_DONT_INLINE void call_ref_2(const Ref<const SparseMatrix<float> >& a, const B &b) { VERIFY_IS_EQUAL(a.toDense(),b.toDense()); }
49
Gael Guennebaudfeaf76c2015-06-09 23:11:24 +020050template<typename B>
51EIGEN_DONT_INLINE void call_ref_3(const Ref<const SparseMatrix<float>, StandardCompressedFormat>& a, const B &b) {
52 VERIFY(a.isCompressed());
53 VERIFY_IS_EQUAL(a.toDense(),b.toDense());
54}
55
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010056void call_ref()
57{
58// SparseVector<std::complex<float> > ca = VectorXcf::Random(10).sparseView();
59// SparseVector<float> a = VectorXf::Random(10).sparseView();
Gael Guennebaudd4ec4852015-02-09 11:14:36 +010060 SparseMatrix<float> A = MatrixXf::Random(10,10).sparseView(0.5,1);
61 SparseMatrix<float,RowMajor> B = MatrixXf::Random(10,10).sparseView(0.5,1);
Gael Guennebaudfeaf76c2015-06-09 23:11:24 +020062 SparseMatrix<float> C = MatrixXf::Random(10,10).sparseView(0.5,1);
63 C.reserve(VectorXi::Constant(C.outerSize(), 2));
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010064 const SparseMatrix<float>& Ac(A);
65 Block<SparseMatrix<float> > Ab(A,0,1, 3,3);
66 const Block<SparseMatrix<float> > Abc(A,0,1,3,3);
Gael Guennebaud27a94292015-10-06 17:23:11 +020067 SparseVector<float> vc = VectorXf::Random(10).sparseView(0.5,1);
68 SparseVector<float,RowMajor> vr = VectorXf::Random(10).sparseView(0.5,1);
Gael Guennebaud6100d1a2015-10-06 11:32:02 +020069 SparseMatrix<float> AA = A*A;
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010070
71
72 VERIFY_EVALUATION_COUNT( call_ref_1(A, A), 0);
73// VERIFY_EVALUATION_COUNT( call_ref_1(Ac, Ac), 0); // does not compile on purpose
74 VERIFY_EVALUATION_COUNT( call_ref_2(A, A), 0);
Gael Guennebaudfeaf76c2015-06-09 23:11:24 +020075 VERIFY_EVALUATION_COUNT( call_ref_3(A, A), 0);
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010076 VERIFY_EVALUATION_COUNT( call_ref_2(A.transpose(), A.transpose()), 1);
Gael Guennebaudfeaf76c2015-06-09 23:11:24 +020077 VERIFY_EVALUATION_COUNT( call_ref_3(A.transpose(), A.transpose()), 1);
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010078 VERIFY_EVALUATION_COUNT( call_ref_2(Ac,Ac), 0);
Gael Guennebaudfeaf76c2015-06-09 23:11:24 +020079 VERIFY_EVALUATION_COUNT( call_ref_3(Ac,Ac), 0);
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010080 VERIFY_EVALUATION_COUNT( call_ref_2(A+A,2*Ac), 1);
Gael Guennebaudfeaf76c2015-06-09 23:11:24 +020081 VERIFY_EVALUATION_COUNT( call_ref_3(A+A,2*Ac), 1);
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010082 VERIFY_EVALUATION_COUNT( call_ref_2(B, B), 1);
Gael Guennebaudfeaf76c2015-06-09 23:11:24 +020083 VERIFY_EVALUATION_COUNT( call_ref_3(B, B), 1);
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010084 VERIFY_EVALUATION_COUNT( call_ref_2(B.transpose(), B.transpose()), 0);
Gael Guennebaudfeaf76c2015-06-09 23:11:24 +020085 VERIFY_EVALUATION_COUNT( call_ref_3(B.transpose(), B.transpose()), 0);
Gael Guennebaud6100d1a2015-10-06 11:32:02 +020086 VERIFY_EVALUATION_COUNT( call_ref_2(A*A, AA), 1);
87 VERIFY_EVALUATION_COUNT( call_ref_3(A*A, AA), 1);
Gael Guennebaudfeaf76c2015-06-09 23:11:24 +020088
89 VERIFY(!C.isCompressed());
90 VERIFY_EVALUATION_COUNT( call_ref_3(C, C), 1);
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010091
92 Ref<SparseMatrix<float> > Ar(A);
Gael Guennebaud3af29ca2015-02-09 10:23:45 +010093 VERIFY_IS_APPROX(Ar+Ar, A+A);
94 VERIFY_EVALUATION_COUNT( call_ref_1(Ar, A), 0);
95 VERIFY_EVALUATION_COUNT( call_ref_2(Ar, A), 0);
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +010096
97 Ref<SparseMatrix<float,RowMajor> > Br(B);
98 VERIFY_EVALUATION_COUNT( call_ref_1(Br.transpose(), Br.transpose()), 0);
99 VERIFY_EVALUATION_COUNT( call_ref_2(Br, Br), 1);
100 VERIFY_EVALUATION_COUNT( call_ref_2(Br.transpose(), Br.transpose()), 0);
101
102 Ref<const SparseMatrix<float> > Arc(A);
103// VERIFY_EVALUATION_COUNT( call_ref_1(Arc, Arc), 0); // does not compile on purpose
104 VERIFY_EVALUATION_COUNT( call_ref_2(Arc, Arc), 0);
105
Gael Guennebaudd4ec4852015-02-09 11:14:36 +0100106 VERIFY_EVALUATION_COUNT( call_ref_2(A.middleCols(1,3), A.middleCols(1,3)), 0);
107
108 VERIFY_EVALUATION_COUNT( call_ref_2(A.col(2), A.col(2)), 0);
Gael Guennebaud27a94292015-10-06 17:23:11 +0200109 VERIFY_EVALUATION_COUNT( call_ref_2(vc, vc), 0);
110 VERIFY_EVALUATION_COUNT( call_ref_2(vr.transpose(), vr.transpose()), 0);
111 VERIFY_EVALUATION_COUNT( call_ref_2(vr, vr.transpose()), 0);
Gael Guennebaudf2ff8c02015-02-07 22:04:18 +0100112
113 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)
114}
115
116void test_sparse_ref()
117{
118 for(int i = 0; i < g_repeat; i++) {
119 CALL_SUBTEST_1( check_const_correctness(SparseMatrix<float>()) );
120 CALL_SUBTEST_1( check_const_correctness(SparseMatrix<double,RowMajor>()) );
121 CALL_SUBTEST_2( call_ref() );
122 }
123}