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 | |
| 27 | template<typename SetterType,typename DenseType, typename SparseType> |
| 28 | bool test_random_setter(SparseType& sm, const DenseType& ref, const std::vector<Vector2i>& nonzeroCoords) |
| 29 | { |
| 30 | { |
| 31 | sm.setZero(); |
| 32 | SetterType w(sm); |
| 33 | std::vector<Vector2i> remaining = nonzeroCoords; |
| 34 | while(!remaining.empty()) |
| 35 | { |
| 36 | int i = ei_random<int>(0,remaining.size()-1); |
| 37 | w(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y()); |
| 38 | remaining[i] = remaining.back(); |
| 39 | remaining.pop_back(); |
| 40 | } |
| 41 | } |
| 42 | return sm.isApprox(ref); |
| 43 | } |
| 44 | |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 45 | template<typename SetterType,typename DenseType, typename T> |
| 46 | 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] | 47 | { |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 48 | sm.setZero(); |
| 49 | std::vector<Vector2i> remaining = nonzeroCoords; |
| 50 | while(!remaining.empty()) |
| 51 | { |
| 52 | int i = ei_random<int>(0,remaining.size()-1); |
| 53 | sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y()); |
| 54 | remaining[i] = remaining.back(); |
| 55 | remaining.pop_back(); |
| 56 | } |
| 57 | return sm.isApprox(ref); |
| 58 | } |
| 59 | |
| 60 | template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref) |
| 61 | { |
| 62 | const int rows = ref.rows(); |
| 63 | const int cols = ref.cols(); |
| 64 | typedef typename SparseMatrixType::Scalar Scalar; |
| 65 | enum { Flags = SparseMatrixType::Flags }; |
| 66 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 67 | double density = std::max(8./(rows*cols), 0.01); |
| 68 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 69 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 70 | Scalar eps = 1e-6; |
| 71 | |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 72 | SparseMatrixType m(rows, cols); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 73 | DenseMatrix refMat = DenseMatrix::Zero(rows, cols); |
| 74 | DenseVector vec1 = DenseVector::Random(rows); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 75 | Scalar s1 = ei_random<Scalar>(); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 76 | |
| 77 | std::vector<Vector2i> zeroCoords; |
| 78 | std::vector<Vector2i> nonzeroCoords; |
| 79 | initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 80 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 81 | if (zeroCoords.size()==0 || nonzeroCoords.size()==0) |
| 82 | return; |
| 83 | |
| 84 | // test coeff and coeffRef |
| 85 | for (int i=0; i<(int)zeroCoords.size(); ++i) |
| 86 | { |
| 87 | 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] | 88 | if(ei_is_same_type<SparseMatrixType,SparseMatrix<Scalar,Flags> >::ret) |
| 89 | VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 ); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 90 | } |
| 91 | VERIFY_IS_APPROX(m, refMat); |
| 92 | |
| 93 | m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 94 | refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 95 | |
| 96 | VERIFY_IS_APPROX(m, refMat); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 97 | /* |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 98 | // test InnerIterators and Block expressions |
| 99 | for (int t=0; t<10; ++t) |
| 100 | { |
| 101 | int j = ei_random<int>(0,cols-1); |
| 102 | int i = ei_random<int>(0,rows-1); |
| 103 | int w = ei_random<int>(1,cols-j-1); |
| 104 | int h = ei_random<int>(1,rows-i-1); |
| 105 | |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 106 | // 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] | 107 | for(int c=0; c<w; c++) |
| 108 | { |
| 109 | VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c)); |
| 110 | for(int r=0; r<h; r++) |
| 111 | { |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 112 | // 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] | 113 | } |
| 114 | } |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 115 | // for(int r=0; r<h; r++) |
| 116 | // { |
| 117 | // VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r)); |
| 118 | // for(int c=0; c<w; c++) |
| 119 | // { |
| 120 | // VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c)); |
| 121 | // } |
| 122 | // } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | for(int c=0; c<cols; c++) |
| 126 | { |
| 127 | VERIFY_IS_APPROX(m.col(c) + m.col(c), (m + m).col(c)); |
| 128 | VERIFY_IS_APPROX(m.col(c) + m.col(c), refMat.col(c) + refMat.col(c)); |
| 129 | } |
| 130 | |
| 131 | for(int r=0; r<rows; r++) |
| 132 | { |
| 133 | VERIFY_IS_APPROX(m.row(r) + m.row(r), (m + m).row(r)); |
| 134 | VERIFY_IS_APPROX(m.row(r) + m.row(r), refMat.row(r) + refMat.row(r)); |
| 135 | } |
| 136 | */ |
| 137 | |
| 138 | // test SparseSetters |
| 139 | // coherent setter |
| 140 | // TODO extend the MatrixSetter |
| 141 | // { |
| 142 | // m.setZero(); |
| 143 | // VERIFY_IS_NOT_APPROX(m, refMat); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 144 | // SparseSetter<SparseMatrixType, FullyCoherentAccessPattern> w(m); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 145 | // for (int i=0; i<nonzeroCoords.size(); ++i) |
| 146 | // { |
| 147 | // w->coeffRef(nonzeroCoords[i].x(),nonzeroCoords[i].y()) = refMat.coeff(nonzeroCoords[i].x(),nonzeroCoords[i].y()); |
| 148 | // } |
| 149 | // } |
| 150 | // VERIFY_IS_APPROX(m, refMat); |
| 151 | |
| 152 | // random setter |
| 153 | // { |
| 154 | // m.setZero(); |
| 155 | // VERIFY_IS_NOT_APPROX(m, refMat); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 156 | // SparseSetter<SparseMatrixType, RandomAccessPattern> w(m); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 157 | // std::vector<Vector2i> remaining = nonzeroCoords; |
| 158 | // while(!remaining.empty()) |
| 159 | // { |
| 160 | // int i = ei_random<int>(0,remaining.size()-1); |
| 161 | // w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y()); |
| 162 | // remaining[i] = remaining.back(); |
| 163 | // remaining.pop_back(); |
| 164 | // } |
| 165 | // } |
| 166 | // VERIFY_IS_APPROX(m, refMat); |
| 167 | |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 168 | VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, StdMapTraits> >(m,refMat,nonzeroCoords) )); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 169 | #ifdef _HASH_MAP |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 170 | VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GnuHashMapTraits> >(m,refMat,nonzeroCoords) )); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 171 | #endif |
| 172 | #ifdef _DENSE_HASH_MAP_H_ |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 173 | VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) )); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 174 | #endif |
| 175 | #ifdef _SPARSE_HASH_MAP_H_ |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 176 | VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) )); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 177 | #endif |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 178 | |
| 179 | // test fillrand |
| 180 | { |
| 181 | DenseMatrix m1(rows,cols); |
| 182 | m1.setZero(); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 183 | SparseMatrixType m2(rows,cols); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 184 | m2.startFill(); |
| 185 | for (int j=0; j<cols; ++j) |
| 186 | { |
| 187 | for (int k=0; k<rows/2; ++k) |
| 188 | { |
| 189 | int i = ei_random<int>(0,rows-1); |
| 190 | if (m1.coeff(i,j)==Scalar(0)) |
| 191 | m2.fillrand(i,j) = m1(i,j) = ei_random<Scalar>(); |
| 192 | } |
| 193 | } |
| 194 | m2.endFill(); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 195 | //std::cerr << m1 << "\n\n" << m2 << "\n"; |
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); |
| 237 | |
| 238 | refM4.setRandom(); |
| 239 | // sparse cwise* dense |
| 240 | VERIFY_IS_APPROX(m3.cwise()*refM4, refM3.cwise()*refM4); |
| 241 | // VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4); |
| 242 | } |
| 243 | |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 244 | // test innerVector() |
| 245 | { |
| 246 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 247 | SparseMatrixType m2(rows, rows); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 248 | initSparse<Scalar>(density, refMat2, m2); |
| 249 | int j0 = ei_random(0,rows-1); |
| 250 | int j1 = ei_random(0,rows-1); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 251 | VERIFY_IS_APPROX(m2.innerVector(j0), refMat2.col(j0)); |
| 252 | VERIFY_IS_APPROX(m2.innerVector(j0)+m2.innerVector(j1), refMat2.col(j0)+refMat2.col(j1)); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 255 | // test transpose |
| 256 | { |
| 257 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 258 | SparseMatrixType m2(rows, rows); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 259 | initSparse<Scalar>(density, refMat2, m2); |
| 260 | VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval()); |
| 261 | VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose()); |
| 262 | } |
| 263 | |
| 264 | // test matrix product |
| 265 | { |
| 266 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 267 | DenseMatrix refMat3 = DenseMatrix::Zero(rows, rows); |
| 268 | DenseMatrix refMat4 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 0b606dc | 2009-01-14 17:41:55 +0000 | [diff] [blame] | 269 | DenseMatrix dm4 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 270 | SparseMatrixType m2(rows, rows); |
| 271 | SparseMatrixType m3(rows, rows); |
| 272 | SparseMatrixType m4(rows, rows); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 273 | initSparse<Scalar>(density, refMat2, m2); |
| 274 | initSparse<Scalar>(density, refMat3, m3); |
| 275 | initSparse<Scalar>(density, refMat4, m4); |
| 276 | VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3); |
| 277 | VERIFY_IS_APPROX(m4=m2.transpose()*m3, refMat4=refMat2.transpose()*refMat3); |
| 278 | VERIFY_IS_APPROX(m4=m2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose()); |
| 279 | VERIFY_IS_APPROX(m4=m2*m3.transpose(), refMat4=refMat2*refMat3.transpose()); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 280 | |
Gael Guennebaud | 0b606dc | 2009-01-14 17:41:55 +0000 | [diff] [blame] | 281 | // sparse * dense |
| 282 | VERIFY_IS_APPROX(dm4=m2*refMat3, refMat4=refMat2*refMat3); |
| 283 | VERIFY_IS_APPROX(dm4=m2*refMat3.transpose(), refMat4=refMat2*refMat3.transpose()); |
| 284 | VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3, refMat4=refMat2.transpose()*refMat3); |
| 285 | 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] | 286 | |
Gael Guennebaud | 0b606dc | 2009-01-14 17:41:55 +0000 | [diff] [blame] | 287 | // dense * sparse |
| 288 | VERIFY_IS_APPROX(dm4=refMat2*m3, refMat4=refMat2*refMat3); |
| 289 | VERIFY_IS_APPROX(dm4=refMat2*m3.transpose(), refMat4=refMat2*refMat3.transpose()); |
| 290 | VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3, refMat4=refMat2.transpose()*refMat3); |
| 291 | 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] | 292 | } |
Gael Guennebaud | ccdcebc | 2009-01-15 18:52:14 +0000 | [diff] [blame] | 293 | |
| 294 | // test self adjoint products |
| 295 | { |
| 296 | DenseMatrix b = DenseMatrix::Random(rows, rows); |
| 297 | DenseMatrix x = DenseMatrix::Random(rows, rows); |
| 298 | DenseMatrix refX = DenseMatrix::Random(rows, rows); |
| 299 | DenseMatrix refUp = DenseMatrix::Zero(rows, rows); |
| 300 | DenseMatrix refLo = DenseMatrix::Zero(rows, rows); |
| 301 | DenseMatrix refS = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 302 | SparseMatrixType mUp(rows, rows); |
| 303 | SparseMatrixType mLo(rows, rows); |
| 304 | SparseMatrixType mS(rows, rows); |
Gael Guennebaud | ccdcebc | 2009-01-15 18:52:14 +0000 | [diff] [blame] | 305 | do { |
| 306 | initSparse<Scalar>(density, refUp, mUp, ForceRealDiag|/*ForceNonZeroDiag|*/MakeUpperTriangular); |
| 307 | } while (refUp.isZero()); |
| 308 | refLo = refUp.transpose().conjugate(); |
| 309 | mLo = mUp.transpose().conjugate(); |
| 310 | refS = refUp + refLo; |
| 311 | refS.diagonal() *= 0.5; |
| 312 | mS = mUp + mLo; |
| 313 | for (int k=0; k<mS.outerSize(); ++k) |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 314 | for (typename SparseMatrixType::InnerIterator it(mS,k); it; ++it) |
Gael Guennebaud | ccdcebc | 2009-01-15 18:52:14 +0000 | [diff] [blame] | 315 | if (it.index() == k) |
| 316 | it.valueRef() *= 0.5; |
| 317 | |
| 318 | VERIFY_IS_APPROX(refS.adjoint(), refS); |
| 319 | VERIFY_IS_APPROX(mS.transpose().conjugate(), mS); |
| 320 | VERIFY_IS_APPROX(mS, refS); |
| 321 | VERIFY_IS_APPROX(x=mS*b, refX=refS*b); |
| 322 | VERIFY_IS_APPROX(x=mUp.template marked<UpperTriangular|SelfAdjoint>()*b, refX=refS*b); |
| 323 | VERIFY_IS_APPROX(x=mLo.template marked<LowerTriangular|SelfAdjoint>()*b, refX=refS*b); |
| 324 | VERIFY_IS_APPROX(x=mS.template marked<SelfAdjoint>()*b, refX=refS*b); |
| 325 | } |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame^] | 326 | |
| 327 | // test prune |
| 328 | { |
| 329 | SparseMatrixType m2(rows, rows); |
| 330 | DenseMatrix refM2(rows, rows); |
| 331 | refM2.setZero(); |
| 332 | int countFalseNonZero = 0; |
| 333 | int countTrueNonZero = 0; |
| 334 | m2.startFill(); |
| 335 | for (int j=0; j<m2.outerSize(); ++j) |
| 336 | for (int i=0; i<m2.innerSize(); ++i) |
| 337 | { |
| 338 | float x = ei_random<float>(0,1); |
| 339 | if (x<0.1) |
| 340 | { |
| 341 | // do nothing |
| 342 | } |
| 343 | else if (x<0.5) |
| 344 | { |
| 345 | countFalseNonZero++; |
| 346 | m2.fill(i,j) = Scalar(0); |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | countTrueNonZero++; |
| 351 | m2.fill(i,j) = refM2(i,j) = Scalar(1); |
| 352 | } |
| 353 | } |
| 354 | m2.endFill(); |
| 355 | VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros()); |
| 356 | VERIFY_IS_APPROX(m2, refM2); |
| 357 | m2.prune(1); |
| 358 | VERIFY(countTrueNonZero==m2.nonZeros()); |
| 359 | VERIFY_IS_APPROX(m2, refM2); |
| 360 | } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void test_sparse_basic() |
| 364 | { |
| 365 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame^] | 366 | CALL_SUBTEST( sparse_basic(SparseMatrix<double>(8, 8)) ); |
| 367 | CALL_SUBTEST( sparse_basic(SparseMatrix<std::complex<double> >(16, 16)) ); |
| 368 | CALL_SUBTEST( sparse_basic(SparseMatrix<double>(33, 33)) ); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 369 | |
| 370 | CALL_SUBTEST( sparse_basic(DynamicSparseMatrix<double>(8, 8)) ); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 371 | } |
| 372 | } |