blob: 8dfa0baff3ae39a2741d170e1558b53d5897ac2c [file] [log] [blame]
Gael Guennebaud86ccd992008-11-05 13:47:55 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Gael Guennebaud86ccd992008-11-05 13:47:55 +00003//
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 Guennebaud178858f2009-01-19 15:20:45 +000027template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref)
28{
Hauke Heibelf1679c72010-06-20 17:37:56 +020029 typedef typename SparseMatrixType::Index Index;
30
31 const Index rows = ref.rows();
32 const Index cols = ref.cols();
Gael Guennebaud178858f2009-01-19 15:20:45 +000033 typedef typename SparseMatrixType::Scalar Scalar;
34 enum { Flags = SparseMatrixType::Flags };
Gael Guennebaud9f795582009-12-16 19:18:40 +010035
Gael Guennebaud86ccd992008-11-05 13:47:55 +000036 double density = std::max(8./(rows*cols), 0.01);
37 typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
38 typedef Matrix<Scalar,Dynamic,1> DenseVector;
39 Scalar eps = 1e-6;
40
Gael Guennebaud178858f2009-01-19 15:20:45 +000041 SparseMatrixType m(rows, cols);
Gael Guennebaud86ccd992008-11-05 13:47:55 +000042 DenseMatrix refMat = DenseMatrix::Zero(rows, cols);
43 DenseVector vec1 = DenseVector::Random(rows);
Benoit Jacob47160402010-10-25 10:15:22 -040044 Scalar s1 = internal::random<Scalar>();
Gael Guennebaud86ccd992008-11-05 13:47:55 +000045
46 std::vector<Vector2i> zeroCoords;
47 std::vector<Vector2i> nonzeroCoords;
48 initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords);
Gael Guennebaud9f795582009-12-16 19:18:40 +010049
Gael Guennebaud86ccd992008-11-05 13:47:55 +000050 if (zeroCoords.size()==0 || nonzeroCoords.size()==0)
51 return;
52
53 // test coeff and coeffRef
54 for (int i=0; i<(int)zeroCoords.size(); ++i)
55 {
56 VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps );
Benoit Jacob47160402010-10-25 10:15:22 -040057 if(internal::is_same_type<SparseMatrixType,SparseMatrix<Scalar,Flags> >::ret)
Gael Guennebaud178858f2009-01-19 15:20:45 +000058 VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 );
Gael Guennebaud86ccd992008-11-05 13:47:55 +000059 }
60 VERIFY_IS_APPROX(m, refMat);
61
62 m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
63 refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5);
64
65 VERIFY_IS_APPROX(m, refMat);
Gael Guennebaudc4c70662009-01-14 14:24:10 +000066 /*
Gael Guennebaud86ccd992008-11-05 13:47:55 +000067 // test InnerIterators and Block expressions
68 for (int t=0; t<10; ++t)
69 {
Benoit Jacob47160402010-10-25 10:15:22 -040070 int j = internal::random<int>(0,cols-1);
71 int i = internal::random<int>(0,rows-1);
72 int w = internal::random<int>(1,cols-j-1);
73 int h = internal::random<int>(1,rows-i-1);
Gael Guennebaud86ccd992008-11-05 13:47:55 +000074
Gael Guennebaudc4c70662009-01-14 14:24:10 +000075// VERIFY_IS_APPROX(m.block(i,j,h,w), refMat.block(i,j,h,w));
Gael Guennebaud86ccd992008-11-05 13:47:55 +000076 for(int c=0; c<w; c++)
77 {
78 VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c));
79 for(int r=0; r<h; r++)
80 {
Gael Guennebaudc4c70662009-01-14 14:24:10 +000081// 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 +000082 }
83 }
Gael Guennebaudc4c70662009-01-14 14:24:10 +000084// for(int r=0; r<h; r++)
85// {
86// VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r));
87// for(int c=0; c<w; c++)
88// {
89// VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c));
90// }
91// }
Gael Guennebaud86ccd992008-11-05 13:47:55 +000092 }
93
94 for(int c=0; c<cols; c++)
95 {
96 VERIFY_IS_APPROX(m.col(c) + m.col(c), (m + m).col(c));
97 VERIFY_IS_APPROX(m.col(c) + m.col(c), refMat.col(c) + refMat.col(c));
98 }
99
100 for(int r=0; r<rows; r++)
101 {
102 VERIFY_IS_APPROX(m.row(r) + m.row(r), (m + m).row(r));
103 VERIFY_IS_APPROX(m.row(r) + m.row(r), refMat.row(r) + refMat.row(r));
104 }
105 */
106
Gael Guennebaud28293142009-05-04 14:25:12 +0000107 // test insert (inner random)
Gael Guennebaud5015e482008-12-11 18:26:24 +0000108 {
109 DenseMatrix m1(rows,cols);
110 m1.setZero();
Gael Guennebaud178858f2009-01-19 15:20:45 +0000111 SparseMatrixType m2(rows,cols);
Gael Guennebaud28293142009-05-04 14:25:12 +0000112 m2.reserve(10);
Gael Guennebaud5015e482008-12-11 18:26:24 +0000113 for (int j=0; j<cols; ++j)
114 {
115 for (int k=0; k<rows/2; ++k)
116 {
Benoit Jacob47160402010-10-25 10:15:22 -0400117 int i = internal::random<int>(0,rows-1);
Gael Guennebaud5015e482008-12-11 18:26:24 +0000118 if (m1.coeff(i,j)==Scalar(0))
Benoit Jacob47160402010-10-25 10:15:22 -0400119 m2.insert(i,j) = m1(i,j) = internal::random<Scalar>();
Gael Guennebaud5015e482008-12-11 18:26:24 +0000120 }
121 }
Gael Guennebaud28293142009-05-04 14:25:12 +0000122 m2.finalize();
123 VERIFY_IS_APPROX(m2,m1);
124 }
Gael Guennebaud9f795582009-12-16 19:18:40 +0100125
Gael Guennebaud28293142009-05-04 14:25:12 +0000126 // test insert (fully random)
127 {
128 DenseMatrix m1(rows,cols);
129 m1.setZero();
130 SparseMatrixType m2(rows,cols);
131 m2.reserve(10);
132 for (int k=0; k<rows*cols; ++k)
133 {
Benoit Jacob47160402010-10-25 10:15:22 -0400134 int i = internal::random<int>(0,rows-1);
135 int j = internal::random<int>(0,cols-1);
Gael Guennebaud28293142009-05-04 14:25:12 +0000136 if (m1.coeff(i,j)==Scalar(0))
Benoit Jacob47160402010-10-25 10:15:22 -0400137 m2.insert(i,j) = m1(i,j) = internal::random<Scalar>();
Gael Guennebaud28293142009-05-04 14:25:12 +0000138 }
139 m2.finalize();
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000140 VERIFY_IS_APPROX(m2,m1);
Gael Guennebaud5015e482008-12-11 18:26:24 +0000141 }
Gael Guennebaud9f795582009-12-16 19:18:40 +0100142
Gael Guennebaud2d534662009-01-14 21:27:54 +0000143 // test basic computations
144 {
145 DenseMatrix refM1 = DenseMatrix::Zero(rows, rows);
146 DenseMatrix refM2 = DenseMatrix::Zero(rows, rows);
147 DenseMatrix refM3 = DenseMatrix::Zero(rows, rows);
148 DenseMatrix refM4 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000149 SparseMatrixType m1(rows, rows);
150 SparseMatrixType m2(rows, rows);
151 SparseMatrixType m3(rows, rows);
152 SparseMatrixType m4(rows, rows);
Gael Guennebaud2d534662009-01-14 21:27:54 +0000153 initSparse<Scalar>(density, refM1, m1);
154 initSparse<Scalar>(density, refM2, m2);
155 initSparse<Scalar>(density, refM3, m3);
156 initSparse<Scalar>(density, refM4, m4);
157
158 VERIFY_IS_APPROX(m1+m2, refM1+refM2);
159 VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3);
Gael Guennebaud9f795582009-12-16 19:18:40 +0100160 VERIFY_IS_APPROX(m3.cwiseProduct(m1+m2), refM3.cwiseProduct(refM1+refM2));
Gael Guennebaud2d534662009-01-14 21:27:54 +0000161 VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2);
162
163 VERIFY_IS_APPROX(m1*=s1, refM1*=s1);
164 VERIFY_IS_APPROX(m1/=s1, refM1/=s1);
Gael Guennebaud9f795582009-12-16 19:18:40 +0100165
Gael Guennebaude7c48fa2009-01-23 13:59:32 +0000166 VERIFY_IS_APPROX(m1+=m2, refM1+=refM2);
167 VERIFY_IS_APPROX(m1-=m2, refM1-=refM2);
Gael Guennebaud9f795582009-12-16 19:18:40 +0100168
Gael Guennebauda9688f02009-02-09 09:59:30 +0000169 VERIFY_IS_APPROX(m1.col(0).dot(refM2.row(0)), refM1.col(0).dot(refM2.row(0)));
Gael Guennebaud9f795582009-12-16 19:18:40 +0100170
Gael Guennebaud2d534662009-01-14 21:27:54 +0000171 refM4.setRandom();
172 // sparse cwise* dense
Gael Guennebaud9f795582009-12-16 19:18:40 +0100173 VERIFY_IS_APPROX(m3.cwiseProduct(refM4), refM3.cwiseProduct(refM4));
Gael Guennebaud2d534662009-01-14 21:27:54 +0000174// VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4);
175 }
176
Gael Guennebaudece48a62010-06-18 11:28:30 +0200177 // test transpose
178 {
179 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
180 SparseMatrixType m2(rows, rows);
181 initSparse<Scalar>(density, refMat2, m2);
182 VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval());
183 VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose());
184
185 VERIFY_IS_APPROX(SparseMatrixType(m2.adjoint()), refMat2.adjoint());
186 }
187
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000188 // test innerVector()
189 {
190 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
Gael Guennebaud178858f2009-01-19 15:20:45 +0000191 SparseMatrixType m2(rows, rows);
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000192 initSparse<Scalar>(density, refMat2, m2);
Benoit Jacob47160402010-10-25 10:15:22 -0400193 int j0 = internal::random(0,rows-1);
194 int j1 = internal::random(0,rows-1);
Gael Guennebaud2d534662009-01-14 21:27:54 +0000195 VERIFY_IS_APPROX(m2.innerVector(j0), refMat2.col(j0));
196 VERIFY_IS_APPROX(m2.innerVector(j0)+m2.innerVector(j1), refMat2.col(j0)+refMat2.col(j1));
Gael Guennebaud8ce45032009-01-27 22:48:17 +0000197 //m2.innerVector(j0) = 2*m2.innerVector(j1);
198 //refMat2.col(j0) = 2*refMat2.col(j1);
199 //VERIFY_IS_APPROX(m2, refMat2);
200 }
Gael Guennebaud9f795582009-12-16 19:18:40 +0100201
Gael Guennebaud8ce45032009-01-27 22:48:17 +0000202 // test innerVectors()
203 {
204 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
205 SparseMatrixType m2(rows, rows);
206 initSparse<Scalar>(density, refMat2, m2);
Benoit Jacob47160402010-10-25 10:15:22 -0400207 int j0 = internal::random(0,rows-2);
208 int j1 = internal::random(0,rows-2);
209 int n0 = internal::random<int>(1,rows-std::max(j0,j1));
Gael Guennebaud8ce45032009-01-27 22:48:17 +0000210 VERIFY_IS_APPROX(m2.innerVectors(j0,n0), refMat2.block(0,j0,rows,n0));
211 VERIFY_IS_APPROX(m2.innerVectors(j0,n0)+m2.innerVectors(j1,n0),
212 refMat2.block(0,j0,rows,n0)+refMat2.block(0,j1,rows,n0));
213 //m2.innerVectors(j0,n0) = m2.innerVectors(j0,n0) + m2.innerVectors(j1,n0);
214 //refMat2.block(0,j0,rows,n0) = refMat2.block(0,j0,rows,n0) + refMat2.block(0,j1,rows,n0);
Gael Guennebaudc4c70662009-01-14 14:24:10 +0000215 }
216
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000217 // test prune
218 {
219 SparseMatrixType m2(rows, rows);
220 DenseMatrix refM2(rows, rows);
221 refM2.setZero();
222 int countFalseNonZero = 0;
223 int countTrueNonZero = 0;
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000224 for (int j=0; j<m2.outerSize(); ++j)
Gael Guennebaud28293142009-05-04 14:25:12 +0000225 {
226 m2.startVec(j);
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000227 for (int i=0; i<m2.innerSize(); ++i)
228 {
Benoit Jacob47160402010-10-25 10:15:22 -0400229 float x = internal::random<float>(0,1);
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000230 if (x<0.1)
231 {
232 // do nothing
233 }
234 else if (x<0.5)
235 {
236 countFalseNonZero++;
Gael Guennebaud8710bd22010-06-02 13:32:13 +0200237 m2.insertBackByOuterInner(j,i) = Scalar(0);
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000238 }
239 else
240 {
241 countTrueNonZero++;
Gael Guennebaud8710bd22010-06-02 13:32:13 +0200242 m2.insertBackByOuterInner(j,i) = refM2(i,j) = Scalar(1);
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000243 }
244 }
Gael Guennebaud28293142009-05-04 14:25:12 +0000245 }
246 m2.finalize();
Gael Guennebaud52cf07d2009-01-21 18:46:04 +0000247 VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros());
248 VERIFY_IS_APPROX(m2, refM2);
249 m2.prune(1);
250 VERIFY(countTrueNonZero==m2.nonZeros());
251 VERIFY_IS_APPROX(m2, refM2);
252 }
Gael Guennebaudfa6d36e2010-07-22 15:57:01 +0200253
254 // test sparseView
255 {
256 DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows);
257 SparseMatrixType m2(rows, rows);
258 VERIFY_IS_APPROX(m2.eval(), refMat2.sparseView().eval());
259 }
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000260}
261
262void test_sparse_basic()
263{
264 for(int i = 0; i < g_repeat; i++) {
Benoit Jacob2840ac72009-10-28 18:19:29 -0400265 CALL_SUBTEST_1( sparse_basic(SparseMatrix<double>(8, 8)) );
266 CALL_SUBTEST_2( sparse_basic(SparseMatrix<std::complex<double> >(16, 16)) );
267 CALL_SUBTEST_1( sparse_basic(SparseMatrix<double>(33, 33)) );
Gael Guennebaud9f795582009-12-16 19:18:40 +0100268
Benoit Jacob2840ac72009-10-28 18:19:29 -0400269 CALL_SUBTEST_3( sparse_basic(DynamicSparseMatrix<double>(8, 8)) );
Gael Guennebaud86ccd992008-11-05 13:47:55 +0000270 }
271}