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 // IWYU pragma: private
16 #include "./InternalHeaderCheck.h"
17 
18 namespace Eigen {
19 
20 namespace internal {
21 
22 template <typename MatrixType, int Direction>
23 struct traits<Reverse<MatrixType, Direction> > : traits<MatrixType> {
24  typedef typename MatrixType::Scalar Scalar;
28  typedef std::remove_reference_t<MatrixTypeNested> MatrixTypeNested_;
29  enum {
30  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
31  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
32  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
33  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
34  Flags = MatrixTypeNested_::Flags & (RowMajorBit | LvalueBit)
35  };
36 };
37 
38 template <typename PacketType, bool ReversePacket>
40  static inline PacketType run(const PacketType& x) { return preverse(x); }
41 };
42 
43 template <typename PacketType>
45  static inline PacketType run(const PacketType& x) { return x; }
46 };
47 
48 } // end namespace internal
49 
64 template <typename MatrixType, int Direction>
65 class Reverse : public internal::dense_xpr_base<Reverse<MatrixType, Direction> >::type {
66  public:
70  using Base::IsRowMajor;
71 
72  protected:
73  enum {
75  IsColMajor = !IsRowMajor,
79  OffsetCol = ReverseCol && IsRowMajor ? PacketSize : 1,
81  ((Direction == Horizontal) && IsRowMajor)
82  };
84 
85  public:
86  EIGEN_DEVICE_FUNC explicit inline Reverse(const MatrixType& matrix) : m_matrix(matrix) {}
87 
89 
92 
93  EIGEN_DEVICE_FUNC inline Index innerStride() const { return -m_matrix.innerStride(); }
94 
96  return m_matrix;
97  }
98 
99  protected:
100  typename MatrixType::Nested m_matrix;
101 };
102 
109 template <typename Derived>
111  return ReverseReturnType(derived());
112 }
113 
114 // reverse const overload moved DenseBase.h due to a CUDA compiler bug
115 
128 template <typename Derived>
130  constexpr int HalfRowsAtCompileTime = RowsAtCompileTime == Dynamic ? Dynamic : RowsAtCompileTime / 2;
131  constexpr int HalfColsAtCompileTime = ColsAtCompileTime == Dynamic ? Dynamic : ColsAtCompileTime / 2;
132  if (cols() > rows()) {
133  Index half = cols() / 2;
134  this->template leftCols<HalfColsAtCompileTime>(half).swap(
135  this->template rightCols<HalfColsAtCompileTime>(half).reverse());
136  if ((cols() % 2) == 1) {
137  Index half2 = rows() / 2;
138  col(half).template head<HalfRowsAtCompileTime>(half2).swap(
139  col(half).template tail<HalfRowsAtCompileTime>(half2).reverse());
140  }
141  } else {
142  Index half = rows() / 2;
143  this->template topRows<HalfRowsAtCompileTime>(half).swap(
144  this->template bottomRows<HalfRowsAtCompileTime>(half).reverse());
145  if ((rows() % 2) == 1) {
146  Index half2 = cols() / 2;
147  row(half).template head<HalfColsAtCompileTime>(half2).swap(
148  row(half).template tail<HalfColsAtCompileTime>(half2).reverse());
149  }
150  }
151 }
152 
153 namespace internal {
154 
155 template <int Direction>
157 
158 template <>
160  template <typename ExpressionType>
161  static void run(ExpressionType& xpr) {
162  constexpr Index HalfAtCompileTime =
163  ExpressionType::RowsAtCompileTime == Dynamic ? Dynamic : ExpressionType::RowsAtCompileTime / 2;
164  Index half = xpr.rows() / 2;
165  xpr.template topRows<HalfAtCompileTime>(half).swap(
166  xpr.template bottomRows<HalfAtCompileTime>(half).colwise().reverse());
167  }
168 };
169 
170 template <>
172  template <typename ExpressionType>
173  static void run(ExpressionType& xpr) {
174  constexpr Index HalfAtCompileTime =
175  ExpressionType::ColsAtCompileTime == Dynamic ? Dynamic : ExpressionType::ColsAtCompileTime / 2;
176  Index half = xpr.cols() / 2;
177  xpr.template leftCols<HalfAtCompileTime>(half).swap(
178  xpr.template rightCols<HalfAtCompileTime>(half).rowwise().reverse());
179  }
180 };
181 
182 } // end namespace internal
183 
195 template <typename ExpressionType, int Direction>
198 }
199 
200 } // end namespace Eigen
201 
202 #endif // EIGEN_REVERSE_H
Direction
An enum that indicates the direction in Cartesian coordinates.
Definition: GeneralDefine.h:56
#define EIGEN_NOEXCEPT
Definition: Macros.h:1267
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1171
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Macro to manually inherit assignment operators. This is necessary, because the implicitly defined ass...
Definition: Macros.h:1126
m col(1)
m row(1)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
void reverse(const MatrixType &m)
Definition: array_reverse.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
EIGEN_DEVICE_FUNC void reverseInPlace()
Definition: Reverse.h:129
EIGEN_DEVICE_FUNC ReverseReturnType reverse()
Definition: Reverse.h:110
Expression of the reverse of a vector or matrix.
Definition: Reverse.h:65
@ OffsetCol
Definition: Reverse.h:79
@ ReversePacket
Definition: Reverse.h:80
@ ReverseRow
Definition: Reverse.h:76
@ PacketSize
Definition: Reverse.h:74
@ ReverseCol
Definition: Reverse.h:77
@ OffsetRow
Definition: Reverse.h:78
@ IsColMajor
Definition: Reverse.h:75
EIGEN_DEVICE_FUNC Index innerStride() const
Definition: Reverse.h:93
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename MatrixType::Nested > & nestedExpression() const
Definition: Reverse.h:95
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Reverse.h:90
internal::reverse_packet_cond< PacketScalar, ReversePacket > reverse_packet
Definition: Reverse.h:83
internal::remove_all_t< MatrixType > NestedExpression
Definition: Reverse.h:69
EIGEN_DEVICE_FUNC Reverse(const MatrixType &matrix)
Definition: Reverse.h:86
MatrixType::Nested m_matrix
Definition: Reverse.h:100
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Reverse.h:91
internal::dense_xpr_base< Reverse >::type Base
Definition: Reverse.h:67
EIGEN_DEVICE_FUNC void reverseInPlace()
Definition: Reverse.h:196
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
@ BothDirections
Definition: Constants.h:272
@ Horizontal
Definition: Constants.h:269
@ Vertical
Definition: Constants.h:266
const unsigned int LvalueBit
Definition: Constants.h:148
const unsigned int RowMajorBit
Definition: Constants.h:70
EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf &a)
Definition: AltiVec/Complex.h:303
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
list x
Definition: plotDoE.py:28
Definition: TensorMeta.h:47
Definition: Half.h:139
Definition: XprHelper.h:558
Definition: GenericPacketMath.h:108
Definition: XprHelper.h:506
static PacketType run(const PacketType &x)
Definition: Reverse.h:45
Definition: Reverse.h:39
static PacketType run(const PacketType &x)
Definition: Reverse.h:40
std::remove_reference_t< MatrixTypeNested > MatrixTypeNested_
Definition: Reverse.h:28
traits< MatrixType >::StorageKind StorageKind
Definition: Reverse.h:25
ref_selector< MatrixType >::type MatrixTypeNested
Definition: Reverse.h:27
traits< MatrixType >::XprKind XprKind
Definition: Reverse.h:26
MatrixType::Scalar Scalar
Definition: Reverse.h:24
Definition: ForwardDeclarations.h:21
static void run(ExpressionType &xpr)
Definition: Reverse.h:173
static void run(ExpressionType &xpr)
Definition: Reverse.h:161
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317