Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
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 <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
17namespace Eigen {
18
19namespace internal {
20
21template<typename MatrixType, int Direction>
22struct 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 typename remove_reference<MatrixTypeNested>::type _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
39template<typename PacketType, bool ReversePacket> struct reverse_packet_cond
40{
41 static inline PacketType run(const PacketType& x) { return preverse(x); }
42};
43
44template<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
65template<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;
71 EIGEN_DENSE_PUBLIC_INTERFACE(Reverse)
72 typedef typename internal::remove_all<MatrixType>::type NestedExpression;
73 using Base::IsRowMajor;
74
75 protected:
76 enum {
77 PacketSize = internal::packet_traits<Scalar>::size,
78 IsColMajor = !IsRowMajor,
79 ReverseRow = (Direction == Vertical) || (Direction == BothDirections),
80 ReverseCol = (Direction == Horizontal) || (Direction == BothDirections),
81 OffsetRow = ReverseRow && IsColMajor ? PacketSize : 1,
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
92 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Reverse)
93
94 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
95 inline Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows(); }
96 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
97 inline Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols(); }
98
99 EIGEN_DEVICE_FUNC inline Index innerStride() const
100 {
101 return -m_matrix.innerStride();
102 }
103
104 EIGEN_DEVICE_FUNC const typename internal::remove_all<typename MatrixType::Nested>::type&
105 nestedExpression() const
106 {
107 return m_matrix;
108 }
109
110 protected:
111 typename MatrixType::Nested m_matrix;
112};
113
120template<typename Derived>
121EIGEN_DEVICE_FUNC inline typename DenseBase<Derived>::ReverseReturnType
123{
124 return ReverseReturnType(derived());
125}
126
127
128//reverse const overload moved DenseBase.h due to a CUDA compiler bug
129
142template<typename Derived>
143EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::reverseInPlace()
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
167namespace internal {
168
169template<int Direction>
170struct vectorwise_reverse_inplace_impl;
171
172template<>
173struct vectorwise_reverse_inplace_impl<Vertical>
174{
175 template<typename ExpressionType>
176 static void run(ExpressionType &xpr)
177 {
178 const int HalfAtCompileTime = ExpressionType::RowsAtCompileTime==Dynamic?Dynamic:ExpressionType::RowsAtCompileTime/2;
179 Index half = xpr.rows()/2;
180 xpr.topRows(fix<HalfAtCompileTime>(half))
181 .swap(xpr.bottomRows(fix<HalfAtCompileTime>(half)).colwise().reverse());
182 }
183};
184
185template<>
186struct vectorwise_reverse_inplace_impl<Horizontal>
187{
188 template<typename ExpressionType>
189 static void run(ExpressionType &xpr)
190 {
191 const int HalfAtCompileTime = ExpressionType::ColsAtCompileTime==Dynamic?Dynamic:ExpressionType::ColsAtCompileTime/2;
192 Index half = xpr.cols()/2;
193 xpr.leftCols(fix<HalfAtCompileTime>(half))
194 .swap(xpr.rightCols(fix<HalfAtCompileTime>(half)).rowwise().reverse());
195 }
196};
197
198} // end namespace internal
199
211template<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
ReverseReturnType reverse()
Definition: Reverse.h:122
void reverseInPlace()
Definition: Reverse.h:143
Expression of the reverse of a vector or matrix.
Definition: Reverse.h:67
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
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41