moved pruning code to SparseVector.h

This commit is contained in:
Erik Schultheis
2021-11-15 22:16:01 +00:00
committed by Rasmus Munk Larsen
parent da79095923
commit 13954c4440
3 changed files with 54 additions and 19 deletions

View File

@@ -209,9 +209,33 @@ class SparseVector
inline void finalize() {}
/** \copydoc SparseMatrix::prune(const Scalar&,const RealScalar&) */
void prune(const Scalar& reference, const RealScalar& epsilon = NumTraits<RealScalar>::dummy_precision())
Index prune(const Scalar& reference, const RealScalar& epsilon = NumTraits<RealScalar>::dummy_precision()) {
return prune([&](const Scalar& val){ return !internal::isMuchSmallerThan(val, reference, epsilon); });
}
/**
* \brief Prunes the entries of the vector based on a `predicate`
* \tparam F Type of the predicate.
* \param keep_predicate The predicate that is used to test whether a value should be kept. A callable that
* gets passed om a `Scalar` value and returns a boolean. If the predicate returns true, the value is kept.
* \return The new number of structural non-zeros.
*/
template<class F>
Index prune(F&& keep_predicate)
{
m_data.prune(reference,epsilon);
Index k = 0;
Index n = m_data.size();
for (Index i = 0; i < n; ++i)
{
if (keep_predicate(m_data.value(i)))
{
m_data.value(k) = std::move(m_data.value(i));
m_data.index(k) = m_data.index(i);
++k;
}
}
m_data.resize(k);
return k;
}
/** Resizes the sparse vector to \a rows x \a cols