From 56c7e164f026c2a07ca6d4d3d81fcce2fd0c570e Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Sat, 24 Jan 2009 15:22:44 +0000 Subject: [PATCH] add partial count redux (adapted patch from Ricard Marxer) --- Eigen/src/Array/PartialRedux.h | 13 ++++++++++++- doc/TutorialSparse.dox | 1 - doc/snippets/PartialRedux_count.cpp | 3 +++ test/array.cpp | 5 +++++ 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 doc/snippets/PartialRedux_count.cpp diff --git a/Eigen/src/Array/PartialRedux.h b/Eigen/src/Array/PartialRedux.h index 6ce72291e..96c13ca2b 100644 --- a/Eigen/src/Array/PartialRedux.h +++ b/Eigen/src/Array/PartialRedux.h @@ -114,6 +114,7 @@ EIGEN_MEMBER_FUNCTOR(minCoeff, (Size-1)*NumTraits::AddCost); EIGEN_MEMBER_FUNCTOR(maxCoeff, (Size-1)*NumTraits::AddCost); EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits::AddCost); EIGEN_MEMBER_FUNCTOR(any, (Size-1)*NumTraits::AddCost); +EIGEN_MEMBER_FUNCTOR(count, (Size-1)*NumTraits::AddCost); /** \internal */ template @@ -173,7 +174,7 @@ template class PartialRedux }; typedef typename ExpressionType::PlainMatrixType CrossReturnType; - + inline PartialRedux(const ExpressionType& matrix) : m_matrix(matrix) {} /** \internal */ @@ -246,6 +247,16 @@ template class PartialRedux * \sa MatrixBase::any() */ const typename ReturnType::Type any() const { return _expression(); } + + /** \returns a row (or column) vector expression representing + * the number of \c true coefficients of each respective column (or row). + * + * Example: \include PartialRedux_count.cpp + * Output: \verbinclude PartialRedux_count.out + * + * \sa MatrixBase::count() */ + const PartialReduxExpr, Direction> count() const + { return _expression(); } /** \returns a 3x3 matrix expression of the cross product * of each column or row of the referenced expression with the \a other vector. diff --git a/doc/TutorialSparse.dox b/doc/TutorialSparse.dox index d9d5e9e0c..a8bfe006e 100644 --- a/doc/TutorialSparse.dox +++ b/doc/TutorialSparse.dox @@ -21,7 +21,6 @@ namespace Eigen { In many applications (e.g., finite element methods) it is common to deal with very large matrices where only a few coefficients are different than zero. Both in term of memory consumption and performance, it is fundamental to use an adequate representation storing only nonzero coefficients. Such a matrix is called a sparse matrix. - \b Declaring \b sparse \b matrices \b and \b vectors \n The SparseMatrix class is the main sparse matrix representation of the Eigen's sparse module which offers high performance, low memory usage, and compatibility with most of sparse linear algebra packages. Because of its limited flexibility, we also provide a DynamicSparseMatrix variante taillored for low-level sparse matrix assembly. Both of them can be either row major or column major: diff --git a/doc/snippets/PartialRedux_count.cpp b/doc/snippets/PartialRedux_count.cpp new file mode 100644 index 000000000..914a5dee3 --- /dev/null +++ b/doc/snippets/PartialRedux_count.cpp @@ -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; diff --git a/test/array.cpp b/test/array.cpp index e1047489d..ea397388d 100644 --- a/test/array.cpp +++ b/test/array.cpp @@ -111,6 +111,11 @@ template void comparisons(const MatrixType& m) .select(m1,0), m3); // even shorter version: VERIFY_IS_APPROX( (m1.cwise().abs().cwise()0.5).count() == rows*cols); + VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>0.5).colwise().count(), RowVectorXi::Constant(cols,rows)); + VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>0.5).rowwise().count(), VectorXi::Constant(rows, cols)); } template void lpNorm(const VectorType& v)