blob: 93065bbde9b7121af186af3a3ea59e6e92901832 [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
Gael Guennebaudd3dcb042009-01-23 09:50:16 +000027template<typename SetterType,typename DenseType, typename Scalar, int Options>
28bool test_random_setter(SparseMatrix<Scalar,Options>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
Gael Guennebaud86ccd992008-11-05 13:47:55 +000029{
Gael Guennebaudd3dcb042009-01-23 09:50:16 +000030 typedef SparseMatrix<Scalar,Options> SparseType;
Gael Guennebaud86ccd992008-11-05 13:47:55 +000031 {
32 sm.setZero();
33 SetterType w(sm);
34 std::vector<Vector2i> remaining = nonzeroCoords;
35 while(!remaining.empty())
36 {
37 int i = ei_random<int>(0,remaining.size()-1);
38 w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
39 remaining[i] = remaining.back();
40 remaining.pop_back();
41 }
42 }
43 return sm.isApprox(ref);
44}
45
Gael Guennebaud178858f2009-01-19 15:20:45 +000046template<typename SetterType,typename DenseType, typename T>
47bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords)
Gael Guennebaud86ccd992008-11-05 13:47:55 +000048{
Gael Guennebaud178858f2009-01-19 15:20:45 +000049 sm.setZero();
50 std::vector<Vector2i> remaining = nonzeroCoords;
51 while(!remaining.empty())
52 {
53 int i = ei_random<int>(0,remaining.size()-1);
54 sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y());
55 remaining[i] = remaining.back();
56 remaining.pop_back();
57 }
58 return sm.isApprox(ref);
59}
60
61template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref)
62{
63 const int rows = ref.rows();
64 const int cols = ref.cols();
65 typedef typename SparseMatrixType::Scalar Scalar;
66 enum { Flags = SparseMatrixType::Flags };
67
Gael Guennebaud86ccd992008-11-05 13:47:55 +000068 double density = std::max(8./(rows*cols), 0.01);
69 typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
70 typedef Matrix<Scalar,Dynamic,1> DenseVector;
71 Scalar eps = 1e-6;
72
Gael Guennebaud178858f2009-01-19 15:20:45 +000073 SparseMatrixType m(rows, cols);
Gael Guennebaud86ccd992008-11-05 13:47:55 +000074 DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
75 DenseVector vec1 = DenseVector::Random(rows);
Gael Guennebaud2d534662009-01-14 21:27:54 +000076 Scalar s1 = ei_random<Scalar>();
Gael Guennebaud86ccd992008-11-05 13:47:55 +000077
78 std::vector<Vector2i> zeroCoords;
79 std::vector<Vector2i> nonzeroCoords;
80 initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
Gael Guennebaud178858f2009-01-19 15:20:45 +000081
Gael Guennebaud86ccd992008-11-05 13:47:55 +000082 if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
83 return;
84
85 // test coeff and coeffRef
86 for (int i=0; i<(int)zeroCoords.size(); ++i)
87 {
88 VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
Gael Guennebaud178858f2009-01-19 15:20:45 +000089 if(ei_is_same_type<SparseMatrixType,SparseMatrix<Scalar,Flags> >::ret)
90 VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
Gael Guennebaud86ccd992008-11-05 13:47:55 +000091 }
92 VERIFY_IS_APPROX(m, refMat);
93
94 m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
95 refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
96
97 VERIFY_IS_APPROX(m, refMat);
Gael Guennebaudc4c70662009-01-14 14:24:10 +000098 /*
Gael Guennebaud86ccd992008-11-05 13:47:55 +000099 // test InnerIterators and Block expressions
100 for (int t=0; t<10; ++t)
101 {
102 int j = ei_random<int>(0,cols-1);
103 int i = ei_random<int>(0,rows-1);
104 int w = ei_random<int>(1,cols-j-1);
105 int h = ei_random<int>(1,rows-i-1);
106
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000107// VERIFY_IS_APPROX(m.block(i,j,h,w), refMat.block(i,j,h,w));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000108 for(int c=0; c<w; c++)
109 {
110 VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c));
111 for(int r=0; r<h; r++)
112 {
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000113// 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 +0000114 }
115 }
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000116// for(int r=0; r<h; r++)
117// {
118// VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r));
119// for(int c=0; c<w; c++)
120// {
121// VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c));
122// }
123// }
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000124 }
125
126 for(int c=0; c<cols; c++)
127 {
128 VERIFY_IS_APPROX(m.col(c) + m.col(c), (m + m).col(c));
129 VERIFY_IS_APPROX(m.col(c) + m.col(c), refMat.col(c) + refMat.col(c));
130 }
131
132 for(int r=0; r<rows; r++)
133 {
134 VERIFY_IS_APPROX(m.row(r) + m.row(r), (m + m).row(r));
135 VERIFY_IS_APPROX(m.row(r) + m.row(r), refMat.row(r) + refMat.row(r));
136 }
137 */
138
139 // test SparseSetters
140 // coherent setter
141 // TODO extend the MatrixSetter
142// {
143// m.setZero();
144// VERIFY_IS_NOT_APPROX(m, refMat);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000145// SparseSetter<SparseMatrixType, FullyCoherentAccessPattern> w(m);
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000146// for (int i=0; i<nonzeroCoords.size(); ++i)
147// {
148// w->coeffRef(nonzeroCoords[i].x(),nonzeroCoords[i].y()) = refMat.coeff(nonzeroCoords[i].x(),nonzeroCoords[i].y());
149// }
150// }
151// VERIFY_IS_APPROX(m, refMat);
152
153 // random setter
154// {
155// m.setZero();
156// VERIFY_IS_NOT_APPROX(m, refMat);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000157// SparseSetter<SparseMatrixType, RandomAccessPattern> w(m);
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000158// std::vector<Vector2i> remaining = nonzeroCoords;
159// while(!remaining.empty())
160// {
161// int i = ei_random<int>(0,remaining.size()-1);
162// w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
163// remaining[i] = remaining.back();
164// remaining.pop_back();
165// }
166// }
167// VERIFY_IS_APPROX(m, refMat);
168
Gael Guennebaud178858f2009-01-19 15:20:45 +0000169 VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) ));
Gael Guennebaud6a722602009-01-23 12:26:32 +0000170 #ifdef EIGEN_UNORDERED_MAP_SUPPORT
171 VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdUnorderedMapTraits> >(m,refMat,nonzeroCoords) ));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000172 #endif
173 #ifdef _DENSE_HASH_MAP_H_
Gael Guennebaud178858f2009-01-19 15:20:45 +0000174 VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000175 #endif
176 #ifdef _SPARSE_HASH_MAP_H_
Gael Guennebaud178858f2009-01-19 15:20:45 +0000177 VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000178 #endif
Gael Guennebaud5015e482008-12-11 18:26:24 +0000179
180 // test fillrand
181 {
182 DenseMatrix m1(rows,cols);
183 m1.setZero();
Gael Guennebaud178858f2009-01-19 15:20:45 +0000184 SparseMatrixType m2(rows,cols);
Gael Guennebaud5015e482008-12-11 18:26:24 +0000185 m2.startFill();
186 for (int j=0; j<cols; ++j)
187 {
188 for (int k=0; k<rows/2; ++k)
189 {
190 int i = ei_random<int>(0,rows-1);
191 if (m1.coeff(i,j)==Scalar(0))
192 m2.fillrand(i,j) = m1(i,j) = ei_random<Scalar>();
193 }
194 }
195 m2.endFill();
Gael Guennebaud178858f2009-01-19 15:20:45 +0000196 //std::cerr << m1 << "\n\n" << m2 << "\n";
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000197 VERIFY_IS_APPROX(m2,m1);
Gael Guennebaud5015e482008-12-11 18:26:24 +0000198 }
Gael Guennebaud9a4b7992009-01-15 14:16:41 +0000199
Gael Guennebaud87241082009-01-15 13:30:50 +0000200 // test RandomSetter
Gael Guennebaud178858f2009-01-19 15:20:45 +0000201 /*{
202 SparseMatrixType m1(rows,cols), m2(rows,cols);
Gael Guennebaud87241082009-01-15 13:30:50 +0000203 DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
204 initSparse<Scalar>(density, refM1, m1);
205 {
Gael Guennebaud178858f2009-01-19 15:20:45 +0000206 Eigen::RandomSetter<SparseMatrixType > setter(m2);
Gael Guennebaud87241082009-01-15 13:30:50 +0000207 for (int j=0; j<m1.outerSize(); ++j)
Gael Guennebaud178858f2009-01-19 15:20:45 +0000208 for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i)
Gael Guennebaud87241082009-01-15 13:30:50 +0000209 setter(i.index(), j) = i.value();
210 }
211 VERIFY_IS_APPROX(m1, m2);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000212 }*/
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000213// std::cerr << m.transpose() << "\n\n" << refMat.transpose() << "\n\n";
214// VERIFY_IS_APPROX(m, refMat);
215
Gael Guennebaud2d534662009-01-14 21:27:54 +0000216 // test basic computations
217 {
218 DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
219 DenseMatrix refM2 = DenseMatrix::Zero(rows, rows);
220 DenseMatrix refM3 = DenseMatrix::Zero(rows, rows);
221 DenseMatrix refM4 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000222 SparseMatrixType m1(rows, rows);
223 SparseMatrixType m2(rows, rows);
224 SparseMatrixType m3(rows, rows);
225 SparseMatrixType m4(rows, rows);
Gael Guennebaud2d534662009-01-14 21:27:54 +0000226 initSparse<Scalar>(density, refM1, m1);
227 initSparse<Scalar>(density, refM2, m2);
228 initSparse<Scalar>(density, refM3, m3);
229 initSparse<Scalar>(density, refM4, m4);
230
231 VERIFY_IS_APPROX(m1+m2, refM1+refM2);
232 VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3);
233 VERIFY_IS_APPROX(m3.cwise()*(m1+m2), refM3.cwise()*(refM1+refM2));
234 VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2);
235
236 VERIFY_IS_APPROX(m1*=s1, refM1*=s1);
237 VERIFY_IS_APPROX(m1/=s1, refM1/=s1);
238
239 refM4.setRandom();
240 // sparse cwise* dense
241 VERIFY_IS_APPROX(m3.cwise()*refM4, refM3.cwise()*refM4);
242// VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4);
243 }
244
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000245 // test innerVector()
246 {
247 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000248 SparseMatrixType m2(rows, rows);
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000249 initSparse<Scalar>(density, refMat2, m2);
250 int j0 = ei_random(0,rows-1);
251 int j1 = ei_random(0,rows-1);
Gael Guennebaud2d534662009-01-14 21:27:54 +0000252 VERIFY_IS_APPROX(m2.innerVector(j0), refMat2.col(j0));
253 VERIFY_IS_APPROX(m2.innerVector(j0)+m2.innerVector(j1), refMat2.col(j0)+refMat2.col(j1));
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000254 }
255
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000256 // test transpose
257 {
258 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000259 SparseMatrixType m2(rows, rows);
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000260 initSparse<Scalar>(density, refMat2, m2);
261 VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval());
262 VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose());
263 }
264
265 // test matrix product
266 {
267 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
268 DenseMatrix refMat3 = DenseMatrix::Zero(rows, rows);
269 DenseMatrix refMat4 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud0b606dc2009-01-14 17:41:55 +0000270 DenseMatrix dm4 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000271 SparseMatrixType m2(rows, rows);
272 SparseMatrixType m3(rows, rows);
273 SparseMatrixType m4(rows, rows);
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000274 initSparse<Scalar>(density, refMat2, m2);
275 initSparse<Scalar>(density, refMat3, m3);
276 initSparse<Scalar>(density, refMat4, m4);
277 VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3);
278 VERIFY_IS_APPROX(m4=m2.transpose()*m3, refMat4=refMat2.transpose()*refMat3);
279 VERIFY_IS_APPROX(m4=m2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
280 VERIFY_IS_APPROX(m4=m2*m3.transpose(), refMat4=refMat2*refMat3.transpose());
Gael Guennebaud2d534662009-01-14 21:27:54 +0000281
Gael Guennebaud0b606dc2009-01-14 17:41:55 +0000282 // sparse * dense
283 VERIFY_IS_APPROX(dm4=m2*refMat3, refMat4=refMat2*refMat3);
284 VERIFY_IS_APPROX(dm4=m2*refMat3.transpose(), refMat4=refMat2*refMat3.transpose());
285 VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3, refMat4=refMat2.transpose()*refMat3);
286 VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
Gael Guennebaud2d534662009-01-14 21:27:54 +0000287
Gael Guennebaud0b606dc2009-01-14 17:41:55 +0000288 // dense * sparse
289 VERIFY_IS_APPROX(dm4=refMat2*m3, refMat4=refMat2*refMat3);
290 VERIFY_IS_APPROX(dm4=refMat2*m3.transpose(), refMat4=refMat2*refMat3.transpose());
291 VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3, refMat4=refMat2.transpose()*refMat3);
292 VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000293 }
Gael Guennebaudccdcebc2009-01-15 18:52:14 +0000294
295 // test self adjoint products
296 {
297 DenseMatrix b = DenseMatrix::Random(rows, rows);
298 DenseMatrix x = DenseMatrix::Random(rows, rows);
299 DenseMatrix refX = DenseMatrix::Random(rows, rows);
300 DenseMatrix refUp = DenseMatrix::Zero(rows, rows);
301 DenseMatrix refLo = DenseMatrix::Zero(rows, rows);
302 DenseMatrix refS = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000303 SparseMatrixType mUp(rows, rows);
304 SparseMatrixType mLo(rows, rows);
305 SparseMatrixType mS(rows, rows);
Gael Guennebaudccdcebc2009-01-15 18:52:14 +0000306 do {
307 initSparse<Scalar>(density, refUp, mUp, ForceRealDiag|/*ForceNonZeroDiag|*/MakeUpperTriangular);
308 } while (refUp.isZero());
309 refLo = refUp.transpose().conjugate();
310 mLo = mUp.transpose().conjugate();
311 refS = refUp + refLo;
312 refS.diagonal() *= 0.5;
313 mS = mUp + mLo;
314 for (int k=0; k<mS.outerSize(); ++k)
Gael Guennebaud178858f2009-01-19 15:20:45 +0000315 for (typename SparseMatrixType::InnerIterator it(mS,k); it; ++it)
Gael Guennebaudccdcebc2009-01-15 18:52:14 +0000316 if (it.index() == k)
317 it.valueRef() *= 0.5;
318
319 VERIFY_IS_APPROX(refS.adjoint(), refS);
320 VERIFY_IS_APPROX(mS.transpose().conjugate(), mS);
321 VERIFY_IS_APPROX(mS, refS);
322 VERIFY_IS_APPROX(x=mS*b, refX=refS*b);
323 VERIFY_IS_APPROX(x=mUp.template marked<UpperTriangular|SelfAdjoint>()*b, refX=refS*b);
324 VERIFY_IS_APPROX(x=mLo.template marked<LowerTriangular|SelfAdjoint>()*b, refX=refS*b);
325 VERIFY_IS_APPROX(x=mS.template marked<SelfAdjoint>()*b, refX=refS*b);
326 }
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000327
328 // test prune
329 {
330 SparseMatrixType m2(rows, rows);
331 DenseMatrix refM2(rows, rows);
332 refM2.setZero();
333 int countFalseNonZero = 0;
334 int countTrueNonZero = 0;
335 m2.startFill();
336 for (int j=0; j<m2.outerSize(); ++j)
337 for (int i=0; i<m2.innerSize(); ++i)
338 {
339 float x = ei_random<float>(0,1);
340 if (x<0.1)
341 {
342 // do nothing
343 }
344 else if (x<0.5)
345 {
346 countFalseNonZero++;
347 m2.fill(i,j) = Scalar(0);
348 }
349 else
350 {
351 countTrueNonZero++;
352 m2.fill(i,j) = refM2(i,j) = Scalar(1);
353 }
354 }
355 m2.endFill();
356 VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros());
357 VERIFY_IS_APPROX(m2, refM2);
358 m2.prune(1);
359 VERIFY(countTrueNonZero==m2.nonZeros());
360 VERIFY_IS_APPROX(m2, refM2);
361 }
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000362}
363
364void test_sparse_basic()
365{
366 for(int i = 0; i < g_repeat; i++) {
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000367 CALL_SUBTEST( sparse_basic(SparseMatrix<double>(8, 8)) );
368 CALL_SUBTEST( sparse_basic(SparseMatrix<std::complex<double> >(16, 16)) );
369 CALL_SUBTEST( sparse_basic(SparseMatrix<double>(33, 33)) );
Gael Guennebaud178858f2009-01-19 15:20:45 +0000370
371 CALL_SUBTEST( sparse_basic(DynamicSparseMatrix<double>(8, 8)) );
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000372 }
373}