blob: c890d952808e03230bd540adbe133ef543a089ef [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
156// {
157// m.setZero();
158// VERIFY_IS_NOT_APPROX(m, refMat);
159// // RandomSetter<SparseMatrix<Scalar> > w(m);
160// RandomSetter<SparseMatrix<Scalar>, GoogleDenseHashMapTraits > w(m);
161// // RandomSetter<SparseMatrix<Scalar>, GnuHashMapTraits > w(m);
162// std::vector<Vector2i> remaining = nonzeroCoords;
163// while(!remaining.empty())
164// {
165// int i = ei_random<int>(0,remaining.size()-1);
166// w(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y());
167// remaining[i] = remaining.back();
168// remaining.pop_back();
169// }
170// }
171// std::cerr << m.transpose() << "\n\n" << refMat.transpose() << "\n\n";
172// VERIFY_IS_APPROX(m, refMat);
173
174 // test transpose
175 {
176 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
177 SparseMatrix<Scalar> m2(rows, rows);
178 initSparse<Scalar>(density, refMat2, m2);
179 VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval());
180 VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose());
181 }
182
183 // test matrix product
184 {
185 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
186 DenseMatrix refMat3 = DenseMatrix::Zero(rows, rows);
187 DenseMatrix refMat4 = DenseMatrix::Zero(rows, rows);
188 SparseMatrix<Scalar> m2(rows, rows);
189 SparseMatrix<Scalar> m3(rows, rows);
190 SparseMatrix<Scalar> m4(rows, rows);
191 initSparse<Scalar>(density, refMat2, m2);
192 initSparse<Scalar>(density, refMat3, m3);
193 initSparse<Scalar>(density, refMat4, m4);
194 VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3);
195 VERIFY_IS_APPROX(m4=m2.transpose()*m3, refMat4=refMat2.transpose()*refMat3);
196 VERIFY_IS_APPROX(m4=m2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose());
197 VERIFY_IS_APPROX(m4=m2*m3.transpose(), refMat4=refMat2*refMat3.transpose());
198 }
199}
200
201void test_sparse_basic()
202{
203 for(int i = 0; i < g_repeat; i++) {
204 CALL_SUBTEST( sparse_basic<double>(8, 8) );
205 CALL_SUBTEST( sparse_basic<std::complex<double> >(16, 16) );
206 CALL_SUBTEST( sparse_basic<double>(33, 33) );
207 }
208}