11#ifndef EIGEN_PARTIAL_REDUX_H
12#define EIGEN_PARTIAL_REDUX_H
15#include "./InternalHeaderCheck.h"
35template <
typename MatrixType,
typename MemberOp,
int Direction>
36class PartialReduxExpr;
39template <
typename MatrixType,
typename MemberOp,
int Direction>
40struct traits<PartialReduxExpr<MatrixType, MemberOp, Direction> > : traits<MatrixType> {
41 typedef typename MemberOp::result_type Scalar;
42 typedef typename traits<MatrixType>::StorageKind StorageKind;
43 typedef typename traits<MatrixType>::XprKind XprKind;
44 typedef typename MatrixType::Scalar InputScalar;
46 RowsAtCompileTime = Direction ==
Vertical ? 1 : MatrixType::RowsAtCompileTime,
47 ColsAtCompileTime = Direction ==
Horizontal ? 1 : MatrixType::ColsAtCompileTime,
48 MaxRowsAtCompileTime = Direction ==
Vertical ? 1 : MatrixType::MaxRowsAtCompileTime,
49 MaxColsAtCompileTime = Direction ==
Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
51 TraversalSize = Direction ==
Vertical ? MatrixType::RowsAtCompileTime : MatrixType::ColsAtCompileTime
56template <
typename MatrixType,
typename MemberOp,
int Direction>
57class PartialReduxExpr :
public internal::dense_xpr_base<PartialReduxExpr<MatrixType, MemberOp, Direction> >::type,
58 internal::no_assignment_operator {
60 typedef typename internal::dense_xpr_base<PartialReduxExpr>::type Base;
63 EIGEN_DEVICE_FUNC
explicit PartialReduxExpr(
const MatrixType& mat,
const MemberOp& func = MemberOp())
64 : m_matrix(mat), m_functor(func) {}
66 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
Index rows()
const EIGEN_NOEXCEPT {
67 return (Direction ==
Vertical ? 1 : m_matrix.rows());
69 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
Index cols()
const EIGEN_NOEXCEPT {
70 return (Direction ==
Horizontal ? 1 : m_matrix.cols());
73 EIGEN_DEVICE_FUNC
typename MatrixType::Nested nestedExpression()
const {
return m_matrix; }
75 EIGEN_DEVICE_FUNC
const MemberOp& functor()
const {
return m_functor; }
78 typename MatrixType::Nested m_matrix;
79 const MemberOp m_functor;
82template <
typename A,
typename B>
83struct partial_redux_dummy_func;
85#define EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(MEMBER, COST, VECTORIZABLE, BINARYOP) \
86 template <typename ResultType, typename Scalar> \
87 struct member_##MEMBER { \
88 typedef ResultType result_type; \
89 typedef BINARYOP<Scalar, Scalar> BinaryOp; \
92 enum { value = COST }; \
94 enum { Vectorizable = VECTORIZABLE }; \
95 template <typename XprType> \
96 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ResultType operator()(const XprType& mat) const { \
97 return mat.MEMBER(); \
99 BinaryOp binaryFunc() const { return BinaryOp(); } \
102#define EIGEN_MEMBER_FUNCTOR(MEMBER, COST) EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(MEMBER, COST, 0, partial_redux_dummy_func)
109EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size - 1) * functor_traits<scalar_hypot_op<Scalar> >::Cost);
119template <
int p,
typename ResultType,
typename Scalar>
120struct member_lpnorm {
121 typedef ResultType result_type;
122 enum { Vectorizable = 0 };
125 enum { value = (Size + 5) * NumTraits<Scalar>::MulCost + (Size - 1) * NumTraits<Scalar>::AddCost };
127 EIGEN_DEVICE_FUNC member_lpnorm() {}
128 template <
typename XprType>
129 EIGEN_DEVICE_FUNC
inline ResultType operator()(
const XprType& mat)
const {
130 return mat.template lpNorm<p>();
134template <
typename BinaryOpT,
typename Scalar>
136 typedef BinaryOpT BinaryOp;
137 typedef typename result_of<BinaryOp(
const Scalar&,
const Scalar&)>::type result_type;
139 enum { Vectorizable = functor_traits<BinaryOp>::PacketAccess };
142 enum { value = (Size - 1) * functor_traits<BinaryOp>::Cost };
144 EIGEN_DEVICE_FUNC
explicit member_redux(
const BinaryOp func) : m_functor(func) {}
145 template <
typename Derived>
146 EIGEN_DEVICE_FUNC
inline result_type operator()(
const DenseBase<Derived>& mat)
const {
147 return mat.redux(m_functor);
149 const BinaryOp& binaryFunc()
const {
return m_functor; }
150 const BinaryOp m_functor;
191template <
typename ExpressionType,
int Direction>
194 typedef typename ExpressionType::Scalar Scalar;
195 typedef typename ExpressionType::RealScalar RealScalar;
197 typedef typename internal::ref_selector<ExpressionType>::non_const_type ExpressionTypeNested;
198 typedef internal::remove_all_t<ExpressionTypeNested> ExpressionTypeNestedCleaned;
200 template <
template <
typename OutScalar,
typename InputScalar>
class Functor,
typename ReturnScalar = Scalar>
205 template <
typename BinaryOp>
206 struct ReduxReturnType {
210 enum { isVertical = (Direction ==
Vertical) ? 1 : 0, isHorizontal = (Direction ==
Horizontal) ? 1 : 0 };
213 template <
typename OtherDerived>
214 struct ExtendedType {
215 typedef Replicate<OtherDerived, isVertical ? 1 : ExpressionType::RowsAtCompileTime,
216 isHorizontal ? 1 : ExpressionType::ColsAtCompileTime>
222 template <
typename OtherDerived>
223 EIGEN_DEVICE_FUNC
typename ExtendedType<OtherDerived>::Type extendedTo(
const DenseBase<OtherDerived>& other)
const {
224 EIGEN_STATIC_ASSERT(internal::check_implication(isVertical, OtherDerived::MaxColsAtCompileTime == 1),
225 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
226 EIGEN_STATIC_ASSERT(internal::check_implication(isHorizontal, OtherDerived::MaxRowsAtCompileTime == 1),
227 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
228 return typename ExtendedType<OtherDerived>::Type(other.derived(), isVertical ? 1 : m_matrix.rows(),
229 isHorizontal ? 1 : m_matrix.cols());
232 template <
typename OtherDerived>
233 struct OppositeExtendedType {
234 typedef Replicate<OtherDerived, isHorizontal ? 1 : ExpressionType::RowsAtCompileTime,
235 isVertical ? 1 : ExpressionType::ColsAtCompileTime>
241 template <
typename OtherDerived>
242 EIGEN_DEVICE_FUNC
typename OppositeExtendedType<OtherDerived>::Type extendedToOpposite(
243 const DenseBase<OtherDerived>& other)
const {
244 EIGEN_STATIC_ASSERT(internal::check_implication(isHorizontal, OtherDerived::MaxColsAtCompileTime == 1),
245 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
246 EIGEN_STATIC_ASSERT(internal::check_implication(isVertical, OtherDerived::MaxRowsAtCompileTime == 1),
247 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
248 return typename OppositeExtendedType<OtherDerived>::Type(other.derived(), isHorizontal ? 1 : m_matrix.rows(),
249 isVertical ? 1 : m_matrix.cols());
253 EIGEN_DEVICE_FUNC
explicit inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
256 EIGEN_DEVICE_FUNC
inline const ExpressionType& _expression()
const {
return m_matrix; }
258#ifdef EIGEN_PARSED_BY_DOXYGEN
268 typedef internal::subvector_stl_reverse_iterator<ExpressionType,
DirectionType(Direction)> reverse_iterator;
269 typedef internal::subvector_stl_reverse_iterator<
const ExpressionType,
DirectionType(Direction)>
270 const_reverse_iterator;
286 return reverse_iterator(m_matrix, m_matrix.template subVectors<
DirectionType(Direction)>() - 1);
290 return const_reverse_iterator(m_matrix, m_matrix.template subVectors<
DirectionType(Direction)>() - 1);
294 return const_reverse_iterator(m_matrix, m_matrix.template subVectors<
DirectionType(Direction)>() - 1);
313 reverse_iterator
rend() {
return reverse_iterator(m_matrix, -1); }
315 const_reverse_iterator
rend()
const {
return const_reverse_iterator(m_matrix, -1); }
317 const_reverse_iterator
crend()
const {
return const_reverse_iterator(m_matrix, -1); }
329 template <
typename BinaryOp>
331 eigen_assert(redux_length() > 0 &&
"you are using an empty matrix");
335 typedef typename ReturnType<internal::member_minCoeff>::Type MinCoeffReturnType;
336 typedef typename ReturnType<internal::member_maxCoeff>::Type MaxCoeffReturnType;
338 internal::member_sum<RealScalar, RealScalar>, Direction>
339 SquaredNormReturnType;
341 typedef typename ReturnType<internal::member_blueNorm, RealScalar>::Type BlueNormReturnType;
342 typedef typename ReturnType<internal::member_stableNorm, RealScalar>::Type StableNormReturnType;
343 typedef typename ReturnType<internal::member_hypotNorm, RealScalar>::Type HypotNormReturnType;
344 typedef typename ReturnType<internal::member_sum>::Type SumReturnType;
345 typedef EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(SumReturnType, Scalar, quotient) MeanReturnType;
346 typedef typename ReturnType<internal::member_all, bool>::Type AllReturnType;
347 typedef typename ReturnType<internal::member_any, bool>::Type AnyReturnType;
349 typedef typename ReturnType<internal::member_prod>::Type ProdReturnType;
354 struct LpNormReturnType {
371 eigen_assert(redux_length() > 0 &&
"you are using an empty matrix");
388 eigen_assert(redux_length() > 0 &&
"you are using an empty matrix");
464 EIGEN_DEVICE_FUNC
const MeanReturnType
mean()
const {
465 return sum() / Scalar(Direction ==
Vertical ? m_matrix.rows() : m_matrix.cols());
517 typedef Replicate<ExpressionType, (isVertical ?
Dynamic : 1), (isHorizontal ?
Dynamic : 1)> ReplicateReturnType;
518 EIGEN_DEVICE_FUNC
const ReplicateReturnType
replicate(
Index factor)
const;
530 template <
int Factor>
531 const Replicate<ExpressionType, isVertical * Factor + isHorizontal,
532 isHorizontal * Factor + isVertical> EIGEN_DEVICE_FUNC
535 _expression(), isVertical ? factor : 1, isHorizontal ? factor : 1);
541 template <
typename OtherDerived>
543 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
544 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
546 return m_matrix = extendedTo(other.
derived());
550 template <
typename OtherDerived>
552 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
553 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
554 return m_matrix += extendedTo(other.
derived());
558 template <
typename OtherDerived>
560 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
561 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
562 return m_matrix -= extendedTo(other.
derived());
566 template <
typename OtherDerived>
568 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
569 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
570 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
571 m_matrix *= extendedTo(other.
derived());
576 template <
typename OtherDerived>
578 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
579 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
580 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
581 m_matrix /= extendedTo(other.
derived());
586 template <
typename OtherDerived>
587 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
589 const typename ExtendedType<OtherDerived>::Type>
591 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
592 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
593 return m_matrix + extendedTo(other.
derived());
597 template <
typename OtherDerived>
599 const ExpressionTypeNestedCleaned,
const typename ExtendedType<OtherDerived>::Type>
601 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
602 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
603 return m_matrix - extendedTo(other.
derived());
608 template <
typename OtherDerived>
609 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
611 const typename ExtendedType<OtherDerived>::Type> EIGEN_DEVICE_FUNC
613 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
614 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
615 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
616 return m_matrix * extendedTo(other.
derived());
621 template <
typename OtherDerived>
623 const typename ExtendedType<OtherDerived>::Type>
625 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
626 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
627 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
628 return m_matrix / extendedTo(other.
derived());
636 const typename OppositeExtendedType<NormReturnType>::Type>
638 return m_matrix.cwiseQuotient(extendedToOpposite(this->
norm()));
651 EIGEN_DEVICE_FUNC HomogeneousReturnType
homogeneous()
const;
653 typedef typename ExpressionType::PlainObject CrossReturnType;
654 template <
typename OtherDerived>
658 HNormalized_Size = Direction ==
Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
659 : internal::traits<ExpressionType>::ColsAtCompileTime,
660 HNormalized_SizeMinusOne = HNormalized_Size ==
Dynamic ?
Dynamic : HNormalized_Size - 1
662 typedef Block<
const ExpressionType,
663 Direction ==
Vertical ? int(HNormalized_SizeMinusOne)
664 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
665 Direction ==
Horizontal ? int(HNormalized_SizeMinusOne)
666 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
668 typedef Block<
const ExpressionType,
669 Direction ==
Vertical ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
670 Direction ==
Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
672 typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
673 const HNormalized_Block,
674 const Replicate<HNormalized_Factors, Direction ==
Vertical ? HNormalized_SizeMinusOne : 1,
675 Direction ==
Horizontal ? HNormalized_SizeMinusOne : 1> >
676 HNormalizedReturnType;
678 EIGEN_DEVICE_FUNC
const HNormalizedReturnType
hnormalized()
const;
680#ifdef EIGEN_VECTORWISEOP_PLUGIN
681#include EIGEN_VECTORWISEOP_PLUGIN
685 EIGEN_DEVICE_FUNC Index redux_length()
const {
return Direction ==
Vertical ? m_matrix.rows() : m_matrix.cols(); }
686 ExpressionTypeNested m_matrix;
695template <
typename Derived>
706template <
typename Derived>
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:79
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition CwiseUnaryOp.h:53
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:44
ConstColwiseReturnType colwise() const
Definition DenseBase.h:511
ConstRowwiseReturnType rowwise() const
Definition DenseBase.h:501
Derived & derived()
Definition EigenBase.h:49
Expression of one (or a set of) homogeneous vector(s)
Definition Homogeneous.h:62
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
Generic expression of a partially reduxed matrix.
Definition VectorwiseOp.h:58
Expression of the multiple replication of a matrix or vector.
Definition Replicate.h:64
Expression of the reverse of a vector or matrix.
Definition Reverse.h:65
Pseudo expression providing broadcasting and partial reduction operations.
Definition VectorwiseOp.h:192
const HypotNormReturnType hypotNorm() const
Definition VectorwiseOp.h:449
const SquaredNormReturnType squaredNorm() const
Definition VectorwiseOp.h:400
const Replicate< ExpressionType, isVertical *Factor+isHorizontal, isHorizontal *Factor+isVertical > replicate(Index factor=Factor) const
Definition VectorwiseOp.h:533
const ProdReturnType prod() const
Definition VectorwiseOp.h:500
CwiseBinaryOp< internal::scalar_product_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator*(const DenseBase< OtherDerived > &other) const
Definition VectorwiseOp.h:612
CwiseBinaryOp< internal::scalar_difference_op< Scalar, typename OtherDerived::Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator-(const DenseBase< OtherDerived > &other) const
Definition VectorwiseOp.h:600
const_reverse_iterator crend() const
Definition VectorwiseOp.h:317
ExpressionType & operator-=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:559
CwiseBinaryOp< internal::scalar_sum_op< Scalar, typename OtherDerived::Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator+(const DenseBase< OtherDerived > &other) const
Definition VectorwiseOp.h:590
Eigen::Index Index
Definition VectorwiseOp.h:196
const BlueNormReturnType blueNorm() const
Definition VectorwiseOp.h:433
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename OppositeExtendedType< NormReturnType >::Type > normalized() const
Definition VectorwiseOp.h:637
ExpressionType & operator/=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:577
const_iterator cend() const
Definition VectorwiseOp.h:306
random_access_iterator_type const_iterator
Definition VectorwiseOp.h:264
const CountReturnType count() const
Definition VectorwiseOp.h:491
const ReplicateReturnType replicate(Index factor) const
Definition Replicate.h:123
const MaxCoeffReturnType maxCoeff() const
Definition VectorwiseOp.h:387
const SumReturnType sum() const
Definition VectorwiseOp.h:458
ExpressionType & operator*=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:567
const_iterator cbegin() const
Definition VectorwiseOp.h:280
const MinCoeffReturnType minCoeff() const
Definition VectorwiseOp.h:370
reverse_iterator rbegin()
Definition VectorwiseOp.h:285
const AnyReturnType any() const
Definition VectorwiseOp.h:480
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator/(const DenseBase< OtherDerived > &other) const
Definition VectorwiseOp.h:624
const AllReturnType all() const
Definition VectorwiseOp.h:473
iterator end()
Definition VectorwiseOp.h:300
random_access_iterator_type iterator
Definition VectorwiseOp.h:262
const_reverse_iterator rbegin() const
Definition VectorwiseOp.h:289
reverse_iterator rend()
Definition VectorwiseOp.h:313
const MeanReturnType mean() const
Definition VectorwiseOp.h:464
const_iterator begin() const
Definition VectorwiseOp.h:278
ExpressionType & operator=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:542
ReverseReturnType reverse()
Definition VectorwiseOp.h:515
void reverseInPlace()
Definition Reverse.h:190
iterator begin()
Definition VectorwiseOp.h:276
ExpressionType & operator+=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:551
const_iterator end() const
Definition VectorwiseOp.h:302
void normalize()
Definition VectorwiseOp.h:644
const StableNormReturnType stableNorm() const
Definition VectorwiseOp.h:441
const LpNormReturnType< p >::Type lpNorm() const
Definition VectorwiseOp.h:423
const ConstReverseReturnType reverse() const
Definition VectorwiseOp.h:509
const_reverse_iterator crbegin() const
Definition VectorwiseOp.h:293
const NormReturnType norm() const
Definition VectorwiseOp.h:412
const ReduxReturnType< BinaryOp >::Type redux(const BinaryOp &func=BinaryOp()) const
Definition VectorwiseOp.h:330
const_reverse_iterator rend() const
Definition VectorwiseOp.h:315
const HNormalizedReturnType hnormalized() const
column or row-wise homogeneous normalization
Definition Homogeneous.h:191
HomogeneousReturnType homogeneous() const
Definition Homogeneous.h:143
DirectionType
Definition Constants.h:263
@ Horizontal
Definition Constants.h:269
@ Vertical
Definition Constants.h:266
const unsigned int RowMajorBit
Definition Constants.h:70
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
const int Dynamic
Definition Constants.h:25
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:523