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