| 15 | #include "sparse.h" |
| 16 | |
| 17 | template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& ref) |
| 18 | { |
| 19 | typedef typename SparseMatrixType::StorageIndex StorageIndex; |
| 20 | typedef Matrix<StorageIndex,2,1> Vector2; |
| 21 | |
| 22 | const Index rows = ref.rows(); |
| 23 | const Index cols = ref.cols(); |
| 24 | //const Index inner = ref.innerSize(); |
| 25 | //const Index outer = ref.outerSize(); |
| 26 | |
| 27 | typedef typename SparseMatrixType::Scalar Scalar; |
| 28 | typedef typename SparseMatrixType::RealScalar RealScalar; |
| 29 | enum { Flags = SparseMatrixType::Flags }; |
| 30 | |
| 31 | double density = (std::max)(8./(rows*cols), 0.01); |
| 32 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 33 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 34 | Scalar eps = 1e-6; |
| 35 | |
| 36 | Scalar s1 = internal::random<Scalar>(); |
| 37 | { |
| 38 | SparseMatrixType m(rows, cols); |
| 39 | DenseMatrix refMat = DenseMatrix::Zero(rows, cols); |
| 40 | DenseVector vec1 = DenseVector::Random(rows); |
| 41 | |
| 42 | std::vector<Vector2> zeroCoords; |
| 43 | std::vector<Vector2> nonzeroCoords; |
| 44 | initSparse<Scalar>(density, refMat, m, 0, &zeroCoords, &nonzeroCoords); |
| 45 | |
| 46 | // test coeff and coeffRef |
| 47 | for (std::size_t i=0; i<zeroCoords.size(); ++i) |
| 48 | { |
| 49 | VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps ); |
| 50 | if(internal::is_same<SparseMatrixType,SparseMatrix<Scalar,Flags> >::value) |
| 51 | VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[i].x(),zeroCoords[i].y()) = 5 ); |
| 52 | } |
| 53 | VERIFY_IS_APPROX(m, refMat); |
| 54 | |
| 55 | if(!nonzeroCoords.empty()) { |
| 56 | m.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 57 | refMat.coeffRef(nonzeroCoords[0].x(), nonzeroCoords[0].y()) = Scalar(5); |
| 58 | } |
| 59 | |
| 60 | VERIFY_IS_APPROX(m, refMat); |
| 61 | |
| 62 | // test assertion |
| 63 | VERIFY_RAISES_ASSERT( m.coeffRef(-1,1) = 0 ); |
| 64 | VERIFY_RAISES_ASSERT( m.coeffRef(0,m.cols()) = 0 ); |
| 65 | } |
| 66 | |
| 67 | // test insert (inner random) |
| 68 | { |
| 69 | DenseMatrix m1(rows,cols); |
| 70 | m1.setZero(); |
| 71 | SparseMatrixType m2(rows,cols); |
| 72 | bool call_reserve = internal::random<int>()%2; |
| 73 | Index nnz = internal::random<int>(1,int(rows)/2); |
| 74 | if(call_reserve) |