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(); |
Gael Guennebaud | 0a537cb | 2016-02-12 15:58:31 +0100 | [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); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 94 | if (m1.coeff(i,j)==Scalar(0)) |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 95 | m2.insert(i,j) = m1(i,j) = internal::random<Scalar>(); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
Gael Guennebaud | c43154b | 2015-03-04 10:16:46 +0100 | [diff] [blame] | 98 | |
| 99 | if(call_reserve && !SparseMatrixType::IsRowMajor) |
| 100 | { |
| 101 | VERIFY(g_realloc_count==0); |
| 102 | } |
| 103 | |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 104 | m2.finalize(); |
| 105 | VERIFY_IS_APPROX(m2,m1); |
| 106 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 107 | |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 108 | // test insert (fully random) |
| 109 | { |
| 110 | DenseMatrix m1(rows,cols); |
| 111 | m1.setZero(); |
| 112 | SparseMatrixType m2(rows,cols); |
Gael Guennebaud | 4ca89f3 | 2011-12-02 19:00:16 +0100 | [diff] [blame] | 113 | if(internal::random<int>()%2) |
| 114 | m2.reserve(VectorXi::Constant(m2.outerSize(), 2)); |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 115 | for (int k=0; k<rows*cols; ++k) |
| 116 | { |
Gael Guennebaud | 6d1f5db | 2013-07-10 23:48:26 +0200 | [diff] [blame] | 117 | Index i = internal::random<Index>(0,rows-1); |
| 118 | Index j = internal::random<Index>(0,cols-1); |
Gael Guennebaud | 4ca89f3 | 2011-12-02 19:00:16 +0100 | [diff] [blame] | 119 | if ((m1.coeff(i,j)==Scalar(0)) && (internal::random<int>()%2)) |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 120 | m2.insert(i,j) = m1(i,j) = internal::random<Scalar>(); |
Gael Guennebaud | 4ca89f3 | 2011-12-02 19:00:16 +0100 | [diff] [blame] | 121 | else |
| 122 | { |
| 123 | Scalar v = internal::random<Scalar>(); |
| 124 | m2.coeffRef(i,j) += v; |
| 125 | m1(i,j) += v; |
| 126 | } |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 127 | } |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 128 | VERIFY_IS_APPROX(m2,m1); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 129 | } |
Gael Guennebaud | 7706baf | 2011-09-08 13:42:54 +0200 | [diff] [blame] | 130 | |
| 131 | // test insert (un-compressed) |
| 132 | for(int mode=0;mode<4;++mode) |
| 133 | { |
| 134 | DenseMatrix m1(rows,cols); |
| 135 | m1.setZero(); |
| 136 | SparseMatrixType m2(rows,cols); |
Gael Guennebaud | b47ef14 | 2014-07-08 16:47:11 +0200 | [diff] [blame] | 137 | 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] | 138 | m2.reserve(r); |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 139 | for (Index k=0; k<rows*cols; ++k) |
Gael Guennebaud | 7706baf | 2011-09-08 13:42:54 +0200 | [diff] [blame] | 140 | { |
Gael Guennebaud | 6d1f5db | 2013-07-10 23:48:26 +0200 | [diff] [blame] | 141 | Index i = internal::random<Index>(0,rows-1); |
| 142 | Index j = internal::random<Index>(0,cols-1); |
Gael Guennebaud | 7706baf | 2011-09-08 13:42:54 +0200 | [diff] [blame] | 143 | if (m1.coeff(i,j)==Scalar(0)) |
| 144 | m2.insert(i,j) = m1(i,j) = internal::random<Scalar>(); |
| 145 | if(mode==3) |
| 146 | m2.reserve(r); |
| 147 | } |
Gael Guennebaud | 4ca89f3 | 2011-12-02 19:00:16 +0100 | [diff] [blame] | 148 | if(internal::random<int>()%2) |
| 149 | m2.makeCompressed(); |
Gael Guennebaud | 7706baf | 2011-09-08 13:42:54 +0200 | [diff] [blame] | 150 | VERIFY_IS_APPROX(m2,m1); |
| 151 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 152 | |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 153 | // test basic computations |
| 154 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 155 | DenseMatrix refM1 = DenseMatrix::Zero(rows, cols); |
| 156 | DenseMatrix refM2 = DenseMatrix::Zero(rows, cols); |
| 157 | DenseMatrix refM3 = DenseMatrix::Zero(rows, cols); |
| 158 | DenseMatrix refM4 = DenseMatrix::Zero(rows, cols); |
| 159 | SparseMatrixType m1(rows, cols); |
| 160 | SparseMatrixType m2(rows, cols); |
| 161 | SparseMatrixType m3(rows, cols); |
| 162 | SparseMatrixType m4(rows, cols); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 163 | initSparse<Scalar>(density, refM1, m1); |
| 164 | initSparse<Scalar>(density, refM2, m2); |
| 165 | initSparse<Scalar>(density, refM3, m3); |
| 166 | initSparse<Scalar>(density, refM4, m4); |
| 167 | |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 168 | if(internal::random<bool>()) |
| 169 | m1.makeCompressed(); |
| 170 | |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 171 | Index m1_nnz = m1.nonZeros(); |
| 172 | |
Gael Guennebaud | 4aac872 | 2014-07-22 12:54:03 +0200 | [diff] [blame] | 173 | VERIFY_IS_APPROX(m1*s1, refM1*s1); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 174 | VERIFY_IS_APPROX(m1+m2, refM1+refM2); |
| 175 | VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3); |
| 176 | VERIFY_IS_APPROX(m3.cwiseProduct(m1+m2), refM3.cwiseProduct(refM1+refM2)); |
| 177 | VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 178 | VERIFY_IS_APPROX(m4=m1/s1, refM1/s1); |
| 179 | VERIFY_IS_EQUAL(m4.nonZeros(), m1_nnz); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 180 | |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 181 | if(SparseMatrixType::IsRowMajor) |
| 182 | VERIFY_IS_APPROX(m1.innerVector(0).dot(refM2.row(0)), refM1.row(0).dot(refM2.row(0))); |
| 183 | else |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 184 | 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] | 185 | |
Gael Guennebaud | 3573a10 | 2014-02-17 13:46:17 +0100 | [diff] [blame] | 186 | DenseVector rv = DenseVector::Random(m1.cols()); |
| 187 | DenseVector cv = DenseVector::Random(m1.rows()); |
| 188 | Index r = internal::random<Index>(0,m1.rows()-2); |
| 189 | Index c = internal::random<Index>(0,m1.cols()-1); |
| 190 | VERIFY_IS_APPROX(( m1.template block<1,Dynamic>(r,0,1,m1.cols()).dot(rv)) , refM1.row(r).dot(rv)); |
| 191 | VERIFY_IS_APPROX(m1.row(r).dot(rv), refM1.row(r).dot(rv)); |
| 192 | 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] | 193 | |
| 194 | VERIFY_IS_APPROX(m1.conjugate(), refM1.conjugate()); |
| 195 | VERIFY_IS_APPROX(m1.real(), refM1.real()); |
| 196 | |
| 197 | refM4.setRandom(); |
| 198 | // sparse cwise* dense |
| 199 | VERIFY_IS_APPROX(m3.cwiseProduct(refM4), refM3.cwiseProduct(refM4)); |
Gael Guennebaud | 9027508 | 2015-11-04 17:42:07 +0100 | [diff] [blame] | 200 | // dense cwise* sparse |
| 201 | VERIFY_IS_APPROX(refM4.cwiseProduct(m3), refM4.cwiseProduct(refM3)); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 202 | // VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4); |
| 203 | |
Gael Guennebaud | eec0dfd | 2018-10-10 22:50:15 +0200 | [diff] [blame] | 204 | // mixed sparse-dense |
Gael Guennebaud | 15084cf | 2016-01-29 22:09:45 +0100 | [diff] [blame] | 205 | VERIFY_IS_APPROX(refM4 + m3, refM4 + refM3); |
| 206 | VERIFY_IS_APPROX(m3 + refM4, refM3 + refM4); |
| 207 | VERIFY_IS_APPROX(refM4 - m3, refM4 - refM3); |
| 208 | VERIFY_IS_APPROX(m3 - refM4, refM3 - refM4); |
Gael Guennebaud | 7136267 | 2016-12-27 16:34:30 +0100 | [diff] [blame] | 209 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + RealScalar(0.5)*m3).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3); |
| 210 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + m3*RealScalar(0.5)).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3); |
| 211 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + m3.cwiseProduct(m3)).eval(), RealScalar(0.5)*refM4 + refM3.cwiseProduct(refM3)); |
| 212 | |
| 213 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + RealScalar(0.5)*m3).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3); |
| 214 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + m3*RealScalar(0.5)).eval(), RealScalar(0.5)*refM4 + RealScalar(0.5)*refM3); |
| 215 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + (m3+m3)).eval(), RealScalar(0.5)*refM4 + (refM3+refM3)); |
| 216 | VERIFY_IS_APPROX(((refM3+m3)+RealScalar(0.5)*m3).eval(), RealScalar(0.5)*refM3 + (refM3+refM3)); |
| 217 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + (refM3+m3)).eval(), RealScalar(0.5)*refM4 + (refM3+refM3)); |
| 218 | VERIFY_IS_APPROX((RealScalar(0.5)*refM4 + (m3+refM3)).eval(), RealScalar(0.5)*refM4 + (refM3+refM3)); |
| 219 | |
Gael Guennebaud | 15084cf | 2016-01-29 22:09:45 +0100 | [diff] [blame] | 220 | |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 221 | VERIFY_IS_APPROX(m1.sum(), refM1.sum()); |
| 222 | |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 223 | m4 = m1; refM4 = m4; |
| 224 | |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 225 | VERIFY_IS_APPROX(m1*=s1, refM1*=s1); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 226 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 227 | VERIFY_IS_APPROX(m1/=s1, refM1/=s1); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 228 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
Gael Guennebaud | 2c1b56f | 2016-05-31 10:56:53 +0200 | [diff] [blame] | 229 | |
| 230 | VERIFY_IS_APPROX(m1+=m2, refM1+=refM2); |
| 231 | VERIFY_IS_APPROX(m1-=m2, refM1-=refM2); |
| 232 | |
Gael Guennebaud | eec0dfd | 2018-10-10 22:50:15 +0200 | [diff] [blame] | 233 | refM3 = refM1; |
| 234 | |
| 235 | VERIFY_IS_APPROX(refM1+=m2, refM3+=refM2); |
| 236 | VERIFY_IS_APPROX(refM1-=m2, refM3-=refM2); |
| 237 | |
| 238 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =m2+refM4, refM3 =refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,10); |
| 239 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=m2+refM4, refM3+=refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 240 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=m2+refM4, refM3-=refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 241 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =refM4+m2, refM3 =refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 242 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=refM4+m2, refM3+=refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 243 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=refM4+m2, refM3-=refM2+refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 244 | |
| 245 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =m2-refM4, refM3 =refM2-refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,20); |
| 246 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=m2-refM4, refM3+=refM2-refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 247 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=m2-refM4, refM3-=refM2-refM4); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 248 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1 =refM4-m2, refM3 =refM4-refM2); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 249 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1+=refM4-m2, refM3+=refM4-refM2); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 250 | g_dense_op_sparse_count=0; VERIFY_IS_APPROX(refM1-=refM4-m2, refM3-=refM4-refM2); VERIFY_IS_EQUAL(g_dense_op_sparse_count,1); |
| 251 | refM3 = m3; |
| 252 | |
Gael Guennebaud | ba3f977 | 2017-01-23 22:06:08 +0100 | [diff] [blame] | 253 | if (rows>=2 && cols>=2) |
| 254 | { |
| 255 | VERIFY_RAISES_ASSERT( m1 += m1.innerVector(0) ); |
| 256 | VERIFY_RAISES_ASSERT( m1 -= m1.innerVector(0) ); |
| 257 | VERIFY_RAISES_ASSERT( refM1 -= m1.innerVector(0) ); |
| 258 | VERIFY_RAISES_ASSERT( refM1 += m1.innerVector(0) ); |
| 259 | } |
Gael Guennebaud | 26a2c6f | 2017-12-14 15:11:04 +0100 | [diff] [blame] | 260 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | ba3f977 | 2017-01-23 22:06:08 +0100 | [diff] [blame] | 261 | |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 262 | // test aliasing |
| 263 | VERIFY_IS_APPROX((m1 = -m1), (refM1 = -refM1)); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 264 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
| 265 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 266 | VERIFY_IS_APPROX((m1 = m1.transpose()), (refM1 = refM1.transpose().eval())); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 267 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
| 268 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 269 | VERIFY_IS_APPROX((m1 = -m1.transpose()), (refM1 = -refM1.transpose().eval())); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 270 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
| 271 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 272 | VERIFY_IS_APPROX((m1 += -m1), (refM1 += -refM1)); |
Gael Guennebaud | c86911a | 2017-01-30 13:38:24 +0100 | [diff] [blame] | 273 | VERIFY_IS_EQUAL(m1.nonZeros(), m1_nnz); |
| 274 | m1 = m4; refM1 = refM4; |
Gael Guennebaud | 7e029d1 | 2016-08-29 12:06:37 +0200 | [diff] [blame] | 275 | |
| 276 | if(m1.isCompressed()) |
| 277 | { |
| 278 | VERIFY_IS_APPROX(m1.coeffs().sum(), m1.sum()); |
| 279 | m1.coeffs() += s1; |
| 280 | for(Index j = 0; j<m1.outerSize(); ++j) |
| 281 | for(typename SparseMatrixType::InnerIterator it(m1,j); it; ++it) |
| 282 | refM1(it.row(), it.col()) += s1; |
| 283 | VERIFY_IS_APPROX(m1, refM1); |
| 284 | } |
Gael Guennebaud | 2e334f5 | 2016-11-14 18:47:02 +0100 | [diff] [blame] | 285 | |
| 286 | // and/or |
| 287 | { |
| 288 | typedef SparseMatrix<bool, SparseMatrixType::Options, typename SparseMatrixType::StorageIndex> SpBool; |
| 289 | SpBool mb1 = m1.real().template cast<bool>(); |
| 290 | SpBool mb2 = m2.real().template cast<bool>(); |
| 291 | VERIFY_IS_EQUAL(mb1.template cast<int>().sum(), refM1.real().template cast<bool>().count()); |
| 292 | VERIFY_IS_EQUAL((mb1 && mb2).template cast<int>().sum(), (refM1.real().template cast<bool>() && refM2.real().template cast<bool>()).count()); |
| 293 | VERIFY_IS_EQUAL((mb1 || mb2).template cast<int>().sum(), (refM1.real().template cast<bool>() || refM2.real().template cast<bool>()).count()); |
| 294 | SpBool mb3 = mb1 && mb2; |
| 295 | if(mb1.coeffs().all() && mb2.coeffs().all()) |
| 296 | { |
| 297 | VERIFY_IS_EQUAL(mb3.nonZeros(), (refM1.real().template cast<bool>() && refM2.real().template cast<bool>()).count()); |
| 298 | } |
| 299 | } |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Gael Guennebaud | eedb87f | 2016-11-14 14:05:53 +0100 | [diff] [blame] | 302 | // test reverse iterators |
| 303 | { |
| 304 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); |
| 305 | SparseMatrixType m2(rows, cols); |
| 306 | initSparse<Scalar>(density, refMat2, m2); |
| 307 | std::vector<Scalar> ref_value(m2.innerSize()); |
| 308 | std::vector<Index> ref_index(m2.innerSize()); |
| 309 | if(internal::random<bool>()) |
| 310 | m2.makeCompressed(); |
| 311 | for(Index j = 0; j<m2.outerSize(); ++j) |
| 312 | { |
| 313 | Index count_forward = 0; |
| 314 | |
| 315 | for(typename SparseMatrixType::InnerIterator it(m2,j); it; ++it) |
| 316 | { |
| 317 | ref_value[ref_value.size()-1-count_forward] = it.value(); |
| 318 | ref_index[ref_index.size()-1-count_forward] = it.index(); |
| 319 | count_forward++; |
| 320 | } |
| 321 | Index count_reverse = 0; |
| 322 | for(typename SparseMatrixType::ReverseInnerIterator it(m2,j); it; --it) |
| 323 | { |
| 324 | VERIFY_IS_APPROX( std::abs(ref_value[ref_value.size()-count_forward+count_reverse])+1, std::abs(it.value())+1); |
| 325 | VERIFY_IS_EQUAL( ref_index[ref_index.size()-count_forward+count_reverse] , it.index()); |
| 326 | count_reverse++; |
| 327 | } |
| 328 | VERIFY_IS_EQUAL(count_forward, count_reverse); |
| 329 | } |
| 330 | } |
| 331 | |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 332 | // test transpose |
| 333 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 334 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); |
| 335 | SparseMatrixType m2(rows, cols); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 336 | initSparse<Scalar>(density, refMat2, m2); |
| 337 | VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval()); |
| 338 | VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose()); |
| 339 | |
| 340 | VERIFY_IS_APPROX(SparseMatrixType(m2.adjoint()), refMat2.adjoint()); |
Gael Guennebaud | ff46ec0 | 2014-09-22 23:33:28 +0200 | [diff] [blame] | 341 | |
| 342 | // check isApprox handles opposite storage order |
| 343 | typename Transpose<SparseMatrixType>::PlainObject m3(m2); |
| 344 | VERIFY(m2.isApprox(m3)); |
Gael Guennebaud | 4e60283 | 2012-11-16 09:02:50 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 347 | // test prune |
| 348 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 349 | SparseMatrixType m2(rows, cols); |
| 350 | DenseMatrix refM2(rows, cols); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 351 | refM2.setZero(); |
| 352 | int countFalseNonZero = 0; |
| 353 | int countTrueNonZero = 0; |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 354 | m2.reserve(VectorXi::Constant(m2.outerSize(), int(m2.innerSize()))); |
| 355 | for (Index j=0; j<m2.cols(); ++j) |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 356 | { |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 357 | for (Index i=0; i<m2.rows(); ++i) |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 358 | { |
Benoit Jacob | 4716040 | 2010-10-25 10:15:22 -0400 | [diff] [blame] | 359 | float x = internal::random<float>(0,1); |
Christoph Hertzberg | dacb469 | 2016-05-05 13:35:45 +0200 | [diff] [blame] | 360 | if (x<0.1f) |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 361 | { |
| 362 | // do nothing |
| 363 | } |
Christoph Hertzberg | dacb469 | 2016-05-05 13:35:45 +0200 | [diff] [blame] | 364 | else if (x<0.5f) |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 365 | { |
| 366 | countFalseNonZero++; |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 367 | m2.insert(i,j) = Scalar(0); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 368 | } |
| 369 | else |
| 370 | { |
| 371 | countTrueNonZero++; |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 372 | m2.insert(i,j) = Scalar(1); |
| 373 | refM2(i,j) = Scalar(1); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 374 | } |
| 375 | } |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 376 | } |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 377 | if(internal::random<bool>()) |
| 378 | m2.makeCompressed(); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 379 | VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros()); |
Gael Guennebaud | a44d91a | 2015-10-13 10:53:38 +0200 | [diff] [blame] | 380 | if(countTrueNonZero>0) |
| 381 | VERIFY_IS_APPROX(m2, refM2); |
Hauke Heibel | d204ec4 | 2010-11-02 14:33:33 +0100 | [diff] [blame] | 382 | m2.prune(Scalar(1)); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 383 | VERIFY(countTrueNonZero==m2.nonZeros()); |
| 384 | VERIFY_IS_APPROX(m2, refM2); |
| 385 | } |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 386 | |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 387 | // test setFromTriplets |
| 388 | { |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 389 | typedef Triplet<Scalar,StorageIndex> TripletType; |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 390 | std::vector<TripletType> triplets; |
Gael Guennebaud | b47ef14 | 2014-07-08 16:47:11 +0200 | [diff] [blame] | 391 | Index ntriplets = rows*cols; |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 392 | triplets.reserve(ntriplets); |
Gael Guennebaud | b4c79ee | 2015-10-13 11:30:41 +0200 | [diff] [blame] | 393 | DenseMatrix refMat_sum = DenseMatrix::Zero(rows,cols); |
| 394 | DenseMatrix refMat_prod = DenseMatrix::Zero(rows,cols); |
| 395 | DenseMatrix refMat_last = DenseMatrix::Zero(rows,cols); |
| 396 | |
Gael Guennebaud | b47ef14 | 2014-07-08 16:47:11 +0200 | [diff] [blame] | 397 | for(Index i=0;i<ntriplets;++i) |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 398 | { |
Gael Guennebaud | aa6c516 | 2015-02-16 13:19:05 +0100 | [diff] [blame] | 399 | StorageIndex r = internal::random<StorageIndex>(0,StorageIndex(rows-1)); |
| 400 | StorageIndex c = internal::random<StorageIndex>(0,StorageIndex(cols-1)); |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 401 | Scalar v = internal::random<Scalar>(); |
Gael Guennebaud | 18e3ac0 | 2012-01-31 09:14:01 +0100 | [diff] [blame] | 402 | triplets.push_back(TripletType(r,c,v)); |
Gael Guennebaud | b4c79ee | 2015-10-13 11:30:41 +0200 | [diff] [blame] | 403 | refMat_sum(r,c) += v; |
| 404 | if(std::abs(refMat_prod(r,c))==0) |
| 405 | refMat_prod(r,c) = v; |
| 406 | else |
| 407 | refMat_prod(r,c) *= v; |
| 408 | refMat_last(r,c) = v; |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 409 | } |
| 410 | SparseMatrixType m(rows,cols); |
| 411 | m.setFromTriplets(triplets.begin(), triplets.end()); |
Gael Guennebaud | b4c79ee | 2015-10-13 11:30:41 +0200 | [diff] [blame] | 412 | VERIFY_IS_APPROX(m, refMat_sum); |
| 413 | |
| 414 | m.setFromTriplets(triplets.begin(), triplets.end(), std::multiplies<Scalar>()); |
| 415 | VERIFY_IS_APPROX(m, refMat_prod); |
| 416 | #if (defined(__cplusplus) && __cplusplus >= 201103L) |
| 417 | m.setFromTriplets(triplets.begin(), triplets.end(), [] (Scalar,Scalar b) { return b; }); |
| 418 | VERIFY_IS_APPROX(m, refMat_last); |
| 419 | #endif |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 420 | } |
Gael Guennebaud | 3af29ca | 2015-02-09 10:23:45 +0100 | [diff] [blame] | 421 | |
| 422 | // test Map |
| 423 | { |
| 424 | DenseMatrix refMat2(rows, cols), refMat3(rows, cols); |
| 425 | SparseMatrixType m2(rows, cols), m3(rows, cols); |
| 426 | initSparse<Scalar>(density, refMat2, m2); |
| 427 | initSparse<Scalar>(density, refMat3, m3); |
| 428 | { |
| 429 | Map<SparseMatrixType> mapMat2(m2.rows(), m2.cols(), m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(), m2.innerNonZeroPtr()); |
| 430 | Map<SparseMatrixType> mapMat3(m3.rows(), m3.cols(), m3.nonZeros(), m3.outerIndexPtr(), m3.innerIndexPtr(), m3.valuePtr(), m3.innerNonZeroPtr()); |
| 431 | VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3); |
| 432 | VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3); |
| 433 | } |
| 434 | { |
Gael Guennebaud | fe51319 | 2015-02-13 10:03:53 +0100 | [diff] [blame] | 435 | MappedSparseMatrix<Scalar,SparseMatrixType::Options,StorageIndex> mapMat2(m2.rows(), m2.cols(), m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(), m2.innerNonZeroPtr()); |
| 436 | MappedSparseMatrix<Scalar,SparseMatrixType::Options,StorageIndex> mapMat3(m3.rows(), m3.cols(), m3.nonZeros(), m3.outerIndexPtr(), m3.innerIndexPtr(), m3.valuePtr(), m3.innerNonZeroPtr()); |
Gael Guennebaud | 3af29ca | 2015-02-09 10:23:45 +0100 | [diff] [blame] | 437 | VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3); |
| 438 | VERIFY_IS_APPROX(mapMat2+mapMat3, refMat2+refMat3); |
| 439 | } |
Gael Guennebaud | 757971e | 2016-07-26 09:40:19 +0200 | [diff] [blame] | 440 | |
| 441 | Index i = internal::random<Index>(0,rows-1); |
| 442 | Index j = internal::random<Index>(0,cols-1); |
| 443 | m2.coeffRef(i,j) = 123; |
| 444 | if(internal::random<bool>()) |
| 445 | m2.makeCompressed(); |
| 446 | Map<SparseMatrixType> mapMat2(rows, cols, m2.nonZeros(), m2.outerIndexPtr(), m2.innerIndexPtr(), m2.valuePtr(), m2.innerNonZeroPtr()); |
| 447 | VERIFY_IS_EQUAL(m2.coeff(i,j),Scalar(123)); |
| 448 | VERIFY_IS_EQUAL(mapMat2.coeff(i,j),Scalar(123)); |
| 449 | mapMat2.coeffRef(i,j) = -123; |
| 450 | VERIFY_IS_EQUAL(m2.coeff(i,j),Scalar(-123)); |
Gael Guennebaud | 3af29ca | 2015-02-09 10:23:45 +0100 | [diff] [blame] | 451 | } |
Gael Guennebaud | 8713807 | 2012-01-28 11:13:59 +0100 | [diff] [blame] | 452 | |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 453 | // test triangularView |
| 454 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 455 | DenseMatrix refMat2(rows, cols), refMat3(rows, cols); |
| 456 | SparseMatrixType m2(rows, cols), m3(rows, cols); |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 457 | initSparse<Scalar>(density, refMat2, m2); |
| 458 | refMat3 = refMat2.template triangularView<Lower>(); |
| 459 | m3 = m2.template triangularView<Lower>(); |
| 460 | VERIFY_IS_APPROX(m3, refMat3); |
| 461 | |
| 462 | refMat3 = refMat2.template triangularView<Upper>(); |
| 463 | m3 = m2.template triangularView<Upper>(); |
| 464 | VERIFY_IS_APPROX(m3, refMat3); |
| 465 | |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 466 | { |
| 467 | refMat3 = refMat2.template triangularView<UnitUpper>(); |
| 468 | m3 = m2.template triangularView<UnitUpper>(); |
| 469 | VERIFY_IS_APPROX(m3, refMat3); |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 470 | |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 471 | refMat3 = refMat2.template triangularView<UnitLower>(); |
| 472 | m3 = m2.template triangularView<UnitLower>(); |
| 473 | VERIFY_IS_APPROX(m3, refMat3); |
| 474 | } |
Desire NUENTSA | 4cd8245 | 2013-06-11 14:42:29 +0200 | [diff] [blame] | 475 | |
| 476 | refMat3 = refMat2.template triangularView<StrictlyUpper>(); |
| 477 | m3 = m2.template triangularView<StrictlyUpper>(); |
| 478 | VERIFY_IS_APPROX(m3, refMat3); |
| 479 | |
| 480 | refMat3 = refMat2.template triangularView<StrictlyLower>(); |
| 481 | m3 = m2.template triangularView<StrictlyLower>(); |
| 482 | VERIFY_IS_APPROX(m3, refMat3); |
Gael Guennebaud | f6b1dee | 2015-11-04 17:02:32 +0100 | [diff] [blame] | 483 | |
Gael Guennebaud | ba3f977 | 2017-01-23 22:06:08 +0100 | [diff] [blame] | 484 | // check sparse-triangular to dense |
Gael Guennebaud | f6b1dee | 2015-11-04 17:02:32 +0100 | [diff] [blame] | 485 | refMat3 = m2.template triangularView<StrictlyUpper>(); |
| 486 | VERIFY_IS_APPROX(refMat3, DenseMatrix(refMat2.template triangularView<StrictlyUpper>())); |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 487 | } |
Gael Guennebaud | fa6d36e | 2010-07-22 15:57:01 +0200 | [diff] [blame] | 488 | |
Gael Guennebaud | 9a3ec63 | 2010-11-15 14:14:05 +0100 | [diff] [blame] | 489 | // test selfadjointView |
Gael Guennebaud | 9353bba | 2011-12-04 14:39:24 +0100 | [diff] [blame] | 490 | if(!SparseMatrixType::IsRowMajor) |
Gael Guennebaud | 9a3ec63 | 2010-11-15 14:14:05 +0100 | [diff] [blame] | 491 | { |
| 492 | DenseMatrix refMat2(rows, rows), refMat3(rows, rows); |
| 493 | SparseMatrixType m2(rows, rows), m3(rows, rows); |
| 494 | initSparse<Scalar>(density, refMat2, m2); |
| 495 | refMat3 = refMat2.template selfadjointView<Lower>(); |
| 496 | m3 = m2.template selfadjointView<Lower>(); |
| 497 | VERIFY_IS_APPROX(m3, refMat3); |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 498 | |
Gael Guennebaud | 11b492e | 2016-12-14 17:53:47 +0100 | [diff] [blame] | 499 | refMat3 += refMat2.template selfadjointView<Lower>(); |
| 500 | m3 += m2.template selfadjointView<Lower>(); |
| 501 | VERIFY_IS_APPROX(m3, refMat3); |
| 502 | |
| 503 | refMat3 -= refMat2.template selfadjointView<Lower>(); |
| 504 | m3 -= m2.template selfadjointView<Lower>(); |
| 505 | VERIFY_IS_APPROX(m3, refMat3); |
| 506 | |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 507 | // selfadjointView only works for square matrices: |
| 508 | SparseMatrixType m4(rows, rows+1); |
| 509 | VERIFY_RAISES_ASSERT(m4.template selfadjointView<Lower>()); |
| 510 | VERIFY_RAISES_ASSERT(m4.template selfadjointView<Upper>()); |
Gael Guennebaud | 9a3ec63 | 2010-11-15 14:14:05 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Gael Guennebaud | fa6d36e | 2010-07-22 15:57:01 +0200 | [diff] [blame] | 513 | // test sparseView |
| 514 | { |
| 515 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 516 | SparseMatrixType m2(rows, rows); |
Gael Guennebaud | 9a3ec63 | 2010-11-15 14:14:05 +0100 | [diff] [blame] | 517 | initSparse<Scalar>(density, refMat2, m2); |
Gael Guennebaud | fa6d36e | 2010-07-22 15:57:01 +0200 | [diff] [blame] | 518 | VERIFY_IS_APPROX(m2.eval(), refMat2.sparseView().eval()); |
Gael Guennebaud | 8456bbb | 2016-05-18 16:53:28 +0200 | [diff] [blame] | 519 | |
| 520 | // sparse view on expressions: |
| 521 | VERIFY_IS_APPROX((s1*m2).eval(), (s1*refMat2).sparseView().eval()); |
| 522 | VERIFY_IS_APPROX((m2+m2).eval(), (refMat2+refMat2).sparseView().eval()); |
| 523 | VERIFY_IS_APPROX((m2*m2).eval(), (refMat2.lazyProduct(refMat2)).sparseView().eval()); |
| 524 | VERIFY_IS_APPROX((m2*m2).eval(), (refMat2*refMat2).sparseView().eval()); |
Gael Guennebaud | fa6d36e | 2010-07-22 15:57:01 +0200 | [diff] [blame] | 525 | } |
Gael Guennebaud | 82f9aa1 | 2011-12-04 21:49:21 +0100 | [diff] [blame] | 526 | |
| 527 | // test diagonal |
| 528 | { |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 529 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); |
| 530 | SparseMatrixType m2(rows, cols); |
Gael Guennebaud | 82f9aa1 | 2011-12-04 21:49:21 +0100 | [diff] [blame] | 531 | initSparse<Scalar>(density, refMat2, m2); |
| 532 | VERIFY_IS_APPROX(m2.diagonal(), refMat2.diagonal().eval()); |
Gael Guennebaud | 296d24b | 2017-01-25 17:39:01 +0100 | [diff] [blame] | 533 | DenseVector d = m2.diagonal(); |
| 534 | VERIFY_IS_APPROX(d, refMat2.diagonal().eval()); |
| 535 | d = m2.diagonal().array(); |
| 536 | VERIFY_IS_APPROX(d, refMat2.diagonal().eval()); |
Gael Guennebaud | b26e697 | 2014-12-01 14:41:39 +0100 | [diff] [blame] | 537 | VERIFY_IS_APPROX(const_cast<const SparseMatrixType&>(m2).diagonal(), refMat2.diagonal().eval()); |
| 538 | |
| 539 | initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag); |
| 540 | m2.diagonal() += refMat2.diagonal(); |
| 541 | refMat2.diagonal() += refMat2.diagonal(); |
| 542 | VERIFY_IS_APPROX(m2, refMat2); |
Gael Guennebaud | 82f9aa1 | 2011-12-04 21:49:21 +0100 | [diff] [blame] | 543 | } |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 544 | |
Gael Guennebaud | 62f21e2 | 2015-06-24 17:55:00 +0200 | [diff] [blame] | 545 | // test diagonal to sparse |
| 546 | { |
| 547 | DenseVector d = DenseVector::Random(rows); |
| 548 | DenseMatrix refMat2 = d.asDiagonal(); |
| 549 | SparseMatrixType m2(rows, rows); |
| 550 | m2 = d.asDiagonal(); |
| 551 | VERIFY_IS_APPROX(m2, refMat2); |
Gael Guennebaud | 4c8cd13 | 2015-06-24 18:11:06 +0200 | [diff] [blame] | 552 | SparseMatrixType m3(d.asDiagonal()); |
| 553 | VERIFY_IS_APPROX(m3, refMat2); |
Gael Guennebaud | 62f21e2 | 2015-06-24 17:55:00 +0200 | [diff] [blame] | 554 | refMat2 += d.asDiagonal(); |
| 555 | m2 += d.asDiagonal(); |
| 556 | VERIFY_IS_APPROX(m2, refMat2); |
| 557 | } |
| 558 | |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 559 | // test conservative resize |
| 560 | { |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 561 | std::vector< std::pair<StorageIndex,StorageIndex> > inc; |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 562 | if(rows > 3 && cols > 2) |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 563 | inc.push_back(std::pair<StorageIndex,StorageIndex>(-3,-2)); |
| 564 | inc.push_back(std::pair<StorageIndex,StorageIndex>(0,0)); |
| 565 | inc.push_back(std::pair<StorageIndex,StorageIndex>(3,2)); |
| 566 | inc.push_back(std::pair<StorageIndex,StorageIndex>(3,0)); |
| 567 | inc.push_back(std::pair<StorageIndex,StorageIndex>(0,3)); |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 568 | |
| 569 | for(size_t i = 0; i< inc.size(); i++) { |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 570 | StorageIndex incRows = inc[i].first; |
| 571 | StorageIndex incCols = inc[i].second; |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 572 | SparseMatrixType m1(rows, cols); |
| 573 | DenseMatrix refMat1 = DenseMatrix::Zero(rows, cols); |
| 574 | initSparse<Scalar>(density, refMat1, m1); |
| 575 | |
| 576 | m1.conservativeResize(rows+incRows, cols+incCols); |
| 577 | refMat1.conservativeResize(rows+incRows, cols+incCols); |
| 578 | if (incRows > 0) refMat1.bottomRows(incRows).setZero(); |
| 579 | if (incCols > 0) refMat1.rightCols(incCols).setZero(); |
| 580 | |
| 581 | VERIFY_IS_APPROX(m1, refMat1); |
| 582 | |
| 583 | // Insert new values |
| 584 | if (incRows > 0) |
Gael Guennebaud | 7ee378d | 2013-07-12 16:40:02 +0200 | [diff] [blame] | 585 | m1.insert(m1.rows()-1, 0) = refMat1(refMat1.rows()-1, 0) = 1; |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 586 | if (incCols > 0) |
Gael Guennebaud | 7ee378d | 2013-07-12 16:40:02 +0200 | [diff] [blame] | 587 | m1.insert(0, m1.cols()-1) = refMat1(0, refMat1.cols()-1) = 1; |
Benjamin Piwowarski | 6bf49ce | 2012-07-19 00:07:06 +0200 | [diff] [blame] | 588 | |
| 589 | VERIFY_IS_APPROX(m1, refMat1); |
| 590 | |
| 591 | |
| 592 | } |
| 593 | } |
Desire NUENTSA | 4cd8245 | 2013-06-11 14:42:29 +0200 | [diff] [blame] | 594 | |
| 595 | // test Identity matrix |
| 596 | { |
| 597 | DenseMatrix refMat1 = DenseMatrix::Identity(rows, rows); |
| 598 | SparseMatrixType m1(rows, rows); |
| 599 | m1.setIdentity(); |
| 600 | VERIFY_IS_APPROX(m1, refMat1); |
Gael Guennebaud | 8a211bb | 2015-10-25 22:01:58 +0100 | [diff] [blame] | 601 | for(int k=0; k<rows*rows/4; ++k) |
| 602 | { |
| 603 | Index i = internal::random<Index>(0,rows-1); |
| 604 | Index j = internal::random<Index>(0,rows-1); |
Gael Guennebaud | 73f692d | 2015-10-27 11:01:37 +0100 | [diff] [blame] | 605 | Scalar v = internal::random<Scalar>(); |
Gael Guennebaud | 8a211bb | 2015-10-25 22:01:58 +0100 | [diff] [blame] | 606 | m1.coeffRef(i,j) = v; |
| 607 | refMat1.coeffRef(i,j) = v; |
| 608 | VERIFY_IS_APPROX(m1, refMat1); |
| 609 | if(internal::random<Index>(0,10)<2) |
| 610 | m1.makeCompressed(); |
| 611 | } |
| 612 | m1.setIdentity(); |
| 613 | refMat1.setIdentity(); |
| 614 | VERIFY_IS_APPROX(m1, refMat1); |
Desire NUENTSA | 4cd8245 | 2013-06-11 14:42:29 +0200 | [diff] [blame] | 615 | } |
Gael Guennebaud | ec46970 | 2016-02-01 15:04:33 +0100 | [diff] [blame] | 616 | |
| 617 | // test array/vector of InnerIterator |
| 618 | { |
| 619 | typedef typename SparseMatrixType::InnerIterator IteratorType; |
| 620 | |
| 621 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); |
| 622 | SparseMatrixType m2(rows, cols); |
| 623 | initSparse<Scalar>(density, refMat2, m2); |
| 624 | IteratorType static_array[2]; |
| 625 | static_array[0] = IteratorType(m2,0); |
| 626 | static_array[1] = IteratorType(m2,m2.outerSize()-1); |
| 627 | VERIFY( static_array[0] || m2.innerVector(static_array[0].outer()).nonZeros() == 0 ); |
| 628 | VERIFY( static_array[1] || m2.innerVector(static_array[1].outer()).nonZeros() == 0 ); |
| 629 | if(static_array[0] && static_array[1]) |
| 630 | { |
| 631 | ++(static_array[1]); |
| 632 | static_array[1] = IteratorType(m2,0); |
| 633 | VERIFY( static_array[1] ); |
| 634 | VERIFY( static_array[1].index() == static_array[0].index() ); |
| 635 | VERIFY( static_array[1].outer() == static_array[0].outer() ); |
| 636 | VERIFY( static_array[1].value() == static_array[0].value() ); |
| 637 | } |
| 638 | |
| 639 | std::vector<IteratorType> iters(2); |
| 640 | iters[0] = IteratorType(m2,0); |
| 641 | iters[1] = IteratorType(m2,m2.outerSize()-1); |
| 642 | } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 645 | |
Christoph Hertzberg | c5a3777 | 2014-10-31 17:19:05 +0100 | [diff] [blame] | 646 | template<typename SparseMatrixType> |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 647 | void big_sparse_triplet(Index rows, Index cols, double density) { |
| 648 | typedef typename SparseMatrixType::StorageIndex StorageIndex; |
| 649 | typedef typename SparseMatrixType::Scalar Scalar; |
| 650 | typedef Triplet<Scalar,Index> TripletType; |
| 651 | std::vector<TripletType> triplets; |
| 652 | double nelements = density * rows*cols; |
| 653 | VERIFY(nelements>=0 && nelements < NumTraits<StorageIndex>::highest()); |
| 654 | Index ntriplets = Index(nelements); |
| 655 | triplets.reserve(ntriplets); |
| 656 | Scalar sum = Scalar(0); |
| 657 | for(Index i=0;i<ntriplets;++i) |
| 658 | { |
| 659 | Index r = internal::random<Index>(0,rows-1); |
| 660 | Index c = internal::random<Index>(0,cols-1); |
Gael Guennebaud | cd25b53 | 2018-12-08 00:13:37 +0100 | [diff] [blame] | 661 | // use positive values to prevent numerical cancellation errors in sum |
| 662 | Scalar v = numext::abs(internal::random<Scalar>()); |
Christoph Hertzberg | e8cdbed | 2014-12-04 22:48:53 +0100 | [diff] [blame] | 663 | triplets.push_back(TripletType(r,c,v)); |
| 664 | sum += v; |
| 665 | } |
| 666 | SparseMatrixType m(rows,cols); |
| 667 | m.setFromTriplets(triplets.begin(), triplets.end()); |
| 668 | VERIFY(m.nonZeros() <= ntriplets); |
| 669 | VERIFY_IS_APPROX(sum, m.sum()); |
Christoph Hertzberg | c5a3777 | 2014-10-31 17:19:05 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Gael Guennebaud | dff3a92 | 2018-07-17 15:52:58 +0200 | [diff] [blame] | 672 | template<int> |
| 673 | void bug1105() |
| 674 | { |
| 675 | // Regression test for bug 1105 |
| 676 | int n = Eigen::internal::random<int>(200,600); |
| 677 | SparseMatrix<std::complex<double>,0, long> mat(n, n); |
| 678 | std::complex<double> val; |
| 679 | |
| 680 | for(int i=0; i<n; ++i) |
| 681 | { |
| 682 | mat.coeffRef(i, i%(n/10)) = val; |
| 683 | VERIFY(mat.data().allocatedSize()<20*n); |
| 684 | } |
| 685 | } |
Christoph Hertzberg | c5a3777 | 2014-10-31 17:19:05 +0100 | [diff] [blame] | 686 | |
Gael Guennebaud | 8214cf1 | 2018-10-11 10:27:23 +0200 | [diff] [blame] | 687 | #ifndef EIGEN_SPARSE_TEST_INCLUDED_FROM_SPARSE_EXTRA |
| 688 | |
Gael Guennebaud | 82f0ce2 | 2018-07-17 14:46:15 +0200 | [diff] [blame] | 689 | EIGEN_DECLARE_TEST(sparse_basic) |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 690 | { |
Rasmus Munk Larsen | 954b4ca | 2018-10-22 13:48:56 -0700 | [diff] [blame] | 691 | g_dense_op_sparse_count = 0; // Suppresses compiler warning. |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 692 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | c43154b | 2015-03-04 10:16:46 +0100 | [diff] [blame] | 693 | 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] | 694 | if(Eigen::internal::random<int>(0,4) == 0) { |
| 695 | r = c; // check square matrices in 25% of tries |
| 696 | } |
| 697 | EIGEN_UNUSED_VARIABLE(r+c); |
| 698 | CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(1, 1)) )); |
Gael Guennebaud | 91fe150 | 2011-06-07 11:28:16 +0200 | [diff] [blame] | 699 | CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(8, 8)) )); |
Christoph Hertzberg | 0833b82 | 2014-10-31 17:12:13 +0100 | [diff] [blame] | 700 | CALL_SUBTEST_2(( sparse_basic(SparseMatrix<std::complex<double>, ColMajor>(r, c)) )); |
| 701 | CALL_SUBTEST_2(( sparse_basic(SparseMatrix<std::complex<double>, RowMajor>(r, c)) )); |
| 702 | CALL_SUBTEST_1(( sparse_basic(SparseMatrix<double>(r, c)) )); |
Gael Guennebaud | d7698c1 | 2015-03-19 15:11:05 +0100 | [diff] [blame] | 703 | CALL_SUBTEST_5(( sparse_basic(SparseMatrix<double,ColMajor,long int>(r, c)) )); |
| 704 | CALL_SUBTEST_5(( sparse_basic(SparseMatrix<double,RowMajor,long int>(r, c)) )); |
Gael Guennebaud | 47e8902 | 2013-06-10 10:34:03 +0200 | [diff] [blame] | 705 | |
Gael Guennebaud | c43154b | 2015-03-04 10:16:46 +0100 | [diff] [blame] | 706 | r = Eigen::internal::random<int>(1,100); |
| 707 | c = Eigen::internal::random<int>(1,100); |
| 708 | if(Eigen::internal::random<int>(0,4) == 0) { |
| 709 | r = c; // check square matrices in 25% of tries |
| 710 | } |
| 711 | |
Gael Guennebaud | d7698c1 | 2015-03-19 15:11:05 +0100 | [diff] [blame] | 712 | CALL_SUBTEST_6(( sparse_basic(SparseMatrix<double,ColMajor,short int>(short(r), short(c))) )); |
| 713 | CALL_SUBTEST_6(( sparse_basic(SparseMatrix<double,RowMajor,short int>(short(r), short(c))) )); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 714 | } |
Christoph Hertzberg | c5a3777 | 2014-10-31 17:19:05 +0100 | [diff] [blame] | 715 | |
| 716 | // Regression test for bug 900: (manually insert higher values here, if you have enough RAM): |
| 717 | CALL_SUBTEST_3((big_sparse_triplet<SparseMatrix<float, RowMajor, int> >(10000, 10000, 0.125))); |
| 718 | CALL_SUBTEST_4((big_sparse_triplet<SparseMatrix<double, ColMajor, long int> >(10000, 10000, 0.125))); |
Gael Guennebaud | bfd6ee6 | 2015-11-06 15:05:37 +0100 | [diff] [blame] | 719 | |
Gael Guennebaud | dff3a92 | 2018-07-17 15:52:58 +0200 | [diff] [blame] | 720 | CALL_SUBTEST_7( bug1105<0>() ); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 721 | } |
Gael Guennebaud | 8214cf1 | 2018-10-11 10:27:23 +0200 | [diff] [blame] | 722 | #endif |