blob: 0a25884ca3bd1dfc3b73ef78ce2ca7876dba8b8e [file] [log] [blame]
Gael Guennebaud709e9032009-01-07 17:01:57 +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 Scalar> void sparse_vector(int rows, int cols)
28{
29 double densityMat = std::max(8./(rows*cols), 0.01);
30 double densityVec = std::max(8./float(rows), 0.1);
31 typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
32 typedef Matrix<Scalar,Dynamic,1> DenseVector;
33 typedef SparseVector<Scalar> SparseVectorType;
34 typedef SparseMatrix<Scalar> SparseMatrixType;
35 Scalar eps = 1e-6;
36
37 SparseMatrixType m1(rows,cols);
38 SparseVectorType v1(rows), v2(rows), v3(rows);
39 DenseMatrix refM1 = DenseMatrix::Zero(rows, cols);
40 DenseVector refV1 = DenseVector::Random(rows),
41 refV2 = DenseVector::Random(rows),
42 refV3 = DenseVector::Random(rows);
43
44 std::vector<int> zerocoords, nonzerocoords;
45 initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords);
46 initSparse<Scalar>(densityMat, refM1, m1);
47
48 initSparse<Scalar>(densityVec, refV2, v2);
49 initSparse<Scalar>(densityVec, refV3, v3);
50
51 Scalar s1 = ei_random<Scalar>();
52
53 // test coeff and coeffRef
54 for (unsigned int i=0; i<zerocoords.size(); ++i)
55 {
56 VERIFY_IS_MUCH_SMALLER_THAN( v1.coeff(zerocoords[i]), eps );
57 VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 );
58 }
59 {
60 VERIFY(int(nonzerocoords.size()) == v1.nonZeros());
61 int j=0;
62 for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j)
63 {
64 VERIFY(nonzerocoords[j]==it.index());
65 VERIFY(it.value()==v1[it.index()]);
66 }
67 }
68 VERIFY_IS_APPROX(v1, refV1);
69
70 v1.coeffRef(nonzerocoords[0]) = Scalar(5);
71 refV1.coeffRef(nonzerocoords[0]) = Scalar(5);
72 VERIFY_IS_APPROX(v1, refV1);
73
74 VERIFY_IS_APPROX(v1+v2, refV1+refV2);
75 VERIFY_IS_APPROX(v1+v2+v3, refV1+refV2+refV3);
76
77 VERIFY_IS_APPROX(v1*s1-v2, refV1*s1-refV2);
78
79 std::cerr << v1.dot(v2) << " == " << refV1.dot(refV2) << "\n";
80 VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2));
81
82}
83
84void test_sparse_vector()
85{
86 for(int i = 0; i < g_repeat; i++) {
87 CALL_SUBTEST( sparse_vector<double>(8, 8) );
88// CALL_SUBTEST( sparse_vector<std::complex<double> >(16, 16) );
89 CALL_SUBTEST( sparse_vector<double>(299, 535) );
90 }
91}
92