12#ifndef EIGEN_RESHAPED_H
13#define EIGEN_RESHAPED_H
16#include "./InternalHeaderCheck.h"
51template <
typename XprType,
int Rows,
int Cols,
int Order>
52struct traits<Reshaped<XprType, Rows, Cols, Order> > : traits<XprType> {
53 using Scalar =
typename traits<XprType>::Scalar;
54 using StorageKind =
typename traits<XprType>::StorageKind;
55 using XprKind =
typename traits<XprType>::XprKind;
57 MatrixRows = traits<XprType>::RowsAtCompileTime,
58 MatrixCols = traits<XprType>::ColsAtCompileTime,
59 RowsAtCompileTime = Rows,
60 ColsAtCompileTime = Cols,
61 MaxRowsAtCompileTime = Rows,
62 MaxColsAtCompileTime = Cols,
64 ReshapedStorageOrder = (RowsAtCompileTime == 1 && ColsAtCompileTime != 1) ?
RowMajor
65 : (ColsAtCompileTime == 1 && RowsAtCompileTime != 1) ?
ColMajor
67 HasSameStorageOrderAsXprType = (ReshapedStorageOrder == XprStorageOrder),
68 InnerSize = (ReshapedStorageOrder == int(
RowMajor)) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
71 InnerStrideAtCompileTime = (HasSameStorageOrderAsXprType || RowsAtCompileTime == 1 || ColsAtCompileTime == 1)
72 ? int(inner_stride_at_compile_time<XprType>::value)
74 OuterStrideAtCompileTime = Dynamic,
76 HasDirectAccess = internal::has_direct_access<XprType>::value && (Order == int(XprStorageOrder)) &&
80 (InnerSize == Dynamic || (InnerSize % packet_traits<Scalar>::size) == 0) && (InnerStrideAtCompileTime == 1)
85 FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ?
LinearAccessBit : 0,
86 FlagsLvalueBit = is_lvalue<XprType>::value ?
LvalueBit : 0,
89 Flags0 = traits<XprType>::Flags & ((HereditaryBits & ~
RowMajorBit) | MaskPacketAccessBit),
91 Flags = (Flags0 | FlagsLinearAccessBit | FlagsLvalueBit | FlagsRowMajorBit | FlagsDirectAccessBit)
95template <
typename XprType,
int Rows,
int Cols,
int Order,
bool HasDirectAccess>
96class ReshapedImpl_dense;
100template <
typename XprType,
int Rows,
int Cols,
int Order,
typename StorageKind>
103template <
typename XprType,
int Rows,
int Cols,
int Order>
104class Reshaped :
public ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind> {
105 using Impl = ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind>;
110 EIGEN_GENERIC_PUBLIC_INTERFACE(
Reshaped)
111 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Reshaped)
115 EIGEN_DEVICE_FUNC constexpr inline
Reshaped(XprType& xpr) : Impl(xpr) {
116 EIGEN_STATIC_ASSERT(RowsAtCompileTime != Dynamic && ColsAtCompileTime != Dynamic,
117 THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
118 eigen_assert(Rows * Cols == xpr.rows() * xpr.cols());
123 EIGEN_DEVICE_FUNC
constexpr inline Reshaped(XprType& xpr, Index reshapeRows, Index reshapeCols)
124 : Impl(xpr, reshapeRows, reshapeCols) {
125 eigen_assert((RowsAtCompileTime == Dynamic || RowsAtCompileTime == reshapeRows) &&
126 (ColsAtCompileTime == Dynamic || ColsAtCompileTime == reshapeCols));
127 eigen_assert(reshapeRows * reshapeCols == xpr.rows() * xpr.cols());
133template <
typename XprType,
int Rows,
int Cols,
int Order>
134class ReshapedImpl<XprType, Rows, Cols, Order, Dense>
135 :
public internal::ReshapedImpl_dense<XprType, Rows, Cols, Order,
136 internal::traits<Reshaped<XprType, Rows, Cols, Order> >::HasDirectAccess> {
137 using Impl = internal::ReshapedImpl_dense<XprType, Rows, Cols, Order,
138 internal::traits<Reshaped<XprType, Rows, Cols, Order>>::HasDirectAccess>;
142 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl)
143 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl(XprType& xpr) : Impl(xpr) {}
144 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl(XprType& xpr, Index reshapeRows, Index reshapeCols)
145 : Impl(xpr, reshapeRows, reshapeCols) {}
151template <
typename XprType,
int Rows,
int Cols,
int Order>
152class ReshapedImpl_dense<XprType, Rows, Cols, Order, false>
153 :
public internal::dense_xpr_base<Reshaped<XprType, Rows, Cols, Order> >::type {
154 using ReshapedType = Reshaped<XprType, Rows, Cols, Order>;
157 using Base =
typename internal::dense_xpr_base<ReshapedType>::type;
158 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
159 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
161 using MatrixTypeNested =
typename internal::ref_selector<XprType>::non_const_type;
162 using NestedExpression = internal::remove_all_t<XprType>;
168 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl_dense(XprType& xpr) : m_xpr(xpr), m_rows(Rows), m_cols(Cols) {}
172 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl_dense(XprType& xpr, Index nRows, Index nCols)
173 : m_xpr(xpr), m_rows(nRows), m_cols(nCols) {}
175 EIGEN_DEVICE_FUNC
constexpr Index rows()
const {
return m_rows; }
176 EIGEN_DEVICE_FUNC
constexpr Index cols()
const {
return m_cols; }
178#ifdef EIGEN_PARSED_BY_DOXYGEN
180 EIGEN_DEVICE_FUNC
constexpr const Scalar* data()
const;
181 EIGEN_DEVICE_FUNC
inline Index innerStride()
const;
182 EIGEN_DEVICE_FUNC
inline Index outerStride()
const;
186 EIGEN_DEVICE_FUNC
constexpr const internal::remove_all_t<XprType>& nestedExpression()
const {
return m_xpr; }
189 EIGEN_DEVICE_FUNC
constexpr std::remove_reference_t<XprType>& nestedExpression() {
return m_xpr; }
192 MatrixTypeNested m_xpr;
193 const internal::variable_if_dynamic<Index, Rows> m_rows;
194 const internal::variable_if_dynamic<Index, Cols> m_cols;
198template <
typename XprType,
int Rows,
int Cols,
int Order>
199class ReshapedImpl_dense<XprType, Rows, Cols, Order, true> :
public MapBase<Reshaped<XprType, Rows, Cols, Order> > {
200 using ReshapedType = Reshaped<XprType, Rows, Cols, Order>;
201 using XprTypeNested =
typename internal::ref_selector<XprType>::non_const_type;
204 using Base = MapBase<ReshapedType>;
205 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
206 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
210 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl_dense(XprType& xpr) : Base(xpr.data()), m_xpr(xpr) {}
214 EIGEN_DEVICE_FUNC
constexpr inline ReshapedImpl_dense(XprType& xpr, Index nRows, Index nCols)
215 : Base(xpr.data(), nRows, nCols), m_xpr(xpr) {}
217 EIGEN_DEVICE_FUNC
constexpr const internal::remove_all_t<XprTypeNested>& nestedExpression()
const {
return m_xpr; }
219 EIGEN_DEVICE_FUNC
constexpr XprType& nestedExpression() {
return m_xpr; }
222 EIGEN_DEVICE_FUNC
constexpr Index innerStride()
const {
return m_xpr.innerStride(); }
225 EIGEN_DEVICE_FUNC
constexpr Index outerStride()
const {
226 return (((Flags & RowMajorBit) == RowMajorBit) ? this->cols() : this->rows()) * m_xpr.innerStride();
234template <
typename ArgType,
int Rows,
int Cols,
int Order,
bool HasDirectAccess>
235struct reshaped_evaluator;
237template <
typename ArgType,
int Rows,
int Cols,
int Order>
238struct evaluator<Reshaped<ArgType, Rows, Cols, Order> >
239 : reshaped_evaluator<ArgType, Rows, Cols, Order, traits<Reshaped<ArgType, Rows, Cols, Order> >::HasDirectAccess> {
240 using XprType = Reshaped<ArgType, Rows, Cols, Order>;
241 using Scalar =
typename XprType::Scalar;
243 using PacketScalar =
typename packet_traits<Scalar>::type;
246 CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
247 HasDirectAccess = traits<XprType>::HasDirectAccess,
261 ForwardLinearAccess = reshaped_evaluator<ArgType, Rows, Cols, Order, bool(HasDirectAccess)>::ForwardLinearAccess,
263 FlagsLinearAccessBit = (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1 ||
264 HasDirectAccess || ForwardLinearAccess)
267 FlagsRowMajorBit = (traits<XprType>::ReshapedStorageOrder == int(
RowMajor)) ?
RowMajorBit : 0,
272 MaskPacketAccessBit = (HasDirectAccess && (traits<XprType>::InnerStrideAtCompileTime == 1)) || ForwardLinearAccess
275 Flags0 = evaluator<ArgType>::Flags & ((HereditaryBits & ~
RowMajorBit) | MaskPacketAccessBit),
276 Flags = Flags0 | FlagsLinearAccessBit | FlagsRowMajorBit | FlagsDirectAccessBit,
278 PacketAlignment = unpacket_traits<PacketScalar>::alignment,
281 Alignment = evaluator<ArgType>::Alignment
283 using reshaped_evaluator_type = reshaped_evaluator<ArgType, Rows, Cols, Order, HasDirectAccess>;
284 EIGEN_DEVICE_FUNC
constexpr explicit evaluator(
const XprType& xpr) : reshaped_evaluator_type(xpr) {
285 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
289template <
typename ArgType,
int Rows,
int Cols,
int Order>
290struct reshaped_evaluator<ArgType, Rows, Cols, Order, false>
291 : evaluator_base<Reshaped<ArgType, Rows, Cols, Order> > {
292 using XprType = Reshaped<ArgType, Rows, Cols, Order>;
295 CoeffReadCost = evaluator<ArgType>::CoeffReadCost ,
300 NestedRowMajor = (int(evaluator<ArgType>::Flags) &
RowMajorBit) != 0,
301 OrderMatchesNested = (Order ==
int(ColMajor)) != NestedRowMajor,
302 ForwardLinearAccess = (OrderMatchesNested || ArgType::RowsAtCompileTime == 1 || ArgType::ColsAtCompileTime == 1) &&
303 (Order ==
int(traits<XprType>::ReshapedStorageOrder) || Rows == 1 || Cols == 1) &&
309 EIGEN_DEVICE_FUNC
constexpr explicit reshaped_evaluator(
const XprType& xpr)
310 : m_argImpl(xpr.nestedExpression()), m_xpr(xpr) {
311 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
314 using Scalar =
typename XprType::Scalar;
315 using CoeffReturnType =
typename XprType::CoeffReturnType;
317 using RowCol = std::pair<Index, Index>;
321 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Index linear_index(Index rowId, Index colId)
const {
322 EIGEN_IF_CONSTEXPR (Order == ColMajor) {
323 return colId * m_xpr.rows() + rowId;
325 return colId + rowId * m_xpr.cols();
329 EIGEN_DEVICE_FUNC
constexpr inline RowCol index_remap(Index rowId, Index colId)
const {
330 const Index nth_elem_idx = linear_index(rowId, colId);
331 EIGEN_IF_CONSTEXPR (Order == ColMajor) {
332 return RowCol(nth_elem_idx % m_xpr.nestedExpression().rows(), nth_elem_idx / m_xpr.nestedExpression().rows());
334 return RowCol(nth_elem_idx / m_xpr.nestedExpression().cols(), nth_elem_idx % m_xpr.nestedExpression().cols());
338 EIGEN_DEVICE_FUNC
constexpr inline Scalar& coeffRef(Index rowId, Index colId) {
339 EIGEN_STATIC_ASSERT_LVALUE(XprType)
340 return coeffRef_impl(rowId, colId, bool_constant<ForwardLinearAccess>());
343 EIGEN_DEVICE_FUNC
constexpr inline const Scalar& coeffRef(Index rowId, Index colId)
const {
344 return coeffRef_impl(rowId, colId, bool_constant<ForwardLinearAccess>());
347 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType coeff(Index rowId, Index colId)
const {
348 return coeff_impl(rowId, colId, bool_constant<ForwardLinearAccess>());
351 EIGEN_DEVICE_FUNC
constexpr inline Scalar& coeffRef(Index index) {
352 EIGEN_STATIC_ASSERT_LVALUE(XprType)
353 return coeffRef_impl(index, bool_constant<ForwardLinearAccess>());
356 EIGEN_DEVICE_FUNC
constexpr inline const Scalar& coeffRef(Index index)
const {
357 return coeffRef_impl(index, bool_constant<ForwardLinearAccess>());
360 EIGEN_DEVICE_FUNC
constexpr inline const CoeffReturnType coeff(Index index)
const {
361 return coeff_impl(index, bool_constant<ForwardLinearAccess>());
366 template <
int LoadMode,
typename PacketType>
367 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index rowId, Index colId)
const {
368 return m_argImpl.template packet<LoadMode, PacketType>(linear_index(rowId, colId));
371 template <
int LoadMode,
typename PacketType>
372 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index index)
const {
373 return m_argImpl.template packet<LoadMode, PacketType>(index);
376 template <
int StoreMode,
typename PacketType>
377 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacket(Index rowId, Index colId,
const PacketType& x) {
378 m_argImpl.template writePacket<StoreMode, PacketType>(linear_index(rowId, colId), x);
381 template <
int StoreMode,
typename PacketType>
382 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacket(Index index,
const PacketType& x) {
383 m_argImpl.template writePacket<StoreMode, PacketType>(index, x);
386 template <
int LoadMode,
typename PacketType>
387 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index rowId, Index colId, Index begin,
389 return m_argImpl.template packetSegment<LoadMode, PacketType>(linear_index(rowId, colId), begin, count);
392 template <
int LoadMode,
typename PacketType>
393 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index index, Index begin, Index count)
const {
394 return m_argImpl.template packetSegment<LoadMode, PacketType>(index, begin, count);
397 template <
int StoreMode,
typename PacketType>
398 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacketSegment(Index rowId, Index colId,
const PacketType& x,
399 Index begin, Index count) {
400 m_argImpl.template writePacketSegment<StoreMode, PacketType>(linear_index(rowId, colId), x, begin, count);
403 template <
int StoreMode,
typename PacketType>
404 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacketSegment(Index index,
const PacketType& x, Index begin,
406 m_argImpl.template writePacketSegment<StoreMode, PacketType>(index, x, begin, count);
411 EIGEN_DEVICE_FUNC
static constexpr Index vector_row(Index index) {
return Rows == 1 ? 0 : index; }
412 EIGEN_DEVICE_FUNC
static constexpr Index vector_col(Index index) {
return Rows == 1 ? index : 0; }
414 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType
415 coeff_impl(Index index, std::true_type )
const {
417 return m_argImpl.coeff(index);
420 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType
421 coeff_impl(Index index, std::false_type )
const {
422 return coeff_impl(vector_row(index), vector_col(index), std::false_type());
425 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType
426 coeff_impl(Index rowId, Index colId, std::true_type )
const {
427 return m_argImpl.coeff(linear_index(rowId, colId));
430 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const CoeffReturnType
431 coeff_impl(Index rowId, Index colId, std::false_type )
const {
432 const RowCol row_col = index_remap(rowId, colId);
433 return m_argImpl.coeff(row_col.first, row_col.second);
436 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index index,
438 return m_argImpl.coeffRef(index);
441 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index index,
443 return coeffRef_impl(vector_row(index), vector_col(index), std::false_type());
446 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index rowId, Index colId,
448 return m_argImpl.coeffRef(linear_index(rowId, colId));
451 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index rowId, Index colId,
453 const RowCol row_col = index_remap(rowId, colId);
454 return m_argImpl.coeffRef(row_col.first, row_col.second);
457 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar& coeffRef_impl(
458 Index index, std::true_type )
const {
459 return m_argImpl.coeffRef(index);
462 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar& coeffRef_impl(
463 Index index, std::false_type )
const {
464 return coeffRef_impl(vector_row(index), vector_col(index), std::false_type());
467 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar& coeffRef_impl(
468 Index rowId, Index colId, std::true_type )
const {
469 return m_argImpl.coeffRef(linear_index(rowId, colId));
472 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
const Scalar& coeffRef_impl(
473 Index rowId, Index colId, std::false_type )
const {
474 const RowCol row_col = index_remap(rowId, colId);
475 return m_argImpl.coeffRef(row_col.first, row_col.second);
478 evaluator<ArgType> m_argImpl;
479 const XprType& m_xpr;
482template <
typename ArgType,
int Rows,
int Cols,
int Order>
483struct reshaped_evaluator<ArgType, Rows, Cols, Order, true>
484 : mapbase_evaluator<Reshaped<ArgType, Rows, Cols, Order>,
485 typename Reshaped<ArgType, Rows, Cols, Order>::PlainObject> {
486 using XprType = Reshaped<ArgType, Rows, Cols, Order>;
487 using Scalar =
typename XprType::Scalar;
490 enum { ForwardLinearAccess =
false };
492 EIGEN_DEVICE_FUNC
constexpr explicit reshaped_evaluator(
const XprType& xpr)
493 : mapbase_evaluator<XprType, typename XprType::PlainObject>(xpr) {
494 eigen_assert(((std::uintptr_t(xpr.data()) % plain_enum_max(1, evaluator<XprType>::Alignment)) == 0) &&
495 "data is not aligned");
constexpr Reshaped(XprType &xpr, Index reshapeRows, Index reshapeCols)
Definition Reshaped.h:123
constexpr Reshaped(XprType &xpr)
Definition Reshaped.h:115
@ 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