11 #ifndef EIGEN_PARTIAL_REDUX_H
12 #define EIGEN_PARTIAL_REDUX_H
32 template<
typename MatrixType,
typename MemberOp,
int Direction>
33 class PartialReduxExpr;
36 template<
typename MatrixType,
typename MemberOp,
int Direction>
37 struct traits<PartialReduxExpr<MatrixType, MemberOp, Direction> >
40 typedef typename MemberOp::result_type Scalar;
41 typedef typename traits<MatrixType>::StorageKind StorageKind;
42 typedef typename traits<MatrixType>::XprKind XprKind;
43 typedef typename MatrixType::Scalar InputScalar;
45 RowsAtCompileTime = Direction==
Vertical ? 1 : MatrixType::RowsAtCompileTime,
46 ColsAtCompileTime = Direction==
Horizontal ? 1 : MatrixType::ColsAtCompileTime,
47 MaxRowsAtCompileTime = Direction==
Vertical ? 1 : MatrixType::MaxRowsAtCompileTime,
48 MaxColsAtCompileTime = Direction==
Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
50 TraversalSize = Direction==
Vertical ? MatrixType::RowsAtCompileTime : MatrixType::ColsAtCompileTime
55 template<
typename MatrixType,
typename MemberOp,
int Direction>
56 class PartialReduxExpr :
public internal::dense_xpr_base< PartialReduxExpr<MatrixType, MemberOp, Direction> >::type,
57 internal::no_assignment_operator
61 typedef typename internal::dense_xpr_base<PartialReduxExpr>::type Base;
65 explicit PartialReduxExpr(
const MatrixType& mat,
const MemberOp& func = MemberOp())
66 : m_matrix(mat), m_functor(func) {}
69 Index rows()
const {
return (Direction==
Vertical ? 1 : m_matrix.rows()); }
71 Index cols()
const {
return (Direction==
Horizontal ? 1 : m_matrix.cols()); }
74 typename MatrixType::Nested nestedExpression()
const {
return m_matrix; }
77 const MemberOp& functor()
const {
return m_functor; }
80 typename MatrixType::Nested m_matrix;
81 const MemberOp m_functor;
84 template<
typename A,
typename B>
struct partial_redux_dummy_func;
86 #define EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(MEMBER,COST,VECTORIZABLE,BINARYOP) \
87 template <typename ResultType,typename Scalar> \
88 struct member_##MEMBER { \
89 EIGEN_EMPTY_STRUCT_CTOR(member_##MEMBER) \
90 typedef ResultType result_type; \
91 typedef BINARYOP<Scalar,Scalar> BinaryOp; \
92 template<int Size> struct Cost { enum { value = COST }; }; \
93 enum { Vectorizable = VECTORIZABLE }; \
94 template<typename XprType> \
95 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE \
96 ResultType operator()(const XprType& mat) const \
97 { return mat.MEMBER(); } \
98 BinaryOp binaryFunc() const { return BinaryOp(); } \
101 #define EIGEN_MEMBER_FUNCTOR(MEMBER,COST) \
102 EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(MEMBER,COST,0,partial_redux_dummy_func)
109 EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size-1) * functor_traits<scalar_hypot_op<Scalar> >::Cost );
119 template <
int p,
typename ResultType,
typename Scalar>
120 struct member_lpnorm {
121 typedef ResultType result_type;
122 enum { Vectorizable = 0 };
123 template<
int Size>
struct Cost
124 {
enum { value = (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost }; };
125 EIGEN_DEVICE_FUNC member_lpnorm() {}
126 template<
typename XprType>
127 EIGEN_DEVICE_FUNC
inline ResultType operator()(
const XprType& mat)
const
128 {
return mat.template lpNorm<p>(); }
131 template <
typename BinaryOpT,
typename Scalar>
132 struct member_redux {
133 typedef BinaryOpT BinaryOp;
134 typedef typename result_of<
135 BinaryOp(
const Scalar&,
const Scalar&)
138 enum { Vectorizable = functor_traits<BinaryOp>::PacketAccess };
139 template<
int Size>
struct Cost {
enum { value = (Size-1) * functor_traits<BinaryOp>::Cost }; };
140 EIGEN_DEVICE_FUNC
explicit member_redux(
const BinaryOp func) : m_functor(func) {}
141 template<
typename Derived>
142 EIGEN_DEVICE_FUNC
inline result_type operator()(
const DenseBase<Derived>& mat)
const
143 {
return mat.redux(m_functor); }
144 const BinaryOp& binaryFunc()
const {
return m_functor; }
145 const BinaryOp m_functor;
190 typedef typename ExpressionType::Scalar Scalar;
191 typedef typename ExpressionType::RealScalar RealScalar;
193 typedef typename internal::ref_selector<ExpressionType>::non_const_type ExpressionTypeNested;
194 typedef typename internal::remove_all<ExpressionTypeNested>::type ExpressionTypeNestedCleaned;
196 template<
template<
typename OutScalar,
typename InputScalar>
class Functor,
197 typename ReturnScalar=Scalar>
struct ReturnType
200 Functor<ReturnScalar,Scalar>,
205 template<
typename BinaryOp>
struct ReduxReturnType
208 internal::member_redux<BinaryOp,Scalar>,
214 isVertical = (Direction==
Vertical) ? 1 : 0,
215 isHorizontal = (Direction==
Horizontal) ? 1 : 0
220 template<
typename OtherDerived>
struct ExtendedType {
221 typedef Replicate<OtherDerived,
222 isVertical ? 1 : ExpressionType::RowsAtCompileTime,
223 isHorizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
228 template<
typename OtherDerived>
230 typename ExtendedType<OtherDerived>::Type
231 extendedTo(
const DenseBase<OtherDerived>& other)
const
233 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(isVertical, OtherDerived::MaxColsAtCompileTime==1),
234 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
235 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(isHorizontal, OtherDerived::MaxRowsAtCompileTime==1),
236 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
237 return typename ExtendedType<OtherDerived>::Type
239 isVertical ? 1 : m_matrix.rows(),
240 isHorizontal ? 1 : m_matrix.cols());
243 template<
typename OtherDerived>
struct OppositeExtendedType {
244 typedef Replicate<OtherDerived,
245 isHorizontal ? 1 : ExpressionType::RowsAtCompileTime,
246 isVertical ? 1 : ExpressionType::ColsAtCompileTime> Type;
251 template<
typename OtherDerived>
253 typename OppositeExtendedType<OtherDerived>::Type
254 extendedToOpposite(
const DenseBase<OtherDerived>& other)
const
256 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(isHorizontal, OtherDerived::MaxColsAtCompileTime==1),
257 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
258 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(isVertical, OtherDerived::MaxRowsAtCompileTime==1),
259 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
260 return typename OppositeExtendedType<OtherDerived>::Type
262 isHorizontal ? 1 : m_matrix.rows(),
263 isVertical ? 1 : m_matrix.cols());
268 explicit inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
272 inline const ExpressionType& _expression()
const {
return m_matrix; }
274 #ifdef EIGEN_PARSED_BY_DOXYGEN
284 typedef internal::subvector_stl_reverse_iterator<ExpressionType,
DirectionType(Direction)> reverse_iterator;
285 typedef internal::subvector_stl_reverse_iterator<
const ExpressionType,
DirectionType(Direction)> const_reverse_iterator;
300 reverse_iterator
rbegin() {
return reverse_iterator (m_matrix, m_matrix.template subVectors<
DirectionType(Direction)>()-1); }
302 const_reverse_iterator
rbegin()
const {
return const_reverse_iterator (m_matrix, m_matrix.template subVectors<
DirectionType(Direction)>()-1); }
304 const_reverse_iterator
crbegin()
const {
return const_reverse_iterator (m_matrix, m_matrix.template subVectors<
DirectionType(Direction)>()-1); }
318 reverse_iterator
rend() {
return reverse_iterator (m_matrix, -1); }
320 const_reverse_iterator
rend()
const {
return const_reverse_iterator (m_matrix, -1); }
322 const_reverse_iterator
crend()
const {
return const_reverse_iterator (m_matrix, -1); }
334 template<
typename BinaryOp>
336 const typename ReduxReturnType<BinaryOp>::Type
337 redux(
const BinaryOp& func = BinaryOp())
const
339 eigen_assert(redux_length()>0 &&
"you are using an empty matrix");
343 typedef typename ReturnType<internal::member_minCoeff>::Type MinCoeffReturnType;
344 typedef typename ReturnType<internal::member_maxCoeff>::Type MaxCoeffReturnType;
347 typedef typename ReturnType<internal::member_blueNorm,RealScalar>::Type BlueNormReturnType;
348 typedef typename ReturnType<internal::member_stableNorm,RealScalar>::Type StableNormReturnType;
349 typedef typename ReturnType<internal::member_hypotNorm,RealScalar>::Type HypotNormReturnType;
350 typedef typename ReturnType<internal::member_sum>::Type SumReturnType;
351 typedef EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(SumReturnType,Scalar,quotient) MeanReturnType;
352 typedef typename ReturnType<internal::member_all>::Type AllReturnType;
353 typedef typename ReturnType<internal::member_any>::Type AnyReturnType;
355 typedef typename ReturnType<internal::member_prod>::Type ProdReturnType;
359 template<
int p>
struct LpNormReturnType {
378 eigen_assert(redux_length()>0 &&
"you are using an empty matrix");
397 eigen_assert(redux_length()>0 &&
"you are using an empty matrix");
487 const MeanReturnType
mean()
const
488 {
return sum() / Scalar(Direction==
Vertical?m_matrix.rows():m_matrix.cols()); }
571 (_expression(),isVertical?factor:1,isHorizontal?factor:1);
577 template<
typename OtherDerived>
581 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
582 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
584 return m_matrix = extendedTo(other.
derived());
588 template<
typename OtherDerived>
592 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
593 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
594 return m_matrix += extendedTo(other.
derived());
598 template<
typename OtherDerived>
602 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
603 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
604 return m_matrix -= extendedTo(other.
derived());
608 template<
typename OtherDerived>
612 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
613 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
614 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
615 m_matrix *= extendedTo(other.
derived());
620 template<
typename OtherDerived>
624 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
625 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
626 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
627 m_matrix /= extendedTo(other.
derived());
632 template<
typename OtherDerived> EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
634 const ExpressionTypeNestedCleaned,
635 const typename ExtendedType<OtherDerived>::Type>
638 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
639 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
640 return m_matrix + extendedTo(other.
derived());
644 template<
typename OtherDerived>
647 const ExpressionTypeNestedCleaned,
648 const typename ExtendedType<OtherDerived>::Type>
651 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
652 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
653 return m_matrix - extendedTo(other.
derived());
658 template<
typename OtherDerived> EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
660 const ExpressionTypeNestedCleaned,
661 const typename ExtendedType<OtherDerived>::Type>
665 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
666 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
667 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
668 return m_matrix * extendedTo(other.
derived());
673 template<
typename OtherDerived>
676 const ExpressionTypeNestedCleaned,
677 const typename ExtendedType<OtherDerived>::Type>
680 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
681 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
682 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
683 return m_matrix / extendedTo(other.
derived());
692 const ExpressionTypeNestedCleaned,
693 const typename OppositeExtendedType<NormReturnType>::Type>
694 normalized()
const {
return m_matrix.cwiseQuotient(extendedToOpposite(this->
norm())); }
712 typedef typename ExpressionType::PlainObject CrossReturnType;
713 template<
typename OtherDerived>
718 HNormalized_Size = Direction==
Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
719 : internal::traits<ExpressionType>::ColsAtCompileTime,
720 HNormalized_SizeMinusOne = HNormalized_Size==
Dynamic ?
Dynamic : HNormalized_Size-1
722 typedef Block<
const ExpressionType,
723 Direction==
Vertical ? int(HNormalized_SizeMinusOne)
724 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
725 Direction==
Horizontal ? int(HNormalized_SizeMinusOne)
726 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
728 typedef Block<
const ExpressionType,
729 Direction==
Vertical ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
730 Direction==
Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
732 typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
733 const HNormalized_Block,
734 const Replicate<HNormalized_Factors,
735 Direction==
Vertical ? HNormalized_SizeMinusOne : 1,
736 Direction==
Horizontal ? HNormalized_SizeMinusOne : 1> >
737 HNormalizedReturnType;
742 # ifdef EIGEN_VECTORWISEOP_PLUGIN
743 # include EIGEN_VECTORWISEOP_PLUGIN
747 Index redux_length()
const
749 return Direction==
Vertical ? m_matrix.rows() : m_matrix.cols();
751 ExpressionTypeNested m_matrix;
761 template<
typename Derived>
762 EIGEN_DEVICE_FUNC
inline typename DenseBase<Derived>::ColwiseReturnType
775 template<
typename Derived>
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:84
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:56
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:47
ConstColwiseReturnType colwise() const
Definition: DenseBase.h:536
ConstRowwiseReturnType rowwise() const
Definition: DenseBase.h:524
Derived & derived()
Definition: EigenBase.h:46
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:50
Generic expression of a partially reduxed matrix.
Definition: VectorwiseOp.h:58
Expression of the multiple replication of a matrix or vector.
Definition: Replicate.h:63
Expression of the reverse of a vector or matrix.
Definition: Reverse.h:65
Pseudo expression providing broadcasting and partial reduction operations.
Definition: VectorwiseOp.h:187
const HypotNormReturnType hypotNorm() const
Definition: VectorwiseOp.h:468
const SquaredNormReturnType squaredNorm() const
Definition: VectorwiseOp.h:410
const ProdReturnType prod() const
Definition: VectorwiseOp.h:529
ExpressionType & operator+=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:590
const_reverse_iterator crend() const
Definition: VectorwiseOp.h:322
CwiseBinaryOp< internal::scalar_product_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator*(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:663
Eigen::Index Index
Definition: VectorwiseOp.h:192
const BlueNormReturnType blueNorm() const
Definition: VectorwiseOp.h:446
const_iterator cend() const
Definition: VectorwiseOp.h:313
random_access_iterator_type const_iterator
Definition: VectorwiseOp.h:280
const CountReturnType count() const
Definition: VectorwiseOp.h:518
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:649
const ReplicateReturnType replicate(Index factor) const
Definition: Replicate.h:134
const MaxCoeffReturnType maxCoeff() const
Definition: VectorwiseOp.h:395
ExpressionType & operator-=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:600
const SumReturnType sum() const
Definition: VectorwiseOp.h:479
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:636
ExpressionType & operator=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:579
const_iterator cbegin() const
Definition: VectorwiseOp.h:295
const MinCoeffReturnType minCoeff() const
Definition: VectorwiseOp.h:376
const Replicate< ExpressionType, isVertical *Factor+isHorizontal, isHorizontal *Factor+isVertical > replicate(Index factor=Factor) const
Definition: VectorwiseOp.h:568
reverse_iterator rbegin()
Definition: VectorwiseOp.h:300
const AnyReturnType any() const
Definition: VectorwiseOp.h:505
const AllReturnType all() const
Definition: VectorwiseOp.h:496
iterator end()
Definition: VectorwiseOp.h:309
ExpressionType & operator/=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:622
random_access_iterator_type iterator
Definition: VectorwiseOp.h:278
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator/(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:678
const_reverse_iterator rbegin() const
Definition: VectorwiseOp.h:302
reverse_iterator rend()
Definition: VectorwiseOp.h:318
const LpNormReturnType< p >::Type lpNorm() const
Definition: VectorwiseOp.h:435
const MeanReturnType mean() const
Definition: VectorwiseOp.h:487
const_iterator begin() const
Definition: VectorwiseOp.h:293
ReverseReturnType reverse()
Definition: VectorwiseOp.h:549
void reverseInPlace()
Definition: Reverse.h:208
iterator begin()
Definition: VectorwiseOp.h:291
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename OppositeExtendedType< NormReturnType >::Type > normalized() const
Definition: VectorwiseOp.h:694
ExpressionType & operator*=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:610
const_iterator end() const
Definition: VectorwiseOp.h:311
void normalize()
Definition: VectorwiseOp.h:700
const StableNormReturnType stableNorm() const
Definition: VectorwiseOp.h:457
const ReduxReturnType< BinaryOp >::Type redux(const BinaryOp &func=BinaryOp()) const
Definition: VectorwiseOp.h:337
const ConstReverseReturnType reverse() const
Definition: VectorwiseOp.h:541
const_reverse_iterator crbegin() const
Definition: VectorwiseOp.h:304
const NormReturnType norm() const
Definition: VectorwiseOp.h:422
const_reverse_iterator rend() const
Definition: VectorwiseOp.h:320
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:171
const HNormalizedReturnType hnormalized() const
column or row-wise homogeneous normalization
Definition: Homogeneous.h:196
const CrossReturnType cross(const MatrixBase< OtherDerived > &other) const
Definition: OrthoMethods.h:111
HomogeneousReturnType homogeneous() const
Definition: Homogeneous.h:148
DirectionType
Definition: Constants.h:261
@ Horizontal
Definition: Constants.h:267
@ Vertical
Definition: Constants.h:264
const unsigned int RowMajorBit
Definition: Constants.h:66
Namespace containing all symbols from the Eigen library.
Definition: Core:134
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int Dynamic
Definition: Constants.h:22
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:213