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