| 10 | #include "sparse.h" |
| 11 | |
| 12 | template<typename Scalar,typename StorageIndex> void sparse_vector(int rows, int cols) |
| 13 | { |
| 14 | double densityMat = (std::max)(8./(rows*cols), 0.01); |
| 15 | double densityVec = (std::max)(8./(rows), 0.1); |
| 16 | typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; |
| 17 | typedef Matrix<Scalar,Dynamic,1> DenseVector; |
| 18 | typedef SparseVector<Scalar,0,StorageIndex> SparseVectorType; |
| 19 | typedef SparseMatrix<Scalar,0,StorageIndex> SparseMatrixType; |
| 20 | Scalar eps = 1e-6; |
| 21 | |
| 22 | SparseMatrixType m1(rows,rows); |
| 23 | SparseVectorType v1(rows), v2(rows), v3(rows); |
| 24 | DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); |
| 25 | DenseVector refV1 = DenseVector::Random(rows), |
| 26 | refV2 = DenseVector::Random(rows), |
| 27 | refV3 = DenseVector::Random(rows); |
| 28 | |
| 29 | std::vector<int> zerocoords, nonzerocoords; |
| 30 | initSparse<Scalar>(densityVec, refV1, v1, &zerocoords, &nonzerocoords); |
| 31 | initSparse<Scalar>(densityMat, refM1, m1); |
| 32 | |
| 33 | initSparse<Scalar>(densityVec, refV2, v2); |
| 34 | initSparse<Scalar>(densityVec, refV3, v3); |
| 35 | |
| 36 | Scalar s1 = internal::random<Scalar>(); |
| 37 | |
| 38 | // test coeff and coeffRef |
| 39 | for (unsigned int i=0; i<zerocoords.size(); ++i) |
| 40 | { |
| 41 | VERIFY_IS_MUCH_SMALLER_THAN( v1.coeff(zerocoords[i]), eps ); |
| 42 | //VERIFY_RAISES_ASSERT( v1.coeffRef(zerocoords[i]) = 5 ); |
| 43 | } |
| 44 | { |
| 45 | VERIFY(int(nonzerocoords.size()) == v1.nonZeros()); |
| 46 | int j=0; |
| 47 | for (typename SparseVectorType::InnerIterator it(v1); it; ++it,++j) |
| 48 | { |
| 49 | VERIFY(nonzerocoords[j]==it.index()); |
| 50 | VERIFY(it.value()==v1.coeff(it.index())); |
| 51 | VERIFY(it.value()==refV1.coeff(it.index())); |
| 52 | } |
| 53 | } |
| 54 | VERIFY_IS_APPROX(v1, refV1); |
| 55 | |
| 56 | // test coeffRef with reallocation |
| 57 | { |
| 58 | SparseVectorType v4(rows); |
| 59 | DenseVector v5 = DenseVector::Zero(rows); |
| 60 | for(int k=0; k<rows; ++k) |
| 61 | { |
| 62 | int i = internal::random<int>(0,rows-1); |
| 63 | Scalar v = internal::random<Scalar>(); |
| 64 | v4.coeffRef(i) += v; |
| 65 | v5.coeffRef(i) += v; |
| 66 | } |
| 67 | VERIFY_IS_APPROX(v4,v5); |
| 68 | } |
| 69 |
nothing calls this directly
no test coverage detected