From 1d4e80f09dd203450e390d80a64d9491e680869b Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Thu, 28 Oct 2010 11:39:31 +0200 Subject: [PATCH] generalize the prune function --- Eigen/src/Sparse/SparseMatrix.h | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Eigen/src/Sparse/SparseMatrix.h b/Eigen/src/Sparse/SparseMatrix.h index 9a6e0b763..3187bca00 100644 --- a/Eigen/src/Sparse/SparseMatrix.h +++ b/Eigen/src/Sparse/SparseMatrix.h @@ -333,16 +333,38 @@ class SparseMatrix /** Suppress all nonzeros which are smaller than \a reference under the tolerence \a epsilon */ void prune(Scalar reference, RealScalar epsilon = NumTraits::dummy_precision()) + { + struct func { + func(Scalar ref, RealScalar eps) : reference(ref), epsilon(eps) {} + inline bool operator() (const Index& row, const Index& col, const Scalar& value) const + { + return !internal::isMuchSmallerThan(value, reference, epsilon); + } + Scalar reference; + RealScalar epsilon; + }; + prune(func(reference,epsilon)); + } + + /** Suppress all nonzeros which do not satisfy the predicate \a keep. + * The functor type \a KeepFunc must implement the following function: + * \code + * bool operator() (const Index& row, const Index& col, const Scalar& value) const; + * \endcode + * \sa prune(Scalar,RealScalar) + */ + template + void prune(const KeepFunc& keep = KeepFunc()) { Index k = 0; - for (Index j=0; j