Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
Reverse.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2008 Benoit Jacob <[email protected]>
5// Copyright (C) 2009 Ricard Marxer <[email protected]>
6// Copyright (C) 2009-2010 Gael Guennebaud <[email protected]>
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
18namespace Eigen {
19
20namespace internal {
21
22template <typename MatrixType, int Direction>
23struct traits<Reverse<MatrixType, Direction> > : traits<MatrixType> {
24 typedef typename MatrixType::Scalar Scalar;
25 typedef typename traits<MatrixType>::StorageKind StorageKind;
26 typedef typename traits<MatrixType>::XprKind XprKind;
27 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
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
38template <typename PacketType, bool ReversePacket>
39struct reverse_packet_cond {
40 static inline PacketType run(const PacketType& x) { return preverse(x); }
41};
42
43template <typename PacketType>
44struct reverse_packet_cond<PacketType, false> {
45 static inline PacketType run(const PacketType& x) { return x; }
46};
47
48} // end namespace internal
49
64template <typename MatrixType, int Direction>
65class Reverse : public internal::dense_xpr_base<Reverse<MatrixType, Direction> >::type {
66 public:
67 typedef typename internal::dense_xpr_base<Reverse>::type Base;
68 EIGEN_DENSE_PUBLIC_INTERFACE(Reverse)
69 typedef internal::remove_all_t<MatrixType> NestedExpression;
70 using Base::IsRowMajor;
71
72 protected:
73 enum {
74 PacketSize = internal::packet_traits<Scalar>::size,
75 IsColMajor = !IsRowMajor,
76 ReverseRow = (Direction == Vertical) || (Direction == BothDirections),
77 ReverseCol = (Direction == Horizontal) || (Direction == BothDirections),
78 OffsetRow = ReverseRow && IsColMajor ? PacketSize : 1,
79 OffsetCol = ReverseCol && IsRowMajor ? PacketSize : 1,
80 ReversePacket = (Direction == BothDirections) || ((Direction == Vertical) && IsColMajor) ||
81 ((Direction == Horizontal) && IsRowMajor)
82 };
83 typedef internal::reverse_packet_cond<PacketScalar, ReversePacket> reverse_packet;
84
85 public:
86 EIGEN_DEVICE_FUNC explicit inline Reverse(const MatrixType& matrix) : m_matrix(matrix) {}
87
88 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Reverse)
89
90 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
91 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
92
93 EIGEN_DEVICE_FUNC inline Index innerStride() const { return -m_matrix.innerStride(); }
94
95 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename MatrixType::Nested>& nestedExpression() const {
96 return m_matrix;
97 }
98
99 protected:
100 typename MatrixType::Nested m_matrix;
101};
102
109template <typename Derived>
111 return ReverseReturnType(derived());
112}
113
114// reverse const overload moved DenseBase.h due to a CUDA compiler bug
115
128template <typename Derived>
129EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::reverseInPlace() {
130 if (cols() > rows()) {
131 Index half = cols() / 2;
132 leftCols(half).swap(rightCols(half).reverse());
133 if ((cols() % 2) == 1) {
134 Index half2 = rows() / 2;
135 col(half).head(half2).swap(col(half).tail(half2).reverse());
136 }
137 } else {
138 Index half = rows() / 2;
139 topRows(half).swap(bottomRows(half).reverse());
140 if ((rows() % 2) == 1) {
141 Index half2 = cols() / 2;
142 row(half).head(half2).swap(row(half).tail(half2).reverse());
143 }
144 }
145}
146
147namespace internal {
148
149template <int Direction>
150struct vectorwise_reverse_inplace_impl;
151
152template <>
153struct vectorwise_reverse_inplace_impl<Vertical> {
154 template <typename ExpressionType>
155 static void run(ExpressionType& xpr) {
156 constexpr Index HalfAtCompileTime =
157 ExpressionType::RowsAtCompileTime == Dynamic ? Dynamic : ExpressionType::RowsAtCompileTime / 2;
158 Index half = xpr.rows() / 2;
159 xpr.template topRows<HalfAtCompileTime>(half).swap(
160 xpr.template bottomRows<HalfAtCompileTime>(half).colwise().reverse());
161 }
162};
163
164template <>
165struct vectorwise_reverse_inplace_impl<Horizontal> {
166 template <typename ExpressionType>
167 static void run(ExpressionType& xpr) {
168 constexpr Index HalfAtCompileTime =
169 ExpressionType::ColsAtCompileTime == Dynamic ? Dynamic : ExpressionType::ColsAtCompileTime / 2;
170 Index half = xpr.cols() / 2;
171 xpr.template leftCols<HalfAtCompileTime>(half).swap(
172 xpr.template rightCols<HalfAtCompileTime>(half).rowwise().reverse());
173 }
174};
175
176} // end namespace internal
177
189template <typename ExpressionType, int Direction>
191 internal::vectorwise_reverse_inplace_impl<Direction>::run(m_matrix);
192}
193
194} // end namespace Eigen
195
196#endif // EIGEN_REVERSE_H
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:44
void swap(const DenseBase< OtherDerived > &other)
Definition DenseBase.h:390
Expression of the reverse of a vector or matrix.
Definition Reverse.h:65
Pseudo expression providing broadcasting and partial reduction operations.
Definition VectorwiseOp.h:192
@ 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
Namespace containing all symbols from the Eigen library.
Definition Core:137
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