diff --git a/Eigen/src/Array/VectorwiseOp.h b/Eigen/src/Array/VectorwiseOp.h index 4cb0083fa..6f6c2c8a7 100644 --- a/Eigen/src/Array/VectorwiseOp.h +++ b/Eigen/src/Array/VectorwiseOp.h @@ -122,6 +122,7 @@ class PartialReduxExpr : ei_no_assignment_operator, EIGEN_MEMBER_FUNCTOR(squaredNorm, Size * NumTraits::MulCost + (Size-1)*NumTraits::AddCost); EIGEN_MEMBER_FUNCTOR(norm, (Size+5) * NumTraits::MulCost + (Size-1)*NumTraits::AddCost); EIGEN_MEMBER_FUNCTOR(sum, (Size-1)*NumTraits::AddCost); +EIGEN_MEMBER_FUNCTOR(mean, (Size-1)*NumTraits::AddCost); EIGEN_MEMBER_FUNCTOR(minCoeff, (Size-1)*NumTraits::AddCost); EIGEN_MEMBER_FUNCTOR(maxCoeff, (Size-1)*NumTraits::AddCost); EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits::AddCost); @@ -297,6 +298,13 @@ template class VectorwiseOp const typename ReturnType::Type sum() const { return _expression(); } + /** \returns a row (or column) vector expression of the mean + * of each column (or row) of the referenced expression. + * + * \sa MatrixBase::mean() */ + const typename ReturnType::Type mean() const + { return _expression(); } + /** \returns a row (or column) vector expression representing * whether \b all coefficients of each respective column (or row) are \c true. * diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 2691fdecd..cb1589f85 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -645,8 +645,9 @@ template class MatrixBase const CwiseBinaryOp binaryExpr(const MatrixBase &other, const CustomBinaryOp& func = CustomBinaryOp()) const; - + Scalar sum() const; + Scalar mean() const; Scalar trace() const; Scalar prod() const; diff --git a/Eigen/src/Core/Redux.h b/Eigen/src/Core/Redux.h index 0df095750..9f796157a 100644 --- a/Eigen/src/Core/Redux.h +++ b/Eigen/src/Core/Redux.h @@ -342,7 +342,7 @@ MatrixBase::maxCoeff() const /** \returns the sum of all coefficients of *this * - * \sa trace(), prod() + * \sa trace(), prod(), mean() */ template EIGEN_STRONG_INLINE typename ei_traits::Scalar @@ -351,12 +351,23 @@ MatrixBase::sum() const return this->redux(Eigen::ei_scalar_sum_op()); } +/** \returns the mean of all coefficients of *this +* +* \sa trace(), prod(), sum() +*/ +template +EIGEN_STRONG_INLINE typename ei_traits::Scalar +MatrixBase::mean() const +{ + return this->redux(Eigen::ei_scalar_sum_op()) / this->size(); +} + /** \returns the product of all coefficients of *this * * Example: \include MatrixBase_prod.cpp * Output: \verbinclude MatrixBase_prod.out * - * \sa sum() + * \sa sum(), mean(), trace() */ template EIGEN_STRONG_INLINE typename ei_traits::Scalar