[eigen] [PATCH] ParialRedux count()

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hi,

I have added count() to PartialRedux.  Attached is the patch which also includes a snippet for the doc.

When telling the cost of the operator I have simplified a bit and implied that the casts to bool and int (which are in the count() method in BooleanRedux.h) are always necessary, I think this needs reviews.

PS: if you think of other easy tasks i could tackle, I would be happy to do it, that way I will get more familiarized with Eigen internals.

--
ricard
http://www.ricardmarxer.com
http://www.caligraft.com
Index: doc/snippets/PartialRedux_count.cpp
===================================================================
--- doc/snippets/PartialRedux_count.cpp	(revision 0)
+++ doc/snippets/PartialRedux_count.cpp	(revision 0)
@@ -0,0 +1,3 @@
+Matrix3d m = Matrix3d::Random();
+cout << "Here is the matrix m:" << endl << m << endl;
+cout << "Here is the count of elements larger or equal than 0.5 of each row:" << endl << (m.cwise() >= 0.5).rowwise().count() << endl;
Index: Eigen/src/Array/PartialRedux.h
===================================================================
--- Eigen/src/Array/PartialRedux.h	(revision 915940)
+++ Eigen/src/Array/PartialRedux.h	(working copy)
@@ -107,6 +107,17 @@
     { return mat.MEMBER(); }                                        \
   }
 
+#define EIGEN_MEMBER_FUNCTOR_RETURNTYPE(MEMBER,COST,RETURNTYPE)     \
+  template <typename ResultType>                                    \
+  struct ei_member_##MEMBER EIGEN_EMPTY_STRUCT {                    \
+    typedef RETURNTYPE result_type;                                 \
+    template<typename Scalar, int Size> struct Cost                 \
+    { enum { value = COST }; };                                     \
+    template<typename Derived>                                      \
+    inline result_type operator()(const MatrixBase<Derived>& mat) const     \
+    { return mat.MEMBER(); }                                        \
+  }
+
 EIGEN_MEMBER_FUNCTOR(squaredNorm, Size * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
 EIGEN_MEMBER_FUNCTOR(norm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
 EIGEN_MEMBER_FUNCTOR(sum, (Size-1)*NumTraits<Scalar>::AddCost);
@@ -114,6 +125,7 @@
 EIGEN_MEMBER_FUNCTOR(maxCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
 EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits<Scalar>::AddCost);
 EIGEN_MEMBER_FUNCTOR(any, (Size-1)*NumTraits<Scalar>::AddCost);
+EIGEN_MEMBER_FUNCTOR_RETURNTYPE(count, (Size-1)*NumTraits<int>::AddCost*NumTraits<bool>::AddCost*NumTraits<int>::AddCost, int);
 
 /** \internal */
 template <typename BinaryOp, typename Scalar>
@@ -233,6 +245,16 @@
     const typename ReturnType<ei_member_sum>::Type sum() const
     { return _expression(); }
 
+    /** \returns a row (or column) vector expression of the count
+      * of each column (or row) of the referenced expression.
+      *
+      * Example: \include PartialRedux_count.cpp
+      * Output: \verbinclude PartialRedux_count.out
+      *
+      * \sa MatrixBase::count() */
+    const typename ReturnType<ei_member_count>::Type count() const
+    { return _expression(); }
+
     /** \returns a row (or column) vector expression representing
       * whether \b all coefficients of each respective column (or row) are \c true.
       *


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/