1
// This file is part of Eigen, a lightweight C++ template library
4
// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
7
// Eigen is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 3 of the License, or (at your option) any later version.
12
// Alternatively, you can redistribute it and/or
13
// modify it under the terms of the GNU General Public License as
14
// published by the Free Software Foundation; either version 2 of
15
// the License, or (at your option) any later version.
17
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20
// GNU General Public License for more details.
22
// You should have received a copy of the GNU Lesser General Public
23
// License and a copy of the GNU General Public License along with
24
// Eigen. If not, see <http://www.gnu.org/licenses/>.
26
#ifndef EIGEN_PARTIAL_REDUX_H
27
#define EIGEN_PARTIAL_REDUX_H
29
/** \class PartialReduxExpr
30
* \ingroup Core_Module
32
* \brief Generic expression of a partially reduxed matrix
34
* \tparam MatrixType the type of the matrix we are applying the redux operation
35
* \tparam MemberOp type of the member functor
36
* \tparam Direction indicates the direction of the redux (#Vertical or #Horizontal)
38
* This class represents an expression of a partial redux operator of a matrix.
39
* It is the return type of some VectorwiseOp functions,
40
* and most of the time this is the only way it is used.
42
* \sa class VectorwiseOp
45
template< typename MatrixType, typename MemberOp, int Direction>
46
class PartialReduxExpr;
49
template<typename MatrixType, typename MemberOp, int Direction>
50
struct traits<PartialReduxExpr<MatrixType, MemberOp, Direction> >
53
typedef typename MemberOp::result_type Scalar;
54
typedef typename traits<MatrixType>::StorageKind StorageKind;
55
typedef typename traits<MatrixType>::XprKind XprKind;
56
typedef typename MatrixType::Scalar InputScalar;
57
typedef typename nested<MatrixType>::type MatrixTypeNested;
58
typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
60
RowsAtCompileTime = Direction==Vertical ? 1 : MatrixType::RowsAtCompileTime,
61
ColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::ColsAtCompileTime,
62
MaxRowsAtCompileTime = Direction==Vertical ? 1 : MatrixType::MaxRowsAtCompileTime,
63
MaxColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
64
Flags0 = (unsigned int)_MatrixTypeNested::Flags & HereditaryBits,
65
Flags = (Flags0 & ~RowMajorBit) | (RowsAtCompileTime == 1 ? RowMajorBit : 0),
66
TraversalSize = Direction==Vertical ? RowsAtCompileTime : ColsAtCompileTime
68
#if EIGEN_GNUC_AT_LEAST(3,4)
69
typedef typename MemberOp::template Cost<InputScalar,int(TraversalSize)> CostOpType;
71
typedef typename MemberOp::template Cost<InputScalar,TraversalSize> CostOpType;
74
CoeffReadCost = TraversalSize * traits<_MatrixTypeNested>::CoeffReadCost + int(CostOpType::value)
79
template< typename MatrixType, typename MemberOp, int Direction>
80
class PartialReduxExpr : internal::no_assignment_operator,
81
public internal::dense_xpr_base< PartialReduxExpr<MatrixType, MemberOp, Direction> >::type
85
typedef typename internal::dense_xpr_base<PartialReduxExpr>::type Base;
86
EIGEN_DENSE_PUBLIC_INTERFACE(PartialReduxExpr)
87
typedef typename internal::traits<PartialReduxExpr>::MatrixTypeNested MatrixTypeNested;
88
typedef typename internal::traits<PartialReduxExpr>::_MatrixTypeNested _MatrixTypeNested;
90
PartialReduxExpr(const MatrixType& mat, const MemberOp& func = MemberOp())
91
: m_matrix(mat), m_functor(func) {}
93
Index rows() const { return (Direction==Vertical ? 1 : m_matrix.rows()); }
94
Index cols() const { return (Direction==Horizontal ? 1 : m_matrix.cols()); }
96
EIGEN_STRONG_INLINE const Scalar coeff(Index i, Index j) const
98
if (Direction==Vertical)
99
return m_functor(m_matrix.col(j));
101
return m_functor(m_matrix.row(i));
104
const Scalar coeff(Index index) const
106
if (Direction==Vertical)
107
return m_functor(m_matrix.col(index));
109
return m_functor(m_matrix.row(index));
113
const MatrixTypeNested m_matrix;
114
const MemberOp m_functor;
117
#define EIGEN_MEMBER_FUNCTOR(MEMBER,COST) \
118
template <typename ResultType> \
119
struct member_##MEMBER { \
120
EIGEN_EMPTY_STRUCT_CTOR(member_##MEMBER) \
121
typedef ResultType result_type; \
122
template<typename Scalar, int Size> struct Cost \
123
{ enum { value = COST }; }; \
124
template<typename XprType> \
125
EIGEN_STRONG_INLINE ResultType operator()(const XprType& mat) const \
126
{ return mat.MEMBER(); } \
131
EIGEN_MEMBER_FUNCTOR(squaredNorm, Size * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
132
EIGEN_MEMBER_FUNCTOR(norm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
133
EIGEN_MEMBER_FUNCTOR(stableNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
134
EIGEN_MEMBER_FUNCTOR(blueNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
135
EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size-1) * functor_traits<scalar_hypot_op<Scalar> >::Cost );
136
EIGEN_MEMBER_FUNCTOR(sum, (Size-1)*NumTraits<Scalar>::AddCost);
137
EIGEN_MEMBER_FUNCTOR(mean, (Size-1)*NumTraits<Scalar>::AddCost + NumTraits<Scalar>::MulCost);
138
EIGEN_MEMBER_FUNCTOR(minCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
139
EIGEN_MEMBER_FUNCTOR(maxCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
140
EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits<Scalar>::AddCost);
141
EIGEN_MEMBER_FUNCTOR(any, (Size-1)*NumTraits<Scalar>::AddCost);
142
EIGEN_MEMBER_FUNCTOR(count, (Size-1)*NumTraits<Scalar>::AddCost);
143
EIGEN_MEMBER_FUNCTOR(prod, (Size-1)*NumTraits<Scalar>::MulCost);
146
template <typename BinaryOp, typename Scalar>
147
struct member_redux {
148
typedef typename result_of<
151
template<typename _Scalar, int Size> struct Cost
152
{ enum { value = (Size-1) * functor_traits<BinaryOp>::Cost }; };
153
member_redux(const BinaryOp func) : m_functor(func) {}
154
template<typename Derived>
155
inline result_type operator()(const DenseBase<Derived>& mat) const
156
{ return mat.redux(m_functor); }
157
const BinaryOp m_functor;
161
/** \class VectorwiseOp
162
* \ingroup Core_Module
164
* \brief Pseudo expression providing partial reduction operations
166
* \param ExpressionType the type of the object on which to do partial reductions
167
* \param Direction indicates the direction of the redux (#Vertical or #Horizontal)
169
* This class represents a pseudo expression with partial reduction features.
170
* It is the return type of DenseBase::colwise() and DenseBase::rowwise()
171
* and most of the time this is the only way it is used.
173
* Example: \include MatrixBase_colwise.cpp
174
* Output: \verbinclude MatrixBase_colwise.out
176
* \sa DenseBase::colwise(), DenseBase::rowwise(), class PartialReduxExpr
178
template<typename ExpressionType, int Direction> class VectorwiseOp
182
typedef typename ExpressionType::Scalar Scalar;
183
typedef typename ExpressionType::RealScalar RealScalar;
184
typedef typename ExpressionType::Index Index;
185
typedef typename internal::conditional<internal::must_nest_by_value<ExpressionType>::ret,
186
ExpressionType, ExpressionType&>::type ExpressionTypeNested;
187
typedef typename internal::remove_all<ExpressionTypeNested>::type ExpressionTypeNestedCleaned;
189
template<template<typename _Scalar> class Functor,
190
typename Scalar=typename internal::traits<ExpressionType>::Scalar> struct ReturnType
192
typedef PartialReduxExpr<ExpressionType,
198
template<typename BinaryOp> struct ReduxReturnType
200
typedef PartialReduxExpr<ExpressionType,
201
internal::member_redux<BinaryOp,typename internal::traits<ExpressionType>::Scalar>,
207
IsVertical = (Direction==Vertical) ? 1 : 0,
208
IsHorizontal = (Direction==Horizontal) ? 1 : 0
214
* \returns the i-th subvector according to the \c Direction */
215
typedef typename internal::conditional<Direction==Vertical,
216
typename ExpressionType::ColXpr,
217
typename ExpressionType::RowXpr>::type SubVector;
218
SubVector subVector(Index i)
220
return SubVector(m_matrix.derived(),i);
224
* \returns the number of subvectors in the direction \c Direction */
225
Index subVectors() const
226
{ return Direction==Vertical?m_matrix.cols():m_matrix.rows(); }
228
template<typename OtherDerived> struct ExtendedType {
229
typedef Replicate<OtherDerived,
230
Direction==Vertical ? 1 : ExpressionType::RowsAtCompileTime,
231
Direction==Horizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
235
* Replicates a vector to match the size of \c *this */
236
template<typename OtherDerived>
237
typename ExtendedType<OtherDerived>::Type
238
extendedTo(const DenseBase<OtherDerived>& other) const
240
EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived);
241
return typename ExtendedType<OtherDerived>::Type
243
Direction==Vertical ? 1 : m_matrix.rows(),
244
Direction==Horizontal ? 1 : m_matrix.cols());
249
inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
252
inline const ExpressionType& _expression() const { return m_matrix; }
254
/** \returns a row or column vector expression of \c *this reduxed by \a func
256
* The template parameter \a BinaryOp is the type of the functor
257
* of the custom redux operator. Note that func must be an associative operator.
259
* \sa class VectorwiseOp, DenseBase::colwise(), DenseBase::rowwise()
261
template<typename BinaryOp>
262
const typename ReduxReturnType<BinaryOp>::Type
263
redux(const BinaryOp& func = BinaryOp()) const
264
{ return typename ReduxReturnType<BinaryOp>::Type(_expression(), func); }
266
/** \returns a row (or column) vector expression of the smallest coefficient
267
* of each column (or row) of the referenced expression.
269
* Example: \include PartialRedux_minCoeff.cpp
270
* Output: \verbinclude PartialRedux_minCoeff.out
272
* \sa DenseBase::minCoeff() */
273
const typename ReturnType<internal::member_minCoeff>::Type minCoeff() const
274
{ return _expression(); }
276
/** \returns a row (or column) vector expression of the largest coefficient
277
* of each column (or row) of the referenced expression.
279
* Example: \include PartialRedux_maxCoeff.cpp
280
* Output: \verbinclude PartialRedux_maxCoeff.out
282
* \sa DenseBase::maxCoeff() */
283
const typename ReturnType<internal::member_maxCoeff>::Type maxCoeff() const
284
{ return _expression(); }
286
/** \returns a row (or column) vector expression of the squared norm
287
* of each column (or row) of the referenced expression.
289
* Example: \include PartialRedux_squaredNorm.cpp
290
* Output: \verbinclude PartialRedux_squaredNorm.out
292
* \sa DenseBase::squaredNorm() */
293
const typename ReturnType<internal::member_squaredNorm,RealScalar>::Type squaredNorm() const
294
{ return _expression(); }
296
/** \returns a row (or column) vector expression of the norm
297
* of each column (or row) of the referenced expression.
299
* Example: \include PartialRedux_norm.cpp
300
* Output: \verbinclude PartialRedux_norm.out
302
* \sa DenseBase::norm() */
303
const typename ReturnType<internal::member_norm,RealScalar>::Type norm() const
304
{ return _expression(); }
307
/** \returns a row (or column) vector expression of the norm
308
* of each column (or row) of the referenced expression, using
311
* \sa DenseBase::blueNorm() */
312
const typename ReturnType<internal::member_blueNorm,RealScalar>::Type blueNorm() const
313
{ return _expression(); }
316
/** \returns a row (or column) vector expression of the norm
317
* of each column (or row) of the referenced expression, avoiding
318
* underflow and overflow.
320
* \sa DenseBase::stableNorm() */
321
const typename ReturnType<internal::member_stableNorm,RealScalar>::Type stableNorm() const
322
{ return _expression(); }
325
/** \returns a row (or column) vector expression of the norm
326
* of each column (or row) of the referenced expression, avoiding
327
* underflow and overflow using a concatenation of hypot() calls.
329
* \sa DenseBase::hypotNorm() */
330
const typename ReturnType<internal::member_hypotNorm,RealScalar>::Type hypotNorm() const
331
{ return _expression(); }
333
/** \returns a row (or column) vector expression of the sum
334
* of each column (or row) of the referenced expression.
336
* Example: \include PartialRedux_sum.cpp
337
* Output: \verbinclude PartialRedux_sum.out
339
* \sa DenseBase::sum() */
340
const typename ReturnType<internal::member_sum>::Type sum() const
341
{ return _expression(); }
343
/** \returns a row (or column) vector expression of the mean
344
* of each column (or row) of the referenced expression.
346
* \sa DenseBase::mean() */
347
const typename ReturnType<internal::member_mean>::Type mean() const
348
{ return _expression(); }
350
/** \returns a row (or column) vector expression representing
351
* whether \b all coefficients of each respective column (or row) are \c true.
353
* \sa DenseBase::all() */
354
const typename ReturnType<internal::member_all>::Type all() const
355
{ return _expression(); }
357
/** \returns a row (or column) vector expression representing
358
* whether \b at \b least one coefficient of each respective column (or row) is \c true.
360
* \sa DenseBase::any() */
361
const typename ReturnType<internal::member_any>::Type any() const
362
{ return _expression(); }
364
/** \returns a row (or column) vector expression representing
365
* the number of \c true coefficients of each respective column (or row).
367
* Example: \include PartialRedux_count.cpp
368
* Output: \verbinclude PartialRedux_count.out
370
* \sa DenseBase::count() */
371
const PartialReduxExpr<ExpressionType, internal::member_count<Index>, Direction> count() const
372
{ return _expression(); }
374
/** \returns a row (or column) vector expression of the product
375
* of each column (or row) of the referenced expression.
377
* Example: \include PartialRedux_prod.cpp
378
* Output: \verbinclude PartialRedux_prod.out
380
* \sa DenseBase::prod() */
381
const typename ReturnType<internal::member_prod>::Type prod() const
382
{ return _expression(); }
385
/** \returns a matrix expression
386
* where each column (or row) are reversed.
388
* Example: \include Vectorwise_reverse.cpp
389
* Output: \verbinclude Vectorwise_reverse.out
391
* \sa DenseBase::reverse() */
392
const Reverse<ExpressionType, Direction> reverse() const
393
{ return Reverse<ExpressionType, Direction>( _expression() ); }
395
typedef Replicate<ExpressionType,Direction==Vertical?Dynamic:1,Direction==Horizontal?Dynamic:1> ReplicateReturnType;
396
const ReplicateReturnType replicate(Index factor) const;
399
* \return an expression of the replication of each column (or row) of \c *this
401
* Example: \include DirectionWise_replicate.cpp
402
* Output: \verbinclude DirectionWise_replicate.out
404
* \sa VectorwiseOp::replicate(Index), DenseBase::replicate(), class Replicate
406
// NOTE implemented here because of sunstudio's compilation errors
407
template<int Factor> const Replicate<ExpressionType,(IsVertical?Factor:1),(IsHorizontal?Factor:1)>
408
replicate(Index factor = Factor) const
410
return Replicate<ExpressionType,Direction==Vertical?Factor:1,Direction==Horizontal?Factor:1>
411
(_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
414
/////////// Artithmetic operators ///////////
416
/** Copies the vector \a other to each subvector of \c *this */
417
template<typename OtherDerived>
418
ExpressionType& operator=(const DenseBase<OtherDerived>& other)
420
EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
421
//eigen_assert((m_matrix.isNull()) == (other.isNull())); FIXME
422
for(Index j=0; j<subVectors(); ++j)
423
subVector(j) = other;
424
return const_cast<ExpressionType&>(m_matrix);
427
/** Adds the vector \a other to each subvector of \c *this */
428
template<typename OtherDerived>
429
ExpressionType& operator+=(const DenseBase<OtherDerived>& other)
431
EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
432
for(Index j=0; j<subVectors(); ++j)
433
subVector(j) += other.derived();
434
return const_cast<ExpressionType&>(m_matrix);
437
/** Substracts the vector \a other to each subvector of \c *this */
438
template<typename OtherDerived>
439
ExpressionType& operator-=(const DenseBase<OtherDerived>& other)
441
EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
442
for(Index j=0; j<subVectors(); ++j)
443
subVector(j) -= other.derived();
444
return const_cast<ExpressionType&>(m_matrix);
447
/** Returns the expression of the sum of the vector \a other to each subvector of \c *this */
448
template<typename OtherDerived> EIGEN_STRONG_INLINE
449
CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
450
const ExpressionTypeNestedCleaned,
451
const typename ExtendedType<OtherDerived>::Type>
452
operator+(const DenseBase<OtherDerived>& other) const
454
EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived);
455
return m_matrix + extendedTo(other.derived());
458
/** Returns the expression of the difference between each subvector of \c *this and the vector \a other */
459
template<typename OtherDerived>
460
CwiseBinaryOp<internal::scalar_difference_op<Scalar>,
461
const ExpressionTypeNestedCleaned,
462
const typename ExtendedType<OtherDerived>::Type>
463
operator-(const DenseBase<OtherDerived>& other) const
465
EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived);
466
return m_matrix - extendedTo(other.derived());
469
/////////// Geometry module ///////////
471
#if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
472
Homogeneous<ExpressionType,Direction> homogeneous() const;
475
typedef typename ExpressionType::PlainObject CrossReturnType;
476
template<typename OtherDerived>
477
const CrossReturnType cross(const MatrixBase<OtherDerived>& other) const;
480
HNormalized_Size = Direction==Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
481
: internal::traits<ExpressionType>::ColsAtCompileTime,
482
HNormalized_SizeMinusOne = HNormalized_Size==Dynamic ? Dynamic : HNormalized_Size-1
484
typedef Block<const ExpressionType,
485
Direction==Vertical ? int(HNormalized_SizeMinusOne)
486
: int(internal::traits<ExpressionType>::RowsAtCompileTime),
487
Direction==Horizontal ? int(HNormalized_SizeMinusOne)
488
: int(internal::traits<ExpressionType>::ColsAtCompileTime)>
490
typedef Block<const ExpressionType,
491
Direction==Vertical ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
492
Direction==Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
494
typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
495
const HNormalized_Block,
496
const Replicate<HNormalized_Factors,
497
Direction==Vertical ? HNormalized_SizeMinusOne : 1,
498
Direction==Horizontal ? HNormalized_SizeMinusOne : 1> >
499
HNormalizedReturnType;
501
const HNormalizedReturnType hnormalized() const;
504
ExpressionTypeNested m_matrix;
507
/** \returns a VectorwiseOp wrapper of *this providing additional partial reduction operations
509
* Example: \include MatrixBase_colwise.cpp
510
* Output: \verbinclude MatrixBase_colwise.out
512
* \sa rowwise(), class VectorwiseOp
514
template<typename Derived>
515
inline const typename DenseBase<Derived>::ConstColwiseReturnType
516
DenseBase<Derived>::colwise() const
521
/** \returns a writable VectorwiseOp wrapper of *this providing additional partial reduction operations
523
* \sa rowwise(), class VectorwiseOp
525
template<typename Derived>
526
inline typename DenseBase<Derived>::ColwiseReturnType
527
DenseBase<Derived>::colwise()
532
/** \returns a VectorwiseOp wrapper of *this providing additional partial reduction operations
534
* Example: \include MatrixBase_rowwise.cpp
535
* Output: \verbinclude MatrixBase_rowwise.out
537
* \sa colwise(), class VectorwiseOp
539
template<typename Derived>
540
inline const typename DenseBase<Derived>::ConstRowwiseReturnType
541
DenseBase<Derived>::rowwise() const
546
/** \returns a writable VectorwiseOp wrapper of *this providing additional partial reduction operations
548
* \sa colwise(), class VectorwiseOp
550
template<typename Derived>
551
inline typename DenseBase<Derived>::RowwiseReturnType
552
DenseBase<Derived>::rowwise()
557
#endif // EIGEN_PARTIAL_REDUX_H