Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +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 | 86ccd99 | 2008-11-05 13:47:55 +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 | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 5 | // Copyright (C) 2008 Daniel Gomez Ferro <dgomezferro@gmail.com> |
Desire NUENTSA | 4cd8245 | 2013-06-11 14:42:29 +0200 | [diff] [blame] | 6 | // Copyright (C) 2013 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr> |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 7 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 8 | // This Source Code Form is subject to the terms of the Mozilla |
| 9 | // Public License v. 2.0. If a copy of the MPL was not distributed |
| 10 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 11 | |
Gael Guennebaud | 8214cf1 | 2018-10-11 10:27:23 +0200 | [diff] [blame] | 12 | #ifndef EIGEN_SPARSE_TEST_INCLUDED_FROM_SPARSE_EXTRA |
Gael Guennebaud | c43154b | 2015-03-04 10:16:46 +0100 | [diff] [blame] | 13 | static long g_realloc_count = 0; |
| 14 | #define EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN g_realloc_count++; |
| 15 | |
Gael Guennebaud | eec0dfd | 2018-10-10 22:50:15 +0200 | [diff] [blame] | 16 | static long g_dense_op_sparse_count = 0; |
| 17 | #define EIGEN_SPARSE_ASSIGNMENT_FROM_DENSE_OP_SPARSE_PLUGIN g_dense_op_sparse_count++; |
| 18 | #define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_ADD_DENSE_PLUGIN g_dense_op_sparse_count+=10; |
| 19 | #define EIGEN_SPARSE_ASSIGNMENT_FROM_SPARSE_SUB_DENSE_PLUGIN g_dense_op_sparse_count+=20; |
Gael Guennebaud | 8214cf1 | 2018-10-11 10:27:23 +0200 | [diff] [blame] | 20 | #endif |
Gael Guennebaud | eec0dfd | 2018-10-10 22:50:15 +0200 | [diff] [blame] | 21 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 22 | #include "sparse.h" |
| 23 | |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 24 | template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref) |
| 25 | { |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 26 | typedef typename SparseMatrixType::StorageIndex StorageIndex; |
| 27 | typedef Matrix<StorageIndex,2,1> Vector2; |
Gael Guennebaud | 6d1f5db | 2013-07-10 23:48:26 +0200 | [diff] [blame] | 28 | |
Gael Guennebaud | fc202ba | 2015-02-13 18:57:41 +0100 | [diff] [blame] | 29 | const Index rows = ref.rows(); |
| 30 | const Index cols = ref.cols(); |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 31 | const Index inner = ref.innerSize(); |
| 32 | const Index outer = ref.outerSize(); |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 33 | |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 34 | typedef typename SparseMatrixType::Scalar Scalar; |
Gael Guennebaud | 7136267 | 2016-12-27 16:34:30 +0100 | [diff] [blame] | 35 | typedef typename SparseMatrixType::RealScalar RealScalar; |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 36 | enum { Flags = SparseMatrixType::Flags }; |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 37 | |
Gael Guennebaud | 42e2578 | 2011-08-19 14:18:05 +0200 | [diff] [blame] | 38 | double density = (std::max)(8./(rows*cols), 0.01); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 39 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 40 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 41 | Scalar eps = 1e-6; |
| 42 | |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 43 | Scalar s1 = internal::random<Scalar>(); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 44 | { |
Gael Guennebaud | d1d7a1a | 2013-06-23 19:11:32 +0200 | [diff] [blame] | 45 | SparseMatrixType m(rows, cols); |
| 46 | DenseMatrix refMat = DenseMatrix::Zero(rows, cols); |
| 47 | DenseVector vec1 = DenseVector::Random(rows); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 48 | |
Gael Guennebaud | 6d1f5db | 2013-07-10 23:48:26 +0200 | [diff] [blame] | 49 | std::vector<Vector2> zeroCoords; |
| 50 | std::vector<Vector2> nonzeroCoords; |
Gael Guennebaud | d1d7a1a | 2013-06-23 19:11:32 +0200 | [diff] [blame] | 51 | initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 52 | |
Gael Guennebaud | d1d7a1a | 2013-06-23 19:11:32 +0200 | [diff] [blame] | 53 | // test coeff and coeffRef |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 54 | for (std::size_t i=0; i<zeroCoords.size(); ++i) |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 55 | { |
Gael Guennebaud | d1d7a1a | 2013-06-23 19:11:32 +0200 | [diff] [blame] | 56 | VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps ); |
| 57 | if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value) |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 58 | VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[i].x(),zeroCoords[i].y()) = 5 ); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 59 | } |
Gael Guennebaud | d1d7a1a | 2013-06-23 19:11:32 +0200 | [diff] [blame] | 60 | VERIFY_IS_APPROX(m, refMat); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 61 | |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 62 | if(!nonzeroCoords.empty()) { |
| 63 | m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 64 | refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 65 | } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 66 | |
Gael Guennebaud | d1d7a1a | 2013-06-23 19:11:32 +0200 | [diff] [blame] | 67 | VERIFY_IS_APPROX(m, refMat); |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 68 | |
Gael Guennebaud | a915f02 | 2013-06-28 16:16:02 +0200 | [diff] [blame] | 69 | // test assertion |
| 70 | VERIFY_RAISES_ASSERT( m.coeffRef(-1,1) = 0 ); |
| 71 | VERIFY_RAISES_ASSERT( m.coeffRef(0,m.cols()) = 0 ); |
Gael Guennebaud | d1d7a1a | 2013-06-23 19:11:32 +0200 | [diff] [blame] | 72 | } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 73 | |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 74 | // test insert (inner random) |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 75 | { |
| 76 | DenseMatrix m1(rows,cols); |
| 77 | m1.setZero(); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 78 | SparseMatrixType m2(rows,cols); |
Gael Guennebaud | c43154b | 2015-03-04 10:16:46 +0100 | [diff] [blame] | 79 | bool call_reserve = internal::random<int>()%2; |
| 80 | Index nnz = internal::random<int>(1,int(rows)/2); |
| 81 | if(call_reserve) |
| 82 | { |
| 83 | if(internal::random<int>()%2) |
| 84 | m2.reserve(VectorXi::Constant(m2.outerSize(), int(nnz))); |
| 85 | else |
| 86 | m2.reserve(m2.outerSize() * nnz); |
| 87 | } |
| 88 | g_realloc_count = 0; |
Gael Guennebaud | 6d1f5db | 2013-07-10 23:48:26 +0200 | [diff] [blame] | 89 | for (Index j=0; j<cols; ++j) |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 90 | { |
Gael Guennebaud | c43154b | 2015-03-04 10:16:46 +0100 | [diff] [blame] | 91 | for (Index k=0; k<nnz; ++k) |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 92 | { |
Gael Guennebaud | 6d1f5db | 2013-07-10 23:48:26 +0200 | [diff] [blame] | 93 | Index i = internal::random<Index>(0,rows-1); |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 94 | if (m1.coeff(i, j) == Scalar(0)) { |
| 95 | Scalar v = internal::random<Scalar>(); |
| 96 | if (v == Scalar(0)) v = Scalar(1); |
| 97 | m1(i, j) = v; |
| 98 | m2.insert(i, j) = v; |
| 99 | } |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
Gael Guennebaud | c43154b | 2015-03-04 10:16:46 +0100 | [diff] [blame] | 102 | |
| 103 | if(call_reserve && !SparseMatrixType::IsRowMajor) |
| 104 | { |
| 105 | VERIFY(g_realloc_count==0); |
| 106 | } |
| 107 | |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 108 | m2.finalize(); |
| 109 | VERIFY_IS_APPROX(m2,m1); |
| 110 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 111 | |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 112 | // test insert (fully random) |
| 113 | { |
| 114 | DenseMatrix m1(rows,cols); |
| 115 | m1.setZero(); |
| 116 | SparseMatrixType m2(rows,cols); |
Gael Guennebaud | 4ca89f3 | 2011-12-02 19:00:16 +0100 | [diff] [blame] | 117 | if(internal::random<int>()%2) |
| 118 | m2.reserve(VectorXi::Constant(m2.outerSize(), 2)); |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 119 | for (int k=0; k<rows*cols; ++k) |
| 120 | { |
Gael Guennebaud | 6d1f5db | 2013-07-10 23:48:26 +0200 | [diff] [blame] | 121 | Index i = internal::random<Index>(0,rows-1); |
| 122 | Index j = internal::random<Index>(0,cols-1); |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 123 | if ((m1.coeff(i, j) == Scalar(0)) && (internal::random<int>() % 2)) { |
| 124 | Scalar v = internal::random<Scalar>(); |
| 125 | if (v == Scalar(0)) v = Scalar(1); |
| 126 | m1(i, j) = v; |
| 127 | m2.insert(i, j) = v; |
| 128 | } |
Gael Guennebaud | 4ca89f3 | 2011-12-02 19:00:16 +0100 | [diff] [blame] | 129 | else |
| 130 | { |
| 131 | Scalar v = internal::random<Scalar>(); |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 132 | if (v == Scalar(0)) v = Scalar(1); |
| 133 | m1(i, j) = v; |
| 134 | m2.coeffRef(i, j) = v; |
Gael Guennebaud | 4ca89f3 | 2011-12-02 19:00:16 +0100 | [diff] [blame] | 135 | } |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 136 | } |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 137 | VERIFY_IS_APPROX(m2,m1); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 138 | } |
Gael Guennebaud | 7706baf | 2011-09-08 13:42:54 +0200 | [diff] [blame] | 139 | |
| 140 | // test insert (un-compressed) |
| 141 | for(int mode=0;mode<4;++mode) |
| 142 | { |
| 143 | DenseMatrix m1(rows,cols); |
| 144 | m1.setZero(); |
| 145 | SparseMatrixType m2(rows,cols); |
Gael Guennebaud | b47ef14 | 2014-07-08 16:47:11 +0200 | [diff] [blame] | 146 | VectorXi r(VectorXi::Constant(m2.outerSize(), ((mode%2)==0) ? int(m2.innerSize()) : std::max<int>(1,int(m2.innerSize())/8))); |
Gael Guennebaud | 7706baf | 2011-09-08 13:42:54 +0200 | [diff] [blame] | 147 | m2.reserve(r); |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 148 | for (Index k=0; k<rows*cols; ++k) |
Gael Guennebaud | 7706baf | 2011-09-08 13:42:54 +0200 | [diff] [blame] | 149 | { |
Gael Guennebaud | 6d1f5db | 2013-07-10 23:48:26 +0200 | [diff] [blame] | 150 | Index i = internal::random<Index>(0,rows-1); |
| 151 | Index j = internal::random<Index>(0,cols-1); |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 152 | if (m1.coeff(i, j) == Scalar(0)) { |
| 153 | Scalar v = internal::random<Scalar>(); |
| 154 | if (v == Scalar(0)) v = Scalar(1); |
| 155 | m1(i, j) = v; |
| 156 | m2.insert(i, j) = v; |
| 157 | } |
Gael Guennebaud | 7706baf | 2011-09-08 13:42:54 +0200 | [diff] [blame] | 158 | if(mode==3) |
| 159 | m2.reserve(r); |
| 160 | } |
Gael Guennebaud | 4ca89f3 | 2011-12-02 19:00:16 +0100 | [diff] [blame] | 161 | if(internal::random<int>()%2) |
| 162 | m2.makeCompressed(); |
Gael Guennebaud | 7706baf | 2011-09-08 13:42:54 +0200 | [diff] [blame] | 163 | VERIFY_IS_APPROX(m2,m1); |
| 164 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 165 | |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 166 | // test sort |
| 167 | if (inner > 1) { |
| 168 | bool StorageOrdersMatch = DenseMatrix::IsRowMajor == SparseMatrixType::IsRowMajor; |
| 169 | DenseMatrix m1(rows, cols); |
| 170 | m1.setZero(); |
| 171 | SparseMatrixType m2(rows, cols); |
| 172 | // generate random inner indices with no repeats |
| 173 | Vector<Index, Dynamic> innerIndices(inner); |
| 174 | innerIndices.setLinSpaced(inner, 0, inner - 1); |
| 175 | for (Index j = 0; j < outer; j++) { |
| 176 | std::random_shuffle(innerIndices.begin(), innerIndices.end()); |
| 177 | Index nzj = internal::random<Index>(2, inner / 2); |
| 178 | for (Index k = 0; k < nzj; k++) { |
| 179 | Index i = innerIndices[k]; |
| 180 | Scalar val = internal::random<Scalar>(); |
| 181 | m1.coeffRefByOuterInner(StorageOrdersMatch ? j : i, StorageOrdersMatch ? i : j) = val; |
| 182 | m2.insertByOuterInner(j, i) = val; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | VERIFY_IS_APPROX(m2, m1); |
| 187 | // sort wrt greater |
| 188 | m2.template sortInnerIndices<std::greater<>>(); |
| 189 | // verify that all inner vectors are not sorted wrt less |
| 190 | VERIFY_IS_EQUAL(m2.template innerIndicesAreSorted<std::less<>>(), 0); |
| 191 | // verify that all inner vectors are sorted wrt greater |
| 192 | VERIFY_IS_EQUAL(m2.template innerIndicesAreSorted<std::greater<>>(), m2.outerSize()); |
| 193 | // verify that sort does not change evaluation |
| 194 | VERIFY_IS_APPROX(m2, m1); |
| 195 | // sort wrt less |
| 196 | m2.template sortInnerIndices<std::less<>>(); |
| 197 | // verify that all inner vectors are sorted wrt less |
| 198 | VERIFY_IS_EQUAL(m2.template innerIndicesAreSorted<std::less<>>(), m2.outerSize()); |
| 199 | // verify that all inner vectors are not sorted wrt greater |
| 200 | VERIFY_IS_EQUAL(m2.template innerIndicesAreSorted<std::greater<>>(), 0); |
| 201 | // verify that sort does not change evaluation |
| 202 | VERIFY_IS_APPROX(m2, m1); |
| 203 | |
| 204 | m2.makeCompressed(); |
| 205 | // sort wrt greater |
| 206 | m2.template sortInnerIndices<std::greater<>>(); |
| 207 | // verify that all inner vectors are not sorted wrt less |
| 208 | VERIFY_IS_EQUAL(m2.template innerIndicesAreSorted<std::less<>>(), 0); |
| 209 | // verify that all inner vectors are sorted wrt greater |
| 210 | VERIFY_IS_EQUAL(m2.template innerIndicesAreSorted<std::greater<>>(), m2.outerSize()); |
| 211 | // verify that sort does not change evaluation |
| 212 | VERIFY_IS_APPROX(m2, m1); |
| 213 | // sort wrt less |
| 214 | m2.template sortInnerIndices<std::less<>>(); |
| 215 | // verify that all inner vectors are sorted wrt less |
| 216 | VERIFY_IS_EQUAL(m2.template innerIndicesAreSorted<std::less<>>(), m2.outerSize()); |
| 217 | // verify that all inner vectors are not sorted wrt greater |
| 218 | VERIFY_IS_EQUAL(m2.template innerIndicesAreSorted<std::greater<>>(), 0); |
| 219 | // verify that sort does not change evaluation |
| 220 | VERIFY_IS_APPROX(m2, m1); |
| 221 | } |
| 222 | |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 223 | // test basic computations |
| 224 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 225 | DenseMatrix refM1 = DenseMatrix::Zero(rows, cols); |
| 226 | DenseMatrix refM2 = DenseMatrix::Zero(rows, cols); |
| 227 | DenseMatrix refM3 = DenseMatrix::Zero(rows, cols); |
| 228 | DenseMatrix refM4 = DenseMatrix::Zero(rows, cols); |
| 229 | SparseMatrixType m1(rows, cols); |
| 230 | SparseMatrixType m2(rows, cols); |
| 231 | SparseMatrixType m3(rows, cols); |
| 232 | SparseMatrixType m4(rows, cols); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 233 | initSparse<Scalar>(density, refM1, m1); |
| 234 | initSparse<Scalar>(density, refM2, m2); |
| 235 | initSparse<Scalar>(density, refM3, m3); |
| 236 | initSparse<Scalar>(density, refM4, m4); |
| 237 | |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 238 | if(internal::random<bool>()) |
| 239 | m1.makeCompressed(); |
| 240 | |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 241 | Index m1_nnz = m1.nonZeros(); |
| 242 | |
Gael Guennebaud | 4aac872 | 2014-07-22 12:54:03 +0200 | [diff] [blame] | 243 | VERIFY_IS_APPROX(m1*s1, refM1*s1); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 244 | VERIFY_IS_APPROX(m1+m2, refM1+refM2); |
| 245 | VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3); |
| 246 | VERIFY_IS_APPROX(m3.cwiseProduct(m1+m2), refM3.cwiseProduct(refM1+refM2)); |
| 247 | VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 248 | VERIFY_IS_APPROX(m4=m1/s1, refM1/s1); |
| 249 | VERIFY_IS_EQUAL(m4.nonZeros(), m1_nnz); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 250 | |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 251 | if(SparseMatrixType::IsRowMajor) |
| 252 | VERIFY_IS_APPROX(m1.innerVector(0).dot(refM2.row(0)), refM1.row(0).dot(refM2.row(0))); |
| 253 | else |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 254 | VERIFY_IS_APPROX(m1.innerVector(0).dot(refM2.col(0)), refM1.col(0).dot(refM2.col(0))); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 255 | |
Gael Guennebaud | 3573a10 | 2014-02-17 13:46:17 +0100 | [diff] [blame] | 256 | DenseVector rv = DenseVector::Random(m1.cols()); |
| 257 | DenseVector cv = DenseVector::Random(m1.rows()); |
| 258 | Index r = internal::random<Index>(0,m1.rows()-2); |
| 259 | Index c = internal::random<Index>(0,m1.cols()-1); |
| 260 | VERIFY_IS_APPROX(( m1.template block<1,Dynamic>(r,0,1,m1.cols()).dot(rv)) , refM1.row(r).dot(rv)); |
| 261 | VERIFY_IS_APPROX(m1.row(r).dot(rv), refM1.row(r).dot(rv)); |
| 262 | VERIFY_IS_APPROX(m1.col(c).dot(cv), refM1.col(c).dot(cv)); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 263 | |
| 264 | VERIFY_IS_APPROX(m1.conjugate(), refM1.conjugate()); |
| 265 | VERIFY_IS_APPROX(m1.real(), refM1.real()); |
| 266 | |
| 267 | refM4.setRandom(); |
| 268 | // sparse cwise* dense |
| 269 | VERIFY_IS_APPROX(m3.cwiseProduct(refM4), refM3.cwiseProduct(refM4)); |
Gael Guennebaud | 9027508 | 2015-11-04 17:42:07 +0100 | [diff] [blame] | 270 | // dense cwise* sparse |
| 271 | VERIFY_IS_APPROX(refM4.cwiseProduct(m3), refM4.cwiseProduct(refM3)); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 272 | // VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4); |
| 273 | |
Gael Guennebaud | eec0dfd | 2018-10-10 22:50:15 +0200 | [diff] [blame] | 274 | // mixed sparse-dense |
Gael Guennebaud | 15084cf | 2016-01-29 22:09:45 +0100 | [diff] [blame] | 275 | VERIFY_IS_APPROX(refM4 + m3, refM4 + refM3); |
| 276 | VERIFY_IS_APPROX(m3 + refM4, refM3 + refM4); |
| 277 | VERIFY_IS_APPROX(refM4 - m3, refM4 - refM3); |
| 278 | VERIFY_IS_APPROX(m3 - refM4, refM3 - refM4); |
Gael Guennebaud | 7136267 | 2016-12-27 16:34:30 +0100 | [diff] [blame] | 279 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + RealScalar(0.5)*m3).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3); |
| 280 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + m3*RealScalar(0.5)).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3); |
| 281 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + m3.cwiseProduct(m3)).eval(), RealScalar(0.5)*refM4 + refM3.cwiseProduct(refM3)); |
| 282 | |
| 283 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + RealScalar(0.5)*m3).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3); |
| 284 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + m3*RealScalar(0.5)).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3); |
| 285 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + (m3+m3)).eval(), RealScalar(0.5)*refM4 + (refM3+refM3)); |
| 286 | VERIFY_IS_APPROX(((refM3+m3)+RealScalar(0.5)*m3).eval(), RealScalar(0.5)*refM3 + (refM3+refM3)); |
| 287 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + (refM3+m3)).eval(), RealScalar(0.5)*refM4 + (refM3+refM3)); |
| 288 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + (m3+refM3)).eval(), RealScalar(0.5)*refM4 + (refM3+refM3)); |
| 289 | |
Gael Guennebaud | 15084cf | 2016-01-29 22:09:45 +0100 | [diff] [blame] | 290 | |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 291 | VERIFY_IS_APPROX(m1.sum(), refM1.sum()); |
| 292 | |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 293 | m4 = m1; refM4 = m4; |
| 294 | |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 295 | VERIFY_IS_APPROX(m1*=s1, refM1*=s1); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 296 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 297 | VERIFY_IS_APPROX(m1/=s1, refM1/=s1); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 298 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 299 | |
| 300 | VERIFY_IS_APPROX(m1+=m2, refM1+=refM2); |
| 301 | VERIFY_IS_APPROX(m1-=m2, refM1-=refM2); |
| 302 | |
Gael Guennebaud | eec0dfd | 2018-10-10 22:50:15 +0200 | [diff] [blame] | 303 | refM3 = refM1; |
| 304 | |
| 305 | VERIFY_IS_APPROX(refM1+=m2, refM3+=refM2); |
| 306 | VERIFY_IS_APPROX(refM1-=m2, refM3-=refM2); |
| 307 | |
| 308 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =m2+refM4, refM3 =refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,10); |
| 309 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=m2+refM4, refM3+=refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 310 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=m2+refM4, refM3-=refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 311 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =refM4+m2, refM3 =refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 312 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=refM4+m2, refM3+=refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 313 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=refM4+m2, refM3-=refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 314 | |
| 315 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =m2-refM4, refM3 =refM2-refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,20); |
| 316 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=m2-refM4, refM3+=refM2-refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 317 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=m2-refM4, refM3-=refM2-refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 318 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =refM4-m2, refM3 =refM4-refM2); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 319 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=refM4-m2, refM3+=refM4-refM2); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 320 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=refM4-m2, refM3-=refM4-refM2); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 321 | refM3 = m3; |
| 322 | |
Gael Guennebaud | ba3f977 | 2017-01-23 22:06:08 +0100 | [diff] [blame] | 323 | if (rows>=2 && cols>=2) |
| 324 | { |
| 325 | VERIFY_RAISES_ASSERT( m1 += m1.innerVector(0) ); |
| 326 | VERIFY_RAISES_ASSERT( m1 -= m1.innerVector(0) ); |
| 327 | VERIFY_RAISES_ASSERT( refM1 -= m1.innerVector(0) ); |
| 328 | VERIFY_RAISES_ASSERT( refM1 += m1.innerVector(0) ); |
| 329 | } |
Gael Guennebaud | 26a2c6f | 2017-12-14 15:11:04 +0100 | [diff] [blame] | 330 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | ba3f977 | 2017-01-23 22:06:08 +0100 | [diff] [blame] | 331 | |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 332 | // test aliasing |
| 333 | VERIFY_IS_APPROX((m1 = -m1), (refM1 = -refM1)); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 334 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
| 335 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 336 | VERIFY_IS_APPROX((m1 = m1.transpose()), (refM1 = refM1.transpose().eval())); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 337 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
| 338 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 339 | VERIFY_IS_APPROX((m1 = -m1.transpose()), (refM1 = -refM1.transpose().eval())); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 340 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
| 341 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 342 | VERIFY_IS_APPROX((m1 += -m1), (refM1 += -refM1)); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 343 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
| 344 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | 7e029d1 | 2016-08-29 12:06:37 +0200 | [diff] [blame] | 345 | |
| 346 | if(m1.isCompressed()) |
| 347 | { |
| 348 | VERIFY_IS_APPROX(m1.coeffs().sum(), m1.sum()); |
| 349 | m1.coeffs() += s1; |
| 350 | for(Index j = 0; j<m1.outerSize(); ++j) |
| 351 | for(typename SparseMatrixType::InnerIterator it(m1,j); it; ++it) |
| 352 | refM1(it.row(), it.col()) += s1; |
| 353 | VERIFY_IS_APPROX(m1, refM1); |
| 354 | } |
Gael Guennebaud | 2e334f5 | 2016-11-14 18:47:02 +0100 | [diff] [blame] | 355 | |
| 356 | // and/or |
| 357 | { |
| 358 | typedef SparseMatrix<bool, SparseMatrixType::Options, typename SparseMatrixType::StorageIndex> SpBool; |
| 359 | SpBool mb1 = m1.real().template cast<bool>(); |
| 360 | SpBool mb2 = m2.real().template cast<bool>(); |
| 361 | VERIFY_IS_EQUAL(mb1.template cast<int>().sum(), refM1.real().template cast<bool>().count()); |
| 362 | VERIFY_IS_EQUAL((mb1 && mb2).template cast<int>().sum(), (refM1.real().template cast<bool>() && refM2.real().template cast<bool>()).count()); |
| 363 | VERIFY_IS_EQUAL((mb1 || mb2).template cast<int>().sum(), (refM1.real().template cast<bool>() || refM2.real().template cast<bool>()).count()); |
| 364 | SpBool mb3 = mb1 && mb2; |
| 365 | if(mb1.coeffs().all() && mb2.coeffs().all()) |
| 366 | { |
| 367 | VERIFY_IS_EQUAL(mb3.nonZeros(), (refM1.real().template cast<bool>() && refM2.real().template cast<bool>()).count()); |
| 368 | } |
| 369 | } |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Gael Guennebaud | eedb87f | 2016-11-14 14:05:53 +0100 | [diff] [blame] | 372 | // test reverse iterators |
| 373 | { |
| 374 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); |
| 375 | SparseMatrixType m2(rows, cols); |
| 376 | initSparse<Scalar>(density, refMat2, m2); |
| 377 | std::vector<Scalar> ref_value(m2.innerSize()); |
| 378 | std::vector<Index> ref_index(m2.innerSize()); |
| 379 | if(internal::random<bool>()) |
| 380 | m2.makeCompressed(); |
| 381 | for(Index j = 0; j<m2.outerSize(); ++j) |
| 382 | { |
| 383 | Index count_forward = 0; |
| 384 | |
| 385 | for(typename SparseMatrixType::InnerIterator it(m2,j); it; ++it) |
| 386 | { |
| 387 | ref_value[ref_value.size()-1-count_forward] = it.value(); |
| 388 | ref_index[ref_index.size()-1-count_forward] = it.index(); |
| 389 | count_forward++; |
| 390 | } |
| 391 | Index count_reverse = 0; |
| 392 | for(typename SparseMatrixType::ReverseInnerIterator it(m2,j); it; --it) |
| 393 | { |
| 394 | VERIFY_IS_APPROX( std::abs(ref_value[ref_value.size()-count_forward+count_reverse])+1, std::abs(it.value())+1); |
| 395 | VERIFY_IS_EQUAL( ref_index[ref_index.size()-count_forward+count_reverse] , it.index()); |
| 396 | count_reverse++; |
| 397 | } |
| 398 | VERIFY_IS_EQUAL(count_forward, count_reverse); |
| 399 | } |
| 400 | } |
| 401 | |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 402 | // test transpose |
| 403 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 404 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); |
| 405 | SparseMatrixType m2(rows, cols); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 406 | initSparse<Scalar>(density, refMat2, m2); |
| 407 | VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval()); |
| 408 | VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose()); |
| 409 | |
| 410 | VERIFY_IS_APPROX(SparseMatrixType(m2.adjoint()), refMat2.adjoint()); |
Gael Guennebaud | ff46ec0 | 2014-09-22 23:33:28 +0200 | [diff] [blame] | 411 | |
| 412 | // check isApprox handles opposite storage order |
| 413 | typename Transpose<SparseMatrixType>::PlainObject m3(m2); |
| 414 | VERIFY(m2.isApprox(m3)); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 415 | } |
| 416 | |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 417 | // test prune |
| 418 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 419 | SparseMatrixType m2(rows, cols); |
| 420 | DenseMatrix refM2(rows, cols); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 421 | refM2.setZero(); |
| 422 | int countFalseNonZero = 0; |
| 423 | int countTrueNonZero = 0; |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 424 | m2.reserve(VectorXi::Constant(m2.outerSize(), int(m2.innerSize()))); |
| 425 | for (Index j=0; j<m2.cols(); ++j) |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 426 | { |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 427 | for (Index i=0; i<m2.rows(); ++i) |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 428 | { |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 429 | float x = internal::random<float>(0,1); |
Christoph Hertzberg | dacb469 | 2016-05-05 13:35:45 +0200 | [diff] [blame] | 430 | if (x<0.1f) |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 431 | { |
| 432 | // do nothing |
| 433 | } |
Christoph Hertzberg | dacb469 | 2016-05-05 13:35:45 +0200 | [diff] [blame] | 434 | else if (x<0.5f) |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 435 | { |
| 436 | countFalseNonZero++; |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 437 | m2.insert(i,j) = Scalar(0); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 438 | } |
| 439 | else |
| 440 | { |
| 441 | countTrueNonZero++; |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 442 | m2.insert(i,j) = Scalar(1); |
| 443 | refM2(i,j) = Scalar(1); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 446 | } |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 447 | if(internal::random<bool>()) |
| 448 | m2.makeCompressed(); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 449 | VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros()); |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 450 | if(countTrueNonZero>0) |
| 451 | VERIFY_IS_APPROX(m2, refM2); |
Hauke Heibel | d204ec4 | 2010-11-02 14:33:33 +0100 | [diff] [blame] | 452 | m2.prune(Scalar(1)); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 453 | VERIFY(countTrueNonZero==m2.nonZeros()); |
| 454 | VERIFY_IS_APPROX(m2, refM2); |
| 455 | } |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 456 | |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 457 | // test setFromTriplets |
| 458 | { |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 459 | typedef Triplet<Scalar,StorageIndex> TripletType; |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 460 | std::vector<TripletType> triplets; |
Gael Guennebaud | b47ef14 | 2014-07-08 16:47:11 +0200 | [diff] [blame] | 461 | Index ntriplets = rows*cols; |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 462 | triplets.reserve(ntriplets); |
Gael Guennebaud | b4c79ee | 2015-10-13 11:30:41 +0200 | [diff] [blame] | 463 | DenseMatrix refMat_sum = DenseMatrix::Zero(rows,cols); |
| 464 | DenseMatrix refMat_prod = DenseMatrix::Zero(rows,cols); |
| 465 | DenseMatrix refMat_last = DenseMatrix::Zero(rows,cols); |
| 466 | |
Gael Guennebaud | b47ef14 | 2014-07-08 16:47:11 +0200 | [diff] [blame] | 467 | for(Index i=0;i<ntriplets;++i) |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 468 | { |
Gael Guennebaud | aa6c516 | 2015-02-16 13:19:05 +0100 | [diff] [blame] | 469 | StorageIndex r = internal::random<StorageIndex>(0,StorageIndex(rows-1)); |
| 470 | StorageIndex c = internal::random<StorageIndex>(0,StorageIndex(cols-1)); |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 471 | Scalar v = internal::random<Scalar>(); |
Gael Guennebaud | 18e3ac0 | 2012-01-31 09:14:01 +0100 | [diff] [blame] | 472 | triplets.push_back(TripletType(r,c,v)); |
Gael Guennebaud | b4c79ee | 2015-10-13 11:30:41 +0200 | [diff] [blame] | 473 | refMat_sum(r,c) += v; |
| 474 | if(std::abs(refMat_prod(r,c))==0) |
| 475 | refMat_prod(r,c) = v; |
| 476 | else |
| 477 | refMat_prod(r,c) *= v; |
| 478 | refMat_last(r,c) = v; |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 479 | } |
| 480 | SparseMatrixType m(rows,cols); |
| 481 | m.setFromTriplets(triplets.begin(), triplets.end()); |
Gael Guennebaud | b4c79ee | 2015-10-13 11:30:41 +0200 | [diff] [blame] | 482 | VERIFY_IS_APPROX(m, refMat_sum); |
| 483 | |
| 484 | m.setFromTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>()); |
| 485 | VERIFY_IS_APPROX(m, refMat_prod); |
Gael Guennebaud | b4c79ee | 2015-10-13 11:30:41 +0200 | [diff] [blame] | 486 | m.setFromTriplets(triplets.begin(), triplets.end(), [] (Scalar,Scalar b) { return b; }); |
| 487 | VERIFY_IS_APPROX(m, refMat_last); |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 488 | } |
Gael Guennebaud | 3af29ca | 2015-02-09 10:23:45 +0100 | [diff] [blame] | 489 | |
| 490 | // test Map |
| 491 | { |
| 492 | DenseMatrix refMat2(rows, cols), refMat3(rows, cols); |
| 493 | SparseMatrixType m2(rows, cols), m3(rows, cols); |
| 494 | initSparse<Scalar>(density, refMat2, m2); |
| 495 | initSparse<Scalar>(density, refMat3, m3); |
| 496 | { |
| 497 | Map<SparseMatrixType> mapMat2(m2.rows(), m2.cols(), m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(), m2.innerNonZeroPtr()); |
| 498 | Map<SparseMatrixType> mapMat3(m3.rows(), m3.cols(), m3.nonZeros(), m3.outerIndexPtr(), m3.innerIndexPtr(), m3.valuePtr(), m3.innerNonZeroPtr()); |
| 499 | VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3); |
| 500 | VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3); |
| 501 | } |
Gael Guennebaud | 757971e | 2016-07-26 09:40:19 +0200 | [diff] [blame] | 502 | |
| 503 | Index i = internal::random<Index>(0,rows-1); |
| 504 | Index j = internal::random<Index>(0,cols-1); |
| 505 | m2.coeffRef(i,j) = 123; |
| 506 | if(internal::random<bool>()) |
| 507 | m2.makeCompressed(); |
| 508 | Map<SparseMatrixType> mapMat2(rows, cols, m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(), m2.innerNonZeroPtr()); |
| 509 | VERIFY_IS_EQUAL(m2.coeff(i,j),Scalar(123)); |
| 510 | VERIFY_IS_EQUAL(mapMat2.coeff(i,j),Scalar(123)); |
| 511 | mapMat2.coeffRef(i,j) = -123; |
| 512 | VERIFY_IS_EQUAL(m2.coeff(i,j),Scalar(-123)); |
Gael Guennebaud | 3af29ca | 2015-02-09 10:23:45 +0100 | [diff] [blame] | 513 | } |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 514 | |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 515 | // test triangularView |
| 516 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 517 | DenseMatrix refMat2(rows, cols), refMat3(rows, cols); |
| 518 | SparseMatrixType m2(rows, cols), m3(rows, cols); |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 519 | initSparse<Scalar>(density, refMat2, m2); |
| 520 | refMat3 = refMat2.template triangularView<Lower>(); |
| 521 | m3 = m2.template triangularView<Lower>(); |
| 522 | VERIFY_IS_APPROX(m3, refMat3); |
| 523 | |
| 524 | refMat3 = refMat2.template triangularView<Upper>(); |
| 525 | m3 = m2.template triangularView<Upper>(); |
| 526 | VERIFY_IS_APPROX(m3, refMat3); |
| 527 | |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 528 | { |
| 529 | refMat3 = refMat2.template triangularView<UnitUpper>(); |
| 530 | m3 = m2.template triangularView<UnitUpper>(); |
| 531 | VERIFY_IS_APPROX(m3, refMat3); |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 532 | |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 533 | refMat3 = refMat2.template triangularView<UnitLower>(); |
| 534 | m3 = m2.template triangularView<UnitLower>(); |
| 535 | VERIFY_IS_APPROX(m3, refMat3); |
| 536 | } |
Desire NUENTSA | 4cd8245 | 2013-06-11 14:42:29 +0200 | [diff] [blame] | 537 | |
| 538 | refMat3 = refMat2.template triangularView<StrictlyUpper>(); |
| 539 | m3 = m2.template triangularView<StrictlyUpper>(); |
| 540 | VERIFY_IS_APPROX(m3, refMat3); |
| 541 | |
| 542 | refMat3 = refMat2.template triangularView<StrictlyLower>(); |
| 543 | m3 = m2.template triangularView<StrictlyLower>(); |
| 544 | VERIFY_IS_APPROX(m3, refMat3); |
Gael Guennebaud | f6b1dee | 2015-11-04 17:02:32 +0100 | [diff] [blame] | 545 | |
Gael Guennebaud | ba3f977 | 2017-01-23 22:06:08 +0100 | [diff] [blame] | 546 | // check sparse-triangular to dense |
Gael Guennebaud | f6b1dee | 2015-11-04 17:02:32 +0100 | [diff] [blame] | 547 | refMat3 = m2.template triangularView<StrictlyUpper>(); |
| 548 | VERIFY_IS_APPROX(refMat3, DenseMatrix(refMat2.template triangularView<StrictlyUpper>())); |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 549 | } |
Gael Guennebaud | fa6d36e | 2010-07-22 15:57:01 +0200 | [diff] [blame] | 550 | |
Gael Guennebaud | 9a3ec63 | 2010-11-15 14:14:05 +0100 | [diff] [blame] | 551 | // test selfadjointView |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 552 | if(!SparseMatrixType::IsRowMajor) |
Gael Guennebaud | 9a3ec63 | 2010-11-15 14:14:05 +0100 | [diff] [blame] | 553 | { |
| 554 | DenseMatrix refMat2(rows, rows), refMat3(rows, rows); |
| 555 | SparseMatrixType m2(rows, rows), m3(rows, rows); |
| 556 | initSparse<Scalar>(density, refMat2, m2); |
| 557 | refMat3 = refMat2.template selfadjointView<Lower>(); |
| 558 | m3 = m2.template selfadjointView<Lower>(); |
| 559 | VERIFY_IS_APPROX(m3, refMat3); |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 560 | |
Gael Guennebaud | 11b492e | 2016-12-14 17:53:47 +0100 | [diff] [blame] | 561 | refMat3 += refMat2.template selfadjointView<Lower>(); |
| 562 | m3 += m2.template selfadjointView<Lower>(); |
| 563 | VERIFY_IS_APPROX(m3, refMat3); |
| 564 | |
| 565 | refMat3 -= refMat2.template selfadjointView<Lower>(); |
| 566 | m3 -= m2.template selfadjointView<Lower>(); |
| 567 | VERIFY_IS_APPROX(m3, refMat3); |
| 568 | |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 569 | // selfadjointView only works for square matrices: |
| 570 | SparseMatrixType m4(rows, rows+1); |
| 571 | VERIFY_RAISES_ASSERT(m4.template selfadjointView<Lower>()); |
| 572 | VERIFY_RAISES_ASSERT(m4.template selfadjointView<Upper>()); |
Gael Guennebaud | 9a3ec63 | 2010-11-15 14:14:05 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Gael Guennebaud | fa6d36e | 2010-07-22 15:57:01 +0200 | [diff] [blame] | 575 | // test sparseView |
| 576 | { |
| 577 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 578 | SparseMatrixType m2(rows, rows); |
Gael Guennebaud | 9a3ec63 | 2010-11-15 14:14:05 +0100 | [diff] [blame] | 579 | initSparse<Scalar>(density, refMat2, m2); |
Gael Guennebaud | fa6d36e | 2010-07-22 15:57:01 +0200 | [diff] [blame] | 580 | VERIFY_IS_APPROX(m2.eval(), refMat2.sparseView().eval()); |
Gael Guennebaud | 8456bbb | 2016-05-18 16:53:28 +0200 | [diff] [blame] | 581 | |
| 582 | // sparse view on expressions: |
| 583 | VERIFY_IS_APPROX((s1*m2).eval(), (s1*refMat2).sparseView().eval()); |
| 584 | VERIFY_IS_APPROX((m2+m2).eval(), (refMat2+refMat2).sparseView().eval()); |
| 585 | VERIFY_IS_APPROX((m2*m2).eval(), (refMat2.lazyProduct(refMat2)).sparseView().eval()); |
| 586 | VERIFY_IS_APPROX((m2*m2).eval(), (refMat2*refMat2).sparseView().eval()); |
Gael Guennebaud | fa6d36e | 2010-07-22 15:57:01 +0200 | [diff] [blame] | 587 | } |
Gael Guennebaud | 82f9aa1 | 2011-12-04 21:49:21 +0100 | [diff] [blame] | 588 | |
| 589 | // test diagonal |
| 590 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 591 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); |
| 592 | SparseMatrixType m2(rows, cols); |
Gael Guennebaud | 82f9aa1 | 2011-12-04 21:49:21 +0100 | [diff] [blame] | 593 | initSparse<Scalar>(density, refMat2, m2); |
| 594 | VERIFY_IS_APPROX(m2.diagonal(), refMat2.diagonal().eval()); |
Gael Guennebaud | 296d24b | 2017-01-25 17:39:01 +0100 | [diff] [blame] | 595 | DenseVector d = m2.diagonal(); |
| 596 | VERIFY_IS_APPROX(d, refMat2.diagonal().eval()); |
| 597 | d = m2.diagonal().array(); |
| 598 | VERIFY_IS_APPROX(d, refMat2.diagonal().eval()); |
Gael Guennebaud | b26e697 | 2014-12-01 14:41:39 +0100 | [diff] [blame] | 599 | VERIFY_IS_APPROX(const_cast<const SparseMatrixType&>(m2).diagonal(), refMat2.diagonal().eval()); |
| 600 | |
| 601 | initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag); |
| 602 | m2.diagonal() += refMat2.diagonal(); |
| 603 | refMat2.diagonal() += refMat2.diagonal(); |
| 604 | VERIFY_IS_APPROX(m2, refMat2); |
Gael Guennebaud | 82f9aa1 | 2011-12-04 21:49:21 +0100 | [diff] [blame] | 605 | } |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 606 | |
Gael Guennebaud | 62f21e2 | 2015-06-24 17:55:00 +0200 | [diff] [blame] | 607 | // test diagonal to sparse |
| 608 | { |
| 609 | DenseVector d = DenseVector::Random(rows); |
| 610 | DenseMatrix refMat2 = d.asDiagonal(); |
Gael Guennebaud | f489f44 | 2019-01-28 17:29:50 +0100 | [diff] [blame] | 611 | SparseMatrixType m2; |
Gael Guennebaud | 62f21e2 | 2015-06-24 17:55:00 +0200 | [diff] [blame] | 612 | m2 = d.asDiagonal(); |
| 613 | VERIFY_IS_APPROX(m2, refMat2); |
Gael Guennebaud | 4c8cd13 | 2015-06-24 18:11:06 +0200 | [diff] [blame] | 614 | SparseMatrixType m3(d.asDiagonal()); |
| 615 | VERIFY_IS_APPROX(m3, refMat2); |
Gael Guennebaud | 62f21e2 | 2015-06-24 17:55:00 +0200 | [diff] [blame] | 616 | refMat2 += d.asDiagonal(); |
| 617 | m2 += d.asDiagonal(); |
| 618 | VERIFY_IS_APPROX(m2, refMat2); |
Gael Guennebaud | f489f44 | 2019-01-28 17:29:50 +0100 | [diff] [blame] | 619 | m2.setZero(); m2 += d.asDiagonal(); |
| 620 | refMat2.setZero(); refMat2 += d.asDiagonal(); |
| 621 | VERIFY_IS_APPROX(m2, refMat2); |
| 622 | m2.setZero(); m2 -= d.asDiagonal(); |
| 623 | refMat2.setZero(); refMat2 -= d.asDiagonal(); |
| 624 | VERIFY_IS_APPROX(m2, refMat2); |
| 625 | |
| 626 | initSparse<Scalar>(density, refMat2, m2); |
| 627 | m2.makeCompressed(); |
| 628 | m2 += d.asDiagonal(); |
| 629 | refMat2 += d.asDiagonal(); |
| 630 | VERIFY_IS_APPROX(m2, refMat2); |
| 631 | |
| 632 | initSparse<Scalar>(density, refMat2, m2); |
| 633 | m2.makeCompressed(); |
| 634 | VectorXi res(rows); |
| 635 | for(Index i=0; i<rows; ++i) |
| 636 | res(i) = internal::random<int>(0,3); |
| 637 | m2.reserve(res); |
| 638 | m2 -= d.asDiagonal(); |
| 639 | refMat2 -= d.asDiagonal(); |
| 640 | VERIFY_IS_APPROX(m2, refMat2); |
Gael Guennebaud | 62f21e2 | 2015-06-24 17:55:00 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 643 | // test conservative resize |
| 644 | { |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 645 | std::vector< std::pair<StorageIndex,StorageIndex> > inc; |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 646 | if(rows > 3 && cols > 2) |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 647 | inc.push_back(std::pair<StorageIndex,StorageIndex>(-3,-2)); |
| 648 | inc.push_back(std::pair<StorageIndex,StorageIndex>(0,0)); |
| 649 | inc.push_back(std::pair<StorageIndex,StorageIndex>(3,2)); |
| 650 | inc.push_back(std::pair<StorageIndex,StorageIndex>(3,0)); |
| 651 | inc.push_back(std::pair<StorageIndex,StorageIndex>(0,3)); |
Adam Shapiro | 2ac0b78 | 2021-02-23 21:32:39 +0000 | [diff] [blame] | 652 | inc.push_back(std::pair<StorageIndex,StorageIndex>(0,-1)); |
| 653 | inc.push_back(std::pair<StorageIndex,StorageIndex>(-1,0)); |
| 654 | inc.push_back(std::pair<StorageIndex,StorageIndex>(-1,-1)); |
| 655 | |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 656 | for(size_t i = 0; i< inc.size(); i++) { |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 657 | StorageIndex incRows = inc[i].first; |
| 658 | StorageIndex incCols = inc[i].second; |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 659 | SparseMatrixType m1(rows, cols); |
| 660 | DenseMatrix refMat1 = DenseMatrix::Zero(rows, cols); |
| 661 | initSparse<Scalar>(density, refMat1, m1); |
Adam Shapiro | 2ac0b78 | 2021-02-23 21:32:39 +0000 | [diff] [blame] | 662 | |
| 663 | SparseMatrixType m2 = m1; |
| 664 | m2.makeCompressed(); |
| 665 | |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 666 | m1.conservativeResize(rows+incRows, cols+incCols); |
Adam Shapiro | 2ac0b78 | 2021-02-23 21:32:39 +0000 | [diff] [blame] | 667 | m2.conservativeResize(rows+incRows, cols+incCols); |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 668 | refMat1.conservativeResize(rows+incRows, cols+incCols); |
| 669 | if (incRows > 0) refMat1.bottomRows(incRows).setZero(); |
| 670 | if (incCols > 0) refMat1.rightCols(incCols).setZero(); |
Adam Shapiro | 2ac0b78 | 2021-02-23 21:32:39 +0000 | [diff] [blame] | 671 | |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 672 | VERIFY_IS_APPROX(m1, refMat1); |
Adam Shapiro | 2ac0b78 | 2021-02-23 21:32:39 +0000 | [diff] [blame] | 673 | VERIFY_IS_APPROX(m2, refMat1); |
| 674 | |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 675 | // Insert new values |
| 676 | if (incRows > 0) |
Gael Guennebaud | 7ee378d | 2013-07-12 16:40:02 +0200 | [diff] [blame] | 677 | m1.insert(m1.rows()-1, 0) = refMat1(refMat1.rows()-1, 0) = 1; |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 678 | if (incCols > 0) |
Gael Guennebaud | 7ee378d | 2013-07-12 16:40:02 +0200 | [diff] [blame] | 679 | m1.insert(0, m1.cols()-1) = refMat1(0, refMat1.cols()-1) = 1; |
Adam Shapiro | 2ac0b78 | 2021-02-23 21:32:39 +0000 | [diff] [blame] | 680 | |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 681 | VERIFY_IS_APPROX(m1, refMat1); |
Adam Shapiro | 2ac0b78 | 2021-02-23 21:32:39 +0000 | [diff] [blame] | 682 | |
| 683 | |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 684 | } |
| 685 | } |
Desire NUENTSA | 4cd8245 | 2013-06-11 14:42:29 +0200 | [diff] [blame] | 686 | |
| 687 | // test Identity matrix |
| 688 | { |
| 689 | DenseMatrix refMat1 = DenseMatrix::Identity(rows, rows); |
| 690 | SparseMatrixType m1(rows, rows); |
| 691 | m1.setIdentity(); |
| 692 | VERIFY_IS_APPROX(m1, refMat1); |
Gael Guennebaud | 8a211bb | 2015-10-25 22:01:58 +0100 | [diff] [blame] | 693 | for(int k=0; k<rows*rows/4; ++k) |
| 694 | { |
| 695 | Index i = internal::random<Index>(0,rows-1); |
| 696 | Index j = internal::random<Index>(0,rows-1); |
Gael Guennebaud | 73f692d | 2015-10-27 11:01:37 +0100 | [diff] [blame] | 697 | Scalar v = internal::random<Scalar>(); |
Gael Guennebaud | 8a211bb | 2015-10-25 22:01:58 +0100 | [diff] [blame] | 698 | m1.coeffRef(i,j) = v; |
| 699 | refMat1.coeffRef(i,j) = v; |
| 700 | VERIFY_IS_APPROX(m1, refMat1); |
| 701 | if(internal::random<Index>(0,10)<2) |
| 702 | m1.makeCompressed(); |
| 703 | } |
| 704 | m1.setIdentity(); |
| 705 | refMat1.setIdentity(); |
| 706 | VERIFY_IS_APPROX(m1, refMat1); |
Desire NUENTSA | 4cd8245 | 2013-06-11 14:42:29 +0200 | [diff] [blame] | 707 | } |
Gael Guennebaud | ec46970 | 2016-02-01 15:04:33 +0100 | [diff] [blame] | 708 | |
| 709 | // test array/vector of InnerIterator |
| 710 | { |
| 711 | typedef typename SparseMatrixType::InnerIterator IteratorType; |
| 712 | |
| 713 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); |
| 714 | SparseMatrixType m2(rows, cols); |
| 715 | initSparse<Scalar>(density, refMat2, m2); |
| 716 | IteratorType static_array[2]; |
| 717 | static_array[0] = IteratorType(m2,0); |
| 718 | static_array[1] = IteratorType(m2,m2.outerSize()-1); |
| 719 | VERIFY( static_array[0] || m2.innerVector(static_array[0].outer()).nonZeros() == 0 ); |
| 720 | VERIFY( static_array[1] || m2.innerVector(static_array[1].outer()).nonZeros() == 0 ); |
| 721 | if(static_array[0] && static_array[1]) |
| 722 | { |
| 723 | ++(static_array[1]); |
| 724 | static_array[1] = IteratorType(m2,0); |
| 725 | VERIFY( static_array[1] ); |
| 726 | VERIFY( static_array[1].index() == static_array[0].index() ); |
| 727 | VERIFY( static_array[1].outer() == static_array[0].outer() ); |
| 728 | VERIFY( static_array[1].value() == static_array[0].value() ); |
| 729 | } |
| 730 | |
| 731 | std::vector<IteratorType> iters(2); |
| 732 | iters[0] = IteratorType(m2,0); |
| 733 | iters[1] = IteratorType(m2,m2.outerSize()-1); |
| 734 | } |
Gael Guennebaud | 25424d9 | 2020-08-26 12:32:20 +0200 | [diff] [blame] | 735 | |
| 736 | // test reserve with empty rows/columns |
| 737 | { |
| 738 | SparseMatrixType m1(0,cols); |
| 739 | m1.reserve(ArrayXi::Constant(m1.outerSize(),1)); |
| 740 | SparseMatrixType m2(rows,0); |
| 741 | m2.reserve(ArrayXi::Constant(m2.outerSize(),1)); |
| 742 | } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 745 | |
Christoph Hertzberg | c5a3777 | 2014-10-31 17:19:05 +0100 | [diff] [blame] | 746 | template<typename SparseMatrixType> |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 747 | void big_sparse_triplet(Index rows, Index cols, double density) { |
| 748 | typedef typename SparseMatrixType::StorageIndex StorageIndex; |
| 749 | typedef typename SparseMatrixType::Scalar Scalar; |
| 750 | typedef Triplet<Scalar,Index> TripletType; |
| 751 | std::vector<TripletType> triplets; |
Erik Schultheis | d271a7d | 2022-01-26 18:16:19 +0000 | [diff] [blame] | 752 | double nelements = density * static_cast<double>(rows*cols); |
Antonio Sanchez | 543e34a | 2021-03-05 12:54:26 -0800 | [diff] [blame] | 753 | VERIFY(nelements>=0 && nelements < static_cast<double>(NumTraits<StorageIndex>::highest())); |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 754 | Index ntriplets = Index(nelements); |
| 755 | triplets.reserve(ntriplets); |
| 756 | Scalar sum = Scalar(0); |
| 757 | for(Index i=0;i<ntriplets;++i) |
| 758 | { |
| 759 | Index r = internal::random<Index>(0,rows-1); |
| 760 | Index c = internal::random<Index>(0,cols-1); |
Gael Guennebaud | cd25b53 | 2018-12-08 00:13:37 +0100 | [diff] [blame] | 761 | // use positive values to prevent numerical cancellation errors in sum |
| 762 | Scalar v = numext::abs(internal::random<Scalar>()); |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 763 | triplets.push_back(TripletType(r,c,v)); |
| 764 | sum += v; |
| 765 | } |
| 766 | SparseMatrixType m(rows,cols); |
| 767 | m.setFromTriplets(triplets.begin(), triplets.end()); |
| 768 | VERIFY(m.nonZeros() <= ntriplets); |
| 769 | VERIFY_IS_APPROX(sum, m.sum()); |
Christoph Hertzberg | c5a3777 | 2014-10-31 17:19:05 +0100 | [diff] [blame] | 770 | } |
| 771 | |
Gael Guennebaud | dff3a92 | 2018-07-17 15:52:58 +0200 | [diff] [blame] | 772 | template<int> |
| 773 | void bug1105() |
| 774 | { |
| 775 | // Regression test for bug 1105 |
| 776 | int n = Eigen::internal::random<int>(200,600); |
| 777 | SparseMatrix<std::complex<double>,0, long> mat(n, n); |
| 778 | std::complex<double> val; |
| 779 | |
| 780 | for(int i=0; i<n; ++i) |
| 781 | { |
| 782 | mat.coeffRef(i, i%(n/10)) = val; |
| 783 | VERIFY(mat.data().allocatedSize()<20*n); |
| 784 | } |
| 785 | } |
Christoph Hertzberg | c5a3777 | 2014-10-31 17:19:05 +0100 | [diff] [blame] | 786 | |
Gael Guennebaud | 8214cf1 | 2018-10-11 10:27:23 +0200 | [diff] [blame] | 787 | #ifndef EIGEN_SPARSE_TEST_INCLUDED_FROM_SPARSE_EXTRA |
| 788 | |
Gael Guennebaud | 82f0ce2 | 2018-07-17 14:46:15 +0200 | [diff] [blame] | 789 | EIGEN_DECLARE_TEST(sparse_basic) |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 790 | { |
Rasmus Munk Larsen | 954b4ca | 2018-10-22 13:48:56 -0700 | [diff] [blame] | 791 | g_dense_op_sparse_count = 0; // Suppresses compiler warning. |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 792 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | c43154b | 2015-03-04 10:16:46 +0100 | [diff] [blame] | 793 | int r = Eigen::internal::random<int>(1,200), c = Eigen::internal::random<int>(1,200); |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 794 | if(Eigen::internal::random<int>(0,4) == 0) { |
| 795 | r = c; // check square matrices in 25% of tries |
| 796 | } |
| 797 | EIGEN_UNUSED_VARIABLE(r+c); |
| 798 | CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(1, 1)) )); |
Gael Guennebaud | 91fe150 | 2011-06-07 11:28:16 +0200 | [diff] [blame] | 799 | CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(8, 8)) )); |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 800 | CALL_SUBTEST_2(( sparse_basic(SparseMatrix<std::complex<double>, ColMajor>(r, c)) )); |
| 801 | CALL_SUBTEST_2(( sparse_basic(SparseMatrix<std::complex<double>, RowMajor>(r, c)) )); |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 802 | CALL_SUBTEST_2(( sparse_basic(SparseMatrix<float, RowMajor>(r, c)))); |
| 803 | CALL_SUBTEST_2(( sparse_basic(SparseMatrix<float, ColMajor>(r, c)))); |
| 804 | CALL_SUBTEST_3(( sparse_basic(SparseMatrix<double, ColMajor>(r, c)))); |
| 805 | CALL_SUBTEST_3(( sparse_basic(SparseMatrix<double, RowMajor>(r, c)))); |
| 806 | CALL_SUBTEST_4(( sparse_basic(SparseMatrix<double, ColMajor,long int>(r, c)) )); |
| 807 | CALL_SUBTEST_4(( sparse_basic(SparseMatrix<double, RowMajor,long int>(r, c)) )); |
Gael Guennebaud | 47e8902 | 2013-06-10 10:34:03 +0200 | [diff] [blame] | 808 | |
Gael Guennebaud | c43154b | 2015-03-04 10:16:46 +0100 | [diff] [blame] | 809 | r = Eigen::internal::random<int>(1,100); |
| 810 | c = Eigen::internal::random<int>(1,100); |
| 811 | if(Eigen::internal::random<int>(0,4) == 0) { |
| 812 | r = c; // check square matrices in 25% of tries |
| 813 | } |
| 814 | |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 815 | CALL_SUBTEST_5(( sparse_basic(SparseMatrix<double,ColMajor,short int>(short(r), short(c))) )); |
| 816 | CALL_SUBTEST_5(( sparse_basic(SparseMatrix<double,RowMajor,short int>(short(r), short(c))) )); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 817 | } |
Christoph Hertzberg | c5a3777 | 2014-10-31 17:19:05 +0100 | [diff] [blame] | 818 | |
| 819 | // Regression test for bug 900: (manually insert higher values here, if you have enough RAM): |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 820 | CALL_SUBTEST_5(( big_sparse_triplet<SparseMatrix<float, RowMajor, int>>(10000, 10000, 0.125))); |
| 821 | CALL_SUBTEST_5(( big_sparse_triplet<SparseMatrix<double, ColMajor, long int>>(10000, 10000, 0.125))); |
Gael Guennebaud | bfd6ee6 | 2015-11-06 15:05:37 +0100 | [diff] [blame] | 822 | |
Charles Schlosser | 44fe539 | 2022-12-01 19:28:56 +0000 | [diff] [blame^] | 823 | CALL_SUBTEST_5(bug1105<0>()); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 824 | } |
Gael Guennebaud | 8214cf1 | 2018-10-11 10:27:23 +0200 | [diff] [blame] | 825 | #endif |