Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | 22c7609 | 2011-03-22 11:58:22 +0100 | [diff] [blame] | 4 | // Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr> |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 5 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 6 | // This Source Code Form is subject to the terms of the Mozilla |
| 7 | // Public License v. 2.0. If a copy of the MPL was not distributed |
| 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 9 | |
| 10 | #include "sparse.h" |
| 11 | |
Gael Guennebaud | 869b444 | 2016-01-25 11:55:39 +0100 | [diff] [blame] | 12 | template<typename Scalar,typename StorageIndex> void sparse_vector(int rows, int cols) |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 13 | { |
Gael Guennebaud | 42e2578 | 2011-08-19 14:18:05 +0200 | [diff] [blame] | 14 | double densityMat = (std::max)(8./(rows*cols), 0.01); |
Christoph Hertzberg | dacb469 | 2016-05-05 13:35:45 +0200 | [diff] [blame] | 15 | double densityVec = (std::max)(8./(rows), 0.1); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 16 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 17 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
Antonio Sánchez | c614b2b | 2022-12-06 00:02:31 +0000 | [diff] [blame] | 18 | typedef Matrix<DenseIndex,Dynamic,1> DenseIndexVector; |
Gael Guennebaud | 869b444 | 2016-01-25 11:55:39 +0100 | [diff] [blame] | 19 | typedef SparseVector<Scalar,0,StorageIndex> SparseVectorType; |
| 20 | typedef SparseMatrix<Scalar,0,StorageIndex> SparseMatrixType; |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 21 | Scalar eps = 1e-6; |
| 22 | |
Gael Guennebaud | 2c03e6f | 2011-12-22 14:01:06 +0100 | [diff] [blame] | 23 | SparseMatrixType m1(rows,rows); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 24 | SparseVectorType v1(rows), v2(rows), v3(rows); |
Gael Guennebaud | 2c03e6f | 2011-12-22 14:01:06 +0100 | [diff] [blame] | 25 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 26 | DenseVector refV1 = DenseVector::Random(rows), |
Gael Guennebaud | 349c2c9 | 2014-10-09 23:35:49 +0200 | [diff] [blame] | 27 | refV2 = DenseVector::Random(rows), |
| 28 | refV3 = DenseVector::Random(rows); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 29 | |
| 30 | std::vector<int> zerocoords, nonzerocoords; |
| 31 | initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords); |
| 32 | initSparse<Scalar>(densityMat, refM1, m1); |
| 33 | |
| 34 | initSparse<Scalar>(densityVec, refV2, v2); |
| 35 | initSparse<Scalar>(densityVec, refV3, v3); |
| 36 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 37 | Scalar s1 = internal::random<Scalar>(); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 38 | |
| 39 | // test coeff and coeffRef |
| 40 | for (unsigned int i=0; i<zerocoords.size(); ++i) |
| 41 | { |
| 42 | VERIFY_IS_MUCH_SMALLER_THAN( v1.coeff(zerocoords[i]), eps ); |
Gael Guennebaud | 36c478c | 2009-01-19 22:29:28 +0000 | [diff] [blame] | 43 | //VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 ); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 44 | } |
| 45 | { |
| 46 | VERIFY(int(nonzerocoords.size()) == v1.nonZeros()); |
| 47 | int j=0; |
| 48 | for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j) |
| 49 | { |
| 50 | VERIFY(nonzerocoords[j]==it.index()); |
Erik Schultheis | d271a7d | 2022-01-26 18:16:19 +0000 | [diff] [blame] | 51 | VERIFY_IS_EQUAL(it.value(), v1.coeff(it.index())); |
| 52 | VERIFY_IS_EQUAL(it.value(), refV1.coeff(it.index())); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | VERIFY_IS_APPROX(v1, refV1); |
Gael Guennebaud | 349c2c9 | 2014-10-09 23:35:49 +0200 | [diff] [blame] | 56 | |
| 57 | // test coeffRef with reallocation |
| 58 | { |
Gael Guennebaud | 9a2447b | 2015-06-09 09:11:12 +0200 | [diff] [blame] | 59 | SparseVectorType v4(rows); |
| 60 | DenseVector v5 = DenseVector::Zero(rows); |
Gael Guennebaud | 349c2c9 | 2014-10-09 23:35:49 +0200 | [diff] [blame] | 61 | for(int k=0; k<rows; ++k) |
| 62 | { |
| 63 | int i = internal::random<int>(0,rows-1); |
| 64 | Scalar v = internal::random<Scalar>(); |
Gael Guennebaud | 9a2447b | 2015-06-09 09:11:12 +0200 | [diff] [blame] | 65 | v4.coeffRef(i) += v; |
| 66 | v5.coeffRef(i) += v; |
Gael Guennebaud | 349c2c9 | 2014-10-09 23:35:49 +0200 | [diff] [blame] | 67 | } |
Gael Guennebaud | 9a2447b | 2015-06-09 09:11:12 +0200 | [diff] [blame] | 68 | VERIFY_IS_APPROX(v4,v5); |
Gael Guennebaud | 349c2c9 | 2014-10-09 23:35:49 +0200 | [diff] [blame] | 69 | } |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 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 Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 80 | VERIFY_IS_APPROX(v1*=s1, refV1*=s1); |
| 81 | VERIFY_IS_APPROX(v1/=s1, refV1/=s1); |
Gael Guennebaud | a2324d6 | 2010-04-13 10:40:55 +0200 | [diff] [blame] | 82 | |
Gael Guennebaud | e7c48fa | 2009-01-23 13:59:32 +0000 | [diff] [blame] | 83 | VERIFY_IS_APPROX(v1+=v2, refV1+=refV2); |
| 84 | VERIFY_IS_APPROX(v1-=v2, refV1-=refV2); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 85 | |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 86 | VERIFY_IS_APPROX(v1.dot(v2), refV1.dot(refV2)); |
Gael Guennebaud | a9688f0 | 2009-02-09 09:59:30 +0000 | [diff] [blame] | 87 | VERIFY_IS_APPROX(v1.dot(refV2), refV1.dot(refV2)); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 88 | |
Gael Guennebaud | fbb53b6 | 2014-09-01 16:53:52 +0200 | [diff] [blame] | 89 | VERIFY_IS_APPROX(m1*v2, refM1*refV2); |
Gael Guennebaud | 2c03e6f | 2011-12-22 14:01:06 +0100 | [diff] [blame] | 90 | VERIFY_IS_APPROX(v1.dot(m1*v2), refV1.dot(refM1*refV2)); |
Gael Guennebaud | 869b444 | 2016-01-25 11:55:39 +0100 | [diff] [blame] | 91 | { |
| 92 | int i = internal::random<int>(0,rows-1); |
| 93 | VERIFY_IS_APPROX(v1.dot(m1.col(i)), refV1.dot(refM1.col(i))); |
| 94 | } |
Gael Guennebaud | 2c03e6f | 2011-12-22 14:01:06 +0100 | [diff] [blame] | 95 | |
| 96 | |
Gael Guennebaud | a2324d6 | 2010-04-13 10:40:55 +0200 | [diff] [blame] | 97 | VERIFY_IS_APPROX(v1.squaredNorm(), refV1.squaredNorm()); |
Desire NUENTSA | 5b9bb00 | 2013-01-21 15:37:06 +0100 | [diff] [blame] | 98 | |
| 99 | VERIFY_IS_APPROX(v1.blueNorm(), refV1.blueNorm()); |
Gael Guennebaud | a2324d6 | 2010-04-13 10:40:55 +0200 | [diff] [blame] | 100 | |
Gael Guennebaud | e75b1eb | 2012-07-25 09:33:50 +0200 | [diff] [blame] | 101 | // test aliasing |
| 102 | VERIFY_IS_APPROX((v1 = -v1), (refV1 = -refV1)); |
| 103 | VERIFY_IS_APPROX((v1 = v1.transpose()), (refV1 = refV1.transpose().eval())); |
| 104 | VERIFY_IS_APPROX((v1 += -v1), (refV1 += -refV1)); |
Gael Guennebaud | 98ce445 | 2013-03-06 11:58:22 +0100 | [diff] [blame] | 105 | |
| 106 | // sparse matrix to sparse vector |
| 107 | SparseMatrixType mv1; |
| 108 | VERIFY_IS_APPROX((mv1=v1),v1); |
| 109 | VERIFY_IS_APPROX(mv1,(v1=mv1)); |
| 110 | VERIFY_IS_APPROX(mv1,(v1=mv1.transpose())); |
Gael Guennebaud | e392948 | 2013-06-10 00:06:40 +0200 | [diff] [blame] | 111 | |
| 112 | // check copy to dense vector with transpose |
| 113 | refV3.resize(0); |
| 114 | VERIFY_IS_APPROX(refV3 = v1.transpose(),v1.toDense()); |
Erik Schultheis | 13954c4 | 2021-11-15 22:16:01 +0000 | [diff] [blame] | 115 | VERIFY_IS_APPROX(DenseVector(v1),v1.toDense()); |
Gael Guennebaud | e75b1eb | 2012-07-25 09:33:50 +0200 | [diff] [blame] | 116 | |
Gael Guennebaud | 869b444 | 2016-01-25 11:55:39 +0100 | [diff] [blame] | 117 | // test conservative resize |
| 118 | { |
| 119 | std::vector<StorageIndex> inc; |
| 120 | if(rows > 3) |
| 121 | inc.push_back(-3); |
| 122 | inc.push_back(0); |
| 123 | inc.push_back(3); |
| 124 | inc.push_back(1); |
| 125 | inc.push_back(10); |
| 126 | |
| 127 | for(std::size_t i = 0; i< inc.size(); i++) { |
| 128 | StorageIndex incRows = inc[i]; |
| 129 | SparseVectorType vec1(rows); |
| 130 | DenseVector refVec1 = DenseVector::Zero(rows); |
| 131 | initSparse<Scalar>(densityVec, refVec1, vec1); |
| 132 | |
| 133 | vec1.conservativeResize(rows+incRows); |
| 134 | refVec1.conservativeResize(rows+incRows); |
| 135 | if (incRows > 0) refVec1.tail(incRows).setZero(); |
| 136 | |
| 137 | VERIFY_IS_APPROX(vec1, refVec1); |
| 138 | |
| 139 | // Insert new values |
| 140 | if (incRows > 0) |
| 141 | vec1.insert(vec1.rows()-1) = refVec1(refVec1.rows()-1) = 1; |
| 142 | |
| 143 | VERIFY_IS_APPROX(vec1, refVec1); |
| 144 | } |
| 145 | } |
| 146 | |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame] | 147 | // test sort |
| 148 | if(rows > 1) |
| 149 | { |
| 150 | SparseVectorType vec1(rows); |
| 151 | DenseVector refVec1 = DenseVector::Zero(rows); |
Antonio Sánchez | c614b2b | 2022-12-06 00:02:31 +0000 | [diff] [blame] | 152 | DenseIndexVector innerIndices(rows); |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame] | 153 | innerIndices.setLinSpaced(0, rows - 1); |
| 154 | std::random_shuffle(innerIndices.begin(), innerIndices.end()); |
| 155 | Index nz = internal::random<Index>(2, rows / 2); |
| 156 | for (Index k = 0; k < nz; k++) |
| 157 | { |
| 158 | Index i = innerIndices[k]; |
| 159 | Scalar val = internal::random<Scalar>(); |
| 160 | refVec1.coeffRef(i) = val; |
| 161 | vec1.insert(i) = val; |
| 162 | } |
| 163 | |
| 164 | vec1.template sortInnerIndices<std::greater<>>(); |
| 165 | VERIFY_IS_APPROX(vec1, refVec1); |
| 166 | VERIFY_IS_EQUAL(vec1.template innerIndicesAreSorted<std::greater<>>(), 1); |
| 167 | VERIFY_IS_EQUAL(vec1.template innerIndicesAreSorted<std::less<>>(), 0); |
| 168 | vec1.template sortInnerIndices<std::less<>>(); |
| 169 | VERIFY_IS_APPROX(vec1, refVec1); |
| 170 | VERIFY_IS_EQUAL(vec1.template innerIndicesAreSorted<std::greater<>>(), 0); |
| 171 | VERIFY_IS_EQUAL(vec1.template innerIndicesAreSorted<std::less<>>(), 1); |
| 172 | } |
| 173 | |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 174 | } |
Erik Schultheis | 13954c4 | 2021-11-15 22:16:01 +0000 | [diff] [blame] | 175 | void test_pruning() { |
| 176 | using SparseVectorType = SparseVector<double, 0, int>; |
| 177 | |
| 178 | SparseVectorType vec; |
| 179 | auto init_vec = [&](){; |
| 180 | vec.resize(10); |
| 181 | vec.insert(3) = 0.1; |
| 182 | vec.insert(5) = 1.0; |
| 183 | vec.insert(8) = -0.1; |
| 184 | vec.insert(9) = -0.2; |
| 185 | }; |
| 186 | init_vec(); |
| 187 | |
| 188 | VERIFY_IS_EQUAL(vec.nonZeros(), 4); |
| 189 | VERIFY_IS_EQUAL(vec.prune(0.1, 1.0), 2); |
| 190 | VERIFY_IS_EQUAL(vec.nonZeros(), 2); |
| 191 | VERIFY_IS_EQUAL(vec.coeff(5), 1.0); |
| 192 | VERIFY_IS_EQUAL(vec.coeff(9), -0.2); |
| 193 | |
| 194 | init_vec(); |
| 195 | VERIFY_IS_EQUAL(vec.prune([](double v) { return v >= 0; }), 2); |
| 196 | VERIFY_IS_EQUAL(vec.nonZeros(), 2); |
| 197 | VERIFY_IS_EQUAL(vec.coeff(3), 0.1); |
| 198 | VERIFY_IS_EQUAL(vec.coeff(5), 1.0); |
| 199 | } |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 200 | |
Gael Guennebaud | 82f0ce2 | 2018-07-17 14:46:15 +0200 | [diff] [blame] | 201 | EIGEN_DECLARE_TEST(sparse_vector) |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 202 | { |
| 203 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | 869b444 | 2016-01-25 11:55:39 +0100 | [diff] [blame] | 204 | int r = Eigen::internal::random<int>(1,500), c = Eigen::internal::random<int>(1,500); |
| 205 | if(Eigen::internal::random<int>(0,4) == 0) { |
| 206 | r = c; // check square matrices in 25% of tries |
| 207 | } |
| 208 | EIGEN_UNUSED_VARIABLE(r+c); |
| 209 | |
Gael Guennebaud | 4b6b3f3 | 2014-02-15 09:35:23 +0100 | [diff] [blame] | 210 | CALL_SUBTEST_1(( sparse_vector<double,int>(8, 8) )); |
Gael Guennebaud | 869b444 | 2016-01-25 11:55:39 +0100 | [diff] [blame] | 211 | CALL_SUBTEST_2(( sparse_vector<std::complex<double>, int>(r, c) )); |
| 212 | CALL_SUBTEST_1(( sparse_vector<double,long int>(r, c) )); |
| 213 | CALL_SUBTEST_1(( sparse_vector<double,short>(r, c) )); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 214 | } |
Erik Schultheis | 13954c4 | 2021-11-15 22:16:01 +0000 | [diff] [blame] | 215 | |
| 216 | CALL_SUBTEST_1(test_pruning()); |
Gael Guennebaud | 709e903 | 2009-01-07 17:01:57 +0000 | [diff] [blame] | 217 | } |
| 218 | |