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 | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 27 | template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref) |
| 28 | { |
| 29 | const int rows = ref.rows(); |
| 30 | const int cols = ref.cols(); |
| 31 | typedef typename SparseMatrixType::Scalar Scalar; |
| 32 | enum { Flags = SparseMatrixType::Flags }; |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 33 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 34 | double density = std::max(8./(rows*cols), 0.01); |
| 35 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 36 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 37 | Scalar eps = 1e-6; |
| 38 | |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 39 | SparseMatrixType m(rows, cols); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 40 | DenseMatrix refMat = DenseMatrix::Zero(rows, cols); |
| 41 | DenseVector vec1 = DenseVector::Random(rows); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 42 | Scalar s1 = ei_random<Scalar>(); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 43 | |
| 44 | std::vector<Vector2i> zeroCoords; |
| 45 | std::vector<Vector2i> nonzeroCoords; |
| 46 | initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 47 | |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 48 | if (zeroCoords.size()==0 || nonzeroCoords.size()==0) |
| 49 | return; |
| 50 | |
| 51 | // test coeff and coeffRef |
| 52 | for (int i=0; i<(int)zeroCoords.size(); ++i) |
| 53 | { |
| 54 | 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] | 55 | if(ei_is_same_type<SparseMatrixType,SparseMatrix<Scalar,Flags> >::ret) |
| 56 | VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 ); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 57 | } |
| 58 | VERIFY_IS_APPROX(m, refMat); |
| 59 | |
| 60 | m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 61 | refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 62 | |
| 63 | VERIFY_IS_APPROX(m, refMat); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 64 | /* |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 65 | // test InnerIterators and Block expressions |
| 66 | for (int t=0; t<10; ++t) |
| 67 | { |
| 68 | int j = ei_random<int>(0,cols-1); |
| 69 | int i = ei_random<int>(0,rows-1); |
| 70 | int w = ei_random<int>(1,cols-j-1); |
| 71 | int h = ei_random<int>(1,rows-i-1); |
| 72 | |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 73 | // 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] | 74 | for(int c=0; c<w; c++) |
| 75 | { |
| 76 | VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c)); |
| 77 | for(int r=0; r<h; r++) |
| 78 | { |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 79 | // 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] | 80 | } |
| 81 | } |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 82 | // for(int r=0; r<h; r++) |
| 83 | // { |
| 84 | // VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r)); |
| 85 | // for(int c=0; c<w; c++) |
| 86 | // { |
| 87 | // VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c)); |
| 88 | // } |
| 89 | // } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | for(int c=0; c<cols; c++) |
| 93 | { |
| 94 | VERIFY_IS_APPROX(m.col(c) + m.col(c), (m + m).col(c)); |
| 95 | VERIFY_IS_APPROX(m.col(c) + m.col(c), refMat.col(c) + refMat.col(c)); |
| 96 | } |
| 97 | |
| 98 | for(int r=0; r<rows; r++) |
| 99 | { |
| 100 | VERIFY_IS_APPROX(m.row(r) + m.row(r), (m + m).row(r)); |
| 101 | VERIFY_IS_APPROX(m.row(r) + m.row(r), refMat.row(r) + refMat.row(r)); |
| 102 | } |
| 103 | */ |
| 104 | |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 105 | // test insert (inner random) |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 106 | { |
| 107 | DenseMatrix m1(rows,cols); |
| 108 | m1.setZero(); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 109 | SparseMatrixType m2(rows,cols); |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 110 | m2.reserve(10); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 111 | for (int j=0; j<cols; ++j) |
| 112 | { |
| 113 | for (int k=0; k<rows/2; ++k) |
| 114 | { |
| 115 | int i = ei_random<int>(0,rows-1); |
| 116 | if (m1.coeff(i,j)==Scalar(0)) |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 117 | m2.insert(i,j) = m1(i,j) = ei_random<Scalar>(); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 120 | m2.finalize(); |
| 121 | VERIFY_IS_APPROX(m2,m1); |
| 122 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 123 | |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 124 | // test insert (fully random) |
| 125 | { |
| 126 | DenseMatrix m1(rows,cols); |
| 127 | m1.setZero(); |
| 128 | SparseMatrixType m2(rows,cols); |
| 129 | m2.reserve(10); |
| 130 | for (int k=0; k<rows*cols; ++k) |
| 131 | { |
| 132 | int i = ei_random<int>(0,rows-1); |
| 133 | int j = ei_random<int>(0,cols-1); |
| 134 | if (m1.coeff(i,j)==Scalar(0)) |
| 135 | m2.insert(i,j) = m1(i,j) = ei_random<Scalar>(); |
| 136 | } |
| 137 | m2.finalize(); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 138 | VERIFY_IS_APPROX(m2,m1); |
Gael Guennebaud | 5015e48 | 2008-12-11 18:26:24 +0000 | [diff] [blame] | 139 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 140 | |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 141 | // test basic computations |
| 142 | { |
| 143 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
| 144 | DenseMatrix refM2 = DenseMatrix::Zero(rows, rows); |
| 145 | DenseMatrix refM3 = DenseMatrix::Zero(rows, rows); |
| 146 | DenseMatrix refM4 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 147 | SparseMatrixType m1(rows, rows); |
| 148 | SparseMatrixType m2(rows, rows); |
| 149 | SparseMatrixType m3(rows, rows); |
| 150 | SparseMatrixType m4(rows, rows); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 151 | initSparse<Scalar>(density, refM1, m1); |
| 152 | initSparse<Scalar>(density, refM2, m2); |
| 153 | initSparse<Scalar>(density, refM3, m3); |
| 154 | initSparse<Scalar>(density, refM4, m4); |
| 155 | |
| 156 | VERIFY_IS_APPROX(m1+m2, refM1+refM2); |
| 157 | VERIFY_IS_APPROX(m1+m2+m3, refM1+refM2+refM3); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 158 | VERIFY_IS_APPROX(m3.cwiseProduct(m1+m2), refM3.cwiseProduct(refM1+refM2)); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 159 | VERIFY_IS_APPROX(m1*s1-m2, refM1*s1-refM2); |
| 160 | |
| 161 | VERIFY_IS_APPROX(m1*=s1, refM1*=s1); |
| 162 | VERIFY_IS_APPROX(m1/=s1, refM1/=s1); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 163 | |
Gael Guennebaud | e7c48fa | 2009-01-23 13:59:32 +0000 | [diff] [blame] | 164 | VERIFY_IS_APPROX(m1+=m2, refM1+=refM2); |
| 165 | VERIFY_IS_APPROX(m1-=m2, refM1-=refM2); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 166 | |
Gael Guennebaud | a9688f0 | 2009-02-09 09:59:30 +0000 | [diff] [blame] | 167 | 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] | 168 | |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 169 | refM4.setRandom(); |
| 170 | // sparse cwise* dense |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 171 | VERIFY_IS_APPROX(m3.cwiseProduct(refM4), refM3.cwiseProduct(refM4)); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 172 | // VERIFY_IS_APPROX(m3.cwise()/refM4, refM3.cwise()/refM4); |
| 173 | } |
| 174 | |
Gael Guennebaud | ece48a6 | 2010-06-18 11:28:30 +0200 | [diff] [blame] | 175 | // test transpose |
| 176 | { |
| 177 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 178 | SparseMatrixType m2(rows, rows); |
| 179 | initSparse<Scalar>(density, refMat2, m2); |
| 180 | VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval()); |
| 181 | VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose()); |
| 182 | |
| 183 | VERIFY_IS_APPROX(SparseMatrixType(m2.adjoint()), refMat2.adjoint()); |
| 184 | } |
| 185 | |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 186 | // test innerVector() |
| 187 | { |
| 188 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
Gael Guennebaud | 178858f | 2009-01-19 15:20:45 +0000 | [diff] [blame] | 189 | SparseMatrixType m2(rows, rows); |
Gael Guennebaud | c4c7066 | 2009-01-14 14:24:10 +0000 | [diff] [blame] | 190 | initSparse<Scalar>(density, refMat2, m2); |
| 191 | int j0 = ei_random(0,rows-1); |
| 192 | int j1 = ei_random(0,rows-1); |
Gael Guennebaud | 2d53466 | 2009-01-14 21:27:54 +0000 | [diff] [blame] | 193 | VERIFY_IS_APPROX(m2.innerVector(j0), refMat2.col(j0)); |
| 194 | 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] | 195 | //m2.innerVector(j0) = 2*m2.innerVector(j1); |
| 196 | //refMat2.col(j0) = 2*refMat2.col(j1); |
| 197 | //VERIFY_IS_APPROX(m2, refMat2); |
| 198 | } |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 199 | |
Gael Guennebaud | 8ce4503 | 2009-01-27 22:48:17 +0000 | [diff] [blame] | 200 | // test innerVectors() |
| 201 | { |
| 202 | DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); |
| 203 | SparseMatrixType m2(rows, rows); |
| 204 | initSparse<Scalar>(density, refMat2, m2); |
| 205 | int j0 = ei_random(0,rows-2); |
| 206 | int j1 = ei_random(0,rows-2); |
| 207 | int n0 = ei_random<int>(1,rows-std::max(j0,j1)); |
| 208 | VERIFY_IS_APPROX(m2.innerVectors(j0,n0), refMat2.block(0,j0,rows,n0)); |
| 209 | VERIFY_IS_APPROX(m2.innerVectors(j0,n0)+m2.innerVectors(j1,n0), |
| 210 | refMat2.block(0,j0,rows,n0)+refMat2.block(0,j1,rows,n0)); |
| 211 | //m2.innerVectors(j0,n0) = m2.innerVectors(j0,n0) + m2.innerVectors(j1,n0); |
| 212 | //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] | 213 | } |
| 214 | |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 215 | // test prune |
| 216 | { |
| 217 | SparseMatrixType m2(rows, rows); |
| 218 | DenseMatrix refM2(rows, rows); |
| 219 | refM2.setZero(); |
| 220 | int countFalseNonZero = 0; |
| 221 | int countTrueNonZero = 0; |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 222 | for (int j=0; j<m2.outerSize(); ++j) |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 223 | { |
| 224 | m2.startVec(j); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 225 | for (int i=0; i<m2.innerSize(); ++i) |
| 226 | { |
| 227 | float x = ei_random<float>(0,1); |
| 228 | if (x<0.1) |
| 229 | { |
| 230 | // do nothing |
| 231 | } |
| 232 | else if (x<0.5) |
| 233 | { |
| 234 | countFalseNonZero++; |
Gael Guennebaud | 8710bd2 | 2010-06-02 13:32:13 +0200 | [diff] [blame] | 235 | m2.insertBackByOuterInner(j,i) = Scalar(0); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 236 | } |
| 237 | else |
| 238 | { |
| 239 | countTrueNonZero++; |
Gael Guennebaud | 8710bd2 | 2010-06-02 13:32:13 +0200 | [diff] [blame] | 240 | m2.insertBackByOuterInner(j,i) = refM2(i,j) = Scalar(1); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
Gael Guennebaud | 2829314 | 2009-05-04 14:25:12 +0000 | [diff] [blame] | 243 | } |
| 244 | m2.finalize(); |
Gael Guennebaud | 52cf07d | 2009-01-21 18:46:04 +0000 | [diff] [blame] | 245 | VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros()); |
| 246 | VERIFY_IS_APPROX(m2, refM2); |
| 247 | m2.prune(1); |
| 248 | VERIFY(countTrueNonZero==m2.nonZeros()); |
| 249 | VERIFY_IS_APPROX(m2, refM2); |
| 250 | } |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void test_sparse_basic() |
| 254 | { |
| 255 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 256 | CALL_SUBTEST_1( sparse_basic(SparseMatrix<double>(8, 8)) ); |
| 257 | CALL_SUBTEST_2( sparse_basic(SparseMatrix<std::complex<double> >(16, 16)) ); |
| 258 | CALL_SUBTEST_1( sparse_basic(SparseMatrix<double>(33, 33)) ); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 259 | |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 260 | CALL_SUBTEST_3( sparse_basic(DynamicSparseMatrix<double>(8, 8)) ); |
Gael Guennebaud | 86ccd99 | 2008-11-05 13:47:55 +0000 | [diff] [blame] | 261 | } |
| 262 | } |