Sparse matrix insertion:

- automatically turn a SparseMatrix to uncompressed mode when calling insert(i,j).
 - now coeffRef insert a new element when it does not already exist
This commit is contained in:
Gael Guennebaud
2011-12-02 19:00:16 +01:00
parent f10bae74e8
commit 4ca89f32ed
2 changed files with 76 additions and 46 deletions

View File

@@ -110,7 +110,8 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
DenseMatrix m1(rows,cols);
m1.setZero();
SparseMatrixType m2(rows,cols);
m2.reserve(10);
if(internal::random<int>()%2)
m2.reserve(VectorXi::Constant(m2.outerSize(), 2));
for (int j=0; j<cols; ++j)
{
for (int k=0; k<rows/2; ++k)
@@ -129,15 +130,21 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
DenseMatrix m1(rows,cols);
m1.setZero();
SparseMatrixType m2(rows,cols);
m2.reserve(10);
if(internal::random<int>()%2)
m2.reserve(VectorXi::Constant(m2.outerSize(), 2));
for (int k=0; k<rows*cols; ++k)
{
int i = internal::random<int>(0,rows-1);
int j = internal::random<int>(0,cols-1);
if (m1.coeff(i,j)==Scalar(0))
if ((m1.coeff(i,j)==Scalar(0)) && (internal::random<int>()%2))
m2.insert(i,j) = m1(i,j) = internal::random<Scalar>();
else
{
Scalar v = internal::random<Scalar>();
m2.coeffRef(i,j) += v;
m1(i,j) += v;
}
}
m2.finalize();
VERIFY_IS_APPROX(m2,m1);
}
@@ -158,8 +165,8 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
if(mode==3)
m2.reserve(r);
}
m2.finalize();
m2.makeCompressed();
if(internal::random<int>()%2)
m2.makeCompressed();
VERIFY_IS_APPROX(m2,m1);
}