11 #ifndef EIGEN_PARTIAL_REDUX_H
12 #define EIGEN_PARTIAL_REDUX_H
14 #include "./InternalHeaderCheck.h"
34 template<
typename MatrixType,
typename MemberOp,
int Direction>
35 class PartialReduxExpr;
38 template<
typename MatrixType,
typename MemberOp,
int Direction>
39 struct traits<PartialReduxExpr<MatrixType, MemberOp, Direction> >
42 typedef typename MemberOp::result_type Scalar;
43 typedef typename traits<MatrixType>::StorageKind StorageKind;
44 typedef typename traits<MatrixType>::XprKind XprKind;
45 typedef typename MatrixType::Scalar InputScalar;
47 RowsAtCompileTime = Direction==
Vertical ? 1 : MatrixType::RowsAtCompileTime,
48 ColsAtCompileTime = Direction==
Horizontal ? 1 : MatrixType::ColsAtCompileTime,
49 MaxRowsAtCompileTime = Direction==
Vertical ? 1 : MatrixType::MaxRowsAtCompileTime,
50 MaxColsAtCompileTime = Direction==
Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
52 TraversalSize = Direction==
Vertical ? MatrixType::RowsAtCompileTime : MatrixType::ColsAtCompileTime
57 template<
typename MatrixType,
typename MemberOp,
int Direction>
58 class PartialReduxExpr :
public internal::dense_xpr_base< PartialReduxExpr<MatrixType, MemberOp, Direction> >::type,
59 internal::no_assignment_operator
63 typedef typename internal::dense_xpr_base<PartialReduxExpr>::type Base;
67 explicit PartialReduxExpr(
const MatrixType& mat,
const MemberOp& func = MemberOp())
68 : m_matrix(mat), m_functor(func) {}
70 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
71 Index rows()
const EIGEN_NOEXCEPT {
return (Direction==
Vertical ? 1 : m_matrix.rows()); }
72 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
73 Index cols()
const EIGEN_NOEXCEPT {
return (Direction==
Horizontal ? 1 : m_matrix.cols()); }
76 typename MatrixType::Nested nestedExpression()
const {
return m_matrix; }
79 const MemberOp& functor()
const {
return m_functor; }
82 typename MatrixType::Nested m_matrix;
83 const MemberOp m_functor;
86 template<
typename A,
typename B>
struct partial_redux_dummy_func;
88 #define EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(MEMBER,COST,VECTORIZABLE,BINARYOP) \
89 template <typename ResultType,typename Scalar> \
90 struct member_##MEMBER { \
91 typedef ResultType result_type; \
92 typedef BINARYOP<Scalar,Scalar> BinaryOp; \
93 template<int Size> struct Cost { enum { value = COST }; }; \
94 enum { Vectorizable = VECTORIZABLE }; \
95 template<typename XprType> \
96 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE \
97 ResultType operator()(const XprType& mat) const \
98 { return mat.MEMBER(); } \
99 BinaryOp binaryFunc() const { return BinaryOp(); } \
102 #define EIGEN_MEMBER_FUNCTOR(MEMBER,COST) \
103 EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(MEMBER,COST,0,partial_redux_dummy_func)
110 EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size-1) * functor_traits<scalar_hypot_op<Scalar> >::Cost );
120 template <
int p,
typename ResultType,
typename Scalar>
121 struct member_lpnorm {
122 typedef ResultType result_type;
123 enum { Vectorizable = 0 };
124 template<
int Size>
struct Cost
125 {
enum { value = (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost }; };
126 EIGEN_DEVICE_FUNC member_lpnorm() {}
127 template<
typename XprType>
128 EIGEN_DEVICE_FUNC
inline ResultType operator()(
const XprType& mat)
const
129 {
return mat.template lpNorm<p>(); }
132 template <
typename BinaryOpT,
typename Scalar>
133 struct member_redux {
134 typedef BinaryOpT BinaryOp;
135 typedef typename result_of<
136 BinaryOp(
const Scalar&,
const Scalar&)
139 enum { Vectorizable = functor_traits<BinaryOp>::PacketAccess };
140 template<
int Size>
struct Cost {
enum { value = (Size-1) * functor_traits<BinaryOp>::Cost }; };
141 EIGEN_DEVICE_FUNC
explicit member_redux(
const BinaryOp func) : m_functor(func) {}
142 template<
typename Derived>
143 EIGEN_DEVICE_FUNC
inline result_type operator()(
const DenseBase<Derived>& mat)
const
144 {
return mat.redux(m_functor); }
145 const BinaryOp& binaryFunc()
const {
return m_functor; }
146 const BinaryOp m_functor;
191 typedef typename ExpressionType::Scalar Scalar;
192 typedef typename ExpressionType::RealScalar RealScalar;
194 typedef typename internal::ref_selector<ExpressionType>::non_const_type ExpressionTypeNested;
195 typedef internal::remove_all_t<ExpressionTypeNested> ExpressionTypeNestedCleaned;
197 template<
template<
typename OutScalar,
typename InputScalar>
class Functor,
198 typename ReturnScalar=Scalar>
struct ReturnType
201 Functor<ReturnScalar,Scalar>,
206 template<
typename BinaryOp>
struct ReduxReturnType
209 internal::member_redux<BinaryOp,Scalar>,
215 isVertical = (Direction==
Vertical) ? 1 : 0,
216 isHorizontal = (Direction==
Horizontal) ? 1 : 0
221 template<
typename OtherDerived>
struct ExtendedType {
222 typedef Replicate<OtherDerived,
223 isVertical ? 1 : ExpressionType::RowsAtCompileTime,
224 isHorizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
229 template<
typename OtherDerived>
231 typename ExtendedType<OtherDerived>::Type
232 extendedTo(
const DenseBase<OtherDerived>& other)
const
234 EIGEN_STATIC_ASSERT(internal::check_implication(isVertical, OtherDerived::MaxColsAtCompileTime==1),
235 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
236 EIGEN_STATIC_ASSERT(internal::check_implication(isHorizontal, OtherDerived::MaxRowsAtCompileTime==1),
237 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
238 return typename ExtendedType<OtherDerived>::Type
240 isVertical ? 1 : m_matrix.rows(),
241 isHorizontal ? 1 : m_matrix.cols());
244 template<
typename OtherDerived>
struct OppositeExtendedType {
245 typedef Replicate<OtherDerived,
246 isHorizontal ? 1 : ExpressionType::RowsAtCompileTime,
247 isVertical ? 1 : ExpressionType::ColsAtCompileTime> Type;
252 template<
typename OtherDerived>
254 typename OppositeExtendedType<OtherDerived>::Type
255 extendedToOpposite(
const DenseBase<OtherDerived>& other)
const
257 EIGEN_STATIC_ASSERT(internal::check_implication(isHorizontal, OtherDerived::MaxColsAtCompileTime==1),
258 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
259 EIGEN_STATIC_ASSERT(internal::check_implication(isVertical, OtherDerived::MaxRowsAtCompileTime==1),
260 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
261 return typename OppositeExtendedType<OtherDerived>::Type
263 isHorizontal ? 1 : m_matrix.rows(),
264 isVertical ? 1 : m_matrix.cols());
269 explicit inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
273 inline const ExpressionType& _expression()
const {
return m_matrix; }
275 #ifdef EIGEN_PARSED_BY_DOXYGEN
285 typedef internal::subvector_stl_reverse_iterator<ExpressionType,
DirectionType(Direction)> reverse_iterator;
286 typedef internal::subvector_stl_reverse_iterator<
const ExpressionType,
DirectionType(Direction)> const_reverse_iterator;
301 reverse_iterator
rbegin() {
return reverse_iterator (m_matrix, m_matrix.template subVectors<
DirectionType(Direction)>()-1); }
303 const_reverse_iterator
rbegin()
const {
return const_reverse_iterator (m_matrix, m_matrix.template subVectors<
DirectionType(Direction)>()-1); }
305 const_reverse_iterator
crbegin()
const {
return const_reverse_iterator (m_matrix, m_matrix.template subVectors<
DirectionType(Direction)>()-1); }
319 reverse_iterator
rend() {
return reverse_iterator (m_matrix, -1); }
321 const_reverse_iterator
rend()
const {
return const_reverse_iterator (m_matrix, -1); }
323 const_reverse_iterator
crend()
const {
return const_reverse_iterator (m_matrix, -1); }
335 template<
typename BinaryOp>
337 const typename ReduxReturnType<BinaryOp>::Type
338 redux(
const BinaryOp& func = BinaryOp())
const
340 eigen_assert(redux_length()>0 &&
"you are using an empty matrix");
344 typedef typename ReturnType<internal::member_minCoeff>::Type MinCoeffReturnType;
345 typedef typename ReturnType<internal::member_maxCoeff>::Type MaxCoeffReturnType;
348 typedef typename ReturnType<internal::member_blueNorm,RealScalar>::Type BlueNormReturnType;
349 typedef typename ReturnType<internal::member_stableNorm,RealScalar>::Type StableNormReturnType;
350 typedef typename ReturnType<internal::member_hypotNorm,RealScalar>::Type HypotNormReturnType;
351 typedef typename ReturnType<internal::member_sum>::Type SumReturnType;
352 typedef EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(SumReturnType,Scalar,quotient) MeanReturnType;
353 typedef typename ReturnType<internal::member_all>::Type AllReturnType;
354 typedef typename ReturnType<internal::member_any>::Type AnyReturnType;
356 typedef typename ReturnType<internal::member_prod>::Type ProdReturnType;
360 template<
int p>
struct LpNormReturnType {
379 eigen_assert(redux_length()>0 &&
"you are using an empty matrix");
398 eigen_assert(redux_length()>0 &&
"you are using an empty matrix");
488 const MeanReturnType
mean()
const
489 {
return sum() / Scalar(Direction==
Vertical?m_matrix.rows():m_matrix.cols()); }
572 (_expression(),isVertical?factor:1,isHorizontal?factor:1);
578 template<
typename OtherDerived>
582 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
583 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
585 return m_matrix = extendedTo(other.
derived());
589 template<
typename OtherDerived>
593 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
594 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
595 return m_matrix += extendedTo(other.
derived());
599 template<
typename OtherDerived>
603 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
604 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
605 return m_matrix -= extendedTo(other.
derived());
609 template<
typename OtherDerived>
613 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
614 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
615 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
616 m_matrix *= extendedTo(other.
derived());
621 template<
typename OtherDerived>
625 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
626 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
627 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
628 m_matrix /= extendedTo(other.
derived());
633 template<
typename OtherDerived> EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
635 const ExpressionTypeNestedCleaned,
636 const typename ExtendedType<OtherDerived>::Type>
639 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
640 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
641 return m_matrix + extendedTo(other.
derived());
645 template<
typename OtherDerived>
648 const ExpressionTypeNestedCleaned,
649 const typename ExtendedType<OtherDerived>::Type>
652 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
653 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
654 return m_matrix - extendedTo(other.
derived());
659 template<
typename OtherDerived> EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
661 const ExpressionTypeNestedCleaned,
662 const typename ExtendedType<OtherDerived>::Type>
666 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
667 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
668 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
669 return m_matrix * extendedTo(other.
derived());
674 template<
typename OtherDerived>
677 const ExpressionTypeNestedCleaned,
678 const typename ExtendedType<OtherDerived>::Type>
681 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
682 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
683 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
684 return m_matrix / extendedTo(other.
derived());
693 const ExpressionTypeNestedCleaned,
694 const typename OppositeExtendedType<NormReturnType>::Type>
695 normalized()
const {
return m_matrix.cwiseQuotient(extendedToOpposite(this->
norm())); }
713 typedef typename ExpressionType::PlainObject CrossReturnType;
714 template<
typename OtherDerived>
719 HNormalized_Size = Direction==
Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
720 : internal::traits<ExpressionType>::ColsAtCompileTime,
721 HNormalized_SizeMinusOne = HNormalized_Size==
Dynamic ?
Dynamic : HNormalized_Size-1
723 typedef Block<
const ExpressionType,
724 Direction==
Vertical ? int(HNormalized_SizeMinusOne)
725 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
726 Direction==
Horizontal ? int(HNormalized_SizeMinusOne)
727 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
729 typedef Block<
const ExpressionType,
730 Direction==
Vertical ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
731 Direction==
Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
733 typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
734 const HNormalized_Block,
735 const Replicate<HNormalized_Factors,
736 Direction==
Vertical ? HNormalized_SizeMinusOne : 1,
737 Direction==
Horizontal ? HNormalized_SizeMinusOne : 1> >
738 HNormalizedReturnType;
743 # ifdef EIGEN_VECTORWISEOP_PLUGIN
744 # include EIGEN_VECTORWISEOP_PLUGIN
748 Index redux_length()
const
750 return Direction==
Vertical ? m_matrix.rows() : m_matrix.cols();
752 ExpressionTypeNested m_matrix;
762 template<
typename Derived>
763 EIGEN_DEVICE_FUNC
inline typename DenseBase<Derived>::ColwiseReturnType
776 template<
typename Derived>
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:86
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:58
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
ConstColwiseReturnType colwise() const
Definition: DenseBase.h:551
ConstRowwiseReturnType rowwise() const
Definition: DenseBase.h:539
Derived & derived()
Definition: EigenBase.h:48
Expression of one (or a set of) homogeneous vector(s)
Definition: Homogeneous.h:64
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Generic expression of a partially reduxed matrix.
Definition: VectorwiseOp.h:60
Expression of the multiple replication of a matrix or vector.
Definition: Replicate.h:65
Expression of the reverse of a vector or matrix.
Definition: Reverse.h:67
Pseudo expression providing broadcasting and partial reduction operations.
Definition: VectorwiseOp.h:188
const HypotNormReturnType hypotNorm() const
Definition: VectorwiseOp.h:469
const SquaredNormReturnType squaredNorm() const
Definition: VectorwiseOp.h:411
const ProdReturnType prod() const
Definition: VectorwiseOp.h:530
ExpressionType & operator+=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:591
const_reverse_iterator crend() const
Definition: VectorwiseOp.h:323
CwiseBinaryOp< internal::scalar_product_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator*(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:664
Eigen::Index Index
Definition: VectorwiseOp.h:193
const BlueNormReturnType blueNorm() const
Definition: VectorwiseOp.h:447
const_iterator cend() const
Definition: VectorwiseOp.h:314
random_access_iterator_type const_iterator
Definition: VectorwiseOp.h:281
const CountReturnType count() const
Definition: VectorwiseOp.h:519
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:650
const ReplicateReturnType replicate(Index factor) const
Definition: Replicate.h:136
const MaxCoeffReturnType maxCoeff() const
Definition: VectorwiseOp.h:396
ExpressionType & operator-=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:601
const SumReturnType sum() const
Definition: VectorwiseOp.h:480
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:637
ExpressionType & operator=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:580
const_iterator cbegin() const
Definition: VectorwiseOp.h:296
const MinCoeffReturnType minCoeff() const
Definition: VectorwiseOp.h:377
const Replicate< ExpressionType, isVertical *Factor+isHorizontal, isHorizontal *Factor+isVertical > replicate(Index factor=Factor) const
Definition: VectorwiseOp.h:569
reverse_iterator rbegin()
Definition: VectorwiseOp.h:301
const AnyReturnType any() const
Definition: VectorwiseOp.h:506
const AllReturnType all() const
Definition: VectorwiseOp.h:497
iterator end()
Definition: VectorwiseOp.h:310
ExpressionType & operator/=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:623
random_access_iterator_type iterator
Definition: VectorwiseOp.h:279
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator/(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:679
const_reverse_iterator rbegin() const
Definition: VectorwiseOp.h:303
reverse_iterator rend()
Definition: VectorwiseOp.h:319
const LpNormReturnType< p >::Type lpNorm() const
Definition: VectorwiseOp.h:436
const MeanReturnType mean() const
Definition: VectorwiseOp.h:488
const_iterator begin() const
Definition: VectorwiseOp.h:294
ReverseReturnType reverse()
Definition: VectorwiseOp.h:550
void reverseInPlace()
Definition: Reverse.h:212
iterator begin()
Definition: VectorwiseOp.h:292
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename OppositeExtendedType< NormReturnType >::Type > normalized() const
Definition: VectorwiseOp.h:695
ExpressionType & operator*=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:611
const_iterator end() const
Definition: VectorwiseOp.h:312
void normalize()
Definition: VectorwiseOp.h:701
const StableNormReturnType stableNorm() const
Definition: VectorwiseOp.h:458
const ReduxReturnType< BinaryOp >::Type redux(const BinaryOp &func=BinaryOp()) const
Definition: VectorwiseOp.h:338
const ConstReverseReturnType reverse() const
Definition: VectorwiseOp.h:542
const_reverse_iterator crbegin() const
Definition: VectorwiseOp.h:305
const NormReturnType norm() const
Definition: VectorwiseOp.h:423
const_reverse_iterator rend() const
Definition: VectorwiseOp.h:321
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:189
const HNormalizedReturnType hnormalized() const
column or row-wise homogeneous normalization
Definition: Homogeneous.h:200
const CrossReturnType cross(const MatrixBase< OtherDerived > &other) const
Definition: OrthoMethods.h:113
HomogeneousReturnType homogeneous() const
Definition: Homogeneous.h:152
DirectionType
Definition: Constants.h:263
@ Horizontal
Definition: Constants.h:269
@ Vertical
Definition: Constants.h:266
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231