blob: 5ae038ef8f3cb23c3eb2cfb010bc4e4e43207c4c [file] [log] [blame]
Gael Guennebaud86ccd992008-11-05 13:47:55 +00001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra. Eigen itself is part of the KDE project.
3//
4// Copyright (C) 2008 Daniel Gomez Ferro <dgomezferro@gmail.com>
5//
6// Eigen is free software; you can redistribute it and/or
7// modify it under the terms of the GNU Lesser General Public
8// License as published by the Free Software Foundation; either
9// version 3 of the License, or (at your option) any later version.
10//
11// Alternatively, you can redistribute it and/or
12// modify it under the terms of the GNU General Public License as
13// published by the Free Software Foundation; either version 2 of
14// the License, or (at your option) any later version.
15//
16// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public
22// License and a copy of the GNU General Public License along with
23// Eigen. If not, see <http://www.gnu.org/licenses/>.
24
25#include "sparse.h"
26
27template<typename SetterType,typename DenseType, typename SparseType>
28bool test_random_setter(SparseType& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
29{
30 {
31 sm.setZero();
32 SetterType w(sm);
33 std::vector<Vector2i> remaining = nonzeroCoords;
34 while(!remaining.empty())
35 {
36 int i = ei_random<int>(0,remaining.size()-1);
37 w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
38 remaining[i] = remaining.back();
39 remaining.pop_back();
40 }
41 }
42 return sm.isApprox(ref);
43}
44
Gael Guennebaud178858f2009-01-19 15:20:45 +000045template<typename SetterType,typename DenseType, typename T>
46bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
Gael Guennebaud86ccd992008-11-05 13:47:55 +000047{
Gael Guennebaud178858f2009-01-19 15:20:45 +000048 sm.setZero();
49 std::vector<Vector2i> remaining = nonzeroCoords;
50 while(!remaining.empty())
51 {
52 int i = ei_random<int>(0,remaining.size()-1);
53 sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
54 remaining[i] = remaining.back();
55 remaining.pop_back();
56 }
57 return sm.isApprox(ref);
58}
59
60template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref)
61{
62 const int rows = ref.rows();
63 const int cols = ref.cols();
64 typedef typename SparseMatrixType::Scalar Scalar;
65 enum { Flags = SparseMatrixType::Flags };
66
Gael Guennebaud86ccd992008-11-05 13:47:55 +000067 double density = std::max(8./(rows*cols), 0.01);
68 typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
69 typedef Matrix<Scalar,Dynamic,1> DenseVector;
70 Scalar eps = 1e-6;
71
Gael Guennebaud178858f2009-01-19 15:20:45 +000072 SparseMatrixType m(rows, cols);
Gael Guennebaud86ccd992008-11-05 13:47:55 +000073 DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
74 DenseVector vec1 = DenseVector::Random(rows);
Gael Guennebaud2d534662009-01-14 21:27:54 +000075 Scalar s1 = ei_random<Scalar>();
Gael Guennebaud86ccd992008-11-05 13:47:55 +000076
77 std::vector<Vector2i> zeroCoords;
78 std::vector<Vector2i> nonzeroCoords;
79 initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
Gael Guennebaud178858f2009-01-19 15:20:45 +000080
Gael Guennebaud86ccd992008-11-05 13:47:55 +000081 if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
82 return;
83
84 // test coeff and coeffRef
85 for (int i=0; i<(int)zeroCoords.size(); ++i)
86 {
87 VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
Gael Guennebaud178858f2009-01-19 15:20:45 +000088 if(ei_is_same_type<SparseMatrixType,SparseMatrix<Scalar,Flags> >::ret)
89 VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
Gael Guennebaud86ccd992008-11-05 13:47:55 +000090 }
91 VERIFY_IS_APPROX(m, refMat);
92
93 m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
94 refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
95
96 VERIFY_IS_APPROX(m, refMat);
Gael Guennebaudc4c70662009-01-14 14:24:10 +000097 /*
Gael Guennebaud86ccd992008-11-05 13:47:55 +000098 // test InnerIterators and Block expressions
99 for (int t=0; t<10; ++t)
100 {
101 int j = ei_random<int>(0,cols-1);
102 int i = ei_random<int>(0,rows-1);
103 int w = ei_random<int>(1,cols-j-1);
104 int h = ei_random<int>(1,rows-i-1);
105
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000106// VERIFY_IS_APPROX(m.block(i,j,h,w), refMat.block(i,j,h,w));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000107 for(int c=0; c<w; c++)
108 {
109 VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c));
110 for(int r=0; r<h; r++)
111 {
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000112// VERIFY_IS_APPROX(m.block(i,j,h,w).col(c).coeff(r), refMat.block(i,j,h,w).col(c).coeff(r));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000113 }
114 }
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000115// for(int r=0; r<h; r++)
116// {
117// VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r));
118// for(int c=0; c<w; c++)
119// {
120// VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c));
121// }
122// }
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000123 }
124
125 for(int c=0; c<cols; c++)
126 {
127 VERIFY_IS_APPROX(m.col(c) + m.col(c), (m + m).col(c));
128 VERIFY_IS_APPROX(m.col(c) + m.col(c), refMat.col(c) + refMat.col(c));
129 }
130
131 for(int r=0; r<rows; r++)
132 {
133 VERIFY_IS_APPROX(m.row(r) + m.row(r), (m + m).row(r));
134 VERIFY_IS_APPROX(m.row(r) + m.row(r), refMat.row(r) + refMat.row(r));
135 }
136 */
137
138 // test SparseSetters
139 // coherent setter
140 // TODO extend the MatrixSetter
141// {
142// m.setZero();
143// VERIFY_IS_NOT_APPROX(m, refMat);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000144// SparseSetter<SparseMatrixType, FullyCoherentAccessPattern> w(m);
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000145// for (int i=0; i<nonzeroCoords.size(); ++i)
146// {
147// w->coeffRef(nonzeroCoords[i].x(),nonzeroCoords[i].y()) = refMat.coeff(nonzeroCoords[i].x(),nonzeroCoords[i].y());
148// }
149// }
150// VERIFY_IS_APPROX(m, refMat);
151
152 // random setter
153// {
154// m.setZero();
155// VERIFY_IS_NOT_APPROX(m, refMat);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000156// SparseSetter<SparseMatrixType, RandomAccessPattern> w(m);
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000157// std::vector<Vector2i> remaining = nonzeroCoords;
158// while(!remaining.empty())
159// {
160// int i = ei_random<int>(0,remaining.size()-1);
161// w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
162// remaining[i] = remaining.back();
163// remaining.pop_back();
164// }
165// }
166// VERIFY_IS_APPROX(m, refMat);
167
Gael Guennebaud178858f2009-01-19 15:20:45 +0000168 VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) ));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000169 #ifdef _HASH_MAP
Gael Guennebaud178858f2009-01-19 15:20:45 +0000170 VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GnuHashMapTraits> >(m,refMat,nonzeroCoords) ));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000171 #endif
172 #ifdef _DENSE_HASH_MAP_H_
Gael Guennebaud178858f2009-01-19 15:20:45 +0000173 VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000174 #endif
175 #ifdef _SPARSE_HASH_MAP_H_
Gael Guennebaud178858f2009-01-19 15:20:45 +0000176 VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000177 #endif
Gael Guennebaud5015e482008-12-11 18:26:24 +0000178
179 // test fillrand
180 {
181 DenseMatrix m1(rows,cols);
182 m1.setZero();
Gael Guennebaud178858f2009-01-19 15:20:45 +0000183 SparseMatrixType m2(rows,cols);
Gael Guennebaud5015e482008-12-11 18:26:24 +0000184 m2.startFill();
185 for (int j=0; j<cols; ++j)
186 {
187 for (int k=0; k<rows/2; ++k)
188 {
189 int i = ei_random<int>(0,rows-1);
190 if (m1.coeff(i,j)==Scalar(0))
191 m2.fillrand(i,j) = m1(i,j) = ei_random<Scalar>();
192 }
193 }
194 m2.endFill();
Gael Guennebaud178858f2009-01-19 15:20:45 +0000195 //std::cerr << m1 << "\n\n" << m2 << "\n";
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000196 VERIFY_IS_APPROX(m2,m1);
Gael Guennebaud5015e482008-12-11 18:26:24 +0000197 }
Gael Guennebaud9a4b7992009-01-15 14:16:41 +0000198
Gael Guennebaud87241082009-01-15 13:30:50 +0000199 // test RandomSetter
Gael Guennebaud178858f2009-01-19 15:20:45 +0000200 /*{
201 SparseMatrixType m1(rows,cols), m2(rows,cols);
Gael Guennebaud87241082009-01-15 13:30:50 +0000202 DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
203 initSparse<Scalar>(density, refM1, m1);
204 {
Gael Guennebaud178858f2009-01-19 15:20:45 +0000205 Eigen::RandomSetter<SparseMatrixType > setter(m2);
Gael Guennebaud87241082009-01-15 13:30:50 +0000206 for (int j=0; j<m1.outerSize(); ++j)
Gael Guennebaud178858f2009-01-19 15:20:45 +0000207 for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
Gael Guennebaud87241082009-01-15 13:30:50 +0000208 setter(i.index(), j) = i.value();
209 }
210 VERIFY_IS_APPROX(m1, m2);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000211 }*/
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000212// std::cerr << m.transpose() << "\n\n" << refMat.transpose() << "\n\n";
213// VERIFY_IS_APPROX(m, refMat);
214
Gael Guennebaud2d534662009-01-14 21:27:54 +0000215 // test basic computations
216 {
217 DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
218 DenseMatrix refM2 = DenseMatrix::Zero(rows, rows);
219 DenseMatrix refM3 = DenseMatrix::Zero(rows, rows);
220 DenseMatrix refM4 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000221 SparseMatrixType m1(rows, rows);
222 SparseMatrixType m2(rows, rows);
223 SparseMatrixType m3(rows, rows);
224 SparseMatrixType m4(rows, rows);
Gael Guennebaud2d534662009-01-14 21:27:54 +0000225 initSparse<Scalar>(density, refM1, m1);
226 initSparse<Scalar>(density, refM2, m2);
227 initSparse<Scalar>(density, refM3, m3);
228 initSparse<Scalar>(density, refM4, m4);
229
230 VERIFY_IS_APPROX(m1+m2, refM1+refM2);
231 VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3);
232 VERIFY_IS_APPROX(m3.cwise()*(m1+m2), refM3.cwise()*(refM1+refM2));
233 VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2);
234
235 VERIFY_IS_APPROX(m1*=s1, refM1*=s1);
236 VERIFY_IS_APPROX(m1/=s1, refM1/=s1);
237
238 refM4.setRandom();
239 // sparse cwise* dense
240 VERIFY_IS_APPROX(m3.cwise()*refM4, refM3.cwise()*refM4);
241// VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4);
242 }
243
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000244 // test innerVector()
245 {
246 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000247 SparseMatrixType m2(rows, rows);
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000248 initSparse<Scalar>(density, refMat2, m2);
249 int j0 = ei_random(0,rows-1);
250 int j1 = ei_random(0,rows-1);
Gael Guennebaud2d534662009-01-14 21:27:54 +0000251 VERIFY_IS_APPROX(m2.innerVector(j0), refMat2.col(j0));
252 VERIFY_IS_APPROX(m2.innerVector(j0)+m2.innerVector(j1), refMat2.col(j0)+refMat2.col(j1));
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000253 }
254
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000255 // test transpose
256 {
257 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000258 SparseMatrixType m2(rows, rows);
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000259 initSparse<Scalar>(density, refMat2, m2);
260 VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval());
261 VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose());
262 }
263
264 // test matrix product
265 {
266 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
267 DenseMatrix refMat3 = DenseMatrix::Zero(rows, rows);
268 DenseMatrix refMat4 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud0b606dc2009-01-14 17:41:55 +0000269 DenseMatrix dm4 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000270 SparseMatrixType m2(rows, rows);
271 SparseMatrixType m3(rows, rows);
272 SparseMatrixType m4(rows, rows);
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000273 initSparse<Scalar>(density, refMat2, m2);
274 initSparse<Scalar>(density, refMat3, m3);
275 initSparse<Scalar>(density, refMat4, m4);
276 VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3);
277 VERIFY_IS_APPROX(m4=m2.transpose()*m3, refMat4=refMat2.transpose()*refMat3);
278 VERIFY_IS_APPROX(m4=m2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
279 VERIFY_IS_APPROX(m4=m2*m3.transpose(), refMat4=refMat2*refMat3.transpose());
Gael Guennebaud2d534662009-01-14 21:27:54 +0000280
Gael Guennebaud0b606dc2009-01-14 17:41:55 +0000281 // sparse * dense
282 VERIFY_IS_APPROX(dm4=m2*refMat3, refMat4=refMat2*refMat3);
283 VERIFY_IS_APPROX(dm4=m2*refMat3.transpose(), refMat4=refMat2*refMat3.transpose());
284 VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3, refMat4=refMat2.transpose()*refMat3);
285 VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
Gael Guennebaud2d534662009-01-14 21:27:54 +0000286
Gael Guennebaud0b606dc2009-01-14 17:41:55 +0000287 // dense * sparse
288 VERIFY_IS_APPROX(dm4=refMat2*m3, refMat4=refMat2*refMat3);
289 VERIFY_IS_APPROX(dm4=refMat2*m3.transpose(), refMat4=refMat2*refMat3.transpose());
290 VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3, refMat4=refMat2.transpose()*refMat3);
291 VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000292 }
Gael Guennebaudccdcebc2009-01-15 18:52:14 +0000293
294 // test self adjoint products
295 {
296 DenseMatrix b = DenseMatrix::Random(rows, rows);
297 DenseMatrix x = DenseMatrix::Random(rows, rows);
298 DenseMatrix refX = DenseMatrix::Random(rows, rows);
299 DenseMatrix refUp = DenseMatrix::Zero(rows, rows);
300 DenseMatrix refLo = DenseMatrix::Zero(rows, rows);
301 DenseMatrix refS = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000302 SparseMatrixType mUp(rows, rows);
303 SparseMatrixType mLo(rows, rows);
304 SparseMatrixType mS(rows, rows);
Gael Guennebaudccdcebc2009-01-15 18:52:14 +0000305 do {
306 initSparse<Scalar>(density, refUp, mUp, ForceRealDiag|/*ForceNonZeroDiag|*/MakeUpperTriangular);
307 } while (refUp.isZero());
308 refLo = refUp.transpose().conjugate();
309 mLo = mUp.transpose().conjugate();
310 refS = refUp + refLo;
311 refS.diagonal() *= 0.5;
312 mS = mUp + mLo;
313 for (int k=0; k<mS.outerSize(); ++k)
Gael Guennebaud178858f2009-01-19 15:20:45 +0000314 for (typename SparseMatrixType::InnerIterator it(mS,k); it; ++it)
Gael Guennebaudccdcebc2009-01-15 18:52:14 +0000315 if (it.index() == k)
316 it.valueRef() *= 0.5;
317
318 VERIFY_IS_APPROX(refS.adjoint(), refS);
319 VERIFY_IS_APPROX(mS.transpose().conjugate(), mS);
320 VERIFY_IS_APPROX(mS, refS);
321 VERIFY_IS_APPROX(x=mS*b, refX=refS*b);
322 VERIFY_IS_APPROX(x=mUp.template marked<UpperTriangular|SelfAdjoint>()*b, refX=refS*b);
323 VERIFY_IS_APPROX(x=mLo.template marked<LowerTriangular|SelfAdjoint>()*b, refX=refS*b);
324 VERIFY_IS_APPROX(x=mS.template marked<SelfAdjoint>()*b, refX=refS*b);
325 }
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000326
327 // test prune
328 {
329 SparseMatrixType m2(rows, rows);
330 DenseMatrix refM2(rows, rows);
331 refM2.setZero();
332 int countFalseNonZero = 0;
333 int countTrueNonZero = 0;
334 m2.startFill();
335 for (int j=0; j<m2.outerSize(); ++j)
336 for (int i=0; i<m2.innerSize(); ++i)
337 {
338 float x = ei_random<float>(0,1);
339 if (x<0.1)
340 {
341 // do nothing
342 }
343 else if (x<0.5)
344 {
345 countFalseNonZero++;
346 m2.fill(i,j) = Scalar(0);
347 }
348 else
349 {
350 countTrueNonZero++;
351 m2.fill(i,j) = refM2(i,j) = Scalar(1);
352 }
353 }
354 m2.endFill();
355 VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros());
356 VERIFY_IS_APPROX(m2, refM2);
357 m2.prune(1);
358 VERIFY(countTrueNonZero==m2.nonZeros());
359 VERIFY_IS_APPROX(m2, refM2);
360 }
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000361}
362
363void test_sparse_basic()
364{
365 for(int i = 0; i < g_repeat; i++) {
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000366 CALL_SUBTEST( sparse_basic(SparseMatrix<double>(8, 8)) );
367 CALL_SUBTEST( sparse_basic(SparseMatrix<std::complex<double> >(16, 16)) );
368 CALL_SUBTEST( sparse_basic(SparseMatrix<double>(33, 33)) );
Gael Guennebaud178858f2009-01-19 15:20:45 +0000369
370 CALL_SUBTEST( sparse_basic(DynamicSparseMatrix<double>(8, 8)) );
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000371 }
372}