new simplified API to fill sparse matrices (the old functions are

deprecated). Basically there are now only 2 functions to set a
coefficient:
1) mat.coeffRef(row,col) = value;
2) mat.insert(row,col) = value;
coeffRef has no limitation, insert assumes the coeff has not already
been set, and raises an assert otherwise.
In addition I added a much lower level, but more efficient filling
mechanism for
internal use only.
This commit is contained in:
Gael Guennebaud
2009-05-04 14:25:12 +00:00
parent ddb6e96d48
commit 2829314284
12 changed files with 287 additions and 110 deletions

View File

@@ -64,9 +64,11 @@ initSparse(double density,
std::vector<Vector2i>* zeroCoords = 0,
std::vector<Vector2i>* nonzeroCoords = 0)
{
sparseMat.startFill(int(refMat.rows()*refMat.cols()*density));
sparseMat.setZero();
sparseMat.reserve(int(refMat.rows()*refMat.cols()*density));
for(int j=0; j<refMat.cols(); j++)
{
sparseMat.startVec(j);
for(int i=0; i<refMat.rows(); i++)
{
Scalar v = (ei_random<double>(0,1) < density) ? ei_random<Scalar>() : Scalar(0);
@@ -85,7 +87,7 @@ initSparse(double density,
if (v!=Scalar(0))
{
sparseMat.fill(i,j) = v;
sparseMat.insertBack(j,i) = v;
if (nonzeroCoords)
nonzeroCoords->push_back(Vector2i(i,j));
}
@@ -96,7 +98,7 @@ initSparse(double density,
refMat(i,j) = v;
}
}
sparseMat.endFill();
sparseMat.finalize();
}
template<typename Scalar> void
@@ -107,9 +109,11 @@ initSparse(double density,
std::vector<Vector2i>* zeroCoords = 0,
std::vector<Vector2i>* nonzeroCoords = 0)
{
sparseMat.startFill(int(refMat.rows()*refMat.cols()*density));
sparseMat.setZero();
sparseMat.reserve(int(refMat.rows()*refMat.cols()*density));
for(int j=0; j<refMat.cols(); j++)
{
sparseMat.startVec(j); // not needed for DynamicSparseMatrix
for(int i=0; i<refMat.rows(); i++)
{
Scalar v = (ei_random<double>(0,1) < density) ? ei_random<Scalar>() : Scalar(0);
@@ -128,7 +132,7 @@ initSparse(double density,
if (v!=Scalar(0))
{
sparseMat.fill(i,j) = v;
sparseMat.insertBack(j,i) = v;
if (nonzeroCoords)
nonzeroCoords->push_back(Vector2i(i,j));
}
@@ -139,7 +143,7 @@ initSparse(double density,
refMat(i,j) = v;
}
}
sparseMat.endFill();
sparseMat.finalize();
}
template<typename Scalar> void
@@ -156,7 +160,7 @@ initSparse(double density,
Scalar v = (ei_random<double>(0,1) < density) ? ei_random<Scalar>() : Scalar(0);
if (v!=Scalar(0))
{
sparseVec.fill(i) = v;
sparseVec.insertBack(i) = v;
if (nonzeroCoords)
nonzeroCoords->push_back(i);
}

View File

@@ -177,22 +177,39 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
VERIFY(( test_random_setter<RandomSetter<SparseMatrixType, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) ));
#endif
// test fillrand
// test insert (inner random)
{
DenseMatrix m1(rows,cols);
m1.setZero();
SparseMatrixType m2(rows,cols);
m2.startFill();
m2.reserve(10);
for (int j=0; j<cols; ++j)
{
for (int k=0; k<rows/2; ++k)
{
int i = ei_random<int>(0,rows-1);
if (m1.coeff(i,j)==Scalar(0))
m2.fillrand(i,j) = m1(i,j) = ei_random<Scalar>();
m2.insert(i,j) = m1(i,j) = ei_random<Scalar>();
}
}
m2.endFill();
m2.finalize();
VERIFY_IS_APPROX(m2,m1);
}
// test insert (fully random)
{
DenseMatrix m1(rows,cols);
m1.setZero();
SparseMatrixType m2(rows,cols);
m2.reserve(10);
for (int k=0; k<rows*cols; ++k)
{
int i = ei_random<int>(0,rows-1);
int j = ei_random<int>(0,cols-1);
if (m1.coeff(i,j)==Scalar(0))
m2.insert(i,j) = m1(i,j) = ei_random<Scalar>();
}
m2.finalize();
VERIFY_IS_APPROX(m2,m1);
}
@@ -291,8 +308,9 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
refM2.setZero();
int countFalseNonZero = 0;
int countTrueNonZero = 0;
m2.startFill();
for (int j=0; j<m2.outerSize(); ++j)
{
m2.startVec(j);
for (int i=0; i<m2.innerSize(); ++i)
{
float x = ei_random<float>(0,1);
@@ -303,15 +321,16 @@ template<typename SparseMatrixType> void sparse_basic(const SparseMatrixType& re
else if (x<0.5)
{
countFalseNonZero++;
m2.fill(i,j) = Scalar(0);
m2.insertBack(j,i) = Scalar(0);
}
else
{
countTrueNonZero++;
m2.fill(i,j) = refM2(i,j) = Scalar(1);
m2.insertBack(j,i) = refM2(i,j) = Scalar(1);
}
}
m2.endFill();
}
m2.finalize();
VERIFY(countFalseNonZero+countTrueNonZero == m2.nonZeros());
VERIFY_IS_APPROX(m2, refM2);
m2.prune(1);

View File

@@ -37,12 +37,12 @@ initSPD(double density,
initSparse(density,aux,sparseMat,ForceNonZeroDiag);
refMat += aux * aux.adjoint();
}
sparseMat.startFill();
sparseMat.setZero();
for (int j=0 ; j<sparseMat.cols(); ++j)
for (int i=j ; i<sparseMat.rows(); ++i)
if (refMat(i,j)!=Scalar(0))
sparseMat.fill(i,j) = refMat(i,j);
sparseMat.endFill();
sparseMat.insert(i,j) = refMat(i,j);
sparseMat.finalize();
}
template<typename Scalar> void sparse_solvers(int rows, int cols)