Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:56:19

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008-2019 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_PARTIAL_REDUX_H
0012 #define EIGEN_PARTIAL_REDUX_H
0013 
0014 namespace Eigen {
0015 
0016 /** \class PartialReduxExpr
0017   * \ingroup Core_Module
0018   *
0019   * \brief Generic expression of a partially reduxed matrix
0020   *
0021   * \tparam MatrixType the type of the matrix we are applying the redux operation
0022   * \tparam MemberOp type of the member functor
0023   * \tparam Direction indicates the direction of the redux (#Vertical or #Horizontal)
0024   *
0025   * This class represents an expression of a partial redux operator of a matrix.
0026   * It is the return type of some VectorwiseOp functions,
0027   * and most of the time this is the only way it is used.
0028   *
0029   * \sa class VectorwiseOp
0030   */
0031 
0032 template< typename MatrixType, typename MemberOp, int Direction>
0033 class PartialReduxExpr;
0034 
0035 namespace internal {
0036 template<typename MatrixType, typename MemberOp, int Direction>
0037 struct traits<PartialReduxExpr<MatrixType, MemberOp, Direction> >
0038  : traits<MatrixType>
0039 {
0040   typedef typename MemberOp::result_type Scalar;
0041   typedef typename traits<MatrixType>::StorageKind StorageKind;
0042   typedef typename traits<MatrixType>::XprKind XprKind;
0043   typedef typename MatrixType::Scalar InputScalar;
0044   enum {
0045     RowsAtCompileTime = Direction==Vertical   ? 1 : MatrixType::RowsAtCompileTime,
0046     ColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::ColsAtCompileTime,
0047     MaxRowsAtCompileTime = Direction==Vertical   ? 1 : MatrixType::MaxRowsAtCompileTime,
0048     MaxColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
0049     Flags = RowsAtCompileTime == 1 ? RowMajorBit : 0,
0050     TraversalSize = Direction==Vertical ? MatrixType::RowsAtCompileTime :  MatrixType::ColsAtCompileTime
0051   };
0052 };
0053 }
0054 
0055 template< typename MatrixType, typename MemberOp, int Direction>
0056 class PartialReduxExpr : public internal::dense_xpr_base< PartialReduxExpr<MatrixType, MemberOp, Direction> >::type,
0057                          internal::no_assignment_operator
0058 {
0059   public:
0060 
0061     typedef typename internal::dense_xpr_base<PartialReduxExpr>::type Base;
0062     EIGEN_DENSE_PUBLIC_INTERFACE(PartialReduxExpr)
0063 
0064     EIGEN_DEVICE_FUNC
0065     explicit PartialReduxExpr(const MatrixType& mat, const MemberOp& func = MemberOp())
0066       : m_matrix(mat), m_functor(func) {}
0067 
0068     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0069     Index rows() const EIGEN_NOEXCEPT { return (Direction==Vertical   ? 1 : m_matrix.rows()); }
0070     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0071     Index cols() const EIGEN_NOEXCEPT { return (Direction==Horizontal ? 1 : m_matrix.cols()); }
0072 
0073     EIGEN_DEVICE_FUNC
0074     typename MatrixType::Nested nestedExpression() const { return m_matrix; }
0075 
0076     EIGEN_DEVICE_FUNC
0077     const MemberOp& functor() const { return m_functor; }
0078 
0079   protected:
0080     typename MatrixType::Nested m_matrix;
0081     const MemberOp m_functor;
0082 };
0083 
0084 template<typename A,typename B> struct partial_redux_dummy_func;
0085 
0086 #define EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(MEMBER,COST,VECTORIZABLE,BINARYOP)                \
0087   template <typename ResultType,typename Scalar>                                                            \
0088   struct member_##MEMBER {                                                                  \
0089     EIGEN_EMPTY_STRUCT_CTOR(member_##MEMBER)                                                \
0090     typedef ResultType result_type;                                                         \
0091     typedef BINARYOP<Scalar,Scalar> BinaryOp;   \
0092     template<int Size> struct Cost { enum { value = COST }; };             \
0093     enum { Vectorizable = VECTORIZABLE };                                                   \
0094     template<typename XprType>                                                              \
0095     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE                                                   \
0096     ResultType operator()(const XprType& mat) const                                         \
0097     { return mat.MEMBER(); }                                                                \
0098     BinaryOp binaryFunc() const { return BinaryOp(); }                                      \
0099   }
0100 
0101 #define EIGEN_MEMBER_FUNCTOR(MEMBER,COST) \
0102   EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(MEMBER,COST,0,partial_redux_dummy_func)
0103 
0104 namespace internal {
0105 
0106 EIGEN_MEMBER_FUNCTOR(norm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
0107 EIGEN_MEMBER_FUNCTOR(stableNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
0108 EIGEN_MEMBER_FUNCTOR(blueNorm, (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost);
0109 EIGEN_MEMBER_FUNCTOR(hypotNorm, (Size-1) * functor_traits<scalar_hypot_op<Scalar> >::Cost );
0110 EIGEN_MEMBER_FUNCTOR(all, (Size-1)*NumTraits<Scalar>::AddCost);
0111 EIGEN_MEMBER_FUNCTOR(any, (Size-1)*NumTraits<Scalar>::AddCost);
0112 EIGEN_MEMBER_FUNCTOR(count, (Size-1)*NumTraits<Scalar>::AddCost);
0113 
0114 EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(sum, (Size-1)*NumTraits<Scalar>::AddCost, 1, internal::scalar_sum_op);
0115 EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(minCoeff, (Size-1)*NumTraits<Scalar>::AddCost, 1, internal::scalar_min_op);
0116 EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(maxCoeff, (Size-1)*NumTraits<Scalar>::AddCost, 1, internal::scalar_max_op);
0117 EIGEN_MAKE_PARTIAL_REDUX_FUNCTOR(prod, (Size-1)*NumTraits<Scalar>::MulCost, 1, internal::scalar_product_op);
0118 
0119 template <int p, typename ResultType,typename Scalar>
0120 struct member_lpnorm {
0121   typedef ResultType result_type;
0122   enum { Vectorizable = 0 };
0123   template<int Size> struct Cost
0124   { enum { value = (Size+5) * NumTraits<Scalar>::MulCost + (Size-1)*NumTraits<Scalar>::AddCost }; };
0125   EIGEN_DEVICE_FUNC member_lpnorm() {}
0126   template<typename XprType>
0127   EIGEN_DEVICE_FUNC inline ResultType operator()(const XprType& mat) const
0128   { return mat.template lpNorm<p>(); }
0129 };
0130 
0131 template <typename BinaryOpT, typename Scalar>
0132 struct member_redux {
0133   typedef BinaryOpT BinaryOp;
0134   typedef typename result_of<
0135                      BinaryOp(const Scalar&,const Scalar&)
0136                    >::type  result_type;
0137 
0138   enum { Vectorizable = functor_traits<BinaryOp>::PacketAccess };
0139   template<int Size> struct Cost { enum { value = (Size-1) * functor_traits<BinaryOp>::Cost }; };
0140   EIGEN_DEVICE_FUNC explicit member_redux(const BinaryOp func) : m_functor(func) {}
0141   template<typename Derived>
0142   EIGEN_DEVICE_FUNC inline result_type operator()(const DenseBase<Derived>& mat) const
0143   { return mat.redux(m_functor); }
0144   const BinaryOp& binaryFunc() const { return m_functor; }
0145   const BinaryOp m_functor;
0146 };
0147 }
0148 
0149 /** \class VectorwiseOp
0150   * \ingroup Core_Module
0151   *
0152   * \brief Pseudo expression providing broadcasting and partial reduction operations
0153   *
0154   * \tparam ExpressionType the type of the object on which to do partial reductions
0155   * \tparam Direction indicates whether to operate on columns (#Vertical) or rows (#Horizontal)
0156   *
0157   * This class represents a pseudo expression with broadcasting and partial reduction features.
0158   * It is the return type of DenseBase::colwise() and DenseBase::rowwise()
0159   * and most of the time this is the only way it is explicitly used.
0160   *
0161   * To understand the logic of rowwise/colwise expression, let's consider a generic case `A.colwise().foo()`
0162   * where `foo` is any method of `VectorwiseOp`. This expression is equivalent to applying `foo()` to each
0163   * column of `A` and then re-assemble the outputs in a matrix expression:
0164   * \code [A.col(0).foo(), A.col(1).foo(), ..., A.col(A.cols()-1).foo()] \endcode
0165   *
0166   * Example: \include MatrixBase_colwise.cpp
0167   * Output: \verbinclude MatrixBase_colwise.out
0168   *
0169   * The begin() and end() methods are obviously exceptions to the previous rule as they
0170   * return STL-compatible begin/end iterators to the rows or columns of the nested expression.
0171   * Typical use cases include for-range-loop and calls to STL algorithms:
0172   *
0173   * Example: \include MatrixBase_colwise_iterator_cxx11.cpp
0174   * Output: \verbinclude MatrixBase_colwise_iterator_cxx11.out
0175   *
0176   * For a partial reduction on an empty input, some rules apply.
0177   * For the sake of clarity, let's consider a vertical reduction:
0178   *   - If the number of columns is zero, then a 1x0 row-major vector expression is returned.
0179   *   - Otherwise, if the number of rows is zero, then
0180   *       - a row vector of zeros is returned for sum-like reductions (sum, squaredNorm, norm, etc.)
0181   *       - a row vector of ones is returned for a product reduction (e.g., <code>MatrixXd(n,0).colwise().prod()</code>)
0182   *       - an assert is triggered for all other reductions (minCoeff,maxCoeff,redux(bin_op))
0183   *
0184   * \sa DenseBase::colwise(), DenseBase::rowwise(), class PartialReduxExpr
0185   */
0186 template<typename ExpressionType, int Direction> class VectorwiseOp
0187 {
0188   public:
0189 
0190     typedef typename ExpressionType::Scalar Scalar;
0191     typedef typename ExpressionType::RealScalar RealScalar;
0192     typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
0193     typedef typename internal::ref_selector<ExpressionType>::non_const_type ExpressionTypeNested;
0194     typedef typename internal::remove_all<ExpressionTypeNested>::type ExpressionTypeNestedCleaned;
0195 
0196     template<template<typename OutScalar,typename InputScalar> class Functor,
0197                       typename ReturnScalar=Scalar> struct ReturnType
0198     {
0199       typedef PartialReduxExpr<ExpressionType,
0200                                Functor<ReturnScalar,Scalar>,
0201                                Direction
0202                               > Type;
0203     };
0204 
0205     template<typename BinaryOp> struct ReduxReturnType
0206     {
0207       typedef PartialReduxExpr<ExpressionType,
0208                                internal::member_redux<BinaryOp,Scalar>,
0209                                Direction
0210                               > Type;
0211     };
0212 
0213     enum {
0214       isVertical   = (Direction==Vertical) ? 1 : 0,
0215       isHorizontal = (Direction==Horizontal) ? 1 : 0
0216     };
0217 
0218   protected:
0219 
0220     template<typename OtherDerived> struct ExtendedType {
0221       typedef Replicate<OtherDerived,
0222                         isVertical   ? 1 : ExpressionType::RowsAtCompileTime,
0223                         isHorizontal ? 1 : ExpressionType::ColsAtCompileTime> Type;
0224     };
0225 
0226     /** \internal
0227       * Replicates a vector to match the size of \c *this */
0228     template<typename OtherDerived>
0229     EIGEN_DEVICE_FUNC
0230     typename ExtendedType<OtherDerived>::Type
0231     extendedTo(const DenseBase<OtherDerived>& other) const
0232     {
0233       EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(isVertical, OtherDerived::MaxColsAtCompileTime==1),
0234                           YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
0235       EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(isHorizontal, OtherDerived::MaxRowsAtCompileTime==1),
0236                           YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
0237       return typename ExtendedType<OtherDerived>::Type
0238                       (other.derived(),
0239                        isVertical   ? 1 : m_matrix.rows(),
0240                        isHorizontal ? 1 : m_matrix.cols());
0241     }
0242 
0243     template<typename OtherDerived> struct OppositeExtendedType {
0244       typedef Replicate<OtherDerived,
0245                         isHorizontal ? 1 : ExpressionType::RowsAtCompileTime,
0246                         isVertical   ? 1 : ExpressionType::ColsAtCompileTime> Type;
0247     };
0248 
0249     /** \internal
0250       * Replicates a vector in the opposite direction to match the size of \c *this */
0251     template<typename OtherDerived>
0252     EIGEN_DEVICE_FUNC
0253     typename OppositeExtendedType<OtherDerived>::Type
0254     extendedToOpposite(const DenseBase<OtherDerived>& other) const
0255     {
0256       EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(isHorizontal, OtherDerived::MaxColsAtCompileTime==1),
0257                           YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED)
0258       EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(isVertical, OtherDerived::MaxRowsAtCompileTime==1),
0259                           YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED)
0260       return typename OppositeExtendedType<OtherDerived>::Type
0261                       (other.derived(),
0262                        isHorizontal  ? 1 : m_matrix.rows(),
0263                        isVertical    ? 1 : m_matrix.cols());
0264     }
0265 
0266   public:
0267     EIGEN_DEVICE_FUNC
0268     explicit inline VectorwiseOp(ExpressionType& matrix) : m_matrix(matrix) {}
0269 
0270     /** \internal */
0271     EIGEN_DEVICE_FUNC
0272     inline const ExpressionType& _expression() const { return m_matrix; }
0273 
0274     #ifdef EIGEN_PARSED_BY_DOXYGEN
0275     /** STL-like <a href="https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator">RandomAccessIterator</a>
0276       * iterator type over the columns or rows as returned by the begin() and end() methods.
0277       */
0278     random_access_iterator_type iterator;
0279     /** This is the const version of iterator (aka read-only) */
0280     random_access_iterator_type const_iterator;
0281     #else
0282     typedef internal::subvector_stl_iterator<ExpressionType,               DirectionType(Direction)> iterator;
0283     typedef internal::subvector_stl_iterator<const ExpressionType,         DirectionType(Direction)> const_iterator;
0284     typedef internal::subvector_stl_reverse_iterator<ExpressionType,       DirectionType(Direction)> reverse_iterator;
0285     typedef internal::subvector_stl_reverse_iterator<const ExpressionType, DirectionType(Direction)> const_reverse_iterator;
0286     #endif
0287 
0288     /** returns an iterator to the first row (rowwise) or column (colwise) of the nested expression.
0289       * \sa end(), cbegin()
0290       */
0291     iterator                 begin()       { return iterator      (m_matrix, 0); }
0292     /** const version of begin() */
0293     const_iterator           begin() const { return const_iterator(m_matrix, 0); }
0294     /** const version of begin() */
0295     const_iterator          cbegin() const { return const_iterator(m_matrix, 0); }
0296 
0297     /** returns a reverse iterator to the last row (rowwise) or column (colwise) of the nested expression.
0298       * \sa rend(), crbegin()
0299       */
0300     reverse_iterator        rbegin()       { return reverse_iterator       (m_matrix, m_matrix.template subVectors<DirectionType(Direction)>()-1); }
0301     /** const version of rbegin() */
0302     const_reverse_iterator  rbegin() const { return const_reverse_iterator (m_matrix, m_matrix.template subVectors<DirectionType(Direction)>()-1); }
0303     /** const version of rbegin() */
0304     const_reverse_iterator crbegin() const { return const_reverse_iterator (m_matrix, m_matrix.template subVectors<DirectionType(Direction)>()-1); }
0305 
0306     /** returns an iterator to the row (resp. column) following the last row (resp. column) of the nested expression
0307       * \sa begin(), cend()
0308       */
0309     iterator                 end()         { return iterator      (m_matrix, m_matrix.template subVectors<DirectionType(Direction)>()); }
0310     /** const version of end() */
0311     const_iterator           end()  const  { return const_iterator(m_matrix, m_matrix.template subVectors<DirectionType(Direction)>()); }
0312     /** const version of end() */
0313     const_iterator          cend()  const  { return const_iterator(m_matrix, m_matrix.template subVectors<DirectionType(Direction)>()); }
0314 
0315     /** returns a reverse iterator to the row (resp. column) before the first row (resp. column) of the nested expression
0316       * \sa begin(), cend()
0317       */
0318     reverse_iterator        rend()         { return reverse_iterator       (m_matrix, -1); }
0319     /** const version of rend() */
0320     const_reverse_iterator  rend()  const  { return const_reverse_iterator (m_matrix, -1); }
0321     /** const version of rend() */
0322     const_reverse_iterator crend()  const  { return const_reverse_iterator (m_matrix, -1); }
0323 
0324     /** \returns a row or column vector expression of \c *this reduxed by \a func
0325       *
0326       * The template parameter \a BinaryOp is the type of the functor
0327       * of the custom redux operator. Note that func must be an associative operator.
0328       *
0329       * \warning the size along the reduction direction must be strictly positive,
0330       *          otherwise an assertion is triggered.
0331       *
0332       * \sa class VectorwiseOp, DenseBase::colwise(), DenseBase::rowwise()
0333       */
0334     template<typename BinaryOp>
0335     EIGEN_DEVICE_FUNC
0336     const typename ReduxReturnType<BinaryOp>::Type
0337     redux(const BinaryOp& func = BinaryOp()) const
0338     {
0339       eigen_assert(redux_length()>0 && "you are using an empty matrix");
0340       return typename ReduxReturnType<BinaryOp>::Type(_expression(), internal::member_redux<BinaryOp,Scalar>(func));
0341     }
0342 
0343     typedef typename ReturnType<internal::member_minCoeff>::Type MinCoeffReturnType;
0344     typedef typename ReturnType<internal::member_maxCoeff>::Type MaxCoeffReturnType;
0345     typedef PartialReduxExpr<const CwiseUnaryOp<internal::scalar_abs2_op<Scalar>, const ExpressionTypeNestedCleaned>,internal::member_sum<RealScalar,RealScalar>,Direction> SquaredNormReturnType;
0346     typedef CwiseUnaryOp<internal::scalar_sqrt_op<RealScalar>, const SquaredNormReturnType> NormReturnType;
0347     typedef typename ReturnType<internal::member_blueNorm,RealScalar>::Type BlueNormReturnType;
0348     typedef typename ReturnType<internal::member_stableNorm,RealScalar>::Type StableNormReturnType;
0349     typedef typename ReturnType<internal::member_hypotNorm,RealScalar>::Type HypotNormReturnType;
0350     typedef typename ReturnType<internal::member_sum>::Type SumReturnType;
0351     typedef EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(SumReturnType,Scalar,quotient) MeanReturnType;
0352     typedef typename ReturnType<internal::member_all>::Type AllReturnType;
0353     typedef typename ReturnType<internal::member_any>::Type AnyReturnType;
0354     typedef PartialReduxExpr<ExpressionType, internal::member_count<Index,Scalar>, Direction> CountReturnType;
0355     typedef typename ReturnType<internal::member_prod>::Type ProdReturnType;
0356     typedef Reverse<const ExpressionType, Direction> ConstReverseReturnType;
0357     typedef Reverse<ExpressionType, Direction> ReverseReturnType;
0358 
0359     template<int p> struct LpNormReturnType {
0360       typedef PartialReduxExpr<ExpressionType, internal::member_lpnorm<p,RealScalar,Scalar>,Direction> Type;
0361     };
0362 
0363     /** \returns a row (or column) vector expression of the smallest coefficient
0364       * of each column (or row) of the referenced expression.
0365       *
0366       * \warning the size along the reduction direction must be strictly positive,
0367       *          otherwise an assertion is triggered.
0368       *
0369       * \warning the result is undefined if \c *this contains NaN.
0370       *
0371       * Example: \include PartialRedux_minCoeff.cpp
0372       * Output: \verbinclude PartialRedux_minCoeff.out
0373       *
0374       * \sa DenseBase::minCoeff() */
0375     EIGEN_DEVICE_FUNC
0376     const MinCoeffReturnType minCoeff() const
0377     {
0378       eigen_assert(redux_length()>0 && "you are using an empty matrix");
0379       return MinCoeffReturnType(_expression());
0380     }
0381 
0382     /** \returns a row (or column) vector expression of the largest coefficient
0383       * of each column (or row) of the referenced expression.
0384       *
0385       * \warning the size along the reduction direction must be strictly positive,
0386       *          otherwise an assertion is triggered.
0387       *
0388       * \warning the result is undefined if \c *this contains NaN.
0389       *
0390       * Example: \include PartialRedux_maxCoeff.cpp
0391       * Output: \verbinclude PartialRedux_maxCoeff.out
0392       *
0393       * \sa DenseBase::maxCoeff() */
0394     EIGEN_DEVICE_FUNC
0395     const MaxCoeffReturnType maxCoeff() const
0396     {
0397       eigen_assert(redux_length()>0 && "you are using an empty matrix");
0398       return MaxCoeffReturnType(_expression());
0399     }
0400 
0401     /** \returns a row (or column) vector expression of the squared norm
0402       * of each column (or row) of the referenced expression.
0403       * This is a vector with real entries, even if the original matrix has complex entries.
0404       *
0405       * Example: \include PartialRedux_squaredNorm.cpp
0406       * Output: \verbinclude PartialRedux_squaredNorm.out
0407       *
0408       * \sa DenseBase::squaredNorm() */
0409     EIGEN_DEVICE_FUNC
0410     const SquaredNormReturnType squaredNorm() const
0411     { return SquaredNormReturnType(m_matrix.cwiseAbs2()); }
0412 
0413     /** \returns a row (or column) vector expression of the norm
0414       * of each column (or row) of the referenced expression.
0415       * This is a vector with real entries, even if the original matrix has complex entries.
0416       *
0417       * Example: \include PartialRedux_norm.cpp
0418       * Output: \verbinclude PartialRedux_norm.out
0419       *
0420       * \sa DenseBase::norm() */
0421     EIGEN_DEVICE_FUNC
0422     const NormReturnType norm() const
0423     { return NormReturnType(squaredNorm()); }
0424 
0425     /** \returns a row (or column) vector expression of the norm
0426       * of each column (or row) of the referenced expression.
0427       * This is a vector with real entries, even if the original matrix has complex entries.
0428       *
0429       * Example: \include PartialRedux_norm.cpp
0430       * Output: \verbinclude PartialRedux_norm.out
0431       *
0432       * \sa DenseBase::norm() */
0433     template<int p>
0434     EIGEN_DEVICE_FUNC
0435     const typename LpNormReturnType<p>::Type lpNorm() const
0436     { return typename LpNormReturnType<p>::Type(_expression()); }
0437 
0438 
0439     /** \returns a row (or column) vector expression of the norm
0440       * of each column (or row) of the referenced expression, using
0441       * Blue's algorithm.
0442       * This is a vector with real entries, even if the original matrix has complex entries.
0443       *
0444       * \sa DenseBase::blueNorm() */
0445     EIGEN_DEVICE_FUNC
0446     const BlueNormReturnType blueNorm() const
0447     { return BlueNormReturnType(_expression()); }
0448 
0449 
0450     /** \returns a row (or column) vector expression of the norm
0451       * of each column (or row) of the referenced expression, avoiding
0452       * underflow and overflow.
0453       * This is a vector with real entries, even if the original matrix has complex entries.
0454       *
0455       * \sa DenseBase::stableNorm() */
0456     EIGEN_DEVICE_FUNC
0457     const StableNormReturnType stableNorm() const
0458     { return StableNormReturnType(_expression()); }
0459 
0460 
0461     /** \returns a row (or column) vector expression of the norm
0462       * of each column (or row) of the referenced expression, avoiding
0463       * underflow and overflow using a concatenation of hypot() calls.
0464       * This is a vector with real entries, even if the original matrix has complex entries.
0465       *
0466       * \sa DenseBase::hypotNorm() */
0467     EIGEN_DEVICE_FUNC
0468     const HypotNormReturnType hypotNorm() const
0469     { return HypotNormReturnType(_expression()); }
0470 
0471     /** \returns a row (or column) vector expression of the sum
0472       * of each column (or row) of the referenced expression.
0473       *
0474       * Example: \include PartialRedux_sum.cpp
0475       * Output: \verbinclude PartialRedux_sum.out
0476       *
0477       * \sa DenseBase::sum() */
0478     EIGEN_DEVICE_FUNC
0479     const SumReturnType sum() const
0480     { return SumReturnType(_expression()); }
0481 
0482     /** \returns a row (or column) vector expression of the mean
0483     * of each column (or row) of the referenced expression.
0484     *
0485     * \sa DenseBase::mean() */
0486     EIGEN_DEVICE_FUNC
0487     const MeanReturnType mean() const
0488     { return sum() / Scalar(Direction==Vertical?m_matrix.rows():m_matrix.cols()); }
0489 
0490     /** \returns a row (or column) vector expression representing
0491       * whether \b all coefficients of each respective column (or row) are \c true.
0492       * This expression can be assigned to a vector with entries of type \c bool.
0493       *
0494       * \sa DenseBase::all() */
0495     EIGEN_DEVICE_FUNC
0496     const AllReturnType all() const
0497     { return AllReturnType(_expression()); }
0498 
0499     /** \returns a row (or column) vector expression representing
0500       * whether \b at \b least one coefficient of each respective column (or row) is \c true.
0501       * This expression can be assigned to a vector with entries of type \c bool.
0502       *
0503       * \sa DenseBase::any() */
0504     EIGEN_DEVICE_FUNC
0505     const AnyReturnType any() const
0506     { return AnyReturnType(_expression()); }
0507 
0508     /** \returns a row (or column) vector expression representing
0509       * the number of \c true coefficients of each respective column (or row).
0510       * This expression can be assigned to a vector whose entries have the same type as is used to
0511       * index entries of the original matrix; for dense matrices, this is \c std::ptrdiff_t .
0512       *
0513       * Example: \include PartialRedux_count.cpp
0514       * Output: \verbinclude PartialRedux_count.out
0515       *
0516       * \sa DenseBase::count() */
0517     EIGEN_DEVICE_FUNC
0518     const CountReturnType count() const
0519     { return CountReturnType(_expression()); }
0520 
0521     /** \returns a row (or column) vector expression of the product
0522       * of each column (or row) of the referenced expression.
0523       *
0524       * Example: \include PartialRedux_prod.cpp
0525       * Output: \verbinclude PartialRedux_prod.out
0526       *
0527       * \sa DenseBase::prod() */
0528     EIGEN_DEVICE_FUNC
0529     const ProdReturnType prod() const
0530     { return ProdReturnType(_expression()); }
0531 
0532 
0533     /** \returns a matrix expression
0534       * where each column (or row) are reversed.
0535       *
0536       * Example: \include Vectorwise_reverse.cpp
0537       * Output: \verbinclude Vectorwise_reverse.out
0538       *
0539       * \sa DenseBase::reverse() */
0540     EIGEN_DEVICE_FUNC
0541     const ConstReverseReturnType reverse() const
0542     { return ConstReverseReturnType( _expression() ); }
0543 
0544     /** \returns a writable matrix expression
0545       * where each column (or row) are reversed.
0546       *
0547       * \sa reverse() const */
0548     EIGEN_DEVICE_FUNC
0549     ReverseReturnType reverse()
0550     { return ReverseReturnType( _expression() ); }
0551 
0552     typedef Replicate<ExpressionType,(isVertical?Dynamic:1),(isHorizontal?Dynamic:1)> ReplicateReturnType;
0553     EIGEN_DEVICE_FUNC
0554     const ReplicateReturnType replicate(Index factor) const;
0555 
0556     /**
0557       * \return an expression of the replication of each column (or row) of \c *this
0558       *
0559       * Example: \include DirectionWise_replicate.cpp
0560       * Output: \verbinclude DirectionWise_replicate.out
0561       *
0562       * \sa VectorwiseOp::replicate(Index), DenseBase::replicate(), class Replicate
0563       */
0564     // NOTE implemented here because of sunstudio's compilation errors
0565     // isVertical*Factor+isHorizontal instead of (isVertical?Factor:1) to handle CUDA bug with ternary operator
0566     template<int Factor> const Replicate<ExpressionType,isVertical*Factor+isHorizontal,isHorizontal*Factor+isVertical>
0567     EIGEN_DEVICE_FUNC
0568     replicate(Index factor = Factor) const
0569     {
0570       return Replicate<ExpressionType,(isVertical?Factor:1),(isHorizontal?Factor:1)>
0571           (_expression(),isVertical?factor:1,isHorizontal?factor:1);
0572     }
0573 
0574 /////////// Artithmetic operators ///////////
0575 
0576     /** Copies the vector \a other to each subvector of \c *this */
0577     template<typename OtherDerived>
0578     EIGEN_DEVICE_FUNC
0579     ExpressionType& operator=(const DenseBase<OtherDerived>& other)
0580     {
0581       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
0582       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
0583       //eigen_assert((m_matrix.isNull()) == (other.isNull())); FIXME
0584       return m_matrix = extendedTo(other.derived());
0585     }
0586 
0587     /** Adds the vector \a other to each subvector of \c *this */
0588     template<typename OtherDerived>
0589     EIGEN_DEVICE_FUNC
0590     ExpressionType& operator+=(const DenseBase<OtherDerived>& other)
0591     {
0592       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
0593       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
0594       return m_matrix += extendedTo(other.derived());
0595     }
0596 
0597     /** Substracts the vector \a other to each subvector of \c *this */
0598     template<typename OtherDerived>
0599     EIGEN_DEVICE_FUNC
0600     ExpressionType& operator-=(const DenseBase<OtherDerived>& other)
0601     {
0602       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
0603       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
0604       return m_matrix -= extendedTo(other.derived());
0605     }
0606 
0607     /** Multiples each subvector of \c *this by the vector \a other */
0608     template<typename OtherDerived>
0609     EIGEN_DEVICE_FUNC
0610     ExpressionType& operator*=(const DenseBase<OtherDerived>& other)
0611     {
0612       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
0613       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
0614       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
0615       m_matrix *= extendedTo(other.derived());
0616       return m_matrix;
0617     }
0618 
0619     /** Divides each subvector of \c *this by the vector \a other */
0620     template<typename OtherDerived>
0621     EIGEN_DEVICE_FUNC
0622     ExpressionType& operator/=(const DenseBase<OtherDerived>& other)
0623     {
0624       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
0625       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
0626       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
0627       m_matrix /= extendedTo(other.derived());
0628       return m_matrix;
0629     }
0630 
0631     /** Returns the expression of the sum of the vector \a other to each subvector of \c *this */
0632     template<typename OtherDerived> EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
0633     CwiseBinaryOp<internal::scalar_sum_op<Scalar,typename OtherDerived::Scalar>,
0634                   const ExpressionTypeNestedCleaned,
0635                   const typename ExtendedType<OtherDerived>::Type>
0636     operator+(const DenseBase<OtherDerived>& other) const
0637     {
0638       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
0639       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
0640       return m_matrix + extendedTo(other.derived());
0641     }
0642 
0643     /** Returns the expression of the difference between each subvector of \c *this and the vector \a other */
0644     template<typename OtherDerived>
0645     EIGEN_DEVICE_FUNC
0646     CwiseBinaryOp<internal::scalar_difference_op<Scalar,typename OtherDerived::Scalar>,
0647                   const ExpressionTypeNestedCleaned,
0648                   const typename ExtendedType<OtherDerived>::Type>
0649     operator-(const DenseBase<OtherDerived>& other) const
0650     {
0651       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
0652       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
0653       return m_matrix - extendedTo(other.derived());
0654     }
0655 
0656     /** Returns the expression where each subvector is the product of the vector \a other
0657       * by the corresponding subvector of \c *this */
0658     template<typename OtherDerived> EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
0659     CwiseBinaryOp<internal::scalar_product_op<Scalar>,
0660                   const ExpressionTypeNestedCleaned,
0661                   const typename ExtendedType<OtherDerived>::Type>
0662     EIGEN_DEVICE_FUNC
0663     operator*(const DenseBase<OtherDerived>& other) const
0664     {
0665       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
0666       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
0667       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
0668       return m_matrix * extendedTo(other.derived());
0669     }
0670 
0671     /** Returns the expression where each subvector is the quotient of the corresponding
0672       * subvector of \c *this by the vector \a other */
0673     template<typename OtherDerived>
0674     EIGEN_DEVICE_FUNC
0675     CwiseBinaryOp<internal::scalar_quotient_op<Scalar>,
0676                   const ExpressionTypeNestedCleaned,
0677                   const typename ExtendedType<OtherDerived>::Type>
0678     operator/(const DenseBase<OtherDerived>& other) const
0679     {
0680       EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
0681       EIGEN_STATIC_ASSERT_ARRAYXPR(ExpressionType)
0682       EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived)
0683       return m_matrix / extendedTo(other.derived());
0684     }
0685 
0686     /** \returns an expression where each column (or row) of the referenced matrix are normalized.
0687       * The referenced matrix is \b not modified.
0688       * \sa MatrixBase::normalized(), normalize()
0689       */
0690     EIGEN_DEVICE_FUNC
0691     CwiseBinaryOp<internal::scalar_quotient_op<Scalar>,
0692                   const ExpressionTypeNestedCleaned,
0693                   const typename OppositeExtendedType<NormReturnType>::Type>
0694     normalized() const { return m_matrix.cwiseQuotient(extendedToOpposite(this->norm())); }
0695 
0696 
0697     /** Normalize in-place each row or columns of the referenced matrix.
0698       * \sa MatrixBase::normalize(), normalized()
0699       */
0700     EIGEN_DEVICE_FUNC void normalize() {
0701       m_matrix = this->normalized();
0702     }
0703 
0704     EIGEN_DEVICE_FUNC inline void reverseInPlace();
0705 
0706 /////////// Geometry module ///////////
0707 
0708     typedef Homogeneous<ExpressionType,Direction> HomogeneousReturnType;
0709     EIGEN_DEVICE_FUNC
0710     HomogeneousReturnType homogeneous() const;
0711 
0712     typedef typename ExpressionType::PlainObject CrossReturnType;
0713     template<typename OtherDerived>
0714     EIGEN_DEVICE_FUNC
0715     const CrossReturnType cross(const MatrixBase<OtherDerived>& other) const;
0716 
0717     enum {
0718       HNormalized_Size = Direction==Vertical ? internal::traits<ExpressionType>::RowsAtCompileTime
0719                                              : internal::traits<ExpressionType>::ColsAtCompileTime,
0720       HNormalized_SizeMinusOne = HNormalized_Size==Dynamic ? Dynamic : HNormalized_Size-1
0721     };
0722     typedef Block<const ExpressionType,
0723                   Direction==Vertical   ? int(HNormalized_SizeMinusOne)
0724                                         : int(internal::traits<ExpressionType>::RowsAtCompileTime),
0725                   Direction==Horizontal ? int(HNormalized_SizeMinusOne)
0726                                         : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
0727             HNormalized_Block;
0728     typedef Block<const ExpressionType,
0729                   Direction==Vertical   ? 1 : int(internal::traits<ExpressionType>::RowsAtCompileTime),
0730                   Direction==Horizontal ? 1 : int(internal::traits<ExpressionType>::ColsAtCompileTime)>
0731             HNormalized_Factors;
0732     typedef CwiseBinaryOp<internal::scalar_quotient_op<typename internal::traits<ExpressionType>::Scalar>,
0733                 const HNormalized_Block,
0734                 const Replicate<HNormalized_Factors,
0735                   Direction==Vertical   ? HNormalized_SizeMinusOne : 1,
0736                   Direction==Horizontal ? HNormalized_SizeMinusOne : 1> >
0737             HNormalizedReturnType;
0738 
0739     EIGEN_DEVICE_FUNC
0740     const HNormalizedReturnType hnormalized() const;
0741 
0742 #   ifdef EIGEN_VECTORWISEOP_PLUGIN
0743 #     include EIGEN_VECTORWISEOP_PLUGIN
0744 #   endif
0745 
0746   protected:
0747     Index redux_length() const
0748     {
0749       return Direction==Vertical ? m_matrix.rows() : m_matrix.cols();
0750     }
0751     ExpressionTypeNested m_matrix;
0752 };
0753 
0754 //const colwise moved to DenseBase.h due to CUDA compiler bug
0755 
0756 
0757 /** \returns a writable VectorwiseOp wrapper of *this providing additional partial reduction operations
0758   *
0759   * \sa rowwise(), class VectorwiseOp, \ref TutorialReductionsVisitorsBroadcasting
0760   */
0761 template<typename Derived>
0762 EIGEN_DEVICE_FUNC inline typename DenseBase<Derived>::ColwiseReturnType
0763 DenseBase<Derived>::colwise()
0764 {
0765   return ColwiseReturnType(derived());
0766 }
0767 
0768 //const rowwise moved to DenseBase.h due to CUDA compiler bug
0769 
0770 
0771 /** \returns a writable VectorwiseOp wrapper of *this providing additional partial reduction operations
0772   *
0773   * \sa colwise(), class VectorwiseOp, \ref TutorialReductionsVisitorsBroadcasting
0774   */
0775 template<typename Derived>
0776 EIGEN_DEVICE_FUNC inline typename DenseBase<Derived>::RowwiseReturnType
0777 DenseBase<Derived>::rowwise()
0778 {
0779   return RowwiseReturnType(derived());
0780 }
0781 
0782 } // end namespace Eigen
0783 
0784 #endif // EIGEN_PARTIAL_REDUX_H