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