VectorwiseOp.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00010 
00011 #ifndef EIGEN_PARTIAL_REDUX_H
00012 #define EIGEN_PARTIAL_REDUX_H
00013 
00014 namespace Eigen { 
00015 
00032 template< typename MatrixType, typename MemberOp, int Direction>
00033 class PartialReduxExpr;
00034 
00035 namespace internal {
00036 template<typename MatrixType, typename MemberOp, int Direction>
00037 struct traits<PartialReduxExpr<MatrixType, MemberOp, Direction> >
00038  : traits<MatrixType>
00039 {
00040   typedef typename MemberOp::result_type Scalar;
00041   typedef typename traits<MatrixType>::StorageKind StorageKind;
00042   typedef typename traits<MatrixType>::XprKind XprKind;
00043   typedef typename MatrixType::Scalar InputScalar;
00044   typedef typename nested<MatrixType>::type MatrixTypeNested;
00045   typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
00046   enum {
00047     RowsAtCompileTime = Direction==Vertical   ? 1 : MatrixType::RowsAtCompileTime,
00048     ColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::ColsAtCompileTime,
00049     MaxRowsAtCompileTime = Direction==Vertical   ? 1 : MatrixType::MaxRowsAtCompileTime,
00050     MaxColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
00051     Flags0 = (unsigned int)_MatrixTypeNested::Flags & HereditaryBits,
00052     Flags = (Flags0 & ~RowMajorBit) | (RowsAtCompileTime == 1 ? RowMajorBit : 0),
00053     TraversalSize = Direction==Vertical ? RowsAtCompileTime : ColsAtCompileTime
00054   };
00055   #if EIGEN_GNUC_AT_LEAST(3,4)
00056   typedef typename MemberOp::template Cost<InputScalar,int(TraversalSize)> CostOpType;
00057   #else
00058   typedef typename MemberOp::template Cost<InputScalar,TraversalSize> CostOpType;
00059   #endif
00060   enum {
00061     CoeffReadCost = TraversalSize * traits<_MatrixTypeNested>::CoeffReadCost + int(CostOpType::value)
00062   };
00063 };
00064 }
00065 
00066 template< typename MatrixType, typename MemberOp, int Direction>
00067 class PartialReduxExpr : internal::no_assignment_operator,
00068   public internal::dense_xpr_base< PartialReduxExpr<MatrixType, MemberOp, Direction> >::type
00069 {
00070   public:
00071 
00072     typedef typename internal::dense_xpr_base<PartialReduxExpr>::type Base;
00073     EIGEN_DENSE_PUBLIC_INTERFACE(PartialReduxExpr)
00074     typedef typename internal::traits<PartialReduxExpr>::MatrixTypeNested MatrixTypeNested;
00075     typedef typename internal::traits<PartialReduxExpr>::_MatrixTypeNested _MatrixTypeNested;
00076 
00077     PartialReduxExpr(const MatrixType& mat, const MemberOp& func = MemberOp())
00078       : m_matrix(mat), m_functor(func) {}
00079 
00080     Index rows() const { return (Direction==Vertical   ? 1 : m_matrix.rows()); }
00081     Index cols() const { return (Direction==Horizontal ? 1 : m_matrix.cols()); }
00082 
00083     EIGEN_STRONG_INLINE const Scalar coeff(Index i, Index j) const
00084     {
00085       if (Direction==Vertical)
00086         return m_functor(m_matrix.col(j));
00087       else
00088         return m_functor(m_matrix.row(i));
00089     }
00090 
00091     const Scalar coeff(Index index) const
00092     {
00093       if (Direction==Vertical)
00094         return m_functor(m_matrix.col(index));
00095       else
00096         return m_functor(m_matrix.row(index));
00097     }
00098 
00099   protected:
00100     MatrixTypeNested m_matrix;
00101     const MemberOp m_functor;
00102 };
00103 
00104 #define EIGEN_MEMBER_FUNCTOR(MEMBER,COST)                               \
00105   template <typename ResultType>                                        \
00106   struct member_##MEMBER {                                              \
00107     EIGEN_EMPTY_STRUCT_CTOR(member_##MEMBER)                            \
00108     typedef ResultType result_type;                                     \
00109     template<typename Scalar, int Size> struct Cost                     \
00110     { enum { value = COST }; };                                         \
00111     template<typename XprType>                                          \
00112     EIGEN_STRONG_INLINE ResultType operator()(const XprType& mat) const \
00113     { return mat.MEMBER(); } \
00114   }
00115 
00116 namespace internal {
00117 
00118 EIGEN_MEMBER_FUNCTOR(squaredNorm, Size * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
00119 EIGEN_MEMBER_FUNCTOR(norm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
00120 EIGEN_MEMBER_FUNCTOR(stableNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
00121 EIGEN_MEMBER_FUNCTOR(blueNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
00122 EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size-1) * functor_traits<scalar_hypot_op<Scalar> >::Cost );
00123 EIGEN_MEMBER_FUNCTOR(sum, (Size-1)*NumTraits<Scalar>::AddCost);
00124 EIGEN_MEMBER_FUNCTOR(mean, (Size-1)*NumTraits<Scalar>::AddCost + NumTraits<Scalar>::MulCost);
00125 EIGEN_MEMBER_FUNCTOR(minCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
00126 EIGEN_MEMBER_FUNCTOR(maxCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
00127 EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits<Scalar>::AddCost);
00128 EIGEN_MEMBER_FUNCTOR(any, (Size-1)*NumTraits<Scalar>::AddCost);
00129 EIGEN_MEMBER_FUNCTOR(count, (Size-1)*NumTraits<Scalar>::AddCost);
00130 EIGEN_MEMBER_FUNCTOR(prod, (Size-1)*NumTraits<Scalar>::MulCost);
00131 
00132 
00133 template <typename BinaryOp, typename Scalar>
00134 struct member_redux {
00135   typedef typename result_of<
00136                      BinaryOp(Scalar)
00137                    >::type  result_type;
00138   template<typename _Scalar, int Size> struct Cost
00139   { enum { value = (Size-1) * functor_traits<BinaryOp>::Cost }; };
00140   member_redux(const BinaryOp func) : m_functor(func) {}
00141   template<typename Derived>
00142   inline result_type operator()(const DenseBase<Derived>& mat) const
00143   { return mat.redux(m_functor); }
00144   const BinaryOp m_functor;
00145 };
00146 }
00147 
00165 template<typename ExpressionType, int Direction> class VectorwiseOp
00166 {
00167   public:
00168 
00169     typedef typename ExpressionType::Scalar Scalar;
00170     typedef typename ExpressionType::RealScalar RealScalar;
00171     typedef typename ExpressionType::Index Index;
00172     typedef typename internal::conditional<internal::must_nest_by_value<ExpressionType>::ret,
00173         ExpressionType, ExpressionType&>::type ExpressionTypeNested;
00174     typedef typename internal::remove_all<ExpressionTypeNested>::type ExpressionTypeNestedCleaned;
00175 
00176     template<template<typename _Scalar> class Functor,
00177                       typename Scalar=typename internal::traits<ExpressionType>::Scalar> struct ReturnType
00178     {
00179       typedef PartialReduxExpr<ExpressionType,
00180                                Functor<Scalar>,
00181                                Direction
00182                               > Type;
00183     };
00184 
00185     template<typename BinaryOp> struct ReduxReturnType
00186     {
00187       typedef PartialReduxExpr<ExpressionType,
00188                                internal::member_redux<BinaryOp,typename internal::traits<ExpressionType>::Scalar>,
00189                                Direction
00190                               > Type;
00191     };
00192 
00193     enum {
00194       IsVertical   = (Direction==Vertical) ? 1 : 0,
00195       IsHorizontal = (Direction==Horizontal) ? 1 : 0
00196     };
00197 
00198   protected:
00199 
00202     typedef typename internal::conditional<Direction==Vertical,
00203                                typename ExpressionType::ColXpr,
00204                                typename ExpressionType::RowXpr>::type SubVector;
00205     SubVector subVector(Index i)
00206     {
00207       return SubVector(m_matrix.derived(),i);
00208     }
00209 
00212     Index subVectors() const
00213     { return Direction==Vertical?m_matrix.cols():m_matrix.rows(); }
00214 
00215     template<typename OtherDerived> struct ExtendedType {
00216       typedef Replicate<OtherDerived,
00217                         Direction==Vertical   ? 1 : ExpressionType::RowsAtCompileTime,
00218                         Direction==Horizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
00219     };
00220 
00223     template<typename OtherDerived>
00224     typename ExtendedType<OtherDerived>::Type
00225     extendedTo(const DenseBase<OtherDerived>& other) const
00226     {
00227       EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==Vertical, OtherDerived::MaxColsAtCompileTime==1),
00228                           YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
00229       EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==Horizontal, OtherDerived::MaxRowsAtCompileTime==1),
00230                           YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
00231       return typename ExtendedType<OtherDerived>::Type
00232                       (other.derived(),
00233                        Direction==Vertical   ? 1 : m_matrix.rows(),
00234                        Direction==Horizontal ? 1 : m_matrix.cols());
00235     }
00236     
00237     template<typename OtherDerived> struct OppositeExtendedType {
00238       typedef Replicate<OtherDerived,
00239                         Direction==Horizontal ? 1 : ExpressionType::RowsAtCompileTime,
00240                         Direction==Vertical   ? 1 : ExpressionType::ColsAtCompileTime> Type;
00241     };
00242 
00245     template<typename OtherDerived>
00246     typename OppositeExtendedType<OtherDerived>::Type
00247     extendedToOpposite(const DenseBase<OtherDerived>& other) const
00248     {
00249       EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==Horizontal, OtherDerived::MaxColsAtCompileTime==1),
00250                           YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
00251       EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==Vertical, OtherDerived::MaxRowsAtCompileTime==1),
00252                           YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
00253       return typename OppositeExtendedType<OtherDerived>::Type
00254                       (other.derived(),
00255                        Direction==Horizontal  ? 1 : m_matrix.rows(),
00256                        Direction==Vertical    ? 1 : m_matrix.cols());
00257     }
00258 
00259   public:
00260 
00261     inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
00262 
00264     inline const ExpressionType& _expression() const { return m_matrix; }
00265 
00273     template<typename BinaryOp>
00274     const typename ReduxReturnType<BinaryOp>::Type
00275     redux(const BinaryOp& func = BinaryOp()) const
00276     { return typename ReduxReturnType<BinaryOp>::Type(_expression(), func); }
00277 
00287     const typename ReturnType<internal::member_minCoeff>::Type minCoeff() const
00288     { return _expression(); }
00289 
00299     const typename ReturnType<internal::member_maxCoeff>::Type maxCoeff() const
00300     { return _expression(); }
00301 
00309     const typename ReturnType<internal::member_squaredNorm,RealScalar>::Type squaredNorm() const
00310     { return _expression(); }
00311 
00319     const typename ReturnType<internal::member_norm,RealScalar>::Type norm() const
00320     { return _expression(); }
00321 
00322 
00328     const typename ReturnType<internal::member_blueNorm,RealScalar>::Type blueNorm() const
00329     { return _expression(); }
00330 
00331 
00337     const typename ReturnType<internal::member_stableNorm,RealScalar>::Type stableNorm() const
00338     { return _expression(); }
00339 
00340 
00346     const typename ReturnType<internal::member_hypotNorm,RealScalar>::Type hypotNorm() const
00347     { return _expression(); }
00348 
00356     const typename ReturnType<internal::member_sum>::Type sum() const
00357     { return _expression(); }
00358 
00363     const typename ReturnType<internal::member_mean>::Type mean() const
00364     { return _expression(); }
00365 
00370     const typename ReturnType<internal::member_all>::Type all() const
00371     { return _expression(); }
00372 
00377     const typename ReturnType<internal::member_any>::Type any() const
00378     { return _expression(); }
00379 
00387     const PartialReduxExpr<ExpressionType, internal::member_count<Index>, Direction> count() const
00388     { return _expression(); }
00389 
00397     const typename ReturnType<internal::member_prod>::Type prod() const
00398     { return _expression(); }
00399 
00400 
00408     const Reverse<ExpressionType, Direction> reverse() const
00409     { return Reverse<ExpressionType, Direction>( _expression() ); }
00410 
00411     typedef Replicate<ExpressionType,Direction==Vertical?Dynamic:1,Direction==Horizontal?Dynamic:1> ReplicateReturnType;
00412     const ReplicateReturnType replicate(Index factor) const;
00413 
00422     // NOTE implemented here because of sunstudio's compilation errors
00423     template<int Factor> const Replicate<ExpressionType,(IsVertical?Factor:1),(IsHorizontal?Factor:1)>
00424     replicate(Index factor = Factor) const
00425     {
00426       return Replicate<ExpressionType,Direction==Vertical?Factor:1,Direction==Horizontal?Factor:1>
00427           (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
00428     }
00429 
00431 
00433     template<typename OtherDerived>
00434     ExpressionType& operator=(const DenseBase<OtherDerived>& other)
00435     {
00436       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00437       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00438       //eigen_assert((m_matrix.isNull()) == (other.isNull())); FIXME
00439       return const_cast<ExpressionType&>(m_matrix = extendedTo(other.derived()));
00440     }
00441 
00443     template<typename OtherDerived>
00444     ExpressionType& operator+=(const DenseBase<OtherDerived>& other)
00445     {
00446       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00447       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00448       return const_cast<ExpressionType&>(m_matrix += extendedTo(other.derived()));
00449     }
00450 
00452     template<typename OtherDerived>
00453     ExpressionType& operator-=(const DenseBase<OtherDerived>& other)
00454     {
00455       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00456       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00457       return const_cast<ExpressionType&>(m_matrix -= extendedTo(other.derived()));
00458     }
00459 
00461     template<typename OtherDerived>
00462     ExpressionType& operator*=(const DenseBase<OtherDerived>& other)
00463     {
00464       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00465       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
00466       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00467       m_matrix *= extendedTo(other.derived());
00468       return const_cast<ExpressionType&>(m_matrix);
00469     }
00470 
00472     template<typename OtherDerived>
00473     ExpressionType& operator/=(const DenseBase<OtherDerived>& other)
00474     {
00475       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00476       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
00477       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00478       m_matrix /= extendedTo(other.derived());
00479       return const_cast<ExpressionType&>(m_matrix);
00480     }
00481 
00483     template<typename OtherDerived> EIGEN_STRONG_INLINE
00484     CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
00485                   const ExpressionTypeNestedCleaned,
00486                   const typename ExtendedType<OtherDerived>::Type>
00487     operator+(const DenseBase<OtherDerived>& other) const
00488     {
00489       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00490       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00491       return m_matrix + extendedTo(other.derived());
00492     }
00493 
00495     template<typename OtherDerived>
00496     CwiseBinaryOp<internal::scalar_difference_op<Scalar>,
00497                   const ExpressionTypeNestedCleaned,
00498                   const typename ExtendedType<OtherDerived>::Type>
00499     operator-(const DenseBase<OtherDerived>& other) const
00500     {
00501       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00502       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00503       return m_matrix - extendedTo(other.derived());
00504     }
00505 
00508     template<typename OtherDerived> EIGEN_STRONG_INLINE
00509     CwiseBinaryOp<internal::scalar_product_op<Scalar>,
00510                   const ExpressionTypeNestedCleaned,
00511                   const typename ExtendedType<OtherDerived>::Type>
00512     operator*(const DenseBase<OtherDerived>& other) const
00513     {
00514       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00515       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
00516       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00517       return m_matrix * extendedTo(other.derived());
00518     }
00519 
00522     template<typename OtherDerived>
00523     CwiseBinaryOp<internal::scalar_quotient_op<Scalar>,
00524                   const ExpressionTypeNestedCleaned,
00525                   const typename ExtendedType<OtherDerived>::Type>
00526     operator/(const DenseBase<OtherDerived>& other) const
00527     {
00528       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00529       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
00530       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00531       return m_matrix / extendedTo(other.derived());
00532     }
00533     
00538     CwiseBinaryOp<internal::scalar_quotient_op<Scalar>,
00539                   const ExpressionTypeNestedCleaned,
00540                   const typename OppositeExtendedType<typename ReturnType<internal::member_norm,RealScalar>::Type>::Type>
00541     normalized() const { return m_matrix.cwiseQuotient(extendedToOpposite(this->norm())); }
00542     
00543     
00547     void normalize() {
00548       m_matrix = this->normalized();
00549     }
00550 
00552 
00553     #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
00554     Homogeneous<ExpressionType,Direction> homogeneous() const;
00555     #endif
00556 
00557     typedef typename ExpressionType::PlainObject CrossReturnType;
00558     template<typename OtherDerived>
00559     const CrossReturnType cross(const MatrixBase<OtherDerived>& other) const;
00560 
00561     enum {
00562       HNormalized_Size = Direction==Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
00563                                              : internal::traits<ExpressionType>::ColsAtCompileTime,
00564       HNormalized_SizeMinusOne = HNormalized_Size==Dynamic ? Dynamic : HNormalized_Size-1
00565     };
00566     typedef Block<const ExpressionType,
00567                   Direction==Vertical   ? int(HNormalized_SizeMinusOne)
00568                                         : int(internal::traits<ExpressionType>::RowsAtCompileTime),
00569                   Direction==Horizontal ? int(HNormalized_SizeMinusOne)
00570                                         : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
00571             HNormalized_Block;
00572     typedef Block<const ExpressionType,
00573                   Direction==Vertical   ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
00574                   Direction==Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
00575             HNormalized_Factors;
00576     typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
00577                 const HNormalized_Block,
00578                 const Replicate<HNormalized_Factors,
00579                   Direction==Vertical   ? HNormalized_SizeMinusOne : 1,
00580                   Direction==Horizontal ? HNormalized_SizeMinusOne : 1> >
00581             HNormalizedReturnType;
00582 
00583     const HNormalizedReturnType hnormalized() const;
00584 
00585   protected:
00586     ExpressionTypeNested m_matrix;
00587 };
00588 
00596 template<typename Derived>
00597 inline const typename DenseBase<Derived>::ConstColwiseReturnType
00598 DenseBase<Derived>::colwise() const
00599 {
00600   return derived();
00601 }
00602 
00607 template<typename Derived>
00608 inline typename DenseBase<Derived>::ColwiseReturnType
00609 DenseBase<Derived>::colwise()
00610 {
00611   return derived();
00612 }
00613 
00621 template<typename Derived>
00622 inline const typename DenseBase<Derived>::ConstRowwiseReturnType
00623 DenseBase<Derived>::rowwise() const
00624 {
00625   return derived();
00626 }
00627 
00632 template<typename Derived>
00633 inline typename DenseBase<Derived>::RowwiseReturnType
00634 DenseBase<Derived>::rowwise()
00635 {
00636   return derived();
00637 }
00638 
00639 } // end namespace Eigen
00640 
00641 #endif // EIGEN_PARTIAL_REDUX_H


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Sat Jun 8 2019 19:40:18