blob: b3249915ea57834ddc6eb6dce63dbc520a242e08 [file] [log] [blame]
Gael Guennebaud709e9032009-01-07 17:01:57 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Gael Guennebaud709e9032009-01-07 17:01:57 +00003//
Gael Guennebaud22c76092011-03-22 11:58:22 +01004// Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
Gael Guennebaud709e9032009-01-07 17:01:57 +00005//
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
Benoit Jacob47160402010-10-25 10:15:22 -040051 Scalar s1 = internal::random<Scalar>();
Gael Guennebaud709e9032009-01-07 17:01:57 +000052
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 );
Gael Guennebaud36c478c2009-01-19 22:29:28 +000057 //VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 );
Gael Guennebaud709e9032009-01-07 17:01:57 +000058 }
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());
Gael Guennebaudc4c70662009-01-14 14:24:10 +000065 VERIFY(it.value()==v1.coeff(it.index()));
66 VERIFY(it.value()==refV1.coeff(it.index()));
Gael Guennebaud709e9032009-01-07 17:01:57 +000067 }
68 }
69 VERIFY_IS_APPROX(v1, refV1);
70
71 v1.coeffRef(nonzerocoords[0]) = Scalar(5);
72 refV1.coeffRef(nonzerocoords[0]) = Scalar(5);
73 VERIFY_IS_APPROX(v1, refV1);
74
75 VERIFY_IS_APPROX(v1+v2, refV1+refV2);
76 VERIFY_IS_APPROX(v1+v2+v3, refV1+refV2+refV3);
77
78 VERIFY_IS_APPROX(v1*s1-v2, refV1*s1-refV2);
79
Gael Guennebaud2d534662009-01-14 21:27:54 +000080 VERIFY_IS_APPROX(v1*=s1, refV1*=s1);
81 VERIFY_IS_APPROX(v1/=s1, refV1/=s1);
Gael Guennebauda2324d62010-04-13 10:40:55 +020082
Gael Guennebaude7c48fa2009-01-23 13:59:32 +000083 VERIFY_IS_APPROX(v1+=v2, refV1+=refV2);
84 VERIFY_IS_APPROX(v1-=v2, refV1-=refV2);
Gael Guennebaud2d534662009-01-14 21:27:54 +000085
Gael Guennebaud709e9032009-01-07 17:01:57 +000086 VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2));
Gael Guennebauda9688f02009-02-09 09:59:30 +000087 VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2));
Gael Guennebaud709e9032009-01-07 17:01:57 +000088
Gael Guennebauda2324d62010-04-13 10:40:55 +020089 VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm());
90
Gael Guennebaud709e9032009-01-07 17:01:57 +000091}
92
93void test_sparse_vector()
94{
95 for(int i = 0; i < g_repeat; i++) {
Benoit Jacob2840ac72009-10-28 18:19:29 -040096 CALL_SUBTEST_1( sparse_vector<double>(8, 8) );
97 CALL_SUBTEST_2( sparse_vector<std::complex<double> >(16, 16) );
98 CALL_SUBTEST_1( sparse_vector<double>(299, 535) );
Gael Guennebaud709e9032009-01-07 17:01:57 +000099 }
100}
101