VectorwiseOp.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_PARTIAL_REDUX_H
12#define EIGEN_PARTIAL_REDUX_H
13
14namespace Eigen {
15
31
32template< typename MatrixType, typename MemberOp, int Direction>
34
35namespace internal {
36template<typename MatrixType, typename MemberOp, int Direction>
37struct traits<PartialReduxExpr<MatrixType, MemberOp, Direction> >
38 : traits<MatrixType>
39{
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;
46 enum {
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,
52 Flags = (Flags0 & ~RowMajorBit) | (RowsAtCompileTime == 1 ? RowMajorBit : 0),
53 TraversalSize = Direction==Vertical ? RowsAtCompileTime : ColsAtCompileTime
54 };
55 #if EIGEN_GNUC_AT_LEAST(3,4)
56 typedef typename MemberOp::template Cost<InputScalar,int(TraversalSize)> CostOpType;
57 #else
58 typedef typename MemberOp::template Cost<InputScalar,TraversalSize> CostOpType;
59 #endif
60 enum {
61 CoeffReadCost = TraversalSize * traits<_MatrixTypeNested>::CoeffReadCost + int(CostOpType::value)
62 };
63};
64}
65
66template< typename MatrixType, typename MemberOp, int Direction>
67class PartialReduxExpr : internal::no_assignment_operator,
68 public internal::dense_xpr_base< PartialReduxExpr<MatrixType, MemberOp, Direction> >::type
69{
70 public:
71
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;
76
77 PartialReduxExpr(const MatrixType& mat, const MemberOp& func = MemberOp())
78 : m_matrix(mat), m_functor(func) {}
79
80 Index rows() const { return (Direction==Vertical ? 1 : m_matrix.rows()); }
81 Index cols() const { return (Direction==Horizontal ? 1 : m_matrix.cols()); }
82
83 EIGEN_STRONG_INLINE const Scalar coeff(Index i, Index j) const
84 {
85 if (Direction==Vertical)
86 return m_functor(m_matrix.col(j));
87 else
88 return m_functor(m_matrix.row(i));
89 }
90
91 const Scalar coeff(Index index) const
92 {
93 if (Direction==Vertical)
94 return m_functor(m_matrix.col(index));
95 else
96 return m_functor(m_matrix.row(index));
97 }
98
99 protected:
100 MatrixTypeNested m_matrix;
101 const MemberOp m_functor;
102};
103
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(); } \
114 }
115
116namespace internal {
117
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);
131
132
133template <typename BinaryOp, typename Scalar>
134struct member_redux {
135 typedef typename result_of<
136 BinaryOp(Scalar)
137 >::type result_type;
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;
145};
146}
147
165template<typename ExpressionType, int Direction> class VectorwiseOp
166{
167 public:
168
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;
175
176 template<template<typename _Scalar> class Functor,
177 typename Scalar=typename internal::traits<ExpressionType>::Scalar> struct ReturnType
178 {
179 typedef PartialReduxExpr<ExpressionType,
180 Functor<Scalar>,
181 Direction
182 > Type;
183 };
184
185 template<typename BinaryOp> struct ReduxReturnType
186 {
187 typedef PartialReduxExpr<ExpressionType,
188 internal::member_redux<BinaryOp,typename internal::traits<ExpressionType>::Scalar>,
189 Direction
190 > Type;
191 };
192
193 enum {
194 IsVertical = (Direction==Vertical) ? 1 : 0,
195 IsHorizontal = (Direction==Horizontal) ? 1 : 0
196 };
197
198 protected:
199
202 typedef typename internal::conditional<Direction==Vertical,
203 typename ExpressionType::ColXpr,
204 typename ExpressionType::RowXpr>::type SubVector;
205 SubVector subVector(Index i)
206 {
207 return SubVector(m_matrix.derived(),i);
208 }
209
212 Index subVectors() const
213 { return Direction==Vertical?m_matrix.cols():m_matrix.rows(); }
214
215 template<typename OtherDerived> struct ExtendedType {
216 typedef Replicate<OtherDerived,
217 Direction==Vertical ? 1 : ExpressionType::RowsAtCompileTime,
218 Direction==Horizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
219 };
220
223 template<typename OtherDerived>
224 typename ExtendedType<OtherDerived>::Type
225 extendedTo(const DenseBase<OtherDerived>& other) const
226 {
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
232 (other.derived(),
233 Direction==Vertical ? 1 : m_matrix.rows(),
234 Direction==Horizontal ? 1 : m_matrix.cols());
235 }
236
237 public:
238
239 inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
240
242 inline const ExpressionType& _expression() const { return m_matrix; }
243
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); }
255
263 const typename ReturnType<internal::member_minCoeff>::Type minCoeff() const
264 { return _expression(); }
265
273 const typename ReturnType<internal::member_maxCoeff>::Type maxCoeff() const
274 { return _expression(); }
275
283 const typename ReturnType<internal::member_squaredNorm,RealScalar>::Type squaredNorm() const
284 { return _expression(); }
285
293 const typename ReturnType<internal::member_norm,RealScalar>::Type norm() const
294 { return _expression(); }
295
296
302 const typename ReturnType<internal::member_blueNorm,RealScalar>::Type blueNorm() const
303 { return _expression(); }
304
305
311 const typename ReturnType<internal::member_stableNorm,RealScalar>::Type stableNorm() const
312 { return _expression(); }
313
314
320 const typename ReturnType<internal::member_hypotNorm,RealScalar>::Type hypotNorm() const
321 { return _expression(); }
322
330 const typename ReturnType<internal::member_sum>::Type sum() const
331 { return _expression(); }
332
337 const typename ReturnType<internal::member_mean>::Type mean() const
338 { return _expression(); }
339
344 const typename ReturnType<internal::member_all>::Type all() const
345 { return _expression(); }
346
351 const typename ReturnType<internal::member_any>::Type any() const
352 { return _expression(); }
353
362 { return _expression(); }
363
371 const typename ReturnType<internal::member_prod>::Type prod() const
372 { return _expression(); }
373
374
384
386 const ReplicateReturnType replicate(Index factor) const;
387
396 // NOTE implemented here because of sunstudio's compilation errors
397 template<int Factor> const Replicate<ExpressionType,(IsVertical?Factor:1),(IsHorizontal?Factor:1)>
398 replicate(Index factor = Factor) const
399 {
401 (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
402 }
403
405
407 template<typename OtherDerived>
408 ExpressionType& operator=(const DenseBase<OtherDerived>& other)
409 {
410 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
411 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
412 //eigen_assert((m_matrix.isNull()) == (other.isNull())); FIXME
413 return const_cast<ExpressionType&>(m_matrix = extendedTo(other.derived()));
414 }
415
417 template<typename OtherDerived>
418 ExpressionType& operator+=(const DenseBase<OtherDerived>& other)
419 {
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()));
423 }
424
426 template<typename OtherDerived>
427 ExpressionType& operator-=(const DenseBase<OtherDerived>& other)
428 {
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()));
432 }
433
435 template<typename OtherDerived>
436 ExpressionType& operator*=(const DenseBase<OtherDerived>& other)
437 {
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);
443 }
444
446 template<typename OtherDerived>
447 ExpressionType& operator/=(const DenseBase<OtherDerived>& other)
448 {
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
458 CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
459 const ExpressionTypeNestedCleaned,
460 const typename ExtendedType<OtherDerived>::Type>
461 operator+(const DenseBase<OtherDerived>& other) const
462 {
463 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
464 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
465 return m_matrix + extendedTo(other.derived());
466 }
467
469 template<typename OtherDerived>
471 const ExpressionTypeNestedCleaned,
472 const typename ExtendedType<OtherDerived>::Type>
474 {
475 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
476 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
477 return m_matrix - extendedTo(other.derived());
478 }
479
482 template<typename OtherDerived> EIGEN_STRONG_INLINE
484 const ExpressionTypeNestedCleaned,
485 const typename ExtendedType<OtherDerived>::Type>
487 {
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());
492 }
493
496 template<typename OtherDerived>
498 const ExpressionTypeNestedCleaned,
499 const typename ExtendedType<OtherDerived>::Type>
501 {
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());
506 }
507
509
510 #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
512 #endif
513
514 typedef typename ExpressionType::PlainObject CrossReturnType;
515 template<typename OtherDerived>
516 const CrossReturnType cross(const MatrixBase<OtherDerived>& other) const;
517
518 enum {
519 HNormalized_Size = Direction==Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
520 : internal::traits<ExpressionType>::ColsAtCompileTime,
521 HNormalized_SizeMinusOne = HNormalized_Size==Dynamic ? Dynamic : HNormalized_Size-1
522 };
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)>
528 HNormalized_Block;
529 typedef Block<const ExpressionType,
530 Direction==Vertical ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
531 Direction==Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
532 HNormalized_Factors;
534 const HNormalized_Block,
535 const Replicate<HNormalized_Factors,
536 Direction==Vertical ? HNormalized_SizeMinusOne : 1,
537 Direction==Horizontal ? HNormalized_SizeMinusOne : 1> >
538 HNormalizedReturnType;
539
540 const HNormalizedReturnType hnormalized() const;
541
542 protected:
543 ExpressionTypeNested m_matrix;
544};
545
553template<typename Derived>
554inline const typename DenseBase<Derived>::ConstColwiseReturnType
556{
557 return derived();
558}
559
564template<typename Derived>
565inline typename DenseBase<Derived>::ColwiseReturnType
567{
568 return derived();
569}
570
578template<typename Derived>
579inline const typename DenseBase<Derived>::ConstRowwiseReturnType
581{
582 return derived();
583}
584
589template<typename Derived>
590inline typename DenseBase<Derived>::RowwiseReturnType
592{
593 return derived();
594}
595
596} // end namespace Eigen
597
598#endif // EIGEN_PARTIAL_REDUX_H
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
Definition LDLT.h:18