aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/SparseCore/SparseMatrix.h
diff options
context:
space:
mode:
authorGravatar Calixte Denizet <calixte.denizet@gmail.com>2015-10-06 13:29:41 +0200
committerGravatar Calixte Denizet <calixte.denizet@gmail.com>2015-10-06 13:29:41 +0200
commitb9d81c915009e08a2397a2fc2d36a15d16b3b32f (patch)
treef904d2453348859cec8468afe4aa1439ac56d99e /Eigen/src/SparseCore/SparseMatrix.h
parent9acfc7c4f34ba9f9f8b2d58380732706642dcc25 (diff)
Add a functor to setFromTriplets to handle duplicated entries
Diffstat (limited to 'Eigen/src/SparseCore/SparseMatrix.h')
-rw-r--r--Eigen/src/SparseCore/SparseMatrix.h25
1 files changed, 21 insertions, 4 deletions
diff --git a/Eigen/src/SparseCore/SparseMatrix.h b/Eigen/src/SparseCore/SparseMatrix.h
index c8c31fd83..22a6bd803 100644
--- a/Eigen/src/SparseCore/SparseMatrix.h
+++ b/Eigen/src/SparseCore/SparseMatrix.h
@@ -437,6 +437,10 @@ class SparseMatrix
template<typename InputIterators>
void setFromTriplets(const InputIterators& begin, const InputIterators& end);
+ template<typename DupFunctor, typename InputIterators>
+ void setFromTriplets(const InputIterators& begin, const InputIterators& end);
+
+ template<typename DupFunctor>
void sumupDuplicates();
//---
@@ -889,7 +893,7 @@ private:
namespace internal {
-template<typename InputIterator, typename SparseMatrixType>
+template<typename InputIterator, typename SparseMatrixType, typename DupFunctor>
void set_from_triplets(const InputIterator& begin, const InputIterator& end, SparseMatrixType& mat, int Options = 0)
{
EIGEN_UNUSED_VARIABLE(Options);
@@ -915,7 +919,7 @@ void set_from_triplets(const InputIterator& begin, const InputIterator& end, Spa
trMat.insertBackUncompressed(it->row(),it->col()) = it->value();
// pass 3:
- trMat.sumupDuplicates();
+ trMat.template sumupDuplicates<DupFunctor>();
}
// pass 4: transposed copy -> implicit sorting
@@ -966,11 +970,24 @@ template<typename Scalar, int _Options, typename _Index>
template<typename InputIterators>
void SparseMatrix<Scalar,_Options,_Index>::setFromTriplets(const InputIterators& begin, const InputIterators& end)
{
- internal::set_from_triplets(begin, end, *this);
+ internal::set_from_triplets<InputIterators, SparseMatrix<Scalar,_Options,_Index>, internal::scalar_sum_op<Scalar> >(begin, end, *this);
+}
+
+/** The same as setFromTriplets but when duplicates are met the functor \a DupFunctor is applied:
+ * \code
+ * value = DupFunctor()(OldValue, NewValue)
+ * \endcode
+ */
+template<typename Scalar, int _Options, typename _Index>
+template<typename DupFunctor, typename InputIterators>
+void SparseMatrix<Scalar,_Options,_Index>::setFromTriplets(const InputIterators& begin, const InputIterators& end)
+{
+ internal::set_from_triplets<InputIterators, SparseMatrix<Scalar,_Options,_Index>, DupFunctor>(begin, end, *this);
}
/** \internal */
template<typename Scalar, int _Options, typename _Index>
+template<typename DupFunctor>
void SparseMatrix<Scalar,_Options,_Index>::sumupDuplicates()
{
eigen_assert(!isCompressed());
@@ -989,7 +1006,7 @@ void SparseMatrix<Scalar,_Options,_Index>::sumupDuplicates()
if(wi(i)>=start)
{
// we already meet this entry => accumulate it
- m_data.value(wi(i)) += m_data.value(k);
+ m_data.value(wi(i)) = DupFunctor()(m_data.value(wi(i)), m_data.value(k));
}
else
{