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   public:
00238 
00239     inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
00240 
00242     inline const ExpressionType& _expression() const { return m_matrix; }
00243 
00251     template<typename BinaryOp>
00252     const typename ReduxReturnType<BinaryOp>::Type
00253     redux(const BinaryOp& func = BinaryOp()) const
00254     { return typename ReduxReturnType<BinaryOp>::Type(_expression(), func); }
00255 
00263     const typename ReturnType<internal::member_minCoeff>::Type minCoeff() const
00264     { return _expression(); }
00265 
00273     const typename ReturnType<internal::member_maxCoeff>::Type maxCoeff() const
00274     { return _expression(); }
00275 
00283     const typename ReturnType<internal::member_squaredNorm,RealScalar>::Type squaredNorm() const
00284     { return _expression(); }
00285 
00293     const typename ReturnType<internal::member_norm,RealScalar>::Type norm() const
00294     { return _expression(); }
00295 
00296 
00302     const typename ReturnType<internal::member_blueNorm,RealScalar>::Type blueNorm() const
00303     { return _expression(); }
00304 
00305 
00311     const typename ReturnType<internal::member_stableNorm,RealScalar>::Type stableNorm() const
00312     { return _expression(); }
00313 
00314 
00320     const typename ReturnType<internal::member_hypotNorm,RealScalar>::Type hypotNorm() const
00321     { return _expression(); }
00322 
00330     const typename ReturnType<internal::member_sum>::Type sum() const
00331     { return _expression(); }
00332 
00337     const typename ReturnType<internal::member_mean>::Type mean() const
00338     { return _expression(); }
00339 
00344     const typename ReturnType<internal::member_all>::Type all() const
00345     { return _expression(); }
00346 
00351     const typename ReturnType<internal::member_any>::Type any() const
00352     { return _expression(); }
00353 
00361     const PartialReduxExpr<ExpressionType, internal::member_count<Index>, Direction> count() const
00362     { return _expression(); }
00363 
00371     const typename ReturnType<internal::member_prod>::Type prod() const
00372     { return _expression(); }
00373 
00374 
00382     const Reverse<ExpressionType, Direction> reverse() const
00383     { return Reverse<ExpressionType, Direction>( _expression() ); }
00384 
00385     typedef Replicate<ExpressionType,Direction==Vertical?Dynamic:1,Direction==Horizontal?Dynamic:1> ReplicateReturnType;
00386     const ReplicateReturnType replicate(Index factor) const;
00387 
00396     // NOTE implemented here because of sunstudio's compilation errors
00397     template<int Factor> const Replicate<ExpressionType,(IsVertical?Factor:1),(IsHorizontal?Factor:1)>
00398     replicate(Index factor = Factor) const
00399     {
00400       return Replicate<ExpressionType,Direction==Vertical?Factor:1,Direction==Horizontal?Factor:1>
00401           (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
00402     }
00403 
00405 
00407     template<typename OtherDerived>
00408     ExpressionType& operator=(const DenseBase<OtherDerived>& other)
00409     {
00410       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00411       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00412       //eigen_assert((m_matrix.isNull()) == (other.isNull())); FIXME
00413       return const_cast<ExpressionType&>(m_matrix = extendedTo(other.derived()));
00414     }
00415 
00417     template<typename OtherDerived>
00418     ExpressionType& operator+=(const DenseBase<OtherDerived>& other)
00419     {
00420       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00421       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00422       return const_cast<ExpressionType&>(m_matrix += extendedTo(other.derived()));
00423     }
00424 
00426     template<typename OtherDerived>
00427     ExpressionType& operator-=(const DenseBase<OtherDerived>& other)
00428     {
00429       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00430       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00431       return const_cast<ExpressionType&>(m_matrix -= extendedTo(other.derived()));
00432     }
00433 
00435     template<typename OtherDerived>
00436     ExpressionType& operator*=(const DenseBase<OtherDerived>& other)
00437     {
00438       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00439       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
00440       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00441       m_matrix *= extendedTo(other.derived());
00442       return const_cast<ExpressionType&>(m_matrix);
00443     }
00444 
00446     template<typename OtherDerived>
00447     ExpressionType& operator/=(const DenseBase<OtherDerived>& other)
00448     {
00449       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00450       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
00451       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00452       m_matrix /= extendedTo(other.derived());
00453       return const_cast<ExpressionType&>(m_matrix);
00454     }
00455 
00457     template<typename OtherDerived> EIGEN_STRONG_INLINE
00458     CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
00459                   const ExpressionTypeNestedCleaned,
00460                   const typename ExtendedType<OtherDerived>::Type>
00461     operator+(const DenseBase<OtherDerived>& other) const
00462     {
00463       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00464       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00465       return m_matrix + extendedTo(other.derived());
00466     }
00467 
00469     template<typename OtherDerived>
00470     CwiseBinaryOp<internal::scalar_difference_op<Scalar>,
00471                   const ExpressionTypeNestedCleaned,
00472                   const typename ExtendedType<OtherDerived>::Type>
00473     operator-(const DenseBase<OtherDerived>& other) const
00474     {
00475       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00476       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00477       return m_matrix - extendedTo(other.derived());
00478     }
00479 
00482     template<typename OtherDerived> EIGEN_STRONG_INLINE
00483     CwiseBinaryOp<internal::scalar_product_op<Scalar>,
00484                   const ExpressionTypeNestedCleaned,
00485                   const typename ExtendedType<OtherDerived>::Type>
00486     operator*(const DenseBase<OtherDerived>& other) const
00487     {
00488       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00489       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
00490       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00491       return m_matrix * extendedTo(other.derived());
00492     }
00493 
00496     template<typename OtherDerived>
00497     CwiseBinaryOp<internal::scalar_quotient_op<Scalar>,
00498                   const ExpressionTypeNestedCleaned,
00499                   const typename ExtendedType<OtherDerived>::Type>
00500     operator/(const DenseBase<OtherDerived>& other) const
00501     {
00502       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00503       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
00504       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
00505       return m_matrix / extendedTo(other.derived());
00506     }
00507 
00509 
00510     #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
00511     Homogeneous<ExpressionType,Direction> homogeneous() const;
00512     #endif
00513 
00514     typedef typename ExpressionType::PlainObject CrossReturnType;
00515     template<typename OtherDerived>
00516     const CrossReturnType cross(const MatrixBase<OtherDerived>& other) const;
00517 
00518     enum {
00519       HNormalized_Size = Direction==Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
00520                                              : internal::traits<ExpressionType>::ColsAtCompileTime,
00521       HNormalized_SizeMinusOne = HNormalized_Size==Dynamic ? Dynamic : HNormalized_Size-1
00522     };
00523     typedef Block<const ExpressionType,
00524                   Direction==Vertical   ? int(HNormalized_SizeMinusOne)
00525                                         : int(internal::traits<ExpressionType>::RowsAtCompileTime),
00526                   Direction==Horizontal ? int(HNormalized_SizeMinusOne)
00527                                         : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
00528             HNormalized_Block;
00529     typedef Block<const ExpressionType,
00530                   Direction==Vertical   ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
00531                   Direction==Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
00532             HNormalized_Factors;
00533     typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
00534                 const HNormalized_Block,
00535                 const Replicate<HNormalized_Factors,
00536                   Direction==Vertical   ? HNormalized_SizeMinusOne : 1,
00537                   Direction==Horizontal ? HNormalized_SizeMinusOne : 1> >
00538             HNormalizedReturnType;
00539 
00540     const HNormalizedReturnType hnormalized() const;
00541 
00542   protected:
00543     ExpressionTypeNested m_matrix;
00544 };
00545 
00553 template<typename Derived>
00554 inline const typename DenseBase<Derived>::ConstColwiseReturnType
00555 DenseBase<Derived>::colwise() const
00556 {
00557   return derived();
00558 }
00559 
00564 template<typename Derived>
00565 inline typename DenseBase<Derived>::ColwiseReturnType
00566 DenseBase<Derived>::colwise()
00567 {
00568   return derived();
00569 }
00570 
00578 template<typename Derived>
00579 inline const typename DenseBase<Derived>::ConstRowwiseReturnType
00580 DenseBase<Derived>::rowwise() const
00581 {
00582   return derived();
00583 }
00584 
00589 template<typename Derived>
00590 inline typename DenseBase<Derived>::RowwiseReturnType
00591 DenseBase<Derived>::rowwise()
00592 {
00593   return derived();
00594 }
00595 
00596 } // end namespace Eigen
00597 
00598 #endif // EIGEN_PARTIAL_REDUX_H


win_eigen
Author(s): Daniel Stonier
autogenerated on Wed Sep 16 2015 07:12:40