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