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;
44 typedef typename nested<MatrixType>::type MatrixTypeNested;
45 typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
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,
51 Flags0 = (
unsigned int)_MatrixTypeNested::Flags & HereditaryBits,
53 TraversalSize = Direction==
Vertical ? MatrixType::RowsAtCompileTime : MatrixType::ColsAtCompileTime
55 #if EIGEN_GNUC_AT_LEAST(3,4)
56 typedef typename MemberOp::template Cost<InputScalar,int(TraversalSize)> CostOpType;
58 typedef typename MemberOp::template Cost<InputScalar,TraversalSize> CostOpType;
62 : TraversalSize * traits<_MatrixTypeNested>::CoeffReadCost + int(CostOpType::value)
67 template<
typename MatrixType,
typename MemberOp,
int Direction>
68 class PartialReduxExpr : internal::no_assignment_operator,
69 public internal::dense_xpr_base< PartialReduxExpr<MatrixType, MemberOp, Direction> >::type
73 typedef typename internal::dense_xpr_base<PartialReduxExpr>::type Base;
74 EIGEN_DENSE_PUBLIC_INTERFACE(PartialReduxExpr)
75 typedef typename internal::traits<PartialReduxExpr>::MatrixTypeNested MatrixTypeNested;
76 typedef typename internal::traits<PartialReduxExpr>::_MatrixTypeNested _MatrixTypeNested;
78 PartialReduxExpr(const MatrixType& mat, const MemberOp& func = MemberOp())
79 : m_matrix(mat), m_functor(func) {}
81 Index rows()
const {
return (Direction==
Vertical ? 1 : m_matrix.rows()); }
82 Index cols()
const {
return (Direction==
Horizontal ? 1 : m_matrix.cols()); }
84 EIGEN_STRONG_INLINE
const Scalar coeff(Index i, Index j)
const
87 return m_functor(m_matrix.col(j));
89 return m_functor(m_matrix.row(i));
92 const Scalar coeff(Index index)
const
95 return m_functor(m_matrix.col(index));
97 return m_functor(m_matrix.row(index));
101 MatrixTypeNested m_matrix;
102 const MemberOp m_functor;
105 #define EIGEN_MEMBER_FUNCTOR(MEMBER,COST) \
106 template <typename ResultType> \
107 struct member_##MEMBER { \
108 EIGEN_EMPTY_STRUCT_CTOR(member_##MEMBER) \
109 typedef ResultType result_type; \
110 template<typename Scalar, int Size> struct Cost \
111 { enum { value = COST }; }; \
112 template<typename XprType> \
113 EIGEN_STRONG_INLINE ResultType operator()(const XprType& mat) const \
114 { return mat.MEMBER(); } \
119 EIGEN_MEMBER_FUNCTOR(squaredNorm, Size * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
120 EIGEN_MEMBER_FUNCTOR(norm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
121 EIGEN_MEMBER_FUNCTOR(stableNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
122 EIGEN_MEMBER_FUNCTOR(blueNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
123 EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size-1) * functor_traits<scalar_hypot_op<Scalar> >::Cost );
124 EIGEN_MEMBER_FUNCTOR(sum, (Size-1)*NumTraits<Scalar>::AddCost);
125 EIGEN_MEMBER_FUNCTOR(mean, (Size-1)*NumTraits<Scalar>::AddCost + NumTraits<Scalar>::MulCost);
126 EIGEN_MEMBER_FUNCTOR(minCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
127 EIGEN_MEMBER_FUNCTOR(maxCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
128 EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits<Scalar>::AddCost);
129 EIGEN_MEMBER_FUNCTOR(any, (Size-1)*NumTraits<Scalar>::AddCost);
130 EIGEN_MEMBER_FUNCTOR(count, (Size-1)*NumTraits<Scalar>::AddCost);
131 EIGEN_MEMBER_FUNCTOR(prod, (Size-1)*NumTraits<Scalar>::MulCost);
134 template <
typename BinaryOp,
typename Scalar>
135 struct member_redux {
136 typedef typename result_of<
139 template<
typename _Scalar,
int Size>
struct Cost
140 {
enum { value = (Size-1) * functor_traits<BinaryOp>::Cost }; };
141 member_redux(
const BinaryOp func) : m_functor(func) {}
142 template<
typename Derived>
143 inline result_type operator()(
const DenseBase<Derived>& mat)
const
144 {
return mat.redux(m_functor); }
145 const BinaryOp m_functor;
166 template<
typename ExpressionType,
int Direction>
class VectorwiseOp
170 typedef typename ExpressionType::Scalar Scalar;
171 typedef typename ExpressionType::RealScalar RealScalar;
172 typedef typename ExpressionType::Index Index;
173 typedef typename internal::conditional<internal::must_nest_by_value<ExpressionType>::ret,
174 ExpressionType, ExpressionType&>::type ExpressionTypeNested;
175 typedef typename internal::remove_all<ExpressionTypeNested>::type ExpressionTypeNestedCleaned;
177 template<
template<
typename _Scalar>
class Functor,
178 typename Scalar=
typename internal::traits<ExpressionType>::Scalar>
struct ReturnType
180 typedef PartialReduxExpr<ExpressionType,
186 template<
typename BinaryOp>
struct ReduxReturnType
188 typedef PartialReduxExpr<ExpressionType,
189 internal::member_redux<BinaryOp,typename internal::traits<ExpressionType>::Scalar>,
195 IsVertical = (Direction==
Vertical) ? 1 : 0,
196 IsHorizontal = (Direction==
Horizontal) ? 1 : 0
203 typedef typename internal::conditional<Direction==
Vertical,
204 typename ExpressionType::ColXpr,
205 typename ExpressionType::RowXpr>::type SubVector;
206 SubVector subVector(Index i)
208 return SubVector(m_matrix.derived(),i);
213 Index subVectors()
const
214 {
return Direction==
Vertical?m_matrix.cols():m_matrix.rows(); }
216 template<
typename OtherDerived>
struct ExtendedType {
217 typedef Replicate<OtherDerived,
218 Direction==
Vertical ? 1 : ExpressionType::RowsAtCompileTime,
219 Direction==
Horizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
224 template<
typename OtherDerived>
225 typename ExtendedType<OtherDerived>::Type
226 extendedTo(
const DenseBase<OtherDerived>& other)
const
228 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==
Vertical, OtherDerived::MaxColsAtCompileTime==1),
229 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
230 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==
Horizontal, OtherDerived::MaxRowsAtCompileTime==1),
231 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
232 return typename ExtendedType<OtherDerived>::Type
234 Direction==
Vertical ? 1 : m_matrix.rows(),
235 Direction==Horizontal ? 1 : m_matrix.cols());
238 template<typename OtherDerived> struct OppositeExtendedType {
239 typedef Replicate<OtherDerived,
240 Direction==Horizontal ? 1 : ExpressionType::RowsAtCompileTime,
241 Direction==
Vertical ? 1 : ExpressionType::ColsAtCompileTime> Type;
246 template<
typename OtherDerived>
247 typename OppositeExtendedType<OtherDerived>::Type
248 extendedToOpposite(
const DenseBase<OtherDerived>& other)
const
250 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==
Horizontal, OtherDerived::MaxColsAtCompileTime==1),
251 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
252 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==
Vertical, OtherDerived::MaxRowsAtCompileTime==1),
253 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
254 return typename OppositeExtendedType<OtherDerived>::Type
257 Direction==Vertical ? 1 : m_matrix.cols());
262 inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
265 inline const ExpressionType& _expression()
const {
return m_matrix; }
274 template<
typename BinaryOp>
275 const typename ReduxReturnType<BinaryOp>::Type
276 redux(
const BinaryOp& func = BinaryOp())
const
289 {
return _expression(); }
301 {
return _expression(); }
311 {
return _expression(); }
321 {
return _expression(); }
330 {
return _expression(); }
339 {
return _expression(); }
348 {
return _expression(); }
358 {
return _expression(); }
365 {
return _expression(); }
372 {
return _expression(); }
379 {
return _expression(); }
389 {
return _expression(); }
399 {
return _expression(); }
413 const ReplicateReturnType replicate(Index factor)
const;
434 template<
typename OtherDerived>
437 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
438 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
440 return const_cast<ExpressionType&
>(m_matrix = extendedTo(other.derived()));
444 template<
typename OtherDerived>
447 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
448 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
449 return const_cast<ExpressionType&
>(m_matrix += extendedTo(other.derived()));
453 template<
typename OtherDerived>
456 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
457 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
458 return const_cast<ExpressionType&
>(m_matrix -= extendedTo(other.derived()));
462 template<
typename OtherDerived>
465 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
466 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
467 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
468 m_matrix *= extendedTo(other.derived());
469 return const_cast<ExpressionType&
>(m_matrix);
473 template<
typename OtherDerived>
476 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
477 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
478 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
479 m_matrix /= extendedTo(other.derived());
480 return const_cast<ExpressionType&
>(m_matrix);
484 template<
typename OtherDerived> EIGEN_STRONG_INLINE
486 const ExpressionTypeNestedCleaned,
487 const typename ExtendedType<OtherDerived>::Type>
490 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
491 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
492 return m_matrix + extendedTo(other.derived());
496 template<
typename OtherDerived>
498 const ExpressionTypeNestedCleaned,
499 const typename ExtendedType<OtherDerived>::Type>
502 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
503 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
504 return m_matrix - extendedTo(other.derived());
509 template<
typename OtherDerived> EIGEN_STRONG_INLINE
511 const ExpressionTypeNestedCleaned,
512 const typename ExtendedType<OtherDerived>::Type>
515 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
516 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
517 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
518 return m_matrix * extendedTo(other.derived());
523 template<
typename OtherDerived>
525 const ExpressionTypeNestedCleaned,
526 const typename ExtendedType<OtherDerived>::Type>
529 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
530 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
531 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
532 return m_matrix / extendedTo(other.derived());
540 const ExpressionTypeNestedCleaned,
541 const typename OppositeExtendedType<typename ReturnType<internal::member_norm,RealScalar>::Type>::Type>
542 normalized()
const {
return m_matrix.cwiseQuotient(extendedToOpposite(this->norm())); }
549 m_matrix = this->normalized();
554 #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
558 typedef typename ExpressionType::PlainObject CrossReturnType;
559 template<
typename OtherDerived>
563 HNormalized_Size = Direction==
Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
564 : internal::traits<ExpressionType>::ColsAtCompileTime,
565 HNormalized_SizeMinusOne = HNormalized_Size==
Dynamic ?
Dynamic : HNormalized_Size-1
567 typedef Block<
const ExpressionType,
568 Direction==
Vertical ? int(HNormalized_SizeMinusOne)
569 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
570 Direction==
Horizontal ? int(HNormalized_SizeMinusOne)
571 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
573 typedef Block<
const ExpressionType,
574 Direction==
Vertical ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
575 Direction==
Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
577 typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
578 const HNormalized_Block,
579 const Replicate<HNormalized_Factors,
580 Direction==
Vertical ? HNormalized_SizeMinusOne : 1,
581 Direction==
Horizontal ? HNormalized_SizeMinusOne : 1> >
582 HNormalizedReturnType;
584 const HNormalizedReturnType hnormalized()
const;
587 ExpressionTypeNested m_matrix;
597 template<
typename Derived>
598 inline const typename DenseBase<Derived>::ConstColwiseReturnType
608 template<
typename Derived>
622 template<
typename Derived>
633 template<
typename Derived>
642 #endif // EIGEN_PARTIAL_REDUX_H
const ReturnType< internal::member_hypotNorm, RealScalar >::Type hypotNorm() const
Definition: VectorwiseOp.h:347
const ReturnType< internal::member_prod >::Type prod() const
Definition: VectorwiseOp.h:398
const ReturnType< internal::member_blueNorm, RealScalar >::Type blueNorm() const
Definition: VectorwiseOp.h:329
void normalize()
Definition: VectorwiseOp.h:548
CwiseBinaryOp< internal::scalar_product_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator*(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:513
const ReturnType< internal::member_mean >::Type mean() const
Definition: VectorwiseOp.h:364
Generic expression of a partially reduxed matrix.
Definition: ForwardDeclarations.h:211
const ReturnType< internal::member_any >::Type any() const
Definition: VectorwiseOp.h:378
const int Dynamic
Definition: Constants.h:21
Pseudo expression providing partial reduction operations.
Definition: ForwardDeclarations.h:212
const ReturnType< internal::member_norm, RealScalar >::Type norm() const
Definition: VectorwiseOp.h:320
ExpressionType & operator*=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:463
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
const Replicate< ExpressionType,(IsVertical?Factor:1),(IsHorizontal?Factor:1)> replicate(Index factor=Factor) const
Definition: VectorwiseOp.h:425
const ReturnType< internal::member_sum >::Type sum() const
Definition: VectorwiseOp.h:357
const Reverse< ExpressionType, Direction > reverse() const
Definition: VectorwiseOp.h:409
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator/(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:527
Definition: Constants.h:212
const ReturnType< internal::member_maxCoeff >::Type maxCoeff() const
Definition: VectorwiseOp.h:300
const ReturnType< internal::member_squaredNorm, RealScalar >::Type squaredNorm() const
Definition: VectorwiseOp.h:310
CwiseBinaryOp< internal::scalar_difference_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator-(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:500
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:107
const ReturnType< internal::member_stableNorm, RealScalar >::Type stableNorm() const
Definition: VectorwiseOp.h:338
ExpressionType & operator-=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:454
const ReduxReturnType< BinaryOp >::Type redux(const BinaryOp &func=BinaryOp()) const
Definition: VectorwiseOp.h:276
ExpressionType & operator+=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:445
Expression of the multiple replication of a matrix or vector.
Definition: Replicate.h:62
const ReturnType< internal::member_all >::Type all() const
Definition: VectorwiseOp.h:371
Definition: Constants.h:209
CwiseBinaryOp< internal::scalar_sum_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator+(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:488
ExpressionType & operator/=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:474
const PartialReduxExpr< ExpressionType, internal::member_count< Index >, Direction > count() const
Definition: VectorwiseOp.h:388
const unsigned int RowMajorBit
Definition: Constants.h:53
const ReturnType< internal::member_minCoeff >::Type minCoeff() const
Definition: VectorwiseOp.h:288
Expression of the reverse of a vector or matrix.
Definition: Reverse.h:70
ConstColwiseReturnType colwise() const
Definition: VectorwiseOp.h:599
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
ConstRowwiseReturnType rowwise() const
Definition: VectorwiseOp.h:624
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename OppositeExtendedType< typename ReturnType< internal::member_norm, RealScalar >::Type >::Type > normalized() const
Definition: VectorwiseOp.h:542
Expression of one (or a set of) homogeneous vector(s)
Definition: ForwardDeclarations.h:268
ExpressionType & operator=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:435