11#ifndef EIGEN_PARTIAL_REDUX_H
12#define EIGEN_PARTIAL_REDUX_H
32template<
typename MatrixType,
typename MemberOp,
int Direction>
36template<
typename MatrixType,
typename MemberOp,
int Direction>
37struct 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)
66template<
typename MatrixType,
typename MemberOp,
int Direction>
67class 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(); } \
118EIGEN_MEMBER_FUNCTOR(squaredNorm, Size * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
119EIGEN_MEMBER_FUNCTOR(norm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
120EIGEN_MEMBER_FUNCTOR(stableNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
121EIGEN_MEMBER_FUNCTOR(blueNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
122EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size-1) * functor_traits<scalar_hypot_op<Scalar> >::Cost );
123EIGEN_MEMBER_FUNCTOR(sum, (Size-1)*NumTraits<Scalar>::AddCost);
124EIGEN_MEMBER_FUNCTOR(mean, (Size-1)*NumTraits<Scalar>::AddCost + NumTraits<Scalar>::MulCost);
125EIGEN_MEMBER_FUNCTOR(minCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
126EIGEN_MEMBER_FUNCTOR(maxCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
127EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits<Scalar>::AddCost);
128EIGEN_MEMBER_FUNCTOR(any, (Size-1)*NumTraits<Scalar>::AddCost);
129EIGEN_MEMBER_FUNCTOR(count, (Size-1)*NumTraits<Scalar>::AddCost);
130EIGEN_MEMBER_FUNCTOR(prod, (Size-1)*NumTraits<Scalar>::MulCost);
133template <
typename BinaryOp,
typename Scalar>
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;
165template<
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
185 template<
typename BinaryOp>
struct ReduxReturnType
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 {
217 Direction==
Vertical ? 1 : ExpressionType::RowsAtCompileTime,
218 Direction==
Horizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
223 template<
typename OtherDerived>
224 typename ExtendedType<OtherDerived>::Type
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(),
239 inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
242 inline const ExpressionType& _expression()
const {
return m_matrix; }
251 template<
typename BinaryOp>
252 const typename ReduxReturnType<BinaryOp>::Type
253 redux(
const BinaryOp& func = BinaryOp())
const
254 {
return typename ReduxReturnType<BinaryOp>::Type(_expression(), func); }
263 const typename ReturnType<internal::member_minCoeff>::Type
minCoeff()
const
264 {
return _expression(); }
273 const typename ReturnType<internal::member_maxCoeff>::Type
maxCoeff()
const
274 {
return _expression(); }
283 const typename ReturnType<internal::member_squaredNorm,RealScalar>::Type
squaredNorm()
const
284 {
return _expression(); }
293 const typename ReturnType<internal::member_norm,RealScalar>::Type
norm()
const
294 {
return _expression(); }
302 const typename ReturnType<internal::member_blueNorm,RealScalar>::Type
blueNorm()
const
303 {
return _expression(); }
311 const typename ReturnType<internal::member_stableNorm,RealScalar>::Type
stableNorm()
const
312 {
return _expression(); }
320 const typename ReturnType<internal::member_hypotNorm,RealScalar>::Type
hypotNorm()
const
321 {
return _expression(); }
330 const typename ReturnType<internal::member_sum>::Type
sum()
const
331 {
return _expression(); }
337 const typename ReturnType<internal::member_mean>::Type
mean()
const
338 {
return _expression(); }
344 const typename ReturnType<internal::member_all>::Type
all()
const
345 {
return _expression(); }
351 const typename ReturnType<internal::member_any>::Type
any()
const
352 {
return _expression(); }
362 {
return _expression(); }
371 const typename ReturnType<internal::member_prod>::Type
prod()
const
372 {
return _expression(); }
386 const ReplicateReturnType
replicate(Index factor)
const;
397 template<
int Factor>
const Replicate<ExpressionType,(IsVertical?Factor:1),(IsHorizontal?Factor:1)>
407 template<
typename OtherDerived>
410 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
411 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
413 return const_cast<ExpressionType&
>(m_matrix = extendedTo(other.derived()));
417 template<
typename OtherDerived>
420 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
421 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
422 return const_cast<ExpressionType&
>(m_matrix += extendedTo(other.derived()));
426 template<
typename OtherDerived>
429 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
430 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
431 return const_cast<ExpressionType&
>(m_matrix -= extendedTo(other.derived()));
435 template<
typename OtherDerived>
438 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
439 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
440 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
441 m_matrix *= extendedTo(other.derived());
442 return const_cast<ExpressionType&
>(m_matrix);
446 template<
typename OtherDerived>
449 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
450 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
451 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
452 m_matrix /= extendedTo(other.derived());
453 return const_cast<ExpressionType&>(m_matrix);
457 template<typename OtherDerived> EIGEN_STRONG_INLINE
459 const ExpressionTypeNestedCleaned,
460 const typename ExtendedType<OtherDerived>::Type>
463 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
464 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
465 return m_matrix + extendedTo(other.derived());
469 template<
typename OtherDerived>
471 const ExpressionTypeNestedCleaned,
472 const typename ExtendedType<OtherDerived>::Type>
475 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
476 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
477 return m_matrix - extendedTo(other.derived());
482 template<
typename OtherDerived> EIGEN_STRONG_INLINE
484 const ExpressionTypeNestedCleaned,
485 const typename ExtendedType<OtherDerived>::Type>
488 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
489 EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
490 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
491 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_ARRAYXPR(ExpressionType)
504 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
505 return m_matrix / extendedTo(other.derived());
510 #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
514 typedef typename ExpressionType::PlainObject CrossReturnType;
515 template<
typename OtherDerived>
519 HNormalized_Size = Direction==
Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
520 : internal::traits<ExpressionType>::ColsAtCompileTime,
521 HNormalized_SizeMinusOne = HNormalized_Size==Dynamic ? Dynamic : HNormalized_Size-1
523 typedef Block<
const ExpressionType,
524 Direction==
Vertical ? int(HNormalized_SizeMinusOne)
525 : int(
internal::traits<ExpressionType>::RowsAtCompileTime),
526 Direction==
Horizontal ? int(HNormalized_SizeMinusOne)
527 : int(
internal::traits<ExpressionType>::ColsAtCompileTime)>
529 typedef Block<
const ExpressionType,
530 Direction==
Vertical ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
531 Direction==
Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
534 const HNormalized_Block,
536 Direction==
Vertical ? HNormalized_SizeMinusOne : 1,
537 Direction==
Horizontal ? HNormalized_SizeMinusOne : 1> >
538 HNormalizedReturnType;
543 ExpressionTypeNested m_matrix;
553template<
typename Derived>
554inline const typename DenseBase<Derived>::ConstColwiseReturnType
564template<
typename Derived>
565inline typename DenseBase<Derived>::ColwiseReturnType
578template<
typename Derived>
579inline const typename DenseBase<Derived>::ConstRowwiseReturnType
589template<
typename Derived>
590inline typename DenseBase<Derived>::RowwiseReturnType
Expression of a fixed-size or dynamic-size block.
Definition Block.h:99
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:111
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:38
ConstColwiseReturnType colwise() const
Definition VectorwiseOp.h:555
ConstRowwiseReturnType rowwise() const
Definition VectorwiseOp.h:580
Expression of one (or a set of) homogeneous vector(s)
Definition Homogeneous.h:63
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
Generic expression of a partially reduxed matrix.
Definition VectorwiseOp.h:69
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:72
const PartialReduxExpr< ExpressionType, internal::member_count< Index >, Direction > count() const
Definition VectorwiseOp.h:361
const Reverse< ExpressionType, Direction > reverse() const
Definition VectorwiseOp.h:382
const ReturnType< internal::member_mean >::Type mean() const
Definition VectorwiseOp.h:337
const ReturnType< internal::member_any >::Type any() const
Definition VectorwiseOp.h:351
const ReturnType< internal::member_blueNorm, RealScalar >::Type blueNorm() const
Definition VectorwiseOp.h:302
ExpressionType & operator-=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:427
const ReturnType< internal::member_sum >::Type sum() const
Definition VectorwiseOp.h:330
ExpressionType & operator=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:408
const ReturnType< internal::member_prod >::Type prod() const
Definition VectorwiseOp.h:371
Homogeneous< ExpressionType, Direction > homogeneous() const
Definition Homogeneous.h:143
const ReturnType< internal::member_norm, RealScalar >::Type norm() const
Definition VectorwiseOp.h:293
CwiseBinaryOp< internal::scalar_quotient_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator/(const DenseBase< OtherDerived > &other) const
Definition VectorwiseOp.h:500
const ReturnType< internal::member_maxCoeff >::Type maxCoeff() const
Definition VectorwiseOp.h:273
const HNormalizedReturnType hnormalized() const
Definition Homogeneous.h:176
const ReduxReturnType< BinaryOp >::Type redux(const BinaryOp &func=BinaryOp()) const
Definition VectorwiseOp.h:253
const ReturnType< internal::member_minCoeff >::Type minCoeff() const
Definition VectorwiseOp.h:263
ExpressionType & operator/=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:447
CwiseBinaryOp< internal::scalar_product_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator*(const DenseBase< OtherDerived > &other) const
Definition VectorwiseOp.h:486
const CrossReturnType cross(const MatrixBase< OtherDerived > &other) const
Definition OrthoMethods.h:101
ExpressionType & operator*=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:436
ExpressionType & operator+=(const DenseBase< OtherDerived > &other)
Definition VectorwiseOp.h:418
const ReturnType< internal::member_squaredNorm, RealScalar >::Type squaredNorm() const
Definition VectorwiseOp.h:283
CwiseBinaryOp< internal::scalar_difference_op< Scalar >, const ExpressionTypeNestedCleaned, const typename ExtendedType< OtherDerived >::Type > operator-(const DenseBase< OtherDerived > &other) const
Definition VectorwiseOp.h:473
const Replicate< ExpressionType,(IsVertical?Factor:1),(IsHorizontal?Factor:1)> replicate(Index factor=Factor) const
Definition VectorwiseOp.h:398
const ReplicateReturnType replicate(Index factor) const
Definition Replicate.h:169
const ReturnType< internal::member_all >::Type all() const
Definition VectorwiseOp.h:344
const ReturnType< internal::member_stableNorm, RealScalar >::Type stableNorm() const
Definition VectorwiseOp.h:311
const ReturnType< internal::member_hypotNorm, RealScalar >::Type hypotNorm() const
Definition VectorwiseOp.h:320
@ Vertical
Definition Constants.h:204
@ Horizontal
Definition Constants.h:207
const unsigned int RowMajorBit
Definition Constants.h:48