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 | |
| 45 | template<typename Scalar> void sparse_basic(int rows, int cols) |
| 46 | { |
| 47 | double density = std::max(8./(rows*cols), 0.01); |
| 48 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 49 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 50 | Scalar eps = 1e-6; |
| 51 | |
| 52 | SparseMatrix<Scalar> m(rows, cols); |
| 53 | DenseMatrix refMat = DenseMatrix::Zero(rows, cols); |
| 54 | DenseVector vec1 = DenseVector::Random(rows); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 55 | Scalar s1 = ei_random<Scalar>(); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 56 | |
| 57 | std::vector<Vector2i> zeroCoords; |
| 58 | std::vector<Vector2i> nonzeroCoords; |
| 59 | initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords); |
| 60 | |
| 61 | if (zeroCoords.size()==0 || nonzeroCoords.size()==0) |
| 62 | return; |
| 63 | |
| 64 | // test coeff and coeffRef |
| 65 | for (int i=0; i<(int)zeroCoords.size(); ++i) |
| 66 | { |
| 67 | VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps ); |
| 68 | VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 ); |
| 69 | } |
| 70 | VERIFY_IS_APPROX(m, refMat); |
| 71 | |
| 72 | m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 73 | refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 74 | |
| 75 | VERIFY_IS_APPROX(m, refMat); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 76 | /* |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 77 | // test InnerIterators and Block expressions |
| 78 | for (int t=0; t<10; ++t) |
| 79 | { |
| 80 | int j = ei_random<int>(0,cols-1); |
| 81 | int i = ei_random<int>(0,rows-1); |
| 82 | int w = ei_random<int>(1,cols-j-1); |
| 83 | int h = ei_random<int>(1,rows-i-1); |
| 84 | |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 85 | // 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] | 86 | for(int c=0; c<w; c++) |
| 87 | { |
| 88 | VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c)); |
| 89 | for(int r=0; r<h; r++) |
| 90 | { |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 91 | // 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] | 92 | } |
| 93 | } |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 94 | // for(int r=0; r<h; r++) |
| 95 | // { |
| 96 | // VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r)); |
| 97 | // for(int c=0; c<w; c++) |
| 98 | // { |
| 99 | // VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c)); |
| 100 | // } |
| 101 | // } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | for(int c=0; c<cols; c++) |
| 105 | { |
| 106 | VERIFY_IS_APPROX(m.col(c) + m.col(c), (m + m).col(c)); |
| 107 | VERIFY_IS_APPROX(m.col(c) + m.col(c), refMat.col(c) + refMat.col(c)); |
| 108 | } |
| 109 | |
| 110 | for(int r=0; r<rows; r++) |
| 111 | { |
| 112 | VERIFY_IS_APPROX(m.row(r) + m.row(r), (m + m).row(r)); |
| 113 | VERIFY_IS_APPROX(m.row(r) + m.row(r), refMat.row(r) + refMat.row(r)); |
| 114 | } |
| 115 | */ |
| 116 | |
| 117 | // test SparseSetters |
| 118 | // coherent setter |
| 119 | // TODO extend the MatrixSetter |
| 120 | // { |
| 121 | // m.setZero(); |
| 122 | // VERIFY_IS_NOT_APPROX(m, refMat); |
| 123 | // SparseSetter<SparseMatrix<Scalar>, FullyCoherentAccessPattern> w(m); |
| 124 | // for (int i=0; i<nonzeroCoords.size(); ++i) |
| 125 | // { |
| 126 | // w->coeffRef(nonzeroCoords[i].x(),nonzeroCoords[i].y()) = refMat.coeff(nonzeroCoords[i].x(),nonzeroCoords[i].y()); |
| 127 | // } |
| 128 | // } |
| 129 | // VERIFY_IS_APPROX(m, refMat); |
| 130 | |
| 131 | // random setter |
| 132 | // { |
| 133 | // m.setZero(); |
| 134 | // VERIFY_IS_NOT_APPROX(m, refMat); |
| 135 | // SparseSetter<SparseMatrix<Scalar>, RandomAccessPattern> w(m); |
| 136 | // std::vector<Vector2i> remaining = nonzeroCoords; |
| 137 | // while(!remaining.empty()) |
| 138 | // { |
| 139 | // int i = ei_random<int>(0,remaining.size()-1); |
| 140 | // w->coeffRef(remaining[i].x(),remaining[i].y()) = refMat.coeff(remaining[i].x(),remaining[i].y()); |
| 141 | // remaining[i] = remaining.back(); |
| 142 | // remaining.pop_back(); |
| 143 | // } |
| 144 | // } |
| 145 | // VERIFY_IS_APPROX(m, refMat); |
| 146 | |
| 147 | VERIFY(( test_random_setter<RandomSetter<SparseMatrix<Scalar>, StdMapTraits> >(m,refMat,nonzeroCoords) )); |
| 148 | #ifdef _HASH_MAP |
| 149 | VERIFY(( test_random_setter<RandomSetter<SparseMatrix<Scalar>, GnuHashMapTraits> >(m,refMat,nonzeroCoords) )); |
| 150 | #endif |
| 151 | #ifdef _DENSE_HASH_MAP_H_ |
| 152 | VERIFY(( test_random_setter<RandomSetter<SparseMatrix<Scalar>, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) )); |
| 153 | #endif |
| 154 | #ifdef _SPARSE_HASH_MAP_H_ |
| 155 | VERIFY(( test_random_setter<RandomSetter<SparseMatrix<Scalar>, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) )); |
| 156 | #endif |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 157 | |
| 158 | // test fillrand |
| 159 | { |
| 160 | DenseMatrix m1(rows,cols); |
| 161 | m1.setZero(); |
| 162 | SparseMatrix<Scalar> m2(rows,cols); |
| 163 | m2.startFill(); |
| 164 | for (int j=0; j<cols; ++j) |
| 165 | { |
| 166 | for (int k=0; k<rows/2; ++k) |
| 167 | { |
| 168 | int i = ei_random<int>(0,rows-1); |
| 169 | if (m1.coeff(i,j)==Scalar(0)) |
| 170 | m2.fillrand(i,j) = m1(i,j) = ei_random<Scalar>(); |
| 171 | } |
| 172 | } |
| 173 | m2.endFill(); |
| 174 | std::cerr << m1 << "\n\n" << m2 << "\n"; |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 175 | VERIFY_IS_APPROX(m2,m1); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 176 | } |
Gael Guennebaud | 8724108 | 2009-01-15 13:30:50 +0000 | [diff] [blame^] | 177 | // test RandomSetter |
| 178 | { |
| 179 | SparseMatrix<Scalar> m1(rows,cols), m2(rows,cols); |
| 180 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
| 181 | initSparse<Scalar>(density, refM1, m1); |
| 182 | { |
| 183 | Eigen::RandomSetter<SparseMatrix<Scalar> > setter(m2); |
| 184 | for (int j=0; j<m1.outerSize(); ++j) |
| 185 | for (typename SparseMatrix<Scalar>::InnerIterator i(m1,j); i; ++i) |
| 186 | setter(i.index(), j) = i.value(); |
| 187 | } |
| 188 | VERIFY_IS_APPROX(m1, m2); |
| 189 | } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 190 | // std::cerr << m.transpose() << "\n\n" << refMat.transpose() << "\n\n"; |
| 191 | // VERIFY_IS_APPROX(m, refMat); |
| 192 | |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 193 | // test basic computations |
| 194 | { |
| 195 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
| 196 | DenseMatrix refM2 = DenseMatrix::Zero(rows, rows); |
| 197 | DenseMatrix refM3 = DenseMatrix::Zero(rows, rows); |
| 198 | DenseMatrix refM4 = DenseMatrix::Zero(rows, rows); |
| 199 | SparseMatrix<Scalar> m1(rows, rows); |
| 200 | SparseMatrix<Scalar> m2(rows, rows); |
| 201 | SparseMatrix<Scalar> m3(rows, rows); |
| 202 | SparseMatrix<Scalar> m4(rows, rows); |
| 203 | initSparse<Scalar>(density, refM1, m1); |
| 204 | initSparse<Scalar>(density, refM2, m2); |
| 205 | initSparse<Scalar>(density, refM3, m3); |
| 206 | initSparse<Scalar>(density, refM4, m4); |
| 207 | |
| 208 | VERIFY_IS_APPROX(m1+m2, refM1+refM2); |
| 209 | VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3); |
| 210 | VERIFY_IS_APPROX(m3.cwise()*(m1+m2), refM3.cwise()*(refM1+refM2)); |
| 211 | VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2); |
| 212 | |
| 213 | VERIFY_IS_APPROX(m1*=s1, refM1*=s1); |
| 214 | VERIFY_IS_APPROX(m1/=s1, refM1/=s1); |
| 215 | |
| 216 | refM4.setRandom(); |
| 217 | // sparse cwise* dense |
| 218 | VERIFY_IS_APPROX(m3.cwise()*refM4, refM3.cwise()*refM4); |
| 219 | // VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4); |
| 220 | } |
| 221 | |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 222 | // test innerVector() |
| 223 | { |
| 224 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 225 | SparseMatrix<Scalar> m2(rows, rows); |
| 226 | initSparse<Scalar>(density, refMat2, m2); |
| 227 | int j0 = ei_random(0,rows-1); |
| 228 | int j1 = ei_random(0,rows-1); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 229 | VERIFY_IS_APPROX(m2.innerVector(j0), refMat2.col(j0)); |
| 230 | 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] | 231 | } |
| 232 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 233 | // test transpose |
| 234 | { |
| 235 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 236 | SparseMatrix<Scalar> m2(rows, rows); |
| 237 | initSparse<Scalar>(density, refMat2, m2); |
| 238 | VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval()); |
| 239 | VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose()); |
| 240 | } |
| 241 | |
| 242 | // test matrix product |
| 243 | { |
| 244 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 245 | DenseMatrix refMat3 = DenseMatrix::Zero(rows, rows); |
| 246 | DenseMatrix refMat4 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 0b606dc | 2009-01-14 17:41:55 +0000 | [diff] [blame] | 247 | DenseMatrix dm4 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 248 | SparseMatrix<Scalar> m2(rows, rows); |
| 249 | SparseMatrix<Scalar> m3(rows, rows); |
| 250 | SparseMatrix<Scalar> m4(rows, rows); |
| 251 | initSparse<Scalar>(density, refMat2, m2); |
| 252 | initSparse<Scalar>(density, refMat3, m3); |
| 253 | initSparse<Scalar>(density, refMat4, m4); |
| 254 | VERIFY_IS_APPROX(m4=m2*m3, refMat4=refMat2*refMat3); |
| 255 | VERIFY_IS_APPROX(m4=m2.transpose()*m3, refMat4=refMat2.transpose()*refMat3); |
| 256 | VERIFY_IS_APPROX(m4=m2.transpose()*m3.transpose(), refMat4=refMat2.transpose()*refMat3.transpose()); |
| 257 | VERIFY_IS_APPROX(m4=m2*m3.transpose(), refMat4=refMat2*refMat3.transpose()); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 258 | |
Gael Guennebaud | 0b606dc | 2009-01-14 17:41:55 +0000 | [diff] [blame] | 259 | // sparse * dense |
| 260 | VERIFY_IS_APPROX(dm4=m2*refMat3, refMat4=refMat2*refMat3); |
| 261 | VERIFY_IS_APPROX(dm4=m2*refMat3.transpose(), refMat4=refMat2*refMat3.transpose()); |
| 262 | VERIFY_IS_APPROX(dm4=m2.transpose()*refMat3, refMat4=refMat2.transpose()*refMat3); |
| 263 | 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] | 264 | |
Gael Guennebaud | 0b606dc | 2009-01-14 17:41:55 +0000 | [diff] [blame] | 265 | // dense * sparse |
| 266 | VERIFY_IS_APPROX(dm4=refMat2*m3, refMat4=refMat2*refMat3); |
| 267 | VERIFY_IS_APPROX(dm4=refMat2*m3.transpose(), refMat4=refMat2*refMat3.transpose()); |
| 268 | VERIFY_IS_APPROX(dm4=refMat2.transpose()*m3, refMat4=refMat2.transpose()*refMat3); |
| 269 | 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] | 270 | } |
| 271 | } |
| 272 | |
| 273 | void test_sparse_basic() |
| 274 | { |
| 275 | for(int i = 0; i < g_repeat; i++) { |
| 276 | CALL_SUBTEST( sparse_basic<double>(8, 8) ); |
| 277 | CALL_SUBTEST( sparse_basic<std::complex<double> >(16, 16) ); |
| 278 | CALL_SUBTEST( sparse_basic<double>(33, 33) ); |
| 279 | } |
| 280 | } |