cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

Reverse.h (7522B)


      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
      5 // Copyright (C) 2009 Ricard Marxer <email@ricardmarxer.com>
      6 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
      7 //
      8 // This Source Code Form is subject to the terms of the Mozilla
      9 // Public License v. 2.0. If a copy of the MPL was not distributed
     10 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     11 
     12 #ifndef EIGEN_REVERSE_H
     13 #define EIGEN_REVERSE_H
     14 
     15 namespace Eigen {
     16 
     17 namespace internal {
     18 
     19 template<typename MatrixType, int Direction>
     20 struct traits<Reverse<MatrixType, Direction> >
     21  : traits<MatrixType>
     22 {
     23   typedef typename MatrixType::Scalar Scalar;
     24   typedef typename traits<MatrixType>::StorageKind StorageKind;
     25   typedef typename traits<MatrixType>::XprKind XprKind;
     26   typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
     27   typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
     28   enum {
     29     RowsAtCompileTime = MatrixType::RowsAtCompileTime,
     30     ColsAtCompileTime = MatrixType::ColsAtCompileTime,
     31     MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
     32     MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
     33     Flags = _MatrixTypeNested::Flags & (RowMajorBit | LvalueBit)
     34   };
     35 };
     36 
     37 template<typename PacketType, bool ReversePacket> struct reverse_packet_cond
     38 {
     39   static inline PacketType run(const PacketType& x) { return preverse(x); }
     40 };
     41 
     42 template<typename PacketType> struct reverse_packet_cond<PacketType,false>
     43 {
     44   static inline PacketType run(const PacketType& x) { return x; }
     45 };
     46 
     47 } // end namespace internal
     48 
     49 /** \class Reverse
     50   * \ingroup Core_Module
     51   *
     52   * \brief Expression of the reverse of a vector or matrix
     53   *
     54   * \tparam MatrixType the type of the object of which we are taking the reverse
     55   * \tparam Direction defines the direction of the reverse operation, can be Vertical, Horizontal, or BothDirections
     56   *
     57   * This class represents an expression of the reverse of a vector.
     58   * It is the return type of MatrixBase::reverse() and VectorwiseOp::reverse()
     59   * and most of the time this is the only way it is used.
     60   *
     61   * \sa MatrixBase::reverse(), VectorwiseOp::reverse()
     62   */
     63 template<typename MatrixType, int Direction> class Reverse
     64   : public internal::dense_xpr_base< Reverse<MatrixType, Direction> >::type
     65 {
     66   public:
     67 
     68     typedef typename internal::dense_xpr_base<Reverse>::type Base;
     69     EIGEN_DENSE_PUBLIC_INTERFACE(Reverse)
     70     typedef typename internal::remove_all<MatrixType>::type NestedExpression;
     71     using Base::IsRowMajor;
     72 
     73   protected:
     74     enum {
     75       PacketSize = internal::packet_traits<Scalar>::size,
     76       IsColMajor = !IsRowMajor,
     77       ReverseRow = (Direction == Vertical)   || (Direction == BothDirections),
     78       ReverseCol = (Direction == Horizontal) || (Direction == BothDirections),
     79       OffsetRow  = ReverseRow && IsColMajor ? PacketSize : 1,
     80       OffsetCol  = ReverseCol && IsRowMajor ? PacketSize : 1,
     81       ReversePacket = (Direction == BothDirections)
     82                     || ((Direction == Vertical)   && IsColMajor)
     83                     || ((Direction == Horizontal) && IsRowMajor)
     84     };
     85     typedef internal::reverse_packet_cond<PacketScalar,ReversePacket> reverse_packet;
     86   public:
     87 
     88     EIGEN_DEVICE_FUNC explicit inline Reverse(const MatrixType& matrix) : m_matrix(matrix) { }
     89 
     90     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Reverse)
     91 
     92     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
     93     inline Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
     94     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
     95     inline Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
     96 
     97     EIGEN_DEVICE_FUNC inline Index innerStride() const
     98     {
     99       return -m_matrix.innerStride();
    100     }
    101 
    102     EIGEN_DEVICE_FUNC const typename internal::remove_all<typename MatrixType::Nested>::type&
    103     nestedExpression() const
    104     {
    105       return m_matrix;
    106     }
    107 
    108   protected:
    109     typename MatrixType::Nested m_matrix;
    110 };
    111 
    112 /** \returns an expression of the reverse of *this.
    113   *
    114   * Example: \include MatrixBase_reverse.cpp
    115   * Output: \verbinclude MatrixBase_reverse.out
    116   *
    117   */
    118 template<typename Derived>
    119 EIGEN_DEVICE_FUNC inline typename DenseBase<Derived>::ReverseReturnType
    120 DenseBase<Derived>::reverse()
    121 {
    122   return ReverseReturnType(derived());
    123 }
    124 
    125 
    126 //reverse const overload moved DenseBase.h due to a CUDA compiler bug
    127 
    128 /** This is the "in place" version of reverse: it reverses \c *this.
    129   *
    130   * In most cases it is probably better to simply use the reversed expression
    131   * of a matrix. However, when reversing the matrix data itself is really needed,
    132   * then this "in-place" version is probably the right choice because it provides
    133   * the following additional benefits:
    134   *  - less error prone: doing the same operation with .reverse() requires special care:
    135   *    \code m = m.reverse().eval(); \endcode
    136   *  - this API enables reverse operations without the need for a temporary
    137   *  - it allows future optimizations (cache friendliness, etc.)
    138   *
    139   * \sa VectorwiseOp::reverseInPlace(), reverse() */
    140 template<typename Derived>
    141 EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::reverseInPlace()
    142 {
    143   if(cols()>rows())
    144   {
    145     Index half = cols()/2;
    146     leftCols(half).swap(rightCols(half).reverse());
    147     if((cols()%2)==1)
    148     {
    149       Index half2 = rows()/2;
    150       col(half).head(half2).swap(col(half).tail(half2).reverse());
    151     }
    152   }
    153   else
    154   {
    155     Index half = rows()/2;
    156     topRows(half).swap(bottomRows(half).reverse());
    157     if((rows()%2)==1)
    158     {
    159       Index half2 = cols()/2;
    160       row(half).head(half2).swap(row(half).tail(half2).reverse());
    161     }
    162   }
    163 }
    164 
    165 namespace internal {
    166 
    167 template<int Direction>
    168 struct vectorwise_reverse_inplace_impl;
    169 
    170 template<>
    171 struct vectorwise_reverse_inplace_impl<Vertical>
    172 {
    173   template<typename ExpressionType>
    174   static void run(ExpressionType &xpr)
    175   {
    176     const int HalfAtCompileTime = ExpressionType::RowsAtCompileTime==Dynamic?Dynamic:ExpressionType::RowsAtCompileTime/2;
    177     Index half = xpr.rows()/2;
    178     xpr.topRows(fix<HalfAtCompileTime>(half))
    179        .swap(xpr.bottomRows(fix<HalfAtCompileTime>(half)).colwise().reverse());
    180   }
    181 };
    182 
    183 template<>
    184 struct vectorwise_reverse_inplace_impl<Horizontal>
    185 {
    186   template<typename ExpressionType>
    187   static void run(ExpressionType &xpr)
    188   {
    189     const int HalfAtCompileTime = ExpressionType::ColsAtCompileTime==Dynamic?Dynamic:ExpressionType::ColsAtCompileTime/2;
    190     Index half = xpr.cols()/2;
    191     xpr.leftCols(fix<HalfAtCompileTime>(half))
    192        .swap(xpr.rightCols(fix<HalfAtCompileTime>(half)).rowwise().reverse());
    193   }
    194 };
    195 
    196 } // end namespace internal
    197 
    198 /** This is the "in place" version of VectorwiseOp::reverse: it reverses each column or row of \c *this.
    199   *
    200   * In most cases it is probably better to simply use the reversed expression
    201   * of a matrix. However, when reversing the matrix data itself is really needed,
    202   * then this "in-place" version is probably the right choice because it provides
    203   * the following additional benefits:
    204   *  - less error prone: doing the same operation with .reverse() requires special care:
    205   *    \code m = m.reverse().eval(); \endcode
    206   *  - this API enables reverse operations without the need for a temporary
    207   *
    208   * \sa DenseBase::reverseInPlace(), reverse() */
    209 template<typename ExpressionType, int Direction>
    210 EIGEN_DEVICE_FUNC void VectorwiseOp<ExpressionType,Direction>::reverseInPlace()
    211 {
    212   internal::vectorwise_reverse_inplace_impl<Direction>::run(m_matrix);
    213 }
    214 
    215 } // end namespace Eigen
    216 
    217 #endif // EIGEN_REVERSE_H