11 #ifndef EIGEN_RESHAPED_H
12 #define EIGEN_RESHAPED_H
49 template<
typename XprType,
int Rows,
int Cols,
int Order>
50 struct 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
69 ? int(inner_stride_at_compile_time<XprType>::ret)
71 OuterStrideAtCompileTime =
Dynamic,
73 HasDirectAccess =
internal::has_direct_access<XprType>::ret
74 && (Order==int(XpxStorageOrder))
77 MaskPacketAccessBit = (InnerSize ==
Dynamic || (InnerSize % packet_traits<Scalar>::
size) == 0)
78 && (InnerStrideAtCompileTime == 1)
81 FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ?
LinearAccessBit : 0,
82 FlagsLvalueBit = is_lvalue<XprType>::value ?
LvalueBit : 0,
87 Flags = (Flags0 | FlagsLinearAccessBit | FlagsLvalueBit | FlagsRowMajorBit | FlagsDirectAccessBit)
91 template<
typename XprType,
int Rows,
int Cols,
int Order,
bool HasDirectAccess>
class ReshapedImpl_dense;
95 template<
typename XprType,
int Rows,
int Cols,
int Order,
typename StorageKind>
class ReshapedImpl;
97 template<
typename XprType,
int Rows,
int Cols,
int Order>
class Reshaped
98 :
public ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind>
122 :
Impl(xpr, reshapeRows, reshapeCols)
125 && (ColsAtCompileTime==
Dynamic || ColsAtCompileTime==reshapeCols));
126 eigen_assert(reshapeRows * reshapeCols == xpr.rows() * xpr.cols());
132 template<
typename XprType,
int Rows,
int Cols,
int Order>
134 :
public internal::ReshapedImpl_dense<XprType, Rows, Cols, Order,internal::traits<Reshaped<XprType,Rows,Cols,Order> >::HasDirectAccess>
136 typedef internal::ReshapedImpl_dense<XprType, Rows, Cols, Order,internal::traits<Reshaped<XprType,Rows,Cols,Order> >::HasDirectAccess>
Impl;
142 :
Impl(xpr, reshapeRows, reshapeCols) {}
148 template<
typename XprType,
int Rows,
int Cols,
int Order>
149 class ReshapedImpl_dense<XprType,Rows,Cols,Order,false>
150 :
public internal::dense_xpr_base<Reshaped<XprType, Rows, Cols, Order> >::type
152 typedef Reshaped<XprType, Rows, Cols, Order> ReshapedType;
155 typedef typename internal::dense_xpr_base<ReshapedType>::type Base;
159 typedef typename internal::ref_selector<XprType>::non_const_type MatrixTypeNested;
160 typedef internal::remove_all_t<XprType> NestedExpression;
167 inline ReshapedImpl_dense(XprType& xpr)
168 : m_xpr(xpr), m_rows(Rows), m_cols(Cols)
174 inline ReshapedImpl_dense(XprType& xpr,
Index nRows,
Index nCols)
175 : m_xpr(xpr), m_rows(nRows), m_cols(nCols)
181 #ifdef EIGEN_PARSED_BY_DOXYGEN
190 const internal::remove_all_t<XprType>&
191 nestedExpression()
const {
return m_xpr; }
195 std::remove_reference_t<XprType>&
196 nestedExpression() {
return m_xpr; }
200 MatrixTypeNested m_xpr;
201 const internal::variable_if_dynamic<Index, Rows> m_rows;
202 const internal::variable_if_dynamic<Index, Cols> m_cols;
207 template<
typename XprType,
int Rows,
int Cols,
int Order>
208 class ReshapedImpl_dense<XprType, Rows, Cols, Order, true>
209 :
public MapBase<Reshaped<XprType, Rows, Cols, Order> >
211 typedef Reshaped<XprType, Rows, Cols, Order> ReshapedType;
212 typedef typename internal::ref_selector<XprType>::non_const_type XprTypeNested;
215 typedef MapBase<ReshapedType> Base;
222 inline ReshapedImpl_dense(XprType& xpr)
223 : Base(xpr.
data()), m_xpr(xpr)
229 inline ReshapedImpl_dense(XprType& xpr,
Index nRows,
Index nCols)
230 : Base(xpr.
data(), nRows, nCols),
235 const internal::remove_all_t<XprTypeNested>& nestedExpression()
const
241 XprType& nestedExpression() {
return m_xpr; }
245 inline Index innerStride()
const
247 return m_xpr.innerStride();
252 inline Index outerStride()
const
263 template<
typename ArgType,
int Rows,
int Cols,
int Order,
bool HasDirectAccess>
struct reshaped_evaluator;
265 template<
typename ArgType,
int Rows,
int Cols,
int Order>
266 struct evaluator<Reshaped<ArgType, Rows, Cols, Order> >
267 : reshaped_evaluator<ArgType, Rows, Cols, Order, traits<Reshaped<ArgType,Rows,Cols,Order> >::HasDirectAccess>
269 typedef Reshaped<ArgType, Rows, Cols, Order> XprType;
270 typedef typename XprType::Scalar Scalar;
272 typedef typename packet_traits<Scalar>::type PacketScalar;
275 CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
276 HasDirectAccess = traits<XprType>::HasDirectAccess,
288 FlagsLinearAccessBit = (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1 || HasDirectAccess) ?
LinearAccessBit : 0,
289 FlagsRowMajorBit = (traits<XprType>::ReshapedStorageOrder==
int(
RowMajor)) ?
RowMajorBit : 0,
292 Flags = Flags0 | FlagsLinearAccessBit | FlagsRowMajorBit | FlagsDirectAccessBit,
294 PacketAlignment = unpacket_traits<PacketScalar>::alignment,
295 Alignment = evaluator<ArgType>::Alignment
297 typedef reshaped_evaluator<ArgType, Rows, Cols, Order, HasDirectAccess> reshaped_evaluator_type;
298 EIGEN_DEVICE_FUNC explicit evaluator(
const XprType& xpr) : reshaped_evaluator_type(xpr)
304 template<
typename ArgType,
int Rows,
int Cols,
int Order>
305 struct reshaped_evaluator<ArgType, Rows, Cols, Order, false>
306 : evaluator_base<Reshaped<ArgType, Rows, Cols, Order> >
308 typedef Reshaped<ArgType, Rows, Cols, Order> XprType;
311 CoeffReadCost = evaluator<ArgType>::CoeffReadCost ,
318 EIGEN_DEVICE_FUNC explicit reshaped_evaluator(
const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_xpr(xpr)
323 typedef typename XprType::Scalar Scalar;
324 typedef typename XprType::CoeffReturnType CoeffReturnType;
326 typedef std::pair<Index, Index> RowCol;
332 const Index nth_elem_idx = colId * m_xpr.rows() + rowId;
333 return RowCol(nth_elem_idx % m_xpr.nestedExpression().rows(),
334 nth_elem_idx / m_xpr.nestedExpression().rows());
338 const Index nth_elem_idx = colId + rowId * m_xpr.cols();
339 return RowCol(nth_elem_idx / m_xpr.nestedExpression().cols(),
340 nth_elem_idx % m_xpr.nestedExpression().cols());
345 inline Scalar& coeffRef(
Index rowId,
Index colId)
348 const RowCol row_col = index_remap(rowId, colId);
349 return m_argImpl.coeffRef(row_col.first, row_col.second);
353 inline const Scalar& coeffRef(
Index rowId,
Index colId)
const
355 const RowCol row_col = index_remap(rowId, colId);
356 return m_argImpl.coeffRef(row_col.first, row_col.second);
360 EIGEN_STRONG_INLINE
const CoeffReturnType coeff(
Index rowId,
Index colId)
const
362 const RowCol row_col = index_remap(rowId, colId);
363 return m_argImpl.coeff(row_col.first, row_col.second);
367 inline Scalar& coeffRef(
Index index)
370 const RowCol row_col = index_remap(Rows == 1 ? 0 : index,
371 Rows == 1 ? index : 0);
372 return m_argImpl.coeffRef(row_col.first, row_col.second);
377 inline const Scalar& coeffRef(
Index index)
const
379 const RowCol row_col = index_remap(Rows == 1 ? 0 : index,
380 Rows == 1 ? index : 0);
381 return m_argImpl.coeffRef(row_col.first, row_col.second);
385 inline const CoeffReturnType coeff(
Index index)
const
387 const RowCol row_col = index_remap(Rows == 1 ? 0 : index,
388 Rows == 1 ? index : 0);
389 return m_argImpl.coeff(row_col.first, row_col.second);
393 template<
int LoadMode>
394 inline PacketScalar packet(
Index rowId,
Index colId)
const
396 const RowCol row_col = index_remap(rowId, colId);
397 return m_argImpl.template packet<Unaligned>(row_col.first, row_col.second);
401 template<
int LoadMode>
403 inline void writePacket(
Index rowId,
Index colId,
const PacketScalar& val)
405 const RowCol row_col = index_remap(rowId, colId);
406 m_argImpl.const_cast_derived().template writePacket<Unaligned>
407 (row_col.first, row_col.second, val);
410 template<
int LoadMode>
412 inline PacketScalar packet(
Index index)
const
414 const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
415 RowsAtCompileTime == 1 ? index : 0);
416 return m_argImpl.template packet<Unaligned>(row_col.first, row_col.second);
419 template<
int LoadMode>
421 inline void writePacket(
Index index,
const PacketScalar& val)
423 const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
424 RowsAtCompileTime == 1 ? index : 0);
425 return m_argImpl.template packet<Unaligned>(row_col.first, row_col.second, val);
430 evaluator<ArgType> m_argImpl;
431 const XprType& m_xpr;
435 template<
typename ArgType,
int Rows,
int Cols,
int Order>
436 struct reshaped_evaluator<ArgType, Rows, Cols, Order, true>
437 : mapbase_evaluator<Reshaped<ArgType, Rows, Cols, Order>,
438 typename Reshaped<ArgType, Rows, Cols, Order>::PlainObject>
440 typedef Reshaped<ArgType, Rows, Cols, Order> XprType;
441 typedef typename XprType::Scalar Scalar;
444 : mapbase_evaluator<XprType, typename XprType::PlainObject>(xpr)
447 eigen_assert(((std::uintptr_t(xpr.data()) %
plain_enum_max(1, evaluator<XprType>::Alignment)) == 0) &&
"data is not aligned");
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
#define EIGEN_DEVICE_FUNC
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
#define EIGEN_STATIC_ASSERT_LVALUE(Derived)
#define EIGEN_STATIC_ASSERT(X, MSG)
#define EIGEN_INTERNAL_CHECK_COST_VALUE(C)
ReshapedImpl(XprType &xpr, Index reshapeRows, Index reshapeCols)
internal::ReshapedImpl_dense< XprType, Rows, Cols, Order, internal::traits< Reshaped< XprType, Rows, Cols, Order > >::HasDirectAccess > Impl
Expression of a fixed-size or dynamic-size reshape.
Reshaped(XprType &xpr, Index reshapeRows, Index reshapeCols)
ReshapedImpl< XprType, Rows, Cols, Order, typename internal::traits< XprType >::StorageKind > Impl
const unsigned int PacketAccessBit
const unsigned int LinearAccessBit
const unsigned int DirectAccessBit
const unsigned int LvalueBit
const unsigned int RowMajorBit
constexpr int plain_enum_max(A a, B b)
const unsigned int HereditaryBits
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.