diff options
author | Gael Guennebaud <g.gael@free.fr> | 2009-05-04 14:25:12 +0000 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2009-05-04 14:25:12 +0000 |
commit | 28293142842c525eec1adde377999b065dea8cbf (patch) | |
tree | 22a6b32d00f507afaaa6a20c712ecd70c8b6ffb7 /test | |
parent | ddb6e96d48e353099911cf4179ea6285dce40d4c (diff) |
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.
Diffstat (limited to 'test')
-rw-r--r-- | test/sparse.h | 18 | ||||
-rw-r--r-- | test/sparse_basic.cpp | 35 | ||||
-rw-r--r-- | test/sparse_solvers.cpp | 6 |
3 files changed, 41 insertions, 18 deletions
diff --git a/test/sparse.h b/test/sparse.h index 80d99dc5b..eb2f98f5f 100644 --- a/test/sparse.h +++ b/test/sparse.h @@ -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); } diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp index 410ef96a6..cf58b30af 100644 --- a/test/sparse_basic.cpp +++ b/test/sparse_basic.cpp @@ -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); diff --git a/test/sparse_solvers.cpp b/test/sparse_solvers.cpp index e1ec1ef35..ce19153ff 100644 --- a/test/sparse_solvers.cpp +++ b/test/sparse_solvers.cpp @@ -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) |