blob: c50682810733ef163ceefdc222ccc887231a5bdc [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
45template<typename Scalar> void sparse_basic(int rows, int cols)
46{
47 double density = std::max(8./(rows*cols), 0.01);
48 typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
49 typedef Matrix<Scalar,Dynamic,1> DenseVector;
50 Scalar eps = 1e-6;
51
52 SparseMatrix<Scalar> m(rows, cols);
53 DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
54 DenseVector vec1 = DenseVector::Random(rows);
55
56 std::vector<Vector2i> zeroCoords;
57 std::vector<Vector2i> nonzeroCoords;
58 initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
59
60 if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
61 return;
62
63 // test coeff and coeffRef
64 for (int i=0; i<(int)zeroCoords.size(); ++i)
65 {
66 VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
67 VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
68 }
69 VERIFY_IS_APPROX(m, refMat);
70
71 m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
72 refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
73
74 VERIFY_IS_APPROX(m, refMat);
75/*
76 // test InnerIterators and Block expressions
77 for (int t=0; t<10; ++t)
78 {
79 int j = ei_random<int>(0,cols-1);
80 int i = ei_random<int>(0,rows-1);
81 int w = ei_random<int>(1,cols-j-1);
82 int h = ei_random<int>(1,rows-i-1);
83
84 VERIFY_IS_APPROX(m.block(i,j,h,w), refMat.block(i,j,h,w));
85 for(int c=0; c<w; c++)
86 {
87 VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c));
88 for(int r=0; r<h; r++)
89 {
90 VERIFY_IS_APPROX(m.block(i,j,h,w).col(c).coeff(r), refMat.block(i,j,h,w).col(c).coeff(r));
91 }
92 }
93 for(int r=0; r<h; r++)
94 {
95 VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r));
96 for(int c=0; c<w; c++)
97 {
98 VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c));
99 }
100 }
101 }
102
103 for(int c=0; c<cols; c++)
104 {
105 VERIFY_IS_APPROX(m.col(c) + m.col(c), (m + m).col(c));
106 VERIFY_IS_APPROX(m.col(c) + m.col(c), refMat.col(c) + refMat.col(c));
107 }
108
109 for(int r=0; r<rows; r++)
110 {
111 VERIFY_IS_APPROX(m.row(r) + m.row(r), (m + m).row(r));
112 VERIFY_IS_APPROX(m.row(r) + m.row(r), refMat.row(r) + refMat.row(r));
113 }
114 */
115
116 // test SparseSetters
117 // coherent setter
118 // TODO extend the MatrixSetter
119// {
120// m.setZero();
121// VERIFY_IS_NOT_APPROX(m, refMat);
122// SparseSetter<SparseMatrix<Scalar>, FullyCoherentAccessPattern> w(m);
123// for (int i=0; i<nonzeroCoords.size(); ++i)
124// {
125// w->coeffRef(nonzeroCoords[i].x(),nonzeroCoords[i].y()) = refMat.coeff(nonzeroCoords[i].x(),nonzeroCoords[i].y());
126// }
127// }
128// VERIFY_IS_APPROX(m, refMat);
129
130 // random setter
131// {
132// m.setZero();
133// VERIFY_IS_NOT_APPROX(m, refMat);
134// SparseSetter<SparseMatrix<Scalar>, RandomAccessPattern> w(m);
135// std::vector<Vector2i> remaining = nonzeroCoords;
136// while(!remaining.empty())
137// {
138// int i = ei_random<int>(0,remaining.size()-1);
139// w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
140// remaining[i] = remaining.back();
141// remaining.pop_back();
142// }
143// }
144// VERIFY_IS_APPROX(m, refMat);
145
146 VERIFY(( test_random_setter<RandomSetter<SparseMatrix<Scalar>, StdMapTraits> >(m,refMat,nonzeroCoords) ));
147 #ifdef _HASH_MAP
148 VERIFY(( test_random_setter<RandomSetter<SparseMatrix<Scalar>, GnuHashMapTraits> >(m,refMat,nonzeroCoords) ));
149 #endif
150 #ifdef _DENSE_HASH_MAP_H_
151 VERIFY(( test_random_setter<RandomSetter<SparseMatrix<Scalar>, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) ));
152 #endif
153 #ifdef _SPARSE_HASH_MAP_H_
154 VERIFY(( test_random_setter<RandomSetter<SparseMatrix<Scalar>, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
155 #endif
Gael Guennebaud5015e482008-12-11 18:26:24 +0000156
157 // test fillrand
158 {
159 DenseMatrix m1(rows,cols);
160 m1.setZero();
161 SparseMatrix<Scalar> m2(rows,cols);
162 m2.startFill();
163 for (int j=0; j<cols; ++j)
164 {
165 for (int k=0; k<rows/2; ++k)
166 {
167 int i = ei_random<int>(0,rows-1);
168 if (m1.coeff(i,j)==Scalar(0))
169 m2.fillrand(i,j) = m1(i,j) = ei_random<Scalar>();
170 }
171 }
172 m2.endFill();
173 std::cerr << m1 << "\n\n" << m2 << "\n";
174 VERIFY_IS_APPROX(m1,m2);
175 }
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000176// {
177// m.setZero();
178// VERIFY_IS_NOT_APPROX(m, refMat);
179// // RandomSetter<SparseMatrix<Scalar> > w(m);
180// RandomSetter<SparseMatrix<Scalar>, GoogleDenseHashMapTraits > w(m);
181// // RandomSetter<SparseMatrix<Scalar>, GnuHashMapTraits > w(m);
182// std::vector<Vector2i> remaining = nonzeroCoords;
183// while(!remaining.empty())
184// {
185// int i = ei_random<int>(0,remaining.size()-1);
186// w(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
187// remaining[i] = remaining.back();
188// remaining.pop_back();
189// }
190// }
191// std::cerr << m.transpose() << "\n\n" << refMat.transpose() << "\n\n";
192// VERIFY_IS_APPROX(m, refMat);
193
194 // test transpose
195 {
196 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
197 SparseMatrix<Scalar> m2(rows, rows);
198 initSparse<Scalar>(density, refMat2, m2);
199 VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval());
200 VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose());
201 }
202
203 // test matrix product
204 {
205 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
206 DenseMatrix refMat3 = DenseMatrix::Zero(rows, rows);
207 DenseMatrix refMat4 = DenseMatrix::Zero(rows, rows);
208 SparseMatrix<Scalar> m2(rows, rows);
209 SparseMatrix<Scalar> m3(rows, rows);
210 SparseMatrix<Scalar> m4(rows, rows);
211 initSparse<Scalar>(density, refMat2, m2);
212 initSparse<Scalar>(density, refMat3, m3);
213 initSparse<Scalar>(density, refMat4, m4);
214 VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3);
215 VERIFY_IS_APPROX(m4=m2.transpose()*m3, refMat4=refMat2.transpose()*refMat3);
216 VERIFY_IS_APPROX(m4=m2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
217 VERIFY_IS_APPROX(m4=m2*m3.transpose(), refMat4=refMat2*refMat3.transpose());
218 }
219}
220
221void test_sparse_basic()
222{
223 for(int i = 0; i < g_repeat; i++) {
224 CALL_SUBTEST( sparse_basic<double>(8, 8) );
225 CALL_SUBTEST( sparse_basic<std::complex<double> >(16, 16) );
226 CALL_SUBTEST( sparse_basic<double>(33, 33) );
227 }
228}