From 178858f1bd4f0661f355d17058d87f8c56a4c0c1 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Mon, 19 Jan 2009 15:20:45 +0000 Subject: add a flexible sparse matrix class designed for fast matrix assembly --- test/sparse_basic.cpp | 89 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 33 deletions(-) (limited to 'test/sparse_basic.cpp') diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp index 31244000d..addd40f9e 100644 --- a/test/sparse_basic.cpp +++ b/test/sparse_basic.cpp @@ -42,14 +42,34 @@ bool test_random_setter(SparseType& sm, const DenseType& ref, const std::vector< return sm.isApprox(ref); } -template void sparse_basic(int rows, int cols) +template +bool test_random_setter(DynamicSparseMatrix& sm, const DenseType& ref, const std::vector& nonzeroCoords) { + sm.setZero(); + std::vector remaining = nonzeroCoords; + while(!remaining.empty()) + { + int i = ei_random(0,remaining.size()-1); + sm.coeffRef(remaining[i].x(),remaining[i].y()) = ref.coeff(remaining[i].x(),remaining[i].y()); + remaining[i] = remaining.back(); + remaining.pop_back(); + } + return sm.isApprox(ref); +} + +template void sparse_basic(const SparseMatrixType& ref) +{ + const int rows = ref.rows(); + const int cols = ref.cols(); + typedef typename SparseMatrixType::Scalar Scalar; + enum { Flags = SparseMatrixType::Flags }; + double density = std::max(8./(rows*cols), 0.01); typedef Matrix DenseMatrix; typedef Matrix DenseVector; Scalar eps = 1e-6; - SparseMatrix m(rows, cols); + SparseMatrixType m(rows, cols); DenseMatrix refMat = DenseMatrix::Zero(rows, cols); DenseVector vec1 = DenseVector::Random(rows); Scalar s1 = ei_random(); @@ -57,7 +77,7 @@ template void sparse_basic(int rows, int cols) std::vector zeroCoords; std::vector nonzeroCoords; initSparse(density, refMat, m, 0, &zeroCoords, &nonzeroCoords); - + if (zeroCoords.size()==0 || nonzeroCoords.size()==0) return; @@ -65,7 +85,8 @@ template void sparse_basic(int rows, int cols) for (int i=0; i<(int)zeroCoords.size(); ++i) { VERIFY_IS_MUCH_SMALLER_THAN( m.coeff(zeroCoords[i].x(),zeroCoords[i].y()), eps ); - VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 ); + if(ei_is_same_type >::ret) + VERIFY_RAISES_ASSERT( m.coeffRef(zeroCoords[0].x(),zeroCoords[0].y()) = 5 ); } VERIFY_IS_APPROX(m, refMat); @@ -120,7 +141,7 @@ template void sparse_basic(int rows, int cols) // { // m.setZero(); // VERIFY_IS_NOT_APPROX(m, refMat); -// SparseSetter, FullyCoherentAccessPattern> w(m); +// SparseSetter w(m); // for (int i=0; icoeffRef(nonzeroCoords[i].x(),nonzeroCoords[i].y()) = refMat.coeff(nonzeroCoords[i].x(),nonzeroCoords[i].y()); @@ -132,7 +153,7 @@ template void sparse_basic(int rows, int cols) // { // m.setZero(); // VERIFY_IS_NOT_APPROX(m, refMat); -// SparseSetter, RandomAccessPattern> w(m); +// SparseSetter w(m); // std::vector remaining = nonzeroCoords; // while(!remaining.empty()) // { @@ -144,22 +165,22 @@ template void sparse_basic(int rows, int cols) // } // VERIFY_IS_APPROX(m, refMat); - VERIFY(( test_random_setter, StdMapTraits> >(m,refMat,nonzeroCoords) )); + VERIFY(( test_random_setter >(m,refMat,nonzeroCoords) )); #ifdef _HASH_MAP - VERIFY(( test_random_setter, GnuHashMapTraits> >(m,refMat,nonzeroCoords) )); + VERIFY(( test_random_setter >(m,refMat,nonzeroCoords) )); #endif #ifdef _DENSE_HASH_MAP_H_ - VERIFY(( test_random_setter, GoogleDenseHashMapTraits> >(m,refMat,nonzeroCoords) )); + VERIFY(( test_random_setter >(m,refMat,nonzeroCoords) )); #endif #ifdef _SPARSE_HASH_MAP_H_ - VERIFY(( test_random_setter, GoogleSparseHashMapTraits> >(m,refMat,nonzeroCoords) )); + VERIFY(( test_random_setter >(m,refMat,nonzeroCoords) )); #endif // test fillrand { DenseMatrix m1(rows,cols); m1.setZero(); - SparseMatrix m2(rows,cols); + SparseMatrixType m2(rows,cols); m2.startFill(); for (int j=0; j void sparse_basic(int rows, int cols) } } m2.endFill(); - std::cerr << m1 << "\n\n" << m2 << "\n"; + //std::cerr << m1 << "\n\n" << m2 << "\n"; VERIFY_IS_APPROX(m2,m1); } // test RandomSetter - { - SparseMatrix m1(rows,cols), m2(rows,cols); + /*{ + SparseMatrixType m1(rows,cols), m2(rows,cols); DenseMatrix refM1 = DenseMatrix::Zero(rows, rows); initSparse(density, refM1, m1); { - Eigen::RandomSetter > setter(m2); + Eigen::RandomSetter setter(m2); for (int j=0; j::InnerIterator i(m1,j); i; ++i) + for (typename SparseMatrixType::InnerIterator i(m1,j); i; ++i) setter(i.index(), j) = i.value(); } VERIFY_IS_APPROX(m1, m2); - } + }*/ // std::cerr << m.transpose() << "\n\n" << refMat.transpose() << "\n\n"; // VERIFY_IS_APPROX(m, refMat); @@ -197,10 +218,10 @@ template void sparse_basic(int rows, int cols) DenseMatrix refM2 = DenseMatrix::Zero(rows, rows); DenseMatrix refM3 = DenseMatrix::Zero(rows, rows); DenseMatrix refM4 = DenseMatrix::Zero(rows, rows); - SparseMatrix m1(rows, rows); - SparseMatrix m2(rows, rows); - SparseMatrix m3(rows, rows); - SparseMatrix m4(rows, rows); + SparseMatrixType m1(rows, rows); + SparseMatrixType m2(rows, rows); + SparseMatrixType m3(rows, rows); + SparseMatrixType m4(rows, rows); initSparse(density, refM1, m1); initSparse(density, refM2, m2); initSparse(density, refM3, m3); @@ -223,7 +244,7 @@ template void sparse_basic(int rows, int cols) // test innerVector() { DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); - SparseMatrix m2(rows, rows); + SparseMatrixType m2(rows, rows); initSparse(density, refMat2, m2); int j0 = ei_random(0,rows-1); int j1 = ei_random(0,rows-1); @@ -234,7 +255,7 @@ template void sparse_basic(int rows, int cols) // test transpose { DenseMatrix refMat2 = DenseMatrix::Zero(rows, rows); - SparseMatrix m2(rows, rows); + SparseMatrixType m2(rows, rows); initSparse(density, refMat2, m2); VERIFY_IS_APPROX(m2.transpose().eval(), refMat2.transpose().eval()); VERIFY_IS_APPROX(m2.transpose(), refMat2.transpose()); @@ -246,9 +267,9 @@ template void sparse_basic(int rows, int cols) DenseMatrix refMat3 = DenseMatrix::Zero(rows, rows); DenseMatrix refMat4 = DenseMatrix::Zero(rows, rows); DenseMatrix dm4 = DenseMatrix::Zero(rows, rows); - SparseMatrix m2(rows, rows); - SparseMatrix m3(rows, rows); - SparseMatrix m4(rows, rows); + SparseMatrixType m2(rows, rows); + SparseMatrixType m3(rows, rows); + SparseMatrixType m4(rows, rows); initSparse(density, refMat2, m2); initSparse(density, refMat3, m3); initSparse(density, refMat4, m4); @@ -278,9 +299,9 @@ template void sparse_basic(int rows, int cols) DenseMatrix refUp = DenseMatrix::Zero(rows, rows); DenseMatrix refLo = DenseMatrix::Zero(rows, rows); DenseMatrix refS = DenseMatrix::Zero(rows, rows); - SparseMatrix mUp(rows, rows); - SparseMatrix mLo(rows, rows); - SparseMatrix mS(rows, rows); + SparseMatrixType mUp(rows, rows); + SparseMatrixType mLo(rows, rows); + SparseMatrixType mS(rows, rows); do { initSparse(density, refUp, mUp, ForceRealDiag|/*ForceNonZeroDiag|*/MakeUpperTriangular); } while (refUp.isZero()); @@ -290,7 +311,7 @@ template void sparse_basic(int rows, int cols) refS.diagonal() *= 0.5; mS = mUp + mLo; for (int k=0; k::InnerIterator it(mS,k); it; ++it) + for (typename SparseMatrixType::InnerIterator it(mS,k); it; ++it) if (it.index() == k) it.valueRef() *= 0.5; @@ -307,8 +328,10 @@ template void sparse_basic(int rows, int cols) void test_sparse_basic() { for(int i = 0; i < g_repeat; i++) { - CALL_SUBTEST( sparse_basic(8, 8) ); - CALL_SUBTEST( sparse_basic >(16, 16) ); - CALL_SUBTEST( sparse_basic(33, 33) ); +// CALL_SUBTEST( sparse_basic(SparseMatrix(8, 8)) ); +// CALL_SUBTEST( sparse_basic(SparseMatrix >(16, 16)) ); +// CALL_SUBTEST( sparse_basic(SparseMatrix(33, 33)) ); + + CALL_SUBTEST( sparse_basic(DynamicSparseMatrix(8, 8)) ); } } -- cgit v1.2.3