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 | // |
| 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 | { |
Hauke Heibel | d088ee3 | 2009-12-12 11:39:07 +0100 | [diff] [blame] | 37 | int i = ei_random<int>(0,static_cast<int>(remaining.size())-1); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 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 | { |
Hauke Heibel | d088ee3 | 2009-12-12 11:39:07 +0100 | [diff] [blame] | 53 | int i = ei_random<int>(0,static_cast<int>(remaining.size())-1); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 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 }; |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 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 | 9f79558 | 2009-12-16 19:18:40 +0100 | [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 | |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 180 | // test insert (inner random) |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 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 | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 185 | m2.reserve(10); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 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)) |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 192 | m2.insert(i,j) = m1(i,j) = ei_random<Scalar>(); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 195 | m2.finalize(); |
| 196 | VERIFY_IS_APPROX(m2,m1); |
| 197 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 198 | |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 199 | // test insert (fully random) |
| 200 | { |
| 201 | DenseMatrix m1(rows,cols); |
| 202 | m1.setZero(); |
| 203 | SparseMatrixType m2(rows,cols); |
| 204 | m2.reserve(10); |
| 205 | for (int k=0; k<rows*cols; ++k) |
| 206 | { |
| 207 | int i = ei_random<int>(0,rows-1); |
| 208 | int j = ei_random<int>(0,cols-1); |
| 209 | if (m1.coeff(i,j)==Scalar(0)) |
| 210 | m2.insert(i,j) = m1(i,j) = ei_random<Scalar>(); |
| 211 | } |
| 212 | m2.finalize(); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 213 | VERIFY_IS_APPROX(m2,m1); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 214 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 215 | |
Gael Guennebaud | 8724108 | 2009-01-15 13:30:50 +0000 | [diff] [blame] | 216 | // test RandomSetter |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 217 | /*{ |
| 218 | SparseMatrixType m1(rows,cols), m2(rows,cols); |
Gael Guennebaud | 8724108 | 2009-01-15 13:30:50 +0000 | [diff] [blame] | 219 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
| 220 | initSparse<Scalar>(density, refM1, m1); |
| 221 | { |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 222 | Eigen::RandomSetter<SparseMatrixType > setter(m2); |
Gael Guennebaud | 8724108 | 2009-01-15 13:30:50 +0000 | [diff] [blame] | 223 | for (int j=0; j<m1.outerSize(); ++j) |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 224 | for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i) |
Gael Guennebaud | 8724108 | 2009-01-15 13:30:50 +0000 | [diff] [blame] | 225 | setter(i.index(), j) = i.value(); |
| 226 | } |
| 227 | VERIFY_IS_APPROX(m1, m2); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 228 | }*/ |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 229 | // std::cerr << m.transpose() << "\n\n" << refMat.transpose() << "\n\n"; |
| 230 | // VERIFY_IS_APPROX(m, refMat); |
| 231 | |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 232 | // test basic computations |
| 233 | { |
| 234 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
| 235 | DenseMatrix refM2 = DenseMatrix::Zero(rows, rows); |
| 236 | DenseMatrix refM3 = DenseMatrix::Zero(rows, rows); |
| 237 | DenseMatrix refM4 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 238 | SparseMatrixType m1(rows, rows); |
| 239 | SparseMatrixType m2(rows, rows); |
| 240 | SparseMatrixType m3(rows, rows); |
| 241 | SparseMatrixType m4(rows, rows); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 242 | initSparse<Scalar>(density, refM1, m1); |
| 243 | initSparse<Scalar>(density, refM2, m2); |
| 244 | initSparse<Scalar>(density, refM3, m3); |
| 245 | initSparse<Scalar>(density, refM4, m4); |
| 246 | |
| 247 | VERIFY_IS_APPROX(m1+m2, refM1+refM2); |
| 248 | VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 249 | VERIFY_IS_APPROX(m3.cwiseProduct(m1+m2), refM3.cwiseProduct(refM1+refM2)); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 250 | VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2); |
| 251 | |
| 252 | VERIFY_IS_APPROX(m1*=s1, refM1*=s1); |
| 253 | VERIFY_IS_APPROX(m1/=s1, refM1/=s1); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 254 | |
Gael Guennebaud | e7c48fa | 2009-01-23 13:59:32 +0000 | [diff] [blame] | 255 | VERIFY_IS_APPROX(m1+=m2, refM1+=refM2); |
| 256 | VERIFY_IS_APPROX(m1-=m2, refM1-=refM2); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 257 | |
Gael Guennebaud | a9688f0 | 2009-02-09 09:59:30 +0000 | [diff] [blame] | 258 | VERIFY_IS_APPROX(m1.col(0).dot(refM2.row(0)), refM1.col(0).dot(refM2.row(0))); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 259 | |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 260 | refM4.setRandom(); |
| 261 | // sparse cwise* dense |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 262 | VERIFY_IS_APPROX(m3.cwiseProduct(refM4), refM3.cwiseProduct(refM4)); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 263 | // VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4); |
| 264 | } |
| 265 | |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 266 | // test innerVector() |
| 267 | { |
| 268 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 269 | SparseMatrixType m2(rows, rows); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 270 | initSparse<Scalar>(density, refMat2, m2); |
| 271 | int j0 = ei_random(0,rows-1); |
| 272 | int j1 = ei_random(0,rows-1); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 273 | VERIFY_IS_APPROX(m2.innerVector(j0), refMat2.col(j0)); |
| 274 | 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] | 275 | //m2.innerVector(j0) = 2*m2.innerVector(j1); |
| 276 | //refMat2.col(j0) = 2*refMat2.col(j1); |
| 277 | //VERIFY_IS_APPROX(m2, refMat2); |
| 278 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 279 | |
Gael Guennebaud | 8ce4503 | 2009-01-27 22:48:17 +0000 | [diff] [blame] | 280 | // test innerVectors() |
| 281 | { |
| 282 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 283 | SparseMatrixType m2(rows, rows); |
| 284 | initSparse<Scalar>(density, refMat2, m2); |
| 285 | int j0 = ei_random(0,rows-2); |
| 286 | int j1 = ei_random(0,rows-2); |
| 287 | int n0 = ei_random<int>(1,rows-std::max(j0,j1)); |
| 288 | VERIFY_IS_APPROX(m2.innerVectors(j0,n0), refMat2.block(0,j0,rows,n0)); |
| 289 | VERIFY_IS_APPROX(m2.innerVectors(j0,n0)+m2.innerVectors(j1,n0), |
| 290 | refMat2.block(0,j0,rows,n0)+refMat2.block(0,j1,rows,n0)); |
| 291 | //m2.innerVectors(j0,n0) = m2.innerVectors(j0,n0) + m2.innerVectors(j1,n0); |
| 292 | //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] | 293 | } |
| 294 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 295 | // test transpose |
| 296 | { |
| 297 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 298 | SparseMatrixType m2(rows, rows); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 299 | initSparse<Scalar>(density, refMat2, m2); |
| 300 | VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval()); |
| 301 | VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose()); |
Gael Guennebaud | 1e7b1a8 | 2009-07-13 14:55:03 +0200 | [diff] [blame] | 302 | |
| 303 | VERIFY_IS_APPROX(SparseMatrixType(m2.adjoint()), refMat2.adjoint()); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 304 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 305 | |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 306 | // test prune |
| 307 | { |
| 308 | SparseMatrixType m2(rows, rows); |
| 309 | DenseMatrix refM2(rows, rows); |
| 310 | refM2.setZero(); |
| 311 | int countFalseNonZero = 0; |
| 312 | int countTrueNonZero = 0; |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 313 | for (int j=0; j<m2.outerSize(); ++j) |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 314 | { |
| 315 | m2.startVec(j); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 316 | for (int i=0; i<m2.innerSize(); ++i) |
| 317 | { |
| 318 | float x = ei_random<float>(0,1); |
| 319 | if (x<0.1) |
| 320 | { |
| 321 | // do nothing |
| 322 | } |
| 323 | else if (x<0.5) |
| 324 | { |
| 325 | countFalseNonZero++; |
Gael Guennebaud | 8710bd2 | 2010-06-02 13:32:13 +0200 | [diff] [blame^] | 326 | m2.insertBackByOuterInner(j,i) = Scalar(0); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 327 | } |
| 328 | else |
| 329 | { |
| 330 | countTrueNonZero++; |
Gael Guennebaud | 8710bd2 | 2010-06-02 13:32:13 +0200 | [diff] [blame^] | 331 | m2.insertBackByOuterInner(j,i) = refM2(i,j) = Scalar(1); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 334 | } |
| 335 | m2.finalize(); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 336 | VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros()); |
| 337 | VERIFY_IS_APPROX(m2, refM2); |
| 338 | m2.prune(1); |
| 339 | VERIFY(countTrueNonZero==m2.nonZeros()); |
| 340 | VERIFY_IS_APPROX(m2, refM2); |
| 341 | } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void test_sparse_basic() |
| 345 | { |
| 346 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 347 | CALL_SUBTEST_1( sparse_basic(SparseMatrix<double>(8, 8)) ); |
| 348 | CALL_SUBTEST_2( sparse_basic(SparseMatrix<std::complex<double> >(16, 16)) ); |
| 349 | CALL_SUBTEST_1( sparse_basic(SparseMatrix<double>(33, 33)) ); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 350 | |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 351 | CALL_SUBTEST_3( sparse_basic(DynamicSparseMatrix<double>(8, 8)) ); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 352 | } |
| 353 | } |