Reverse.h
Go to the documentation of this file.
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 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 namespace internal {
20 
21 template<typename MatrixType, int Direction>
22 struct traits<Reverse<MatrixType, Direction> >
23  : traits<MatrixType>
24 {
25  typedef typename MatrixType::Scalar Scalar;
26  typedef typename traits<MatrixType>::StorageKind StorageKind;
27  typedef typename traits<MatrixType>::XprKind XprKind;
28  typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
29  typedef std::remove_reference_t<MatrixTypeNested> MatrixTypeNested_;
30  enum {
31  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
32  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
33  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
34  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
35  Flags = MatrixTypeNested_::Flags & (RowMajorBit | LvalueBit)
36  };
37 };
38 
39 template<typename PacketType, bool ReversePacket> struct reverse_packet_cond
40 {
41  static inline PacketType run(const PacketType& x) { return preverse(x); }
42 };
43 
44 template<typename PacketType> struct reverse_packet_cond<PacketType,false>
45 {
46  static inline PacketType run(const PacketType& x) { return x; }
47 };
48 
49 } // end namespace internal
50 
65 template<typename MatrixType, int Direction> class Reverse
66  : public internal::dense_xpr_base< Reverse<MatrixType, Direction> >::type
67 {
68  public:
69 
70  typedef typename internal::dense_xpr_base<Reverse>::type Base;
73  using Base::IsRowMajor;
74 
75  protected:
76  enum {
78  IsColMajor = !IsRowMajor,
79  ReverseRow = (Direction == Vertical) || (Direction == BothDirections),
80  ReverseCol = (Direction == Horizontal) || (Direction == BothDirections),
82  OffsetCol = ReverseCol && IsRowMajor ? PacketSize : 1,
83  ReversePacket = (Direction == BothDirections)
84  || ((Direction == Vertical) && IsColMajor)
85  || ((Direction == Horizontal) && IsRowMajor)
86  };
87  typedef internal::reverse_packet_cond<PacketScalar,ReversePacket> reverse_packet;
88  public:
89 
90  EIGEN_DEVICE_FUNC explicit inline Reverse(const MatrixType& matrix) : m_matrix(matrix) { }
91 
93 
95  inline Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
97  inline Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
98 
100  {
101  return -m_matrix.innerStride();
102  }
103 
106  {
107  return m_matrix;
108  }
109 
110  protected:
111  typename MatrixType::Nested m_matrix;
112 };
113 
120 template<typename Derived>
123 {
124  return ReverseReturnType(derived());
125 }
126 
127 
128 //reverse const overload moved DenseBase.h due to a CUDA compiler bug
129 
142 template<typename Derived>
144 {
145  if(cols()>rows())
146  {
147  Index half = cols()/2;
148  leftCols(half).swap(rightCols(half).reverse());
149  if((cols()%2)==1)
150  {
151  Index half2 = rows()/2;
152  col(half).head(half2).swap(col(half).tail(half2).reverse());
153  }
154  }
155  else
156  {
157  Index half = rows()/2;
158  topRows(half).swap(bottomRows(half).reverse());
159  if((rows()%2)==1)
160  {
161  Index half2 = cols()/2;
162  row(half).head(half2).swap(row(half).tail(half2).reverse());
163  }
164  }
165 }
166 
167 namespace internal {
168 
169 template<int Direction>
170 struct vectorwise_reverse_inplace_impl;
171 
172 template<>
173 struct vectorwise_reverse_inplace_impl<Vertical>
174 {
175  template<typename ExpressionType>
176  static void run(ExpressionType &xpr)
177  {
178  constexpr Index HalfAtCompileTime = ExpressionType::RowsAtCompileTime==Dynamic?Dynamic:ExpressionType::RowsAtCompileTime/2;
179  Index half = xpr.rows()/2;
180  xpr.template topRows<HalfAtCompileTime>(half)
181  .swap(xpr.template bottomRows<HalfAtCompileTime>(half).colwise().reverse());
182  }
183 };
184 
185 template<>
186 struct vectorwise_reverse_inplace_impl<Horizontal>
187 {
188  template<typename ExpressionType>
189  static void run(ExpressionType &xpr)
190  {
191  constexpr Index HalfAtCompileTime = ExpressionType::ColsAtCompileTime==Dynamic?Dynamic:ExpressionType::ColsAtCompileTime/2;
192  Index half = xpr.cols()/2;
193  xpr.template leftCols<HalfAtCompileTime>(half)
194  .swap(xpr.template rightCols<HalfAtCompileTime>(half).rowwise().reverse());
195  }
196 };
197 
198 } // end namespace internal
199 
211 template<typename ExpressionType, int Direction>
213 {
214  internal::vectorwise_reverse_inplace_impl<Direction>::run(m_matrix);
215 }
216 
217 } // end namespace Eigen
218 
219 #endif // EIGEN_REVERSE_H
NRowsBlockXpr<... >::Type topRows(NRowsType n)
Definition: BlockMethods.h:570
RowXpr row(Index i)
This is the const version of row(). *‍/.
NColsBlockXpr<... >::Type leftCols(NColsType n)
Definition: BlockMethods.h:797
ColXpr col(Index i)
This is the const version of col().
NColsBlockXpr<... >::Type rightCols(NColsType n)
Definition: BlockMethods.h:872
NRowsBlockXpr<... >::Type bottomRows(NRowsType n)
Definition: BlockMethods.h:645
FixedSegmentReturnType<... >::Type tail(NType n)
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1122
Matrix< float, 1, Dynamic > MatrixType
ReverseReturnType reverse()
Definition: Reverse.h:122
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
void reverseInPlace()
Definition: Reverse.h:143
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Expression of the reverse of a vector or matrix.
Definition: Reverse.h:67
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Reverse.h:97
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Reverse.h:95
internal::reverse_packet_cond< PacketScalar, ReversePacket > reverse_packet
Definition: Reverse.h:87
Reverse(const MatrixType &matrix)
Definition: Reverse.h:90
internal::remove_all_t< MatrixType > NestedExpression
Definition: Reverse.h:72
const internal::remove_all_t< typename MatrixType::Nested > & nestedExpression() const
Definition: Reverse.h:105
Index innerStride() const
Definition: Reverse.h:99
MatrixType::Nested m_matrix
Definition: Reverse.h:111
internal::dense_xpr_base< Reverse >::type Base
Definition: Reverse.h:70
void reverseInPlace()
Definition: Reverse.h:212
@ BothDirections
Definition: Constants.h:272
@ Horizontal
Definition: Constants.h:269
@ Vertical
Definition: Constants.h:266
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
Packet2cf preverse(const Packet2cf &a)
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41