11#ifndef EIGEN_RESHAPED_H
12#define EIGEN_RESHAPED_H
15#include "./InternalHeaderCheck.h"
50template <
typename XprType,
int Rows,
int Cols,
int Order>
51struct traits<Reshaped<XprType, Rows, Cols, Order> > : traits<XprType> {
52 typedef typename traits<XprType>::Scalar Scalar;
53 typedef typename traits<XprType>::StorageKind StorageKind;
54 typedef typename traits<XprType>::XprKind XprKind;
56 MatrixRows = traits<XprType>::RowsAtCompileTime,
57 MatrixCols = traits<XprType>::ColsAtCompileTime,
58 RowsAtCompileTime = Rows,
59 ColsAtCompileTime = Cols,
60 MaxRowsAtCompileTime = Rows,
61 MaxColsAtCompileTime = Cols,
63 ReshapedStorageOrder = (RowsAtCompileTime == 1 && ColsAtCompileTime != 1) ? RowMajor
64 : (ColsAtCompileTime == 1 && RowsAtCompileTime != 1) ?
ColMajor
66 HasSameStorageOrderAsXprType = (ReshapedStorageOrder == XpxStorageOrder),
67 InnerSize = (ReshapedStorageOrder == int(
RowMajor)) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
68 InnerStrideAtCompileTime = HasSameStorageOrderAsXprType ? int(inner_stride_at_compile_time<XprType>::ret) :
Dynamic,
69 OuterStrideAtCompileTime =
Dynamic,
71 HasDirectAccess = internal::has_direct_access<XprType>::ret && (Order == int(XpxStorageOrder)) &&
75 (InnerSize ==
Dynamic || (InnerSize % packet_traits<Scalar>::size) == 0) && (InnerStrideAtCompileTime == 1)
80 FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ?
LinearAccessBit : 0,
81 FlagsLvalueBit = is_lvalue<XprType>::value ?
LvalueBit : 0,
84 Flags0 = traits<XprType>::Flags & ((HereditaryBits & ~
RowMajorBit) | MaskPacketAccessBit),
86 Flags = (Flags0 | FlagsLinearAccessBit | FlagsLvalueBit | FlagsRowMajorBit | FlagsDirectAccessBit)
90template <
typename XprType,
int Rows,
int Cols,
int Order,
bool HasDirectAccess>
91class ReshapedImpl_dense;
95template <
typename XprType,
int Rows,
int Cols,
int Order,
typename StorageKind>
98template <
typename XprType,
int Rows,
int Cols,
int Order>
99class Reshaped :
public ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind> {
100 typedef ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind> Impl;
105 EIGEN_GENERIC_PUBLIC_INTERFACE(
Reshaped)
106 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Reshaped)
110 EIGEN_DEVICE_FUNC inline
Reshaped(XprType& xpr) : Impl(xpr) {
111 EIGEN_STATIC_ASSERT(RowsAtCompileTime !=
Dynamic && ColsAtCompileTime !=
Dynamic,
112 THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
113 eigen_assert(Rows * Cols == xpr.rows() * xpr.cols());
119 : Impl(xpr, reshapeRows, reshapeCols) {
120 eigen_assert((RowsAtCompileTime ==
Dynamic || RowsAtCompileTime == reshapeRows) &&
121 (ColsAtCompileTime ==
Dynamic || ColsAtCompileTime == reshapeCols));
122 eigen_assert(reshapeRows * reshapeCols == xpr.rows() * xpr.cols());
128template <
typename XprType,
int Rows,
int Cols,
int Order>
129class ReshapedImpl<XprType, Rows, Cols, Order, Dense>
130 :
public internal::ReshapedImpl_dense<XprType, Rows, Cols, Order,
131 internal::traits<Reshaped<XprType, Rows, Cols, Order> >::HasDirectAccess> {
132 typedef internal::ReshapedImpl_dense<XprType, Rows, Cols, Order,
133 internal::traits<Reshaped<XprType, Rows, Cols, Order> >::HasDirectAccess>
138 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl)
139 EIGEN_DEVICE_FUNC
inline ReshapedImpl(XprType& xpr) : Impl(xpr) {}
140 EIGEN_DEVICE_FUNC
inline ReshapedImpl(XprType& xpr, Index reshapeRows, Index reshapeCols)
141 : Impl(xpr, reshapeRows, reshapeCols) {}
147template <
typename XprType,
int Rows,
int Cols,
int Order>
148class ReshapedImpl_dense<XprType, Rows, Cols, Order, false>
149 :
public internal::dense_xpr_base<Reshaped<XprType, Rows, Cols, Order> >::type {
150 typedef Reshaped<XprType, Rows, Cols, Order> ReshapedType;
153 typedef typename internal::dense_xpr_base<ReshapedType>::type Base;
154 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
155 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
157 typedef typename internal::ref_selector<XprType>::non_const_type MatrixTypeNested;
158 typedef internal::remove_all_t<XprType> NestedExpression;
164 EIGEN_DEVICE_FUNC
inline ReshapedImpl_dense(XprType& xpr) : m_xpr(xpr), m_rows(Rows), m_cols(Cols) {}
168 EIGEN_DEVICE_FUNC
inline ReshapedImpl_dense(XprType& xpr, Index nRows, Index nCols)
169 : m_xpr(xpr), m_rows(nRows), m_cols(nCols) {}
171 EIGEN_DEVICE_FUNC Index rows()
const {
return m_rows; }
172 EIGEN_DEVICE_FUNC Index cols()
const {
return m_cols; }
174#ifdef EIGEN_PARSED_BY_DOXYGEN
176 EIGEN_DEVICE_FUNC
inline const Scalar* data()
const;
177 EIGEN_DEVICE_FUNC
inline Index innerStride()
const;
178 EIGEN_DEVICE_FUNC
inline Index outerStride()
const;
182 EIGEN_DEVICE_FUNC
const internal::remove_all_t<XprType>& nestedExpression()
const {
return m_xpr; }
185 EIGEN_DEVICE_FUNC std::remove_reference_t<XprType>& nestedExpression() {
return m_xpr; }
188 MatrixTypeNested m_xpr;
189 const internal::variable_if_dynamic<Index, Rows> m_rows;
190 const internal::variable_if_dynamic<Index, Cols> m_cols;
194template <
typename XprType,
int Rows,
int Cols,
int Order>
195class ReshapedImpl_dense<XprType, Rows, Cols, Order, true> :
public MapBase<Reshaped<XprType, Rows, Cols, Order> > {
196 typedef Reshaped<XprType, Rows, Cols, Order> ReshapedType;
197 typedef typename internal::ref_selector<XprType>::non_const_type XprTypeNested;
200 typedef MapBase<ReshapedType> Base;
201 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
202 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
206 EIGEN_DEVICE_FUNC
inline ReshapedImpl_dense(XprType& xpr) : Base(xpr.data()), m_xpr(xpr) {}
210 EIGEN_DEVICE_FUNC
inline ReshapedImpl_dense(XprType& xpr, Index nRows, Index nCols)
211 : Base(xpr.data(), nRows, nCols), m_xpr(xpr) {}
213 EIGEN_DEVICE_FUNC
const internal::remove_all_t<XprTypeNested>& nestedExpression()
const {
return m_xpr; }
215 EIGEN_DEVICE_FUNC XprType& nestedExpression() {
return m_xpr; }
218 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline Index innerStride()
const {
return m_xpr.innerStride(); }
221 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline Index outerStride()
const {
222 return (((Flags & RowMajorBit) == RowMajorBit) ? this->cols() : this->rows()) * m_xpr.innerStride();
230template <
typename ArgType,
int Rows,
int Cols,
int Order,
bool HasDirectAccess>
231struct reshaped_evaluator;
233template <
typename ArgType,
int Rows,
int Cols,
int Order>
234struct evaluator<Reshaped<ArgType, Rows, Cols, Order> >
235 : reshaped_evaluator<ArgType, Rows, Cols, Order, traits<Reshaped<ArgType, Rows, Cols, Order> >::HasDirectAccess> {
236 typedef Reshaped<ArgType, Rows, Cols, Order> XprType;
237 typedef typename XprType::Scalar Scalar;
239 typedef typename packet_traits<Scalar>::type PacketScalar;
242 CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
243 HasDirectAccess = traits<XprType>::HasDirectAccess,
255 FlagsLinearAccessBit =
256 (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1 || HasDirectAccess)
259 FlagsRowMajorBit = (traits<XprType>::ReshapedStorageOrder == int(
RowMajor)) ?
RowMajorBit : 0,
261 Flags0 = evaluator<ArgType>::Flags & (HereditaryBits & ~
RowMajorBit),
262 Flags = Flags0 | FlagsLinearAccessBit | FlagsRowMajorBit | FlagsDirectAccessBit,
264 PacketAlignment = unpacket_traits<PacketScalar>::alignment,
265 Alignment = evaluator<ArgType>::Alignment
267 typedef reshaped_evaluator<ArgType, Rows, Cols, Order, HasDirectAccess> reshaped_evaluator_type;
268 EIGEN_DEVICE_FUNC
explicit evaluator(
const XprType& xpr) : reshaped_evaluator_type(xpr) {
269 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
273template <
typename ArgType,
int Rows,
int Cols,
int Order>
274struct reshaped_evaluator<ArgType, Rows, Cols, Order, false>
275 : evaluator_base<Reshaped<ArgType, Rows, Cols, Order> > {
276 typedef Reshaped<ArgType, Rows, Cols, Order> XprType;
279 CoeffReadCost = evaluator<ArgType>::CoeffReadCost ,
281 Flags = (evaluator<ArgType>::Flags & (HereditaryBits )),
286 EIGEN_DEVICE_FUNC
explicit reshaped_evaluator(
const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_xpr(xpr) {
287 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
290 typedef typename XprType::Scalar Scalar;
291 typedef typename XprType::CoeffReturnType CoeffReturnType;
293 typedef std::pair<Index, Index> RowCol;
295 EIGEN_DEVICE_FUNC
inline RowCol index_remap(Index rowId, Index colId)
const {
296 if (Order == ColMajor) {
297 const Index nth_elem_idx = colId * m_xpr.rows() + rowId;
298 return RowCol(nth_elem_idx % m_xpr.nestedExpression().rows(), nth_elem_idx / m_xpr.nestedExpression().rows());
300 const Index nth_elem_idx = colId + rowId * m_xpr.cols();
301 return RowCol(nth_elem_idx / m_xpr.nestedExpression().cols(), nth_elem_idx % m_xpr.nestedExpression().cols());
305 EIGEN_DEVICE_FUNC
inline Scalar& coeffRef(Index rowId, Index colId) {
306 EIGEN_STATIC_ASSERT_LVALUE(XprType)
307 const RowCol row_col = index_remap(rowId, colId);
308 return m_argImpl.coeffRef(row_col.first, row_col.second);
311 EIGEN_DEVICE_FUNC
inline const Scalar& coeffRef(Index rowId, Index colId)
const {
312 const RowCol row_col = index_remap(rowId, colId);
313 return m_argImpl.coeffRef(row_col.first, row_col.second);
316 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const CoeffReturnType coeff(Index rowId, Index colId)
const {
317 const RowCol row_col = index_remap(rowId, colId);
318 return m_argImpl.coeff(row_col.first, row_col.second);
321 EIGEN_DEVICE_FUNC
inline Scalar& coeffRef(Index index) {
322 EIGEN_STATIC_ASSERT_LVALUE(XprType)
323 const RowCol row_col = index_remap(Rows == 1 ? 0 : index, Rows == 1 ? index : 0);
324 return m_argImpl.coeffRef(row_col.first, row_col.second);
327 EIGEN_DEVICE_FUNC
inline const Scalar& coeffRef(Index index)
const {
328 const RowCol row_col = index_remap(Rows == 1 ? 0 : index, Rows == 1 ? index : 0);
329 return m_argImpl.coeffRef(row_col.first, row_col.second);
332 EIGEN_DEVICE_FUNC
inline const CoeffReturnType coeff(Index index)
const {
333 const RowCol row_col = index_remap(Rows == 1 ? 0 : index, Rows == 1 ? index : 0);
334 return m_argImpl.coeff(row_col.first, row_col.second);
338 template<
int LoadMode>
339 inline PacketScalar packet(Index rowId, Index colId)
const
341 const RowCol row_col = index_remap(rowId, colId);
342 return m_argImpl.template packet<Unaligned>(row_col.first, row_col.second);
346 template<
int LoadMode>
348 inline void writePacket(Index rowId, Index colId,
const PacketScalar& val)
350 const RowCol row_col = index_remap(rowId, colId);
351 m_argImpl.const_cast_derived().template writePacket<Unaligned>
352 (row_col.first, row_col.second, val);
355 template<
int LoadMode>
357 inline PacketScalar packet(Index index)
const
359 const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
360 RowsAtCompileTime == 1 ? index : 0);
361 return m_argImpl.template packet<Unaligned>(row_col.first, row_col.second);
364 template<
int LoadMode>
366 inline void writePacket(Index index,
const PacketScalar& val)
368 const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
369 RowsAtCompileTime == 1 ? index : 0);
370 return m_argImpl.template packet<Unaligned>(row_col.first, row_col.second, val);
374 evaluator<ArgType> m_argImpl;
375 const XprType& m_xpr;
378template <
typename ArgType,
int Rows,
int Cols,
int Order>
379struct reshaped_evaluator<ArgType, Rows, Cols, Order, true>
380 : mapbase_evaluator<Reshaped<ArgType, Rows, Cols, Order>,
381 typename Reshaped<ArgType, Rows, Cols, Order>::PlainObject> {
382 typedef Reshaped<ArgType, Rows, Cols, Order> XprType;
383 typedef typename XprType::Scalar Scalar;
385 EIGEN_DEVICE_FUNC
explicit reshaped_evaluator(
const XprType& xpr)
386 : mapbase_evaluator<XprType, typename XprType::PlainObject>(xpr) {
389 eigen_assert(((std::uintptr_t(xpr.data()) % plain_enum_max(1, evaluator<XprType>::Alignment)) == 0) &&
390 "data is not aligned");
Expression of a fixed-size or dynamic-size reshape.
Definition Reshaped.h:99
Reshaped(XprType &xpr, Index reshapeRows, Index reshapeCols)
Definition Reshaped.h:118
@ ColMajor
Definition Constants.h:318
@ RowMajor
Definition Constants.h:320
const unsigned int PacketAccessBit
Definition Constants.h:97
const unsigned int LinearAccessBit
Definition Constants.h:133
const unsigned int DirectAccessBit
Definition Constants.h:159
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