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) 2011-2015 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
11*bf2c3715SXin Li static long int nb_transposed_copies;
12*bf2c3715SXin Li #define EIGEN_SPARSE_TRANSPOSED_COPY_PLUGIN {nb_transposed_copies++;}
13*bf2c3715SXin Li #define VERIFY_TRANSPOSITION_COUNT(XPR,N) {\
14*bf2c3715SXin Li nb_transposed_copies = 0; \
15*bf2c3715SXin Li XPR; \
16*bf2c3715SXin Li if(nb_transposed_copies!=N) std::cerr << "nb_transposed_copies == " << nb_transposed_copies << "\n"; \
17*bf2c3715SXin Li VERIFY( (#XPR) && nb_transposed_copies==N ); \
18*bf2c3715SXin Li }
19*bf2c3715SXin Li
20*bf2c3715SXin Li #include "sparse.h"
21*bf2c3715SXin Li
22*bf2c3715SXin Li template<typename T>
is_sorted(const T & mat)23*bf2c3715SXin Li bool is_sorted(const T& mat) {
24*bf2c3715SXin Li for(Index k = 0; k<mat.outerSize(); ++k)
25*bf2c3715SXin Li {
26*bf2c3715SXin Li Index prev = -1;
27*bf2c3715SXin Li for(typename T::InnerIterator it(mat,k); it; ++it)
28*bf2c3715SXin Li {
29*bf2c3715SXin Li if(prev>=it.index())
30*bf2c3715SXin Li return false;
31*bf2c3715SXin Li prev = it.index();
32*bf2c3715SXin Li }
33*bf2c3715SXin Li }
34*bf2c3715SXin Li return true;
35*bf2c3715SXin Li }
36*bf2c3715SXin Li
37*bf2c3715SXin Li template<typename T>
eval(const T & xpr)38*bf2c3715SXin Li typename internal::nested_eval<T,1>::type eval(const T &xpr)
39*bf2c3715SXin Li {
40*bf2c3715SXin Li VERIFY( int(internal::nested_eval<T,1>::type::Flags&RowMajorBit) == int(internal::evaluator<T>::Flags&RowMajorBit) );
41*bf2c3715SXin Li return xpr;
42*bf2c3715SXin Li }
43*bf2c3715SXin Li
sparse_permutations(const SparseMatrixType & ref)44*bf2c3715SXin Li template<int OtherStorage, typename SparseMatrixType> void sparse_permutations(const SparseMatrixType& ref)
45*bf2c3715SXin Li {
46*bf2c3715SXin Li const Index rows = ref.rows();
47*bf2c3715SXin Li const Index cols = ref.cols();
48*bf2c3715SXin Li typedef typename SparseMatrixType::Scalar Scalar;
49*bf2c3715SXin Li typedef typename SparseMatrixType::StorageIndex StorageIndex;
50*bf2c3715SXin Li typedef SparseMatrix<Scalar, OtherStorage, StorageIndex> OtherSparseMatrixType;
51*bf2c3715SXin Li typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
52*bf2c3715SXin Li typedef Matrix<StorageIndex,Dynamic,1> VectorI;
53*bf2c3715SXin Li // bool IsRowMajor1 = SparseMatrixType::IsRowMajor;
54*bf2c3715SXin Li // bool IsRowMajor2 = OtherSparseMatrixType::IsRowMajor;
55*bf2c3715SXin Li
56*bf2c3715SXin Li double density = (std::max)(8./(rows*cols), 0.01);
57*bf2c3715SXin Li
58*bf2c3715SXin Li SparseMatrixType mat(rows, cols), up(rows,cols), lo(rows,cols);
59*bf2c3715SXin Li OtherSparseMatrixType res;
60*bf2c3715SXin Li DenseMatrix mat_d = DenseMatrix::Zero(rows, cols), up_sym_d, lo_sym_d, res_d;
61*bf2c3715SXin Li
62*bf2c3715SXin Li initSparse<Scalar>(density, mat_d, mat, 0);
63*bf2c3715SXin Li
64*bf2c3715SXin Li up = mat.template triangularView<Upper>();
65*bf2c3715SXin Li lo = mat.template triangularView<Lower>();
66*bf2c3715SXin Li
67*bf2c3715SXin Li up_sym_d = mat_d.template selfadjointView<Upper>();
68*bf2c3715SXin Li lo_sym_d = mat_d.template selfadjointView<Lower>();
69*bf2c3715SXin Li
70*bf2c3715SXin Li VERIFY_IS_APPROX(mat, mat_d);
71*bf2c3715SXin Li VERIFY_IS_APPROX(up, DenseMatrix(mat_d.template triangularView<Upper>()));
72*bf2c3715SXin Li VERIFY_IS_APPROX(lo, DenseMatrix(mat_d.template triangularView<Lower>()));
73*bf2c3715SXin Li
74*bf2c3715SXin Li PermutationMatrix<Dynamic> p, p_null;
75*bf2c3715SXin Li VectorI pi;
76*bf2c3715SXin Li randomPermutationVector(pi, cols);
77*bf2c3715SXin Li p.indices() = pi;
78*bf2c3715SXin Li
79*bf2c3715SXin Li VERIFY( is_sorted( ::eval(mat*p) ));
80*bf2c3715SXin Li VERIFY( is_sorted( res = mat*p ));
81*bf2c3715SXin Li VERIFY_TRANSPOSITION_COUNT( ::eval(mat*p), 0);
82*bf2c3715SXin Li //VERIFY_TRANSPOSITION_COUNT( res = mat*p, IsRowMajor ? 1 : 0 );
83*bf2c3715SXin Li res_d = mat_d*p;
84*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "mat*p");
85*bf2c3715SXin Li
86*bf2c3715SXin Li VERIFY( is_sorted( ::eval(p*mat) ));
87*bf2c3715SXin Li VERIFY( is_sorted( res = p*mat ));
88*bf2c3715SXin Li VERIFY_TRANSPOSITION_COUNT( ::eval(p*mat), 0);
89*bf2c3715SXin Li res_d = p*mat_d;
90*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "p*mat");
91*bf2c3715SXin Li
92*bf2c3715SXin Li VERIFY( is_sorted( (mat*p).eval() ));
93*bf2c3715SXin Li VERIFY( is_sorted( res = mat*p.inverse() ));
94*bf2c3715SXin Li VERIFY_TRANSPOSITION_COUNT( ::eval(mat*p.inverse()), 0);
95*bf2c3715SXin Li res_d = mat*p.inverse();
96*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "mat*inv(p)");
97*bf2c3715SXin Li
98*bf2c3715SXin Li VERIFY( is_sorted( (p*mat+p*mat).eval() ));
99*bf2c3715SXin Li VERIFY( is_sorted( res = p.inverse()*mat ));
100*bf2c3715SXin Li VERIFY_TRANSPOSITION_COUNT( ::eval(p.inverse()*mat), 0);
101*bf2c3715SXin Li res_d = p.inverse()*mat_d;
102*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "inv(p)*mat");
103*bf2c3715SXin Li
104*bf2c3715SXin Li VERIFY( is_sorted( (p * mat * p.inverse()).eval() ));
105*bf2c3715SXin Li VERIFY( is_sorted( res = mat.twistedBy(p) ));
106*bf2c3715SXin Li VERIFY_TRANSPOSITION_COUNT( ::eval(p * mat * p.inverse()), 0);
107*bf2c3715SXin Li res_d = (p * mat_d) * p.inverse();
108*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "p*mat*inv(p)");
109*bf2c3715SXin Li
110*bf2c3715SXin Li
111*bf2c3715SXin Li VERIFY( is_sorted( res = mat.template selfadjointView<Upper>().twistedBy(p_null) ));
112*bf2c3715SXin Li res_d = up_sym_d;
113*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint upper to full");
114*bf2c3715SXin Li
115*bf2c3715SXin Li VERIFY( is_sorted( res = mat.template selfadjointView<Lower>().twistedBy(p_null) ));
116*bf2c3715SXin Li res_d = lo_sym_d;
117*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint lower to full");
118*bf2c3715SXin Li
119*bf2c3715SXin Li
120*bf2c3715SXin Li VERIFY( is_sorted( res = up.template selfadjointView<Upper>().twistedBy(p_null) ));
121*bf2c3715SXin Li res_d = up_sym_d;
122*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "upper selfadjoint to full");
123*bf2c3715SXin Li
124*bf2c3715SXin Li VERIFY( is_sorted( res = lo.template selfadjointView<Lower>().twistedBy(p_null) ));
125*bf2c3715SXin Li res_d = lo_sym_d;
126*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "lower selfadjoint full");
127*bf2c3715SXin Li
128*bf2c3715SXin Li
129*bf2c3715SXin Li VERIFY( is_sorted( res = mat.template selfadjointView<Upper>() ));
130*bf2c3715SXin Li res_d = up_sym_d;
131*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint upper to full");
132*bf2c3715SXin Li
133*bf2c3715SXin Li VERIFY( is_sorted( res = mat.template selfadjointView<Lower>() ));
134*bf2c3715SXin Li res_d = lo_sym_d;
135*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint lower to full");
136*bf2c3715SXin Li
137*bf2c3715SXin Li VERIFY( is_sorted( res = up.template selfadjointView<Upper>() ));
138*bf2c3715SXin Li res_d = up_sym_d;
139*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "upper selfadjoint to full");
140*bf2c3715SXin Li
141*bf2c3715SXin Li VERIFY( is_sorted( res = lo.template selfadjointView<Lower>() ));
142*bf2c3715SXin Li res_d = lo_sym_d;
143*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "lower selfadjoint full");
144*bf2c3715SXin Li
145*bf2c3715SXin Li
146*bf2c3715SXin Li res.template selfadjointView<Upper>() = mat.template selfadjointView<Upper>();
147*bf2c3715SXin Li res_d = up_sym_d.template triangularView<Upper>();
148*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint upper to upper");
149*bf2c3715SXin Li
150*bf2c3715SXin Li res.template selfadjointView<Lower>() = mat.template selfadjointView<Upper>();
151*bf2c3715SXin Li res_d = up_sym_d.template triangularView<Lower>();
152*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint upper to lower");
153*bf2c3715SXin Li
154*bf2c3715SXin Li res.template selfadjointView<Upper>() = mat.template selfadjointView<Lower>();
155*bf2c3715SXin Li res_d = lo_sym_d.template triangularView<Upper>();
156*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint lower to upper");
157*bf2c3715SXin Li
158*bf2c3715SXin Li res.template selfadjointView<Lower>() = mat.template selfadjointView<Lower>();
159*bf2c3715SXin Li res_d = lo_sym_d.template triangularView<Lower>();
160*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint lower to lower");
161*bf2c3715SXin Li
162*bf2c3715SXin Li
163*bf2c3715SXin Li
164*bf2c3715SXin Li res.template selfadjointView<Upper>() = mat.template selfadjointView<Upper>().twistedBy(p);
165*bf2c3715SXin Li res_d = ((p * up_sym_d) * p.inverse()).eval().template triangularView<Upper>();
166*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint upper twisted to upper");
167*bf2c3715SXin Li
168*bf2c3715SXin Li res.template selfadjointView<Upper>() = mat.template selfadjointView<Lower>().twistedBy(p);
169*bf2c3715SXin Li res_d = ((p * lo_sym_d) * p.inverse()).eval().template triangularView<Upper>();
170*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint lower twisted to upper");
171*bf2c3715SXin Li
172*bf2c3715SXin Li res.template selfadjointView<Lower>() = mat.template selfadjointView<Lower>().twistedBy(p);
173*bf2c3715SXin Li res_d = ((p * lo_sym_d) * p.inverse()).eval().template triangularView<Lower>();
174*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint lower twisted to lower");
175*bf2c3715SXin Li
176*bf2c3715SXin Li res.template selfadjointView<Lower>() = mat.template selfadjointView<Upper>().twistedBy(p);
177*bf2c3715SXin Li res_d = ((p * up_sym_d) * p.inverse()).eval().template triangularView<Lower>();
178*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint upper twisted to lower");
179*bf2c3715SXin Li
180*bf2c3715SXin Li
181*bf2c3715SXin Li res.template selfadjointView<Upper>() = up.template selfadjointView<Upper>().twistedBy(p);
182*bf2c3715SXin Li res_d = ((p * up_sym_d) * p.inverse()).eval().template triangularView<Upper>();
183*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "upper selfadjoint twisted to upper");
184*bf2c3715SXin Li
185*bf2c3715SXin Li res.template selfadjointView<Upper>() = lo.template selfadjointView<Lower>().twistedBy(p);
186*bf2c3715SXin Li res_d = ((p * lo_sym_d) * p.inverse()).eval().template triangularView<Upper>();
187*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "lower selfadjoint twisted to upper");
188*bf2c3715SXin Li
189*bf2c3715SXin Li res.template selfadjointView<Lower>() = lo.template selfadjointView<Lower>().twistedBy(p);
190*bf2c3715SXin Li res_d = ((p * lo_sym_d) * p.inverse()).eval().template triangularView<Lower>();
191*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "lower selfadjoint twisted to lower");
192*bf2c3715SXin Li
193*bf2c3715SXin Li res.template selfadjointView<Lower>() = up.template selfadjointView<Upper>().twistedBy(p);
194*bf2c3715SXin Li res_d = ((p * up_sym_d) * p.inverse()).eval().template triangularView<Lower>();
195*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "upper selfadjoint twisted to lower");
196*bf2c3715SXin Li
197*bf2c3715SXin Li
198*bf2c3715SXin Li VERIFY( is_sorted( res = mat.template selfadjointView<Upper>().twistedBy(p) ));
199*bf2c3715SXin Li res_d = (p * up_sym_d) * p.inverse();
200*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint upper twisted to full");
201*bf2c3715SXin Li
202*bf2c3715SXin Li VERIFY( is_sorted( res = mat.template selfadjointView<Lower>().twistedBy(p) ));
203*bf2c3715SXin Li res_d = (p * lo_sym_d) * p.inverse();
204*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "full selfadjoint lower twisted to full");
205*bf2c3715SXin Li
206*bf2c3715SXin Li VERIFY( is_sorted( res = up.template selfadjointView<Upper>().twistedBy(p) ));
207*bf2c3715SXin Li res_d = (p * up_sym_d) * p.inverse();
208*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "upper selfadjoint twisted to full");
209*bf2c3715SXin Li
210*bf2c3715SXin Li VERIFY( is_sorted( res = lo.template selfadjointView<Lower>().twistedBy(p) ));
211*bf2c3715SXin Li res_d = (p * lo_sym_d) * p.inverse();
212*bf2c3715SXin Li VERIFY(res.isApprox(res_d) && "lower selfadjoint twisted to full");
213*bf2c3715SXin Li }
214*bf2c3715SXin Li
sparse_permutations_all(int size)215*bf2c3715SXin Li template<typename Scalar> void sparse_permutations_all(int size)
216*bf2c3715SXin Li {
217*bf2c3715SXin Li CALL_SUBTEST(( sparse_permutations<ColMajor>(SparseMatrix<Scalar, ColMajor>(size,size)) ));
218*bf2c3715SXin Li CALL_SUBTEST(( sparse_permutations<ColMajor>(SparseMatrix<Scalar, RowMajor>(size,size)) ));
219*bf2c3715SXin Li CALL_SUBTEST(( sparse_permutations<RowMajor>(SparseMatrix<Scalar, ColMajor>(size,size)) ));
220*bf2c3715SXin Li CALL_SUBTEST(( sparse_permutations<RowMajor>(SparseMatrix<Scalar, RowMajor>(size,size)) ));
221*bf2c3715SXin Li }
222*bf2c3715SXin Li
EIGEN_DECLARE_TEST(sparse_permutations)223*bf2c3715SXin Li EIGEN_DECLARE_TEST(sparse_permutations)
224*bf2c3715SXin Li {
225*bf2c3715SXin Li for(int i = 0; i < g_repeat; i++) {
226*bf2c3715SXin Li int s = Eigen::internal::random<int>(1,50);
227*bf2c3715SXin Li CALL_SUBTEST_1(( sparse_permutations_all<double>(s) ));
228*bf2c3715SXin Li CALL_SUBTEST_2(( sparse_permutations_all<std::complex<double> >(s) ));
229*bf2c3715SXin Li }
230*bf2c3715SXin Li
231*bf2c3715SXin Li VERIFY((internal::is_same<internal::permutation_matrix_product<SparseMatrix<double>,OnTheRight,false,SparseShape>::ReturnType,
232*bf2c3715SXin Li internal::nested_eval<Product<SparseMatrix<double>,PermutationMatrix<Dynamic,Dynamic>,AliasFreeProduct>,1>::type>::value));
233*bf2c3715SXin Li
234*bf2c3715SXin Li VERIFY((internal::is_same<internal::permutation_matrix_product<SparseMatrix<double>,OnTheLeft,false,SparseShape>::ReturnType,
235*bf2c3715SXin Li internal::nested_eval<Product<PermutationMatrix<Dynamic,Dynamic>,SparseMatrix<double>,AliasFreeProduct>,1>::type>::value));
236*bf2c3715SXin Li }
237