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 ? RowsAtCompileTime : 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;
61 CoeffReadCost = TraversalSize * traits<_MatrixTypeNested>::CoeffReadCost + int(CostOpType::value)
66 template<
typename MatrixType,
typename MemberOp,
int Direction>
67 class PartialReduxExpr : internal::no_assignment_operator,
68 public internal::dense_xpr_base< PartialReduxExpr<MatrixType, MemberOp, Direction> >::type
72 typedef typename internal::dense_xpr_base<PartialReduxExpr>::type Base;
73 EIGEN_DENSE_PUBLIC_INTERFACE(PartialReduxExpr)
74 typedef typename internal::traits<PartialReduxExpr>::MatrixTypeNested MatrixTypeNested;
75 typedef typename internal::traits<PartialReduxExpr>::_MatrixTypeNested _MatrixTypeNested;
77 PartialReduxExpr(const MatrixType& mat, const MemberOp& func = MemberOp())
78 : m_matrix(mat), m_functor(func) {}
80 Index rows()
const {
return (Direction==
Vertical ? 1 : m_matrix.rows()); }
81 Index cols()
const {
return (Direction==
Horizontal ? 1 : m_matrix.cols()); }
83 EIGEN_STRONG_INLINE
const Scalar coeff(Index i, Index j)
const
86 return m_functor(m_matrix.col(j));
88 return m_functor(m_matrix.row(i));
91 const Scalar coeff(Index index)
const
94 return m_functor(m_matrix.col(index));
96 return m_functor(m_matrix.row(index));
100 MatrixTypeNested m_matrix;
101 const MemberOp m_functor;
104 #define EIGEN_MEMBER_FUNCTOR(MEMBER,COST) \
105 template <typename ResultType> \
106 struct member_##MEMBER { \
107 EIGEN_EMPTY_STRUCT_CTOR(member_##MEMBER) \
108 typedef ResultType result_type; \
109 template<typename Scalar, int Size> struct Cost \
110 { enum { value = COST }; }; \
111 template<typename XprType> \
112 EIGEN_STRONG_INLINE ResultType operator()(const XprType& mat) const \
113 { return mat.MEMBER(); } \
118 EIGEN_MEMBER_FUNCTOR(squaredNorm, Size * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
119 EIGEN_MEMBER_FUNCTOR(norm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
120 EIGEN_MEMBER_FUNCTOR(stableNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
121 EIGEN_MEMBER_FUNCTOR(blueNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
122 EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size-1) * functor_traits<scalar_hypot_op<Scalar> >::Cost );
123 EIGEN_MEMBER_FUNCTOR(sum, (Size-1)*NumTraits<Scalar>::AddCost);
124 EIGEN_MEMBER_FUNCTOR(mean, (Size-1)*NumTraits<Scalar>::AddCost + NumTraits<Scalar>::MulCost);
125 EIGEN_MEMBER_FUNCTOR(minCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
126 EIGEN_MEMBER_FUNCTOR(maxCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
127 EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits<Scalar>::AddCost);
128 EIGEN_MEMBER_FUNCTOR(any, (Size-1)*NumTraits<Scalar>::AddCost);
129 EIGEN_MEMBER_FUNCTOR(count, (Size-1)*NumTraits<Scalar>::AddCost);
130 EIGEN_MEMBER_FUNCTOR(prod, (Size-1)*NumTraits<Scalar>::MulCost);
133 template <
typename BinaryOp,
typename Scalar>
134 struct member_redux {
135 typedef typename result_of<
138 template<
typename _Scalar,
int Size>
struct Cost
139 {
enum { value = (Size-1) * functor_traits<BinaryOp>::Cost }; };
140 member_redux(
const BinaryOp func) : m_functor(func) {}
141 template<
typename Derived>
142 inline result_type operator()(
const DenseBase<Derived>& mat)
const
143 {
return mat.redux(m_functor); }
144 const BinaryOp m_functor;
165 template<
typename ExpressionType,
int Direction>
class VectorwiseOp
169 typedef typename ExpressionType::Scalar Scalar;
170 typedef typename ExpressionType::RealScalar RealScalar;
171 typedef typename ExpressionType::Index Index;
172 typedef typename internal::conditional<internal::must_nest_by_value<ExpressionType>::ret,
173 ExpressionType, ExpressionType&>::type ExpressionTypeNested;
174 typedef typename internal::remove_all<ExpressionTypeNested>::type ExpressionTypeNestedCleaned;
176 template<
template<
typename _Scalar>
class Functor,
177 typename Scalar=
typename internal::traits<ExpressionType>::Scalar>
struct ReturnType
179 typedef PartialReduxExpr<ExpressionType,
185 template<
typename BinaryOp>
struct ReduxReturnType
187 typedef PartialReduxExpr<ExpressionType,
188 internal::member_redux<BinaryOp,typename internal::traits<ExpressionType>::Scalar>,
194 IsVertical = (Direction==
Vertical) ? 1 : 0,
195 IsHorizontal = (Direction==
Horizontal) ? 1 : 0
202 typedef typename internal::conditional<Direction==
Vertical,
203 typename ExpressionType::ColXpr,
204 typename ExpressionType::RowXpr>::type SubVector;
205 SubVector subVector(Index i)
207 return SubVector(m_matrix.derived(),i);
212 Index subVectors()
const
213 {
return Direction==
Vertical?m_matrix.cols():m_matrix.rows(); }
215 template<
typename OtherDerived>
struct ExtendedType {
216 typedef Replicate<OtherDerived,
217 Direction==
Vertical ? 1 : ExpressionType::RowsAtCompileTime,
218 Direction==
Horizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
223 template<
typename OtherDerived>
224 typename ExtendedType<OtherDerived>::Type
225 extendedTo(
const DenseBase<OtherDerived>& other)
const
227 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==
Vertical, OtherDerived::MaxColsAtCompileTime==1),
228 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
229 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==
Horizontal, OtherDerived::MaxRowsAtCompileTime==1),
230 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
231 return typename ExtendedType<OtherDerived>::Type
233 Direction==
Vertical ? 1 : m_matrix.rows(),
234 Direction==Horizontal ? 1 : m_matrix.cols());
237 template<typename OtherDerived> struct OppositeExtendedType {
238 typedef Replicate<OtherDerived,
239 Direction==Horizontal ? 1 : ExpressionType::RowsAtCompileTime,
240 Direction==
Vertical ? 1 : ExpressionType::ColsAtCompileTime> Type;
245 template<
typename OtherDerived>
246 typename OppositeExtendedType<OtherDerived>::Type
247 extendedToOpposite(
const DenseBase<OtherDerived>& other)
const
249 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==
Horizontal, OtherDerived::MaxColsAtCompileTime==1),
250 YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
251 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==
Vertical, OtherDerived::MaxRowsAtCompileTime==1),
252 YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
253 return typename OppositeExtendedType<OtherDerived>::Type
256 Direction==Vertical ? 1 : m_matrix.cols());
261 inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
264 inline const ExpressionType& _expression()
const {
return m_matrix; }
273 template<
typename BinaryOp>
274 const typename ReduxReturnType<BinaryOp>::Type
275 redux(
const BinaryOp& func = BinaryOp())
const
288 {
return _expression(); }
300 {
return _expression(); }
310 {
return _expression(); }
320 {
return _expression(); }
329 {
return _expression(); }
338 {
return _expression(); }
347 {
return _expression(); }
357 {
return _expression(); }
364 {
return _expression(); }
371 {
return _expression(); }
378 {
return _expression(); }
388 {
return _expression(); }
398 {
return _expression(); }
412 const ReplicateReturnType replicate(Index factor)
const;
433 template<
typename OtherDerived>
436 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
437 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
439 return const_cast<ExpressionType&
>(m_matrix = extendedTo(other.derived()));
443 template<
typename OtherDerived>
446 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
447 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
448 return const_cast<ExpressionType&
>(m_matrix += extendedTo(other.derived()));
452 template<
typename OtherDerived>
455 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
456 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
457 return const_cast<ExpressionType&
>(m_matrix -= extendedTo(other.derived()));
461 template<
typename OtherDerived>
464 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
465 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
466 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
467 m_matrix *= extendedTo(other.derived());
468 return const_cast<ExpressionType&
>(m_matrix);
472 template<
typename OtherDerived>
475 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
476 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
477 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
478 m_matrix /= extendedTo(other.derived());
479 return const_cast<ExpressionType&
>(m_matrix);
483 template<
typename OtherDerived> EIGEN_STRONG_INLINE
485 const ExpressionTypeNestedCleaned,
486 const typename ExtendedType<OtherDerived>::Type>
489 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
490 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
491 return m_matrix + extendedTo(other.derived());
495 template<
typename OtherDerived>
497 const ExpressionTypeNestedCleaned,
498 const typename ExtendedType<OtherDerived>::Type>
501 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
502 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
503 return m_matrix - extendedTo(other.derived());
508 template<
typename OtherDerived> EIGEN_STRONG_INLINE
510 const ExpressionTypeNestedCleaned,
511 const typename ExtendedType<OtherDerived>::Type>
514 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
515 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
516 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
517 return m_matrix * extendedTo(other.derived());
522 template<
typename OtherDerived>
524 const ExpressionTypeNestedCleaned,
525 const typename ExtendedType<OtherDerived>::Type>
528 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
529 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
530 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
531 return m_matrix / extendedTo(other.derived());
539 const ExpressionTypeNestedCleaned,
540 const typename OppositeExtendedType<typename ReturnType<internal::member_norm,RealScalar>::Type>::Type>
541 normalized()
const {
return m_matrix.cwiseQuotient(extendedToOpposite(this->norm())); }
548 m_matrix = this->normalized();
553 #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
557 typedef typename ExpressionType::PlainObject CrossReturnType;
558 template<
typename OtherDerived>
562 HNormalized_Size = Direction==
Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
563 : internal::traits<ExpressionType>::ColsAtCompileTime,
564 HNormalized_SizeMinusOne = HNormalized_Size==
Dynamic ?
Dynamic : HNormalized_Size-1
566 typedef Block<
const ExpressionType,
567 Direction==
Vertical ? int(HNormalized_SizeMinusOne)
568 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
569 Direction==
Horizontal ? int(HNormalized_SizeMinusOne)
570 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
572 typedef Block<
const ExpressionType,
573 Direction==
Vertical ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
574 Direction==
Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
576 typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
577 const HNormalized_Block,
578 const Replicate<HNormalized_Factors,
579 Direction==
Vertical ? HNormalized_SizeMinusOne : 1,
580 Direction==
Horizontal ? HNormalized_SizeMinusOne : 1> >
581 HNormalizedReturnType;
583 const HNormalizedReturnType hnormalized()
const;
586 ExpressionTypeNested m_matrix;
596 template<
typename Derived>
597 inline const typename DenseBase<Derived>::ConstColwiseReturnType
607 template<
typename Derived>
621 template<
typename Derived>
632 template<
typename Derived>
641 #endif // EIGEN_PARTIAL_REDUX_H
const ReturnType< internal::member_hypotNorm, RealScalar >::Type hypotNorm() const
Definition: VectorwiseOp.h:346
const ReturnType< internal::member_prod >::Type prod() const
Definition: VectorwiseOp.h:397
const ReturnType< internal::member_blueNorm, RealScalar >::Type blueNorm() const
Definition: VectorwiseOp.h:328
void normalize()
Definition: VectorwiseOp.h:547
CwiseBinaryOp< internal::scalar_product_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator*(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:512
const ReturnType< internal::member_mean >::Type mean() const
Definition: VectorwiseOp.h:363
Generic expression of a partially reduxed matrix.
Definition: ForwardDeclarations.h:211
const ReturnType< internal::member_any >::Type any() const
Definition: VectorwiseOp.h:377
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:319
ExpressionType & operator*=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:462
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:424
const ReturnType< internal::member_sum >::Type sum() const
Definition: VectorwiseOp.h:356
const Reverse< ExpressionType, Direction > reverse() const
Definition: VectorwiseOp.h:408
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator/(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:526
Definition: Constants.h:212
const ReturnType< internal::member_maxCoeff >::Type maxCoeff() const
Definition: VectorwiseOp.h:299
const ReturnType< internal::member_squaredNorm, RealScalar >::Type squaredNorm() const
Definition: VectorwiseOp.h:309
CwiseBinaryOp< internal::scalar_difference_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator-(const DenseBase< OtherDerived > &other) const
Definition: VectorwiseOp.h:499
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:106
const ReturnType< internal::member_stableNorm, RealScalar >::Type stableNorm() const
Definition: VectorwiseOp.h:337
ExpressionType & operator-=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:453
const ReduxReturnType< BinaryOp >::Type redux(const BinaryOp &func=BinaryOp()) const
Definition: VectorwiseOp.h:275
ExpressionType & operator+=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:444
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:370
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:487
ExpressionType & operator/=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:473
const PartialReduxExpr< ExpressionType, internal::member_count< Index >, Direction > count() const
Definition: VectorwiseOp.h:387
const unsigned int RowMajorBit
Definition: Constants.h:53
const ReturnType< internal::member_minCoeff >::Type minCoeff() const
Definition: VectorwiseOp.h:287
Expression of the reverse of a vector or matrix.
Definition: Reverse.h:70
ConstColwiseReturnType colwise() const
Definition: VectorwiseOp.h:598
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
ConstRowwiseReturnType rowwise() const
Definition: VectorwiseOp.h:623
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename OppositeExtendedType< typename ReturnType< internal::member_norm, RealScalar >::Type >::Type > normalized() const
Definition: VectorwiseOp.h:541
Expression of one (or a set of) homogeneous vector(s)
Definition: ForwardDeclarations.h:265
ExpressionType & operator=(const DenseBase< OtherDerived > &other)
Definition: VectorwiseOp.h:434