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 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_PARTIAL_REDUX_H
00027 #define EIGEN_PARTIAL_REDUX_H
00028 
00045 template< typename MatrixType, typename MemberOp, int Direction>
00046 class PartialReduxExpr;
00047 
00048 namespace internal {
00049 template<typename MatrixType, typename MemberOp, int Direction>
00050 struct traits<PartialReduxExpr<MatrixType, MemberOp, Direction> >
00051  : traits<MatrixType>
00052 {
00053   typedef typename MemberOp::result_type Scalar;
00054   typedef typename traits<MatrixType>::StorageKind StorageKind;
00055   typedef typename traits<MatrixType>::XprKind XprKind;
00056   typedef typename MatrixType::Scalar InputScalar;
00057   typedef typename nested<MatrixType>::type MatrixTypeNested;
00058   typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
00059   enum {
00060     RowsAtCompileTime = Direction==Vertical   ? 1 : MatrixType::RowsAtCompileTime,
00061     ColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::ColsAtCompileTime,
00062     MaxRowsAtCompileTime = Direction==Vertical   ? 1 : MatrixType::MaxRowsAtCompileTime,
00063     MaxColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
00064     Flags0 = (unsigned int)_MatrixTypeNested::Flags & HereditaryBits,
00065     Flags = (Flags0 & ~RowMajorBit) | (RowsAtCompileTime == 1 ? RowMajorBit : 0),
00066     TraversalSize = Direction==Vertical ? RowsAtCompileTime : ColsAtCompileTime
00067   };
00068   #if EIGEN_GNUC_AT_LEAST(3,4)
00069   typedef typename MemberOp::template Cost<InputScalar,int(TraversalSize)> CostOpType;
00070   #else
00071   typedef typename MemberOp::template Cost<InputScalar,TraversalSize> CostOpType;
00072   #endif
00073   enum {
00074     CoeffReadCost = TraversalSize * traits<_MatrixTypeNested>::CoeffReadCost + int(CostOpType::value)
00075   };
00076 };
00077 }
00078 
00079 template< typename MatrixType, typename MemberOp, int Direction>
00080 class PartialReduxExpr : internal::no_assignment_operator,
00081   public internal::dense_xpr_base< PartialReduxExpr<MatrixType, MemberOp, Direction> >::type
00082 {
00083   public:
00084 
00085     typedef typename internal::dense_xpr_base<PartialReduxExpr>::type Base;
00086     EIGEN_DENSE_PUBLIC_INTERFACE(PartialReduxExpr)
00087     typedef typename internal::traits<PartialReduxExpr>::MatrixTypeNested MatrixTypeNested;
00088     typedef typename internal::traits<PartialReduxExpr>::_MatrixTypeNested _MatrixTypeNested;
00089 
00090     PartialReduxExpr(const MatrixType& mat, const MemberOp& func = MemberOp())
00091       : m_matrix(mat), m_functor(func) {}
00092 
00093     Index rows() const { return (Direction==Vertical   ? 1 : m_matrix.rows()); }
00094     Index cols() const { return (Direction==Horizontal ? 1 : m_matrix.cols()); }
00095 
00096     EIGEN_STRONG_INLINE const Scalar coeff(Index i, Index j) const
00097     {
00098       if (Direction==Vertical)
00099         return m_functor(m_matrix.col(j));
00100       else
00101         return m_functor(m_matrix.row(i));
00102     }
00103 
00104     const Scalar coeff(Index index) const
00105     {
00106       if (Direction==Vertical)
00107         return m_functor(m_matrix.col(index));
00108       else
00109         return m_functor(m_matrix.row(index));
00110     }
00111 
00112   protected:
00113     const MatrixTypeNested m_matrix;
00114     const MemberOp m_functor;
00115 };
00116 
00117 #define EIGEN_MEMBER_FUNCTOR(MEMBER,COST)                               \
00118   template <typename ResultType>                                        \
00119   struct member_##MEMBER {                                           \
00120     EIGEN_EMPTY_STRUCT_CTOR(member_##MEMBER)                         \
00121     typedef ResultType result_type;                                     \
00122     template<typename Scalar, int Size> struct Cost                     \
00123     { enum { value = COST }; };                                         \
00124     template<typename XprType>                                          \
00125     EIGEN_STRONG_INLINE ResultType operator()(const XprType& mat) const \
00126     { return mat.MEMBER(); } \
00127   }
00128 
00129 namespace internal {
00130 
00131 EIGEN_MEMBER_FUNCTOR(squaredNorm, Size * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
00132 EIGEN_MEMBER_FUNCTOR(norm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
00133 EIGEN_MEMBER_FUNCTOR(stableNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
00134 EIGEN_MEMBER_FUNCTOR(blueNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
00135 EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size-1) * functor_traits<scalar_hypot_op<Scalar> >::Cost );
00136 EIGEN_MEMBER_FUNCTOR(sum, (Size-1)*NumTraits<Scalar>::AddCost);
00137 EIGEN_MEMBER_FUNCTOR(mean, (Size-1)*NumTraits<Scalar>::AddCost + NumTraits<Scalar>::MulCost);
00138 EIGEN_MEMBER_FUNCTOR(minCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
00139 EIGEN_MEMBER_FUNCTOR(maxCoeff, (Size-1)*NumTraits<Scalar>::AddCost);
00140 EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits<Scalar>::AddCost);
00141 EIGEN_MEMBER_FUNCTOR(any, (Size-1)*NumTraits<Scalar>::AddCost);
00142 EIGEN_MEMBER_FUNCTOR(count, (Size-1)*NumTraits<Scalar>::AddCost);
00143 EIGEN_MEMBER_FUNCTOR(prod, (Size-1)*NumTraits<Scalar>::MulCost);
00144 
00145 
00146 template <typename BinaryOp, typename Scalar>
00147 struct member_redux {
00148   typedef typename result_of<
00149                      BinaryOp(Scalar)
00150                    >::type  result_type;
00151   template<typename _Scalar, int Size> struct Cost
00152   { enum { value = (Size-1) * functor_traits<BinaryOp>::Cost }; };
00153   member_redux(const BinaryOp func) : m_functor(func) {}
00154   template<typename Derived>
00155   inline result_type operator()(const DenseBase<Derived>& mat) const
00156   { return mat.redux(m_functor); }
00157   const BinaryOp m_functor;
00158 };
00159 }
00160 
00178 template<typename ExpressionType, int Direction> class VectorwiseOp
00179 {
00180   public:
00181 
00182     typedef typename ExpressionType::Scalar Scalar;
00183     typedef typename ExpressionType::RealScalar RealScalar;
00184     typedef typename ExpressionType::Index Index;
00185     typedef typename internal::conditional<internal::must_nest_by_value<ExpressionType>::ret,
00186         ExpressionType, ExpressionType&>::type ExpressionTypeNested;
00187     typedef typename internal::remove_all<ExpressionTypeNested>::type ExpressionTypeNestedCleaned;
00188 
00189     template<template<typename _Scalar> class Functor,
00190                       typename Scalar=typename internal::traits<ExpressionType>::Scalar> struct ReturnType
00191     {
00192       typedef PartialReduxExpr<ExpressionType,
00193                                Functor<Scalar>,
00194                                Direction
00195                               > Type;
00196     };
00197 
00198     template<typename BinaryOp> struct ReduxReturnType
00199     {
00200       typedef PartialReduxExpr<ExpressionType,
00201                                internal::member_redux<BinaryOp,typename internal::traits<ExpressionType>::Scalar>,
00202                                Direction
00203                               > Type;
00204     };
00205 
00206     enum {
00207       IsVertical   = (Direction==Vertical) ? 1 : 0,
00208       IsHorizontal = (Direction==Horizontal) ? 1 : 0
00209     };
00210 
00211   protected:
00212 
00215     typedef typename internal::conditional<Direction==Vertical,
00216                                typename ExpressionType::ColXpr,
00217                                typename ExpressionType::RowXpr>::type SubVector;
00218     SubVector subVector(Index i)
00219     {
00220       return SubVector(m_matrix.derived(),i);
00221     }
00222 
00225     Index subVectors() const
00226     { return Direction==Vertical?m_matrix.cols():m_matrix.rows(); }
00227 
00228     template<typename OtherDerived> struct ExtendedType {
00229       typedef Replicate<OtherDerived,
00230                         Direction==Vertical   ? 1 : ExpressionType::RowsAtCompileTime,
00231                         Direction==Horizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
00232     };
00233 
00236     template<typename OtherDerived>
00237     typename ExtendedType<OtherDerived>::Type
00238     extendedTo(const DenseBase<OtherDerived>& other) const
00239     {
00240       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived);
00241       return typename ExtendedType<OtherDerived>::Type
00242                       (other.derived(),
00243                        Direction==Vertical   ? 1 : m_matrix.rows(),
00244                        Direction==Horizontal ? 1 : m_matrix.cols());
00245     }
00246 
00247   public:
00248 
00249     inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
00250 
00252     inline const ExpressionType& _expression() const { return m_matrix; }
00253 
00261     template<typename BinaryOp>
00262     const typename ReduxReturnType<BinaryOp>::Type
00263     redux(const BinaryOp& func = BinaryOp()) const
00264     { return typename ReduxReturnType<BinaryOp>::Type(_expression(), func); }
00265 
00273     const typename ReturnType<internal::member_minCoeff>::Type minCoeff() const
00274     { return _expression(); }
00275 
00283     const typename ReturnType<internal::member_maxCoeff>::Type maxCoeff() const
00284     { return _expression(); }
00285 
00293     const typename ReturnType<internal::member_squaredNorm,RealScalar>::Type squaredNorm() const
00294     { return _expression(); }
00295 
00303     const typename ReturnType<internal::member_norm,RealScalar>::Type norm() const
00304     { return _expression(); }
00305 
00306 
00312     const typename ReturnType<internal::member_blueNorm,RealScalar>::Type blueNorm() const
00313     { return _expression(); }
00314 
00315 
00321     const typename ReturnType<internal::member_stableNorm,RealScalar>::Type stableNorm() const
00322     { return _expression(); }
00323 
00324 
00330     const typename ReturnType<internal::member_hypotNorm,RealScalar>::Type hypotNorm() const
00331     { return _expression(); }
00332 
00340     const typename ReturnType<internal::member_sum>::Type sum() const
00341     { return _expression(); }
00342 
00347     const typename ReturnType<internal::member_mean>::Type mean() const
00348     { return _expression(); }
00349 
00354     const typename ReturnType<internal::member_all>::Type all() const
00355     { return _expression(); }
00356 
00361     const typename ReturnType<internal::member_any>::Type any() const
00362     { return _expression(); }
00363 
00371     const PartialReduxExpr<ExpressionType, internal::member_count<Index>, Direction> count() const
00372     { return _expression(); }
00373 
00381     const typename ReturnType<internal::member_prod>::Type prod() const
00382     { return _expression(); }
00383 
00384 
00392     const Reverse<ExpressionType, Direction> reverse() const
00393     { return Reverse<ExpressionType, Direction>( _expression() ); }
00394 
00395     typedef Replicate<ExpressionType,Direction==Vertical?Dynamic:1,Direction==Horizontal?Dynamic:1> ReplicateReturnType;
00396     const ReplicateReturnType replicate(Index factor) const;
00397 
00406     // NOTE implemented here because of sunstudio's compilation errors
00407     template<int Factor> const Replicate<ExpressionType,(IsVertical?Factor:1),(IsHorizontal?Factor:1)>
00408     replicate(Index factor = Factor) const
00409     {
00410       return Replicate<ExpressionType,Direction==Vertical?Factor:1,Direction==Horizontal?Factor:1>
00411           (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
00412     }
00413 
00415 
00417     template<typename OtherDerived>
00418     ExpressionType& operator=(const DenseBase<OtherDerived>& other)
00419     {
00420       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00421       //eigen_assert((m_matrix.isNull()) == (other.isNull())); FIXME
00422       for(Index j=0; j<subVectors(); ++j)
00423         subVector(j) = other;
00424       return const_cast<ExpressionType&>(m_matrix);
00425     }
00426 
00428     template<typename OtherDerived>
00429     ExpressionType& operator+=(const DenseBase<OtherDerived>& other)
00430     {
00431       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00432       for(Index j=0; j<subVectors(); ++j)
00433         subVector(j) += other.derived();
00434       return const_cast<ExpressionType&>(m_matrix);
00435     }
00436 
00438     template<typename OtherDerived>
00439     ExpressionType& operator-=(const DenseBase<OtherDerived>& other)
00440     {
00441       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00442       for(Index j=0; j<subVectors(); ++j)
00443         subVector(j) -= other.derived();
00444       return const_cast<ExpressionType&>(m_matrix);
00445     }
00446 
00448     template<typename OtherDerived> EIGEN_STRONG_INLINE
00449     CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
00450                   const ExpressionTypeNestedCleaned,
00451                   const typename ExtendedType<OtherDerived>::Type>
00452     operator+(const DenseBase<OtherDerived>& other) const
00453     {
00454       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived);
00455       return m_matrix + extendedTo(other.derived());
00456     }
00457 
00459     template<typename OtherDerived>
00460     CwiseBinaryOp<internal::scalar_difference_op<Scalar>,
00461                   const ExpressionTypeNestedCleaned,
00462                   const typename ExtendedType<OtherDerived>::Type>
00463     operator-(const DenseBase<OtherDerived>& other) const
00464     {
00465       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived);
00466       return m_matrix - extendedTo(other.derived());
00467     }
00468 
00470 
00471     #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
00472     Homogeneous<ExpressionType,Direction> homogeneous() const;
00473     #endif
00474 
00475     typedef typename ExpressionType::PlainObject CrossReturnType;
00476     template<typename OtherDerived>
00477     const CrossReturnType cross(const MatrixBase<OtherDerived>& other) const;
00478 
00479     enum {
00480       HNormalized_Size = Direction==Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
00481                                              : internal::traits<ExpressionType>::ColsAtCompileTime,
00482       HNormalized_SizeMinusOne = HNormalized_Size==Dynamic ? Dynamic : HNormalized_Size-1
00483     };
00484     typedef Block<const ExpressionType,
00485                   Direction==Vertical   ? int(HNormalized_SizeMinusOne)
00486                                         : int(internal::traits<ExpressionType>::RowsAtCompileTime),
00487                   Direction==Horizontal ? int(HNormalized_SizeMinusOne)
00488                                         : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
00489             HNormalized_Block;
00490     typedef Block<const ExpressionType,
00491                   Direction==Vertical   ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
00492                   Direction==Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
00493             HNormalized_Factors;
00494     typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
00495                 const HNormalized_Block,
00496                 const Replicate<HNormalized_Factors,
00497                   Direction==Vertical   ? HNormalized_SizeMinusOne : 1,
00498                   Direction==Horizontal ? HNormalized_SizeMinusOne : 1> >
00499             HNormalizedReturnType;
00500 
00501     const HNormalizedReturnType hnormalized() const;
00502 
00503   protected:
00504     ExpressionTypeNested m_matrix;
00505 };
00506 
00514 template<typename Derived>
00515 inline const typename DenseBase<Derived>::ConstColwiseReturnType
00516 DenseBase<Derived>::colwise() const
00517 {
00518   return derived();
00519 }
00520 
00525 template<typename Derived>
00526 inline typename DenseBase<Derived>::ColwiseReturnType
00527 DenseBase<Derived>::colwise()
00528 {
00529   return derived();
00530 }
00531 
00539 template<typename Derived>
00540 inline const typename DenseBase<Derived>::ConstRowwiseReturnType
00541 DenseBase<Derived>::rowwise() const
00542 {
00543   return derived();
00544 }
00545 
00550 template<typename Derived>
00551 inline typename DenseBase<Derived>::RowwiseReturnType
00552 DenseBase<Derived>::rowwise()
00553 {
00554   return derived();
00555 }
00556 
00557 #endif // EIGEN_PARTIAL_REDUX_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:41