| 24 | } |
| 25 | |
| 26 | template<typename SparseMatrixType> void sparse_block(const SparseMatrixType& ref) |
| 27 | { |
| 28 | const Index rows = ref.rows(); |
| 29 | const Index cols = ref.cols(); |
| 30 | const Index inner = ref.innerSize(); |
| 31 | const Index outer = ref.outerSize(); |
| 32 | |
| 33 | typedef typename SparseMatrixType::Scalar Scalar; |
| 34 | typedef typename SparseMatrixType::StorageIndex StorageIndex; |
| 35 | |
| 36 | double density = (std::max)(8./(rows*cols), 0.01); |
| 37 | typedef Matrix<Scalar,Dynamic,Dynamic,SparseMatrixType::IsRowMajor?RowMajor:ColMajor> DenseMatrix; |
| 38 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 39 | typedef Matrix<Scalar,1,Dynamic> RowDenseVector; |
| 40 | typedef SparseVector<Scalar> SparseVectorType; |
| 41 | |
| 42 | Scalar s1 = internal::random<Scalar>(); |
| 43 | { |
| 44 | SparseMatrixType m(rows, cols); |
| 45 | DenseMatrix refMat = DenseMatrix::Zero(rows, cols); |
| 46 | initSparse<Scalar>(density, refMat, m); |
| 47 | |
| 48 | VERIFY_IS_APPROX(m, refMat); |
| 49 | |
| 50 | // test InnerIterators and Block expressions |
| 51 | for (int t=0; t<10; ++t) |
| 52 | { |
| 53 | Index j = internal::random<Index>(0,cols-2); |
| 54 | Index i = internal::random<Index>(0,rows-2); |
| 55 | Index w = internal::random<Index>(1,cols-j); |
| 56 | Index h = internal::random<Index>(1,rows-i); |
| 57 | |
| 58 | VERIFY_IS_APPROX(m.block(i,j,h,w), refMat.block(i,j,h,w)); |
| 59 | for(Index c=0; c<w; c++) |
| 60 | { |
| 61 | VERIFY_IS_APPROX(m.block(i,j,h,w).col(c), refMat.block(i,j,h,w).col(c)); |
| 62 | for(Index r=0; r<h; r++) |
| 63 | { |
| 64 | VERIFY_IS_APPROX(m.block(i,j,h,w).col(c).coeff(r), refMat.block(i,j,h,w).col(c).coeff(r)); |
| 65 | VERIFY_IS_APPROX(m.block(i,j,h,w).coeff(r,c), refMat.block(i,j,h,w).coeff(r,c)); |
| 66 | } |
| 67 | } |
| 68 | for(Index r=0; r<h; r++) |
| 69 | { |
| 70 | VERIFY_IS_APPROX(m.block(i,j,h,w).row(r), refMat.block(i,j,h,w).row(r)); |
| 71 | for(Index c=0; c<w; c++) |
| 72 | { |
| 73 | VERIFY_IS_APPROX(m.block(i,j,h,w).row(r).coeff(c), refMat.block(i,j,h,w).row(r).coeff(c)); |
| 74 | VERIFY_IS_APPROX(m.block(i,j,h,w).coeff(r,c), refMat.block(i,j,h,w).coeff(r,c)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | VERIFY_IS_APPROX(m.middleCols(j,w), refMat.middleCols(j,w)); |
| 79 | VERIFY_IS_APPROX(m.middleRows(i,h), refMat.middleRows(i,h)); |
| 80 | for(Index r=0; r<h; r++) |
| 81 | { |
| 82 | VERIFY_IS_APPROX(m.middleCols(j,w).row(r), refMat.middleCols(j,w).row(r)); |
| 83 | VERIFY_IS_APPROX(m.middleRows(i,h).row(r), refMat.middleRows(i,h).row(r)); |