11 #ifndef EIGEN_RESHAPED_H
12 #define EIGEN_RESHAPED_H
14 #include "./InternalHeaderCheck.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,
85 Flags0 = traits<XprType>::Flags & ( (HereditaryBits & ~
RowMajorBit) | MaskPacketAccessBit),
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>
100 typedef ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind> Impl;
104 EIGEN_GENERIC_PUBLIC_INTERFACE(
Reshaped)
105 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Reshaped)
113 EIGEN_STATIC_ASSERT(RowsAtCompileTime!=
Dynamic && ColsAtCompileTime!=
Dynamic,THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
114 eigen_assert(Rows * Cols == xpr.rows() * xpr.cols());
122 : Impl(xpr, reshapeRows, reshapeCols)
124 eigen_assert((RowsAtCompileTime==
Dynamic || RowsAtCompileTime==reshapeRows)
125 && (ColsAtCompileTime==
Dynamic || ColsAtCompileTime==reshapeCols));
126 eigen_assert(reshapeRows * reshapeCols == xpr.rows() * xpr.cols());
132 template<
typename XprType,
int Rows,
int Cols,
int Order>
133 class ReshapedImpl<XprType, Rows, Cols, Order, Dense>
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;
139 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl)
140 EIGEN_DEVICE_FUNC
inline ReshapedImpl(XprType& xpr) : Impl(xpr) {}
141 EIGEN_DEVICE_FUNC
inline ReshapedImpl(XprType& xpr,
Index reshapeRows,
Index reshapeCols)
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;
156 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
157 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
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)
178 EIGEN_DEVICE_FUNC
Index rows()
const {
return m_rows; }
179 EIGEN_DEVICE_FUNC
Index cols()
const {
return m_cols; }
181 #ifdef EIGEN_PARSED_BY_DOXYGEN
183 EIGEN_DEVICE_FUNC
inline const Scalar* data()
const;
184 EIGEN_DEVICE_FUNC
inline Index innerStride()
const;
185 EIGEN_DEVICE_FUNC
inline Index outerStride()
const;
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;
216 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
217 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
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; }
244 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
245 inline Index innerStride()
const
247 return m_xpr.innerStride();
251 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
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,
291 Flags0 = evaluator<ArgType>::Flags & (HereditaryBits & ~
RowMajorBit),
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)
300 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
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 ,
313 Flags = (evaluator<ArgType>::Flags & (HereditaryBits )),
318 EIGEN_DEVICE_FUNC
explicit reshaped_evaluator(
const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_xpr(xpr)
320 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
323 typedef typename XprType::Scalar Scalar;
324 typedef typename XprType::CoeffReturnType CoeffReturnType;
326 typedef std::pair<Index, Index> RowCol;
328 inline RowCol index_remap(
Index rowId,
Index colId)
const
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)
347 EIGEN_STATIC_ASSERT_LVALUE(XprType)
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)
369 EIGEN_STATIC_ASSERT_LVALUE(XprType)
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;
443 EIGEN_DEVICE_FUNC
explicit reshaped_evaluator(
const XprType& xpr)
444 : mapbase_evaluator<XprType, typename XprType::PlainObject>(xpr)
447 eigen_assert(((internal::UIntPtr(xpr.data()) % plain_enum_max(1, evaluator<XprType>::Alignment)) == 0) &&
"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:120
Reshaped(XprType &xpr)
Definition: Reshaped.h:110
@ ColMajor
Definition: Constants.h:321
@ RowMajor
Definition: Constants.h:323
const unsigned int PacketAccessBit
Definition: Constants.h:96
const unsigned int LinearAccessBit
Definition: Constants.h:132
const unsigned int DirectAccessBit
Definition: Constants.h:157
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: Core:139
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