Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. Eigen itself is part of the KDE project. |
| 3 | // |
| 4 | // Copyright (C) 2008 Daniel Gomez Ferro <dgomezferro@gmail.com> |
| 5 | // |
| 6 | // Eigen is free software; you can redistribute it and/or |
| 7 | // modify it under the terms of the GNU Lesser General Public |
| 8 | // License as published by the Free Software Foundation; either |
| 9 | // version 3 of the License, or (at your option) any later version. |
| 10 | // |
| 11 | // Alternatively, you can redistribute it and/or |
| 12 | // modify it under the terms of the GNU General Public License as |
| 13 | // published by the Free Software Foundation; either version 2 of |
| 14 | // the License, or (at your option) any later version. |
| 15 | // |
| 16 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 17 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the |
| 19 | // GNU General Public License for more details. |
| 20 | // |
| 21 | // You should have received a copy of the GNU Lesser General Public |
| 22 | // License and a copy of the GNU General Public License along with |
| 23 | // Eigen. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | #include "sparse.h" |
| 26 | |
Gael Guennebaud | d3dcb04 | 2009-01-23 09:50:16 +0000 | [diff] [blame] | 27 | template<typename SetterType,typename DenseType, typename Scalar, int Options> |
| 28 | bool test_random_setter(SparseMatrix<Scalar,Options>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords) |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 29 | { |
Gael Guennebaud | d3dcb04 | 2009-01-23 09:50:16 +0000 | [diff] [blame] | 30 | typedef SparseMatrix<Scalar,Options> SparseType; |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 31 | { |
| 32 | sm.setZero(); |
| 33 | SetterType w(sm); |
| 34 | std::vector<Vector2i> remaining = nonzeroCoords; |
| 35 | while(!remaining.empty()) |
| 36 | { |
| 37 | int i = ei_random<int>(0,remaining.size()-1); |
| 38 | w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y()); |
| 39 | remaining[i] = remaining.back(); |
| 40 | remaining.pop_back(); |
| 41 | } |
| 42 | } |
| 43 | return sm.isApprox(ref); |
| 44 | } |
| 45 | |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 46 | template<typename SetterType,typename DenseType, typename T> |
| 47 | bool test_random_setter(DynamicSparseMatrix<T>& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords) |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 48 | { |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 49 | sm.setZero(); |
| 50 | std::vector<Vector2i> remaining = nonzeroCoords; |
| 51 | while(!remaining.empty()) |
| 52 | { |
| 53 | int i = ei_random<int>(0,remaining.size()-1); |
| 54 | sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y()); |
| 55 | remaining[i] = remaining.back(); |
| 56 | remaining.pop_back(); |
| 57 | } |
| 58 | return sm.isApprox(ref); |
| 59 | } |
| 60 | |
| 61 | template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref) |
| 62 | { |
| 63 | const int rows = ref.rows(); |
| 64 | const int cols = ref.cols(); |
| 65 | typedef typename SparseMatrixType::Scalar Scalar; |
| 66 | enum { Flags = SparseMatrixType::Flags }; |
| 67 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 68 | double density = std::max(8./(rows*cols), 0.01); |
| 69 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 70 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 71 | Scalar eps = 1e-6; |
| 72 | |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 73 | SparseMatrixType m(rows, cols); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 74 | DenseMatrix refMat = DenseMatrix::Zero(rows, cols); |
| 75 | DenseVector vec1 = DenseVector::Random(rows); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 76 | Scalar s1 = ei_random<Scalar>(); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 77 | |
| 78 | std::vector<Vector2i> zeroCoords; |
| 79 | std::vector<Vector2i> nonzeroCoords; |
| 80 | initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 81 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 82 | if (zeroCoords.size()==0 || nonzeroCoords.size()==0) |
| 83 | return; |
| 84 | |
| 85 | // test coeff and coeffRef |
| 86 | for (int i=0; i<(int)zeroCoords.size(); ++i) |
| 87 | { |
| 88 | VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps ); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 89 | if(ei_is_same_type<SparseMatrixType,SparseMatrix<Scalar,Flags> >::ret) |
| 90 | VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 ); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 91 | } |
| 92 | VERIFY_IS_APPROX(m, refMat); |
| 93 | |
| 94 | m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 95 | refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 96 | |
| 97 | VERIFY_IS_APPROX(m, refMat); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 98 | /* |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 99 | // test InnerIterators and Block expressions |
| 100 | for (int t=0; t<10; ++t) |
| 101 | { |
| 102 | int j = ei_random<int>(0,cols-1); |
| 103 | int i = ei_random<int>(0,rows-1); |
| 104 | int w = ei_random<int>(1,cols-j-1); |
| 105 | int h = ei_random<int>(1,rows-i-1); |
| 106 | |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 107 | // VERIFY_IS_APPROX(m.block(i,j,h,w), refMat.block(i,j,h,w)); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 108 | for(int c=0; c<w; c++) |
| 109 | { |
| 110 | VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c)); |
| 111 | for(int r=0; r<h; r++) |
| 112 | { |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 113 | // 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 | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 116 | // for(int r=0; r<h; r++) |
| 117 | // { |
| 118 | // VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r)); |
| 119 | // for(int c=0; c<w; c++) |
| 120 | // { |
| 121 | // VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c)); |
| 122 | // } |
| 123 | // } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | for(int c=0; c<cols; c++) |
| 127 | { |
| 128 | VERIFY_IS_APPROX(m.col(c) + m.col(c), (m + m).col(c)); |
| 129 | VERIFY_IS_APPROX(m.col(c) + m.col(c), refMat.col(c) + refMat.col(c)); |
| 130 | } |
| 131 | |
| 132 | for(int r=0; r<rows; r++) |
| 133 | { |
| 134 | VERIFY_IS_APPROX(m.row(r) + m.row(r), (m + m).row(r)); |
| 135 | VERIFY_IS_APPROX(m.row(r) + m.row(r), refMat.row(r) + refMat.row(r)); |
| 136 | } |
| 137 | */ |
| 138 | |
| 139 | // test SparseSetters |
| 140 | // coherent setter |
| 141 | // TODO extend the MatrixSetter |
| 142 | // { |
| 143 | // m.setZero(); |
| 144 | // VERIFY_IS_NOT_APPROX(m, refMat); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 145 | // SparseSetter<SparseMatrixType, FullyCoherentAccessPattern> w(m); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 146 | // for (int i=0; i<nonzeroCoords.size(); ++i) |
| 147 | // { |
| 148 | // w->coeffRef(nonzeroCoords[i].x(),nonzeroCoords[i].y()) = refMat.coeff(nonzeroCoords[i].x(),nonzeroCoords[i].y()); |
| 149 | // } |
| 150 | // } |
| 151 | // VERIFY_IS_APPROX(m, refMat); |
| 152 | |
| 153 | // random setter |
| 154 | // { |
| 155 | // m.setZero(); |
| 156 | // VERIFY_IS_NOT_APPROX(m, refMat); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 157 | // SparseSetter<SparseMatrixType, RandomAccessPattern> w(m); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 158 | // std::vector<Vector2i> remaining = nonzeroCoords; |
| 159 | // while(!remaining.empty()) |
| 160 | // { |
| 161 | // int i = ei_random<int>(0,remaining.size()-1); |
| 162 | // w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y()); |
| 163 | // remaining[i] = remaining.back(); |
| 164 | // remaining.pop_back(); |
| 165 | // } |
| 166 | // } |
| 167 | // VERIFY_IS_APPROX(m, refMat); |
| 168 | |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 169 | VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) )); |
Gael Guennebaud | 6a72260 | 2009-01-23 12:26:32 +0000 | [diff] [blame] | 170 | #ifdef EIGEN_UNORDERED_MAP_SUPPORT |
| 171 | VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdUnorderedMapTraits> >(m,refMat,nonzeroCoords) )); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 172 | #endif |
| 173 | #ifdef _DENSE_HASH_MAP_H_ |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 174 | VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) )); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 175 | #endif |
| 176 | #ifdef _SPARSE_HASH_MAP_H_ |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 177 | VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) )); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 178 | #endif |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 179 | |
| 180 | // test fillrand |
| 181 | { |
| 182 | DenseMatrix m1(rows,cols); |
| 183 | m1.setZero(); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 184 | SparseMatrixType m2(rows,cols); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 185 | m2.startFill(); |
| 186 | for (int j=0; j<cols; ++j) |
| 187 | { |
| 188 | for (int k=0; k<rows/2; ++k) |
| 189 | { |
| 190 | int i = ei_random<int>(0,rows-1); |
| 191 | if (m1.coeff(i,j)==Scalar(0)) |
| 192 | m2.fillrand(i,j) = m1(i,j) = ei_random<Scalar>(); |
| 193 | } |
| 194 | } |
| 195 | m2.endFill(); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 196 | VERIFY_IS_APPROX(m2,m1); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 197 | } |
Gael Guennebaud | 9a4b799 | 2009-01-15 14:16:41 +0000 | [diff] [blame] | 198 | |
Gael Guennebaud | 8724108 | 2009-01-15 13:30:50 +0000 | [diff] [blame] | 199 | // test RandomSetter |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 200 | /*{ |
| 201 | SparseMatrixType m1(rows,cols), m2(rows,cols); |
Gael Guennebaud | 8724108 | 2009-01-15 13:30:50 +0000 | [diff] [blame] | 202 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
| 203 | initSparse<Scalar>(density, refM1, m1); |
| 204 | { |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 205 | Eigen::RandomSetter<SparseMatrixType > setter(m2); |
Gael Guennebaud | 8724108 | 2009-01-15 13:30:50 +0000 | [diff] [blame] | 206 | for (int j=0; j<m1.outerSize(); ++j) |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 207 | for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i) |
Gael Guennebaud | 8724108 | 2009-01-15 13:30:50 +0000 | [diff] [blame] | 208 | setter(i.index(), j) = i.value(); |
| 209 | } |
| 210 | VERIFY_IS_APPROX(m1, m2); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 211 | }*/ |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 212 | // std::cerr << m.transpose() << "\n\n" << refMat.transpose() << "\n\n"; |
| 213 | // VERIFY_IS_APPROX(m, refMat); |
| 214 | |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 215 | // test basic computations |
| 216 | { |
| 217 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
| 218 | DenseMatrix refM2 = DenseMatrix::Zero(rows, rows); |
| 219 | DenseMatrix refM3 = DenseMatrix::Zero(rows, rows); |
| 220 | DenseMatrix refM4 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 221 | SparseMatrixType m1(rows, rows); |
| 222 | SparseMatrixType m2(rows, rows); |
| 223 | SparseMatrixType m3(rows, rows); |
| 224 | SparseMatrixType m4(rows, rows); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 225 | initSparse<Scalar>(density, refM1, m1); |
| 226 | initSparse<Scalar>(density, refM2, m2); |
| 227 | initSparse<Scalar>(density, refM3, m3); |
| 228 | initSparse<Scalar>(density, refM4, m4); |
| 229 | |
| 230 | VERIFY_IS_APPROX(m1+m2, refM1+refM2); |
| 231 | VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3); |
| 232 | VERIFY_IS_APPROX(m3.cwise()*(m1+m2), refM3.cwise()*(refM1+refM2)); |
| 233 | VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2); |
| 234 | |
| 235 | VERIFY_IS_APPROX(m1*=s1, refM1*=s1); |
| 236 | VERIFY_IS_APPROX(m1/=s1, refM1/=s1); |
Gael Guennebaud | e7c48fa | 2009-01-23 13:59:32 +0000 | [diff] [blame] | 237 | |
| 238 | VERIFY_IS_APPROX(m1+=m2, refM1+=refM2); |
| 239 | VERIFY_IS_APPROX(m1-=m2, refM1-=refM2); |
| 240 | |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 241 | refM4.setRandom(); |
| 242 | // sparse cwise* dense |
| 243 | VERIFY_IS_APPROX(m3.cwise()*refM4, refM3.cwise()*refM4); |
| 244 | // VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4); |
| 245 | } |
| 246 | |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 247 | // test innerVector() |
| 248 | { |
| 249 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 250 | SparseMatrixType m2(rows, rows); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 251 | initSparse<Scalar>(density, refMat2, m2); |
| 252 | int j0 = ei_random(0,rows-1); |
| 253 | int j1 = ei_random(0,rows-1); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 254 | VERIFY_IS_APPROX(m2.innerVector(j0), refMat2.col(j0)); |
| 255 | VERIFY_IS_APPROX(m2.innerVector(j0)+m2.innerVector(j1), refMat2.col(j0)+refMat2.col(j1)); |
Gael Guennebaud | 8ce4503 | 2009-01-27 22:48:17 +0000 | [diff] [blame] | 256 | //m2.innerVector(j0) = 2*m2.innerVector(j1); |
| 257 | //refMat2.col(j0) = 2*refMat2.col(j1); |
| 258 | //VERIFY_IS_APPROX(m2, refMat2); |
| 259 | } |
| 260 | |
| 261 | // test innerVectors() |
| 262 | { |
| 263 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 264 | SparseMatrixType m2(rows, rows); |
| 265 | initSparse<Scalar>(density, refMat2, m2); |
| 266 | int j0 = ei_random(0,rows-2); |
| 267 | int j1 = ei_random(0,rows-2); |
| 268 | int n0 = ei_random<int>(1,rows-std::max(j0,j1)); |
| 269 | VERIFY_IS_APPROX(m2.innerVectors(j0,n0), refMat2.block(0,j0,rows,n0)); |
| 270 | VERIFY_IS_APPROX(m2.innerVectors(j0,n0)+m2.innerVectors(j1,n0), |
| 271 | refMat2.block(0,j0,rows,n0)+refMat2.block(0,j1,rows,n0)); |
| 272 | //m2.innerVectors(j0,n0) = m2.innerVectors(j0,n0) + m2.innerVectors(j1,n0); |
| 273 | //refMat2.block(0,j0,rows,n0) = refMat2.block(0,j0,rows,n0) + refMat2.block(0,j1,rows,n0); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 276 | // test transpose |
| 277 | { |
| 278 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 279 | SparseMatrixType m2(rows, rows); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 280 | initSparse<Scalar>(density, refMat2, m2); |
| 281 | VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval()); |
| 282 | VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose()); |
| 283 | } |
| 284 | |
| 285 | // test matrix product |
| 286 | { |
| 287 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 288 | DenseMatrix refMat3 = DenseMatrix::Zero(rows, rows); |
| 289 | DenseMatrix refMat4 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 0b606dc | 2009-01-14 17:41:55 +0000 | [diff] [blame] | 290 | DenseMatrix dm4 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 291 | SparseMatrixType m2(rows, rows); |
| 292 | SparseMatrixType m3(rows, rows); |
| 293 | SparseMatrixType m4(rows, rows); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 294 | initSparse<Scalar>(density, refMat2, m2); |
| 295 | initSparse<Scalar>(density, refMat3, m3); |
| 296 | initSparse<Scalar>(density, refMat4, m4); |
| 297 | VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3); |
| 298 | VERIFY_IS_APPROX(m4=m2.transpose()*m3, refMat4=refMat2.transpose()*refMat3); |
| 299 | VERIFY_IS_APPROX(m4=m2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose()); |
| 300 | VERIFY_IS_APPROX(m4=m2*m3.transpose(), refMat4=refMat2*refMat3.transpose()); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 301 | |
Gael Guennebaud | 0b606dc | 2009-01-14 17:41:55 +0000 | [diff] [blame] | 302 | // sparse * dense |
| 303 | VERIFY_IS_APPROX(dm4=m2*refMat3, refMat4=refMat2*refMat3); |
| 304 | VERIFY_IS_APPROX(dm4=m2*refMat3.transpose(), refMat4=refMat2*refMat3.transpose()); |
| 305 | VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3, refMat4=refMat2.transpose()*refMat3); |
| 306 | VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose()); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 307 | |
Gael Guennebaud | 0b606dc | 2009-01-14 17:41:55 +0000 | [diff] [blame] | 308 | // dense * sparse |
| 309 | VERIFY_IS_APPROX(dm4=refMat2*m3, refMat4=refMat2*refMat3); |
| 310 | VERIFY_IS_APPROX(dm4=refMat2*m3.transpose(), refMat4=refMat2*refMat3.transpose()); |
| 311 | VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3, refMat4=refMat2.transpose()*refMat3); |
| 312 | VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose()); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 313 | } |
Gael Guennebaud | ccdcebc | 2009-01-15 18:52:14 +0000 | [diff] [blame] | 314 | |
| 315 | // test self adjoint products |
| 316 | { |
| 317 | DenseMatrix b = DenseMatrix::Random(rows, rows); |
| 318 | DenseMatrix x = DenseMatrix::Random(rows, rows); |
| 319 | DenseMatrix refX = DenseMatrix::Random(rows, rows); |
| 320 | DenseMatrix refUp = DenseMatrix::Zero(rows, rows); |
| 321 | DenseMatrix refLo = DenseMatrix::Zero(rows, rows); |
| 322 | DenseMatrix refS = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 323 | SparseMatrixType mUp(rows, rows); |
| 324 | SparseMatrixType mLo(rows, rows); |
| 325 | SparseMatrixType mS(rows, rows); |
Gael Guennebaud | ccdcebc | 2009-01-15 18:52:14 +0000 | [diff] [blame] | 326 | do { |
| 327 | initSparse<Scalar>(density, refUp, mUp, ForceRealDiag|/*ForceNonZeroDiag|*/MakeUpperTriangular); |
| 328 | } while (refUp.isZero()); |
| 329 | refLo = refUp.transpose().conjugate(); |
| 330 | mLo = mUp.transpose().conjugate(); |
| 331 | refS = refUp + refLo; |
| 332 | refS.diagonal() *= 0.5; |
| 333 | mS = mUp + mLo; |
| 334 | for (int k=0; k<mS.outerSize(); ++k) |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 335 | for (typename SparseMatrixType::InnerIterator it(mS,k); it; ++it) |
Gael Guennebaud | ccdcebc | 2009-01-15 18:52:14 +0000 | [diff] [blame] | 336 | if (it.index() == k) |
| 337 | it.valueRef() *= 0.5; |
| 338 | |
| 339 | VERIFY_IS_APPROX(refS.adjoint(), refS); |
| 340 | VERIFY_IS_APPROX(mS.transpose().conjugate(), mS); |
| 341 | VERIFY_IS_APPROX(mS, refS); |
| 342 | VERIFY_IS_APPROX(x=mS*b, refX=refS*b); |
| 343 | VERIFY_IS_APPROX(x=mUp.template marked<UpperTriangular|SelfAdjoint>()*b, refX=refS*b); |
| 344 | VERIFY_IS_APPROX(x=mLo.template marked<LowerTriangular|SelfAdjoint>()*b, refX=refS*b); |
| 345 | VERIFY_IS_APPROX(x=mS.template marked<SelfAdjoint>()*b, refX=refS*b); |
| 346 | } |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 347 | |
| 348 | // test prune |
| 349 | { |
| 350 | SparseMatrixType m2(rows, rows); |
| 351 | DenseMatrix refM2(rows, rows); |
| 352 | refM2.setZero(); |
| 353 | int countFalseNonZero = 0; |
| 354 | int countTrueNonZero = 0; |
| 355 | m2.startFill(); |
| 356 | for (int j=0; j<m2.outerSize(); ++j) |
| 357 | for (int i=0; i<m2.innerSize(); ++i) |
| 358 | { |
| 359 | float x = ei_random<float>(0,1); |
| 360 | if (x<0.1) |
| 361 | { |
| 362 | // do nothing |
| 363 | } |
| 364 | else if (x<0.5) |
| 365 | { |
| 366 | countFalseNonZero++; |
| 367 | m2.fill(i,j) = Scalar(0); |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | countTrueNonZero++; |
| 372 | m2.fill(i,j) = refM2(i,j) = Scalar(1); |
| 373 | } |
| 374 | } |
| 375 | m2.endFill(); |
| 376 | VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros()); |
| 377 | VERIFY_IS_APPROX(m2, refM2); |
| 378 | m2.prune(1); |
| 379 | VERIFY(countTrueNonZero==m2.nonZeros()); |
| 380 | VERIFY_IS_APPROX(m2, refM2); |
| 381 | } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void test_sparse_basic() |
| 385 | { |
| 386 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 387 | CALL_SUBTEST( sparse_basic(SparseMatrix<double>(8, 8)) ); |
| 388 | CALL_SUBTEST( sparse_basic(SparseMatrix<std::complex<double> >(16, 16)) ); |
| 389 | CALL_SUBTEST( sparse_basic(SparseMatrix<double>(33, 33)) ); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 390 | |
| 391 | CALL_SUBTEST( sparse_basic(DynamicSparseMatrix<double>(8, 8)) ); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 392 | } |
| 393 | } |