| 12 | #include "sparse.h" |
| 13 | |
| 14 | template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref) |
| 15 | { |
| 16 | typedef typename SparseMatrixType::Index Index; |
| 17 | typedef Matrix<Index,2,1> Vector2; |
| 18 | |
| 19 | const Index rows = ref.rows(); |
| 20 | const Index cols = ref.cols(); |
| 21 | typedef typename SparseMatrixType::Scalar Scalar; |
| 22 | enum { Flags = SparseMatrixType::Flags }; |
| 23 | |
| 24 | double density = (std::max)(8./(rows*cols), 0.01); |
| 25 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 26 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 27 | typedef Matrix<Scalar,1,Dynamic> RowDenseVector; |
| 28 | Scalar eps = 1e-6; |
| 29 | |
| 30 | Scalar s1 = internal::random<Scalar>(); |
| 31 | { |
| 32 | SparseMatrixType m(rows, cols); |
| 33 | DenseMatrix refMat = DenseMatrix::Zero(rows, cols); |
| 34 | DenseVector vec1 = DenseVector::Random(rows); |
| 35 | |
| 36 | std::vector<Vector2> zeroCoords; |
| 37 | std::vector<Vector2> nonzeroCoords; |
| 38 | initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords); |
| 39 | |
| 40 | if (zeroCoords.size()==0 || nonzeroCoords.size()==0) |
| 41 | return; |
| 42 | |
| 43 | // test coeff and coeffRef |
| 44 | for (int i=0; i<(int)zeroCoords.size(); ++i) |
| 45 | { |
| 46 | VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps ); |
| 47 | if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value) |
| 48 | VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 ); |
| 49 | } |
| 50 | VERIFY_IS_APPROX(m, refMat); |
| 51 | |
| 52 | m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 53 | refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 54 | |
| 55 | VERIFY_IS_APPROX(m, refMat); |
| 56 | |
| 57 | // test InnerIterators and Block expressions |
| 58 | for (int t=0; t<10; ++t) |
| 59 | { |
| 60 | int j = internal::random<int>(0,cols-1); |
| 61 | int i = internal::random<int>(0,rows-1); |
| 62 | int w = internal::random<int>(1,cols-j-1); |
| 63 | int h = internal::random<int>(1,rows-i-1); |
| 64 | |
| 65 | VERIFY_IS_APPROX(m.block(i,j,h,w), refMat.block(i,j,h,w)); |
| 66 | for(int c=0; c<w; c++) |
| 67 | { |
| 68 | VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c)); |
| 69 | for(int r=0; r<h; r++) |
| 70 | { |
| 71 | VERIFY_IS_APPROX(m.block(i,j,h,w).col(c).coeff(r), refMat.block(i,j,h,w).col(c).coeff(r)); |
no test coverage detected