12#ifndef EIGEN_RESHAPED_H
13#define EIGEN_RESHAPED_H
16#include "./InternalHeaderCheck.h"
54template <
typename XprType,
int Rows,
int Cols,
int Order>
55struct traits<Reshaped<XprType, Rows, Cols, Order> > : traits<XprType> {
56 using Scalar =
typename traits<XprType>::Scalar;
57 using StorageKind =
typename traits<XprType>::StorageKind;
58 using XprKind =
typename traits<XprType>::XprKind;
60 MatrixRows = traits<XprType>::RowsAtCompileTime,
61 MatrixCols = traits<XprType>::ColsAtCompileTime,
62 RowsAtCompileTime = Rows,
63 ColsAtCompileTime = Cols,
64 MaxRowsAtCompileTime = Rows,
65 MaxColsAtCompileTime = Cols,
67 ReshapedStorageOrder = (RowsAtCompileTime == 1 && ColsAtCompileTime != 1) ?
RowMajor
68 : (ColsAtCompileTime == 1 && RowsAtCompileTime != 1) ?
ColMajor
70 HasSameStorageOrderAsXprType = (ReshapedStorageOrder == XprStorageOrder),
71 InnerSize = (ReshapedStorageOrder == int(
RowMajor)) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
74 InnerStrideAtCompileTime = (HasSameStorageOrderAsXprType || RowsAtCompileTime == 1 || ColsAtCompileTime == 1)
75 ? int(inner_stride_at_compile_time<XprType>::value)
77 OuterStrideAtCompileTime = Dynamic,
79 HasDirectAccess = internal::has_direct_access<XprType>::value && (Order == int(XprStorageOrder)) &&
83 (InnerSize == Dynamic || (InnerSize % packet_traits<Scalar>::size) == 0) && (InnerStrideAtCompileTime == 1)
88 FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ?
LinearAccessBit : 0,
89 FlagsLvalueBit = is_lvalue<XprType>::value ?
LvalueBit : 0,
92 Flags0 = traits<XprType>::Flags & ((HereditaryBits & ~
RowMajorBit) | MaskPacketAccessBit),
94 Flags = (Flags0 | FlagsLinearAccessBit | FlagsLvalueBit | FlagsRowMajorBit | FlagsDirectAccessBit)
98template <
typename XprType,
int Rows,
int Cols,
int Order,
bool HasDirectAccess>
99class ReshapedImpl_dense;
103template <
typename XprType,
int Rows,
int Cols,
int Order,
typename StorageKind>
106template <
typename XprType,
int Rows,
int Cols,
int Order>
107class Reshaped :
public ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind> {
108 using Impl = ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind>;
113 EIGEN_GENERIC_PUBLIC_INTERFACE(
Reshaped)
114 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Reshaped)
118 EIGEN_DEVICE_FUNC constexpr inline
Reshaped(XprType& xpr) : Impl(xpr) {
119 EIGEN_STATIC_ASSERT(RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic,
120 THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
121 EIGEN_INTERNAL_ASSERT_RESHAPE_SIZE(xpr, Rows, Cols);
126 EIGEN_DEVICE_FUNC
constexpr inline Reshaped(XprType& xpr, Index reshapeRows, Index reshapeCols)
127 : Impl(xpr, reshapeRows, reshapeCols) {
128 EIGEN_STATIC_ASSERT((internal::reshape_dimensions<XprType, Rows, Cols>::Compatible),
129 YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES)
130 eigen_assert((RowsAtCompileTime == Dynamic || RowsAtCompileTime == reshapeRows) &&
131 (ColsAtCompileTime == Dynamic || ColsAtCompileTime == reshapeCols));
132 eigen_assert(reshapeRows * reshapeCols == xpr.rows() * xpr.cols());
138template <
typename XprType,
int Rows,
int Cols,
int Order>
139class ReshapedImpl<XprType, Rows, Cols, Order, Dense>
140 :
public internal::ReshapedImpl_dense<XprType, Rows, Cols, Order,
141 internal::traits<Reshaped<XprType, Rows, Cols, Order> >::HasDirectAccess> {
142 using Impl = internal::ReshapedImpl_dense<XprType, Rows, Cols, Order,
143 internal::traits<Reshaped<XprType, Rows, Cols, Order>>::HasDirectAccess>;
147 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl)
148 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl(XprType& xpr) : Impl(xpr) {}
149 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl(XprType& xpr, Index reshapeRows, Index reshapeCols)
150 : Impl(xpr, reshapeRows, reshapeCols) {}
156template <
typename XprType,
int Rows,
int Cols,
int Order>
157class ReshapedImpl_dense<XprType, Rows, Cols, Order, false>
158 :
public internal::dense_xpr_base<Reshaped<XprType, Rows, Cols, Order> >::type {
159 using ReshapedType = Reshaped<XprType, Rows, Cols, Order>;
162 using Base =
typename internal::dense_xpr_base<ReshapedType>::type;
163 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
164 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
166 using MatrixTypeNested =
typename internal::ref_selector<XprType>::non_const_type;
167 using NestedExpression = internal::remove_all_t<XprType>;
171 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl_dense(XprType& xpr) : m_xpr(xpr), m_rows(Rows), m_cols(Cols) {}
175 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl_dense(XprType& xpr, Index nRows, Index nCols)
176 : m_xpr(xpr), m_rows(nRows), m_cols(nCols) {}
178 EIGEN_DEVICE_FUNC
constexpr Index rows()
const {
return m_rows; }
179 EIGEN_DEVICE_FUNC
constexpr Index cols()
const {
return m_cols; }
181#ifdef EIGEN_PARSED_BY_DOXYGEN
183 EIGEN_DEVICE_FUNC
constexpr const Scalar* data()
const;
184 EIGEN_DEVICE_FUNC
inline Index innerStride()
const;
185 EIGEN_DEVICE_FUNC
inline Index outerStride()
const;
189 EIGEN_DEVICE_FUNC
constexpr const internal::remove_all_t<XprType>& nestedExpression()
const {
return m_xpr; }
192 EIGEN_DEVICE_FUNC
constexpr std::remove_reference_t<XprType>& nestedExpression() {
return m_xpr; }
195 MatrixTypeNested m_xpr;
196 const internal::variable_if_dynamic<Index, Rows> m_rows;
197 const internal::variable_if_dynamic<Index, Cols> m_cols;
201template <
typename XprType,
int Rows,
int Cols,
int Order>
202class ReshapedImpl_dense<XprType, Rows, Cols, Order, true> :
public MapBase<Reshaped<XprType, Rows, Cols, Order> > {
203 using ReshapedType = Reshaped<XprType, Rows, Cols, Order>;
204 using XprTypeNested =
typename internal::ref_selector<XprType>::non_const_type;
207 using Base = MapBase<ReshapedType>;
208 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
209 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
213 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl_dense(XprType& xpr) : Base(xpr.data()), m_xpr(xpr) {}
217 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl_dense(XprType& xpr, Index nRows, Index nCols)
218 : Base(xpr.data(), nRows, nCols), m_xpr(xpr) {}
220 EIGEN_DEVICE_FUNC
constexpr const internal::remove_all_t<XprTypeNested>& nestedExpression()
const {
return m_xpr; }
222 EIGEN_DEVICE_FUNC
constexpr XprType& nestedExpression() {
return m_xpr; }
225 EIGEN_DEVICE_FUNC
constexpr Index innerStride()
const {
return m_xpr.innerStride(); }
228 EIGEN_DEVICE_FUNC
constexpr Index outerStride()
const {
229 return (((Flags & RowMajorBit) == RowMajorBit) ? this->cols() : this->rows()) * m_xpr.innerStride();
237template <
typename ArgType,
int Rows,
int Cols,
int Order,
bool HasDirectAccess>
238struct reshaped_evaluator;
240template <
typename ArgType,
int Rows,
int Cols,
int Order>
241struct evaluator<Reshaped<ArgType, Rows, Cols, Order> >
242 : reshaped_evaluator<ArgType, Rows, Cols, Order, traits<Reshaped<ArgType, Rows, Cols, Order> >::HasDirectAccess> {
243 using XprType = Reshaped<ArgType, Rows, Cols, Order>;
244 using Scalar =
typename XprType::Scalar;
246 using PacketScalar =
typename packet_traits<Scalar>::type;
249 CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
250 HasDirectAccess = traits<XprType>::HasDirectAccess,
264 ForwardLinearAccess = reshaped_evaluator<ArgType, Rows, Cols, Order, bool(HasDirectAccess)>::ForwardLinearAccess,
266 FlagsLinearAccessBit = (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1 ||
267 HasDirectAccess || ForwardLinearAccess)
270 FlagsRowMajorBit = (traits<XprType>::ReshapedStorageOrder == int(
RowMajor)) ?
RowMajorBit : 0,
275 MaskPacketAccessBit = (HasDirectAccess && (traits<XprType>::InnerStrideAtCompileTime == 1)) || ForwardLinearAccess
278 Flags0 = evaluator<ArgType>::Flags & ((HereditaryBits & ~
RowMajorBit) | MaskPacketAccessBit),
279 Flags = Flags0 | FlagsLinearAccessBit | FlagsRowMajorBit | FlagsDirectAccessBit,
281 PacketAlignment = unpacket_traits<PacketScalar>::alignment,
284 Alignment = evaluator<ArgType>::Alignment
286 using reshaped_evaluator_type = reshaped_evaluator<ArgType, Rows, Cols, Order, HasDirectAccess>;
287 EIGEN_DEVICE_FUNC
constexpr explicit evaluator(
const XprType& xpr) : reshaped_evaluator_type(xpr) {
288 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
292template <
typename ArgType,
int Rows,
int Cols,
int Order>
293struct reshaped_evaluator<ArgType, Rows, Cols, Order, false>
294 : evaluator_base<Reshaped<ArgType, Rows, Cols, Order> > {
295 using XprType = Reshaped<ArgType, Rows, Cols, Order>;
298 CoeffReadCost = evaluator<ArgType>::CoeffReadCost ,
303 NestedRowMajor = (int(evaluator<ArgType>::Flags) &
RowMajorBit) != 0,
304 OrderMatchesNested = (Order ==
int(ColMajor)) != NestedRowMajor,
305 ForwardLinearAccess = (OrderMatchesNested || ArgType::RowsAtCompileTime == 1 || ArgType::ColsAtCompileTime == 1) &&
306 (Order ==
int(traits<XprType>::ReshapedStorageOrder) || Rows == 1 || Cols == 1) &&
312 EIGEN_DEVICE_FUNC
constexpr explicit reshaped_evaluator(
const XprType& xpr)
313 : m_argImpl(xpr.nestedExpression()),
314 m_innerSize(Order ==
ColMajor ? xpr.rows() : xpr.cols()),
315 m_nestedInnerSize(Order ==
ColMajor ? xpr.nestedExpression().rows() : xpr.nestedExpression().cols()) {
316 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
319 using Scalar =
typename XprType::Scalar;
320 using CoeffReturnType =
typename XprType::CoeffReturnType;
322 using RowCol = std::pair<Index, Index>;
326 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Index linear_index(Index rowId, Index colId)
const {
327 EIGEN_IF_CONSTEXPR (Order == ColMajor) {
328 return colId * m_innerSize.value() + rowId;
330 return colId + rowId * m_innerSize.value();
334 EIGEN_DEVICE_FUNC
constexpr inline RowCol index_remap(Index rowId, Index colId)
const {
335 const Index nth_elem_idx = linear_index(rowId, colId);
336 EIGEN_IF_CONSTEXPR (Order == ColMajor) {
337 return RowCol(nth_elem_idx % m_nestedInnerSize.value(), nth_elem_idx / m_nestedInnerSize.value());
339 return RowCol(nth_elem_idx / m_nestedInnerSize.value(), nth_elem_idx % m_nestedInnerSize.value());
343 EIGEN_DEVICE_FUNC
constexpr inline Scalar& coeffRef(Index rowId, Index colId) {
344 EIGEN_STATIC_ASSERT_LVALUE(XprType)
345 return coeffRef_impl(rowId, colId, bool_constant<ForwardLinearAccess>());
348 EIGEN_DEVICE_FUNC
constexpr inline const Scalar& coeffRef(Index rowId, Index colId)
const {
349 return coeffRef_impl(rowId, colId, bool_constant<ForwardLinearAccess>());
352 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType coeff(Index rowId, Index colId)
const {
353 return coeff_impl(rowId, colId, bool_constant<ForwardLinearAccess>());
356 EIGEN_DEVICE_FUNC
constexpr inline Scalar& coeffRef(Index index) {
357 EIGEN_STATIC_ASSERT_LVALUE(XprType)
358 return coeffRef_impl(index, bool_constant<ForwardLinearAccess>());
361 EIGEN_DEVICE_FUNC
constexpr inline const Scalar& coeffRef(Index index)
const {
362 return coeffRef_impl(index, bool_constant<ForwardLinearAccess>());
365 EIGEN_DEVICE_FUNC
constexpr inline const CoeffReturnType coeff(Index index)
const {
366 return coeff_impl(index, bool_constant<ForwardLinearAccess>());
371 template <
int LoadMode,
typename PacketType>
372 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index rowId, Index colId)
const {
373 return m_argImpl.template packet<LoadMode, PacketType>(linear_index(rowId, colId));
376 template <
int LoadMode,
typename PacketType>
377 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index index)
const {
378 return m_argImpl.template packet<LoadMode, PacketType>(index);
381 template <
int StoreMode,
typename PacketType>
382 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacket(Index rowId, Index colId,
const PacketType& x) {
383 m_argImpl.template writePacket<StoreMode, PacketType>(linear_index(rowId, colId), x);
386 template <
int StoreMode,
typename PacketType>
387 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacket(Index index,
const PacketType& x) {
388 m_argImpl.template writePacket<StoreMode, PacketType>(index, x);
391 template <
int LoadMode,
typename PacketType>
392 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index rowId, Index colId, Index begin,
394 return m_argImpl.template packetSegment<LoadMode, PacketType>(linear_index(rowId, colId), begin, count);
397 template <
int LoadMode,
typename PacketType>
398 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index index, Index begin, Index count)
const {
399 return m_argImpl.template packetSegment<LoadMode, PacketType>(index, begin, count);
402 template <
int StoreMode,
typename PacketType>
403 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacketSegment(Index rowId, Index colId,
const PacketType& x,
404 Index begin, Index count) {
405 m_argImpl.template writePacketSegment<StoreMode, PacketType>(linear_index(rowId, colId), x, begin, count);
408 template <
int StoreMode,
typename PacketType>
409 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacketSegment(Index index,
const PacketType& x, Index begin,
411 m_argImpl.template writePacketSegment<StoreMode, PacketType>(index, x, begin, count);
416 EIGEN_DEVICE_FUNC
static constexpr Index vector_row(Index index) {
return Rows == 1 ? 0 : index; }
417 EIGEN_DEVICE_FUNC
static constexpr Index vector_col(Index index) {
return Rows == 1 ? index : 0; }
419 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType
420 coeff_impl(Index index, std::true_type )
const {
422 return m_argImpl.coeff(index);
425 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType
426 coeff_impl(Index index, std::false_type )
const {
427 return coeff_impl(vector_row(index), vector_col(index), std::false_type());
430 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType
431 coeff_impl(Index rowId, Index colId, std::true_type )
const {
432 return m_argImpl.coeff(linear_index(rowId, colId));
435 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType
436 coeff_impl(Index rowId, Index colId, std::false_type )
const {
437 const RowCol row_col = index_remap(rowId, colId);
438 return m_argImpl.coeff(row_col.first, row_col.second);
441 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index index,
443 return m_argImpl.coeffRef(index);
446 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index index,
448 return coeffRef_impl(vector_row(index), vector_col(index), std::false_type());
451 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index rowId, Index colId,
453 return m_argImpl.coeffRef(linear_index(rowId, colId));
456 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index rowId, Index colId,
458 const RowCol row_col = index_remap(rowId, colId);
459 return m_argImpl.coeffRef(row_col.first, row_col.second);
462 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar& coeffRef_impl(
463 Index index, std::true_type )
const {
464 return m_argImpl.coeffRef(index);
467 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar& coeffRef_impl(
468 Index index, std::false_type )
const {
469 return coeffRef_impl(vector_row(index), vector_col(index), std::false_type());
472 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar& coeffRef_impl(
473 Index rowId, Index colId, std::true_type )
const {
474 return m_argImpl.coeffRef(linear_index(rowId, colId));
477 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar& coeffRef_impl(
478 Index rowId, Index colId, std::false_type )
const {
479 const RowCol row_col = index_remap(rowId, colId);
480 return m_argImpl.coeffRef(row_col.first, row_col.second);
483 evaluator<ArgType> m_argImpl;
486 const variable_if_dynamic<Index, Order == ColMajor ? Rows : Cols> m_innerSize;
487 const variable_if_dynamic<Index, Order == ColMajor ? ArgType::RowsAtCompileTime : ArgType::ColsAtCompileTime>
491template <
typename ArgType,
int Rows,
int Cols,
int Order>
492struct reshaped_evaluator<ArgType, Rows, Cols, Order, true>
493 : mapbase_evaluator<Reshaped<ArgType, Rows, Cols, Order>,
494 typename Reshaped<ArgType, Rows, Cols, Order>::PlainObject> {
495 using XprType = Reshaped<ArgType, Rows, Cols, Order>;
496 using Scalar =
typename XprType::Scalar;
499 enum { ForwardLinearAccess =
false };
501 EIGEN_DEVICE_FUNC
constexpr explicit reshaped_evaluator(
const XprType& xpr)
502 : mapbase_evaluator<XprType, typename XprType::PlainObject>(xpr) {
503 eigen_assert(((std::uintptr_t(xpr.data()) % plain_enum_max(1, evaluator<XprType>::Alignment)) == 0) &&
504 "data is not aligned");
constexpr Reshaped(XprType &xpr, Index reshapeRows, Index reshapeCols)
Definition Reshaped.h:126
constexpr Reshaped(XprType &xpr)
Definition Reshaped.h:118
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71