Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Reshaped.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2017 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2014 yoco <peter.xiau@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_RESHAPED_H
13#define EIGEN_RESHAPED_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
51
52namespace internal {
53
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;
59 enum {
60 MatrixRows = traits<XprType>::RowsAtCompileTime,
61 MatrixCols = traits<XprType>::ColsAtCompileTime,
62 RowsAtCompileTime = Rows,
63 ColsAtCompileTime = Cols,
64 MaxRowsAtCompileTime = Rows,
65 MaxColsAtCompileTime = Cols,
66 XprStorageOrder = ((int(traits<XprType>::Flags) & RowMajorBit) == RowMajorBit) ? RowMajor : ColMajor,
67 ReshapedStorageOrder = (RowsAtCompileTime == 1 && ColsAtCompileTime != 1) ? RowMajor
68 : (ColsAtCompileTime == 1 && RowsAtCompileTime != 1) ? ColMajor
69 : XprStorageOrder,
70 HasSameStorageOrderAsXprType = (ReshapedStorageOrder == XprStorageOrder),
71 InnerSize = (ReshapedStorageOrder == int(RowMajor)) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
72 // A mismatched ReshapedStorageOrder only happens for vector shapes, where the storage order is
73 // immaterial: the runtime innerStride() is the nested expression's in all cases (see below).
74 InnerStrideAtCompileTime = (HasSameStorageOrderAsXprType || RowsAtCompileTime == 1 || ColsAtCompileTime == 1)
75 ? int(inner_stride_at_compile_time<XprType>::value)
76 : Dynamic,
77 OuterStrideAtCompileTime = Dynamic,
78
79 HasDirectAccess = internal::has_direct_access<XprType>::value && (Order == int(XprStorageOrder)) &&
80 ((evaluator<XprType>::Flags & LinearAccessBit) == LinearAccessBit),
81
82 MaskPacketAccessBit =
83 (InnerSize == Dynamic || (InnerSize % packet_traits<Scalar>::size) == 0) && (InnerStrideAtCompileTime == 1)
85 : 0,
86 // MaskAlignedBit = ((OuterStrideAtCompileTime!=Dynamic) && (((OuterStrideAtCompileTime * int(sizeof(Scalar))) % 16)
87 // == 0)) ? AlignedBit : 0,
88 FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ? LinearAccessBit : 0,
89 FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0,
90 FlagsRowMajorBit = (ReshapedStorageOrder == int(RowMajor)) ? RowMajorBit : 0,
91 FlagsDirectAccessBit = HasDirectAccess ? DirectAccessBit : 0,
92 Flags0 = traits<XprType>::Flags & ((HereditaryBits & ~RowMajorBit) | MaskPacketAccessBit),
93
94 Flags = (Flags0 | FlagsLinearAccessBit | FlagsLvalueBit | FlagsRowMajorBit | FlagsDirectAccessBit)
95 };
96};
97
98template <typename XprType, int Rows, int Cols, int Order, bool HasDirectAccess>
99class ReshapedImpl_dense;
100
101} // end namespace internal
102
103template <typename XprType, int Rows, int Cols, int Order, typename StorageKind>
104class ReshapedImpl;
105
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>;
109
110 public:
111 // typedef typename Impl::Base Base;
112 using Base = Impl;
113 EIGEN_GENERIC_PUBLIC_INTERFACE(Reshaped)
114 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Reshaped)
115
116
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);
122 }
123
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());
133 }
134};
135
136// The generic default implementation for dense reshape simply forward to the internal::ReshapedImpl_dense
137// that must be specialized for direct and non-direct access...
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>;
144
145 public:
146 using Base = Impl;
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) {}
151};
152
153namespace internal {
154
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>;
160
161 public:
162 using Base = typename internal::dense_xpr_base<ReshapedType>::type;
163 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
164 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
165
166 using MatrixTypeNested = typename internal::ref_selector<XprType>::non_const_type;
167 using NestedExpression = internal::remove_all_t<XprType>;
168
171 EIGEN_DEVICE_FUNC constexpr inline ReshapedImpl_dense(XprType& xpr) : m_xpr(xpr), m_rows(Rows), m_cols(Cols) {}
172
175 EIGEN_DEVICE_FUNC constexpr inline ReshapedImpl_dense(XprType& xpr, Index nRows, Index nCols)
176 : m_xpr(xpr), m_rows(nRows), m_cols(nCols) {}
177
178 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
179 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
180
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;
186#endif
187
189 EIGEN_DEVICE_FUNC constexpr const internal::remove_all_t<XprType>& nestedExpression() const { return m_xpr; }
190
192 EIGEN_DEVICE_FUNC constexpr std::remove_reference_t<XprType>& nestedExpression() { return m_xpr; }
193
194 protected:
195 MatrixTypeNested m_xpr;
196 const internal::variable_if_dynamic<Index, Rows> m_rows;
197 const internal::variable_if_dynamic<Index, Cols> m_cols;
198};
199
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;
205
206 public:
207 using Base = MapBase<ReshapedType>;
208 EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
209 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
210
211
213 EIGEN_DEVICE_FUNC constexpr inline ReshapedImpl_dense(XprType& xpr) : Base(xpr.data()), m_xpr(xpr) {}
214
217 EIGEN_DEVICE_FUNC constexpr inline ReshapedImpl_dense(XprType& xpr, Index nRows, Index nCols)
218 : Base(xpr.data(), nRows, nCols), m_xpr(xpr) {}
219
220 EIGEN_DEVICE_FUNC constexpr const internal::remove_all_t<XprTypeNested>& nestedExpression() const { return m_xpr; }
221
222 EIGEN_DEVICE_FUNC constexpr XprType& nestedExpression() { return m_xpr; }
223
225 EIGEN_DEVICE_FUNC constexpr Index innerStride() const { return m_xpr.innerStride(); }
226
228 EIGEN_DEVICE_FUNC constexpr Index outerStride() const {
229 return (((Flags & RowMajorBit) == RowMajorBit) ? this->cols() : this->rows()) * m_xpr.innerStride();
230 }
231
232 protected:
233 XprTypeNested m_xpr;
234};
235
236// Evaluators
237template <typename ArgType, int Rows, int Cols, int Order, bool HasDirectAccess>
238struct reshaped_evaluator;
239
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;
245 // TODO: should check for smaller packet types
246 using PacketScalar = typename packet_traits<Scalar>::type;
247
248 enum {
249 CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
250 HasDirectAccess = traits<XprType>::HasDirectAccess,
251
252 // RowsAtCompileTime = traits<XprType>::RowsAtCompileTime,
253 // ColsAtCompileTime = traits<XprType>::ColsAtCompileTime,
254 // MaxRowsAtCompileTime = traits<XprType>::MaxRowsAtCompileTime,
255 // MaxColsAtCompileTime = traits<XprType>::MaxColsAtCompileTime,
256 //
257 // InnerStrideAtCompileTime = traits<XprType>::HasSameStorageOrderAsXprType
258 // ? int(inner_stride_at_compile_time<ArgType>::value)
259 // : Dynamic,
260 // OuterStrideAtCompileTime = Dynamic,
261
262 // Whether the coeff-based specialization below serves all accesses by forwarding the nested
263 // evaluator's own linear accesses; always false for the direct-access specialization.
264 ForwardLinearAccess = reshaped_evaluator<ArgType, Rows, Cols, Order, bool(HasDirectAccess)>::ForwardLinearAccess,
265
266 FlagsLinearAccessBit = (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1 ||
267 HasDirectAccess || ForwardLinearAccess)
268 ? LinearAccessBit
269 : 0,
270 FlagsRowMajorBit = (traits<XprType>::ReshapedStorageOrder == int(RowMajor)) ? RowMajorBit : 0,
271 FlagsDirectAccessBit = HasDirectAccess ? DirectAccessBit : 0,
272 // A direct-access reshape with unit inner stride is the nested expression's buffer, contiguous
273 // from data(), so the mapbase_evaluator packet paths apply whenever the nested evaluator's do.
274 // A forwarding coeff-based reshape serves the nested evaluator's own packets directly.
275 MaskPacketAccessBit = (HasDirectAccess && (traits<XprType>::InnerStrideAtCompileTime == 1)) || ForwardLinearAccess
277 : 0,
278 Flags0 = evaluator<ArgType>::Flags & ((HereditaryBits & ~RowMajorBit) | MaskPacketAccessBit),
279 Flags = Flags0 | FlagsLinearAccessBit | FlagsRowMajorBit | FlagsDirectAccessBit,
280
281 PacketAlignment = unpacket_traits<PacketScalar>::alignment,
282 // The view starts at the nested data() with no offset (direct access) or forwards the nested
283 // evaluator's accesses element-for-element, so its alignment carries over.
284 Alignment = evaluator<ArgType>::Alignment
285 };
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);
289 }
290};
291
292template <typename ArgType, int Rows, int Cols, int Order>
293struct reshaped_evaluator<ArgType, Rows, Cols, Order, /* HasDirectAccess */ false>
294 : evaluator_base<Reshaped<ArgType, Rows, Cols, Order> > {
295 using XprType = Reshaped<ArgType, Rows, Cols, Order>;
296
297 enum {
298 CoeffReadCost = evaluator<ArgType>::CoeffReadCost /* TODO + cost of index computations */,
299
300 // The reshape enumerates the nested expression's elements in `Order`. When the nested
301 // evaluator and the reshape's storage both enumerate in that order, linear accesses and
302 // packets can forward unchanged. A vector shape makes only its own storage order immaterial.
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) &&
307 ((int(evaluator<ArgType>::Flags) & LinearAccessBit) != 0)
308
309 // Flags and Alignment are defined by evaluator<Reshaped>, which derives from this evaluator.
310 };
311
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);
317 }
318
319 using Scalar = typename XprType::Scalar;
320 using CoeffReturnType = typename XprType::CoeffReturnType;
321
322 using RowCol = std::pair<Index, Index>;
323
324 // The n-th element of the reshape in `Order` enumeration; under ForwardLinearAccess this is also
325 // the nested evaluator's linear index of that element.
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;
329 } else {
330 return colId + rowId * m_innerSize.value();
331 }
332 }
333
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());
338 } else {
339 return RowCol(nth_elem_idx / m_nestedInnerSize.value(), nth_elem_idx % m_nestedInnerSize.value());
340 }
341 }
342
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>());
346 }
347
348 EIGEN_DEVICE_FUNC constexpr inline const Scalar& coeffRef(Index rowId, Index colId) const {
349 return coeffRef_impl(rowId, colId, bool_constant<ForwardLinearAccess>());
350 }
351
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>());
354 }
355
356 EIGEN_DEVICE_FUNC constexpr inline Scalar& coeffRef(Index index) {
357 EIGEN_STATIC_ASSERT_LVALUE(XprType)
358 return coeffRef_impl(index, bool_constant<ForwardLinearAccess>());
359 }
360
361 EIGEN_DEVICE_FUNC constexpr inline const Scalar& coeffRef(Index index) const {
362 return coeffRef_impl(index, bool_constant<ForwardLinearAccess>());
363 }
364
365 EIGEN_DEVICE_FUNC constexpr inline const CoeffReturnType coeff(Index index) const {
366 return coeff_impl(index, bool_constant<ForwardLinearAccess>());
367 }
368
369 // The packet paths are advertised only under ForwardLinearAccess (see evaluator<Reshaped>), so
370 // they forward the nested evaluator's linear packets without index remapping.
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));
374 }
375
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);
379 }
380
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);
384 }
385
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);
389 }
390
391 template <int LoadMode, typename PacketType>
392 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index rowId, Index colId, Index begin,
393 Index count) const {
394 return m_argImpl.template packetSegment<LoadMode, PacketType>(linear_index(rowId, colId), begin, count);
395 }
396
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);
400 }
401
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);
406 }
407
408 template <int StoreMode, typename PacketType>
409 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void writePacketSegment(Index index, const PacketType& x, Index begin,
410 Index count) {
411 m_argImpl.template writePacketSegment<StoreMode, PacketType>(index, x, begin, count);
412 }
413
414 protected:
415 // Linear-access members map an index onto the vector shape's single row or column.
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; }
418
419 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const CoeffReturnType
420 coeff_impl(Index index, std::true_type /* ForwardLinearAccess */) const {
421 // The one-dimensional index already follows the nested evaluator's linear enumeration.
422 return m_argImpl.coeff(index);
423 }
424
425 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const CoeffReturnType
426 coeff_impl(Index index, std::false_type /* not ForwardLinearAccess */) const {
427 return coeff_impl(vector_row(index), vector_col(index), std::false_type());
428 }
429
430 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const CoeffReturnType
431 coeff_impl(Index rowId, Index colId, std::true_type /* ForwardLinearAccess */) const {
432 return m_argImpl.coeff(linear_index(rowId, colId));
433 }
434
435 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const CoeffReturnType
436 coeff_impl(Index rowId, Index colId, std::false_type /* not ForwardLinearAccess */) const {
437 const RowCol row_col = index_remap(rowId, colId);
438 return m_argImpl.coeff(row_col.first, row_col.second);
439 }
440
441 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index index,
442 std::true_type /* ForwardLinearAccess */) {
443 return m_argImpl.coeffRef(index);
444 }
445
446 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index index,
447 std::false_type /* not ForwardLinearAccess */) {
448 return coeffRef_impl(vector_row(index), vector_col(index), std::false_type());
449 }
450
451 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index rowId, Index colId,
452 std::true_type /* ForwardLinearAccess */) {
453 return m_argImpl.coeffRef(linear_index(rowId, colId));
454 }
455
456 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar& coeffRef_impl(Index rowId, Index colId,
457 std::false_type /* not ForwardLinearAccess */) {
458 const RowCol row_col = index_remap(rowId, colId);
459 return m_argImpl.coeffRef(row_col.first, row_col.second);
460 }
461
462 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar& coeffRef_impl(
463 Index index, std::true_type /* ForwardLinearAccess */) const {
464 return m_argImpl.coeffRef(index);
465 }
466
467 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar& coeffRef_impl(
468 Index index, std::false_type /* not ForwardLinearAccess */) const {
469 return coeffRef_impl(vector_row(index), vector_col(index), std::false_type());
470 }
471
472 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar& coeffRef_impl(
473 Index rowId, Index colId, std::true_type /* ForwardLinearAccess */) const {
474 return m_argImpl.coeffRef(linear_index(rowId, colId));
475 }
476
477 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar& coeffRef_impl(
478 Index rowId, Index colId, std::false_type /* not ForwardLinearAccess */) const {
479 const RowCol row_col = index_remap(rowId, colId);
480 return m_argImpl.coeffRef(row_col.first, row_col.second);
481 }
482
483 evaluator<ArgType> m_argImpl;
484 // Sizes are copied rather than read through the expression: select() evaluates a rewritten temporary expression
485 // that does not outlive this evaluator.
486 const variable_if_dynamic<Index, Order == ColMajor ? Rows : Cols> m_innerSize;
487 const variable_if_dynamic<Index, Order == ColMajor ? ArgType::RowsAtCompileTime : ArgType::ColsAtCompileTime>
488 m_nestedInnerSize;
489};
490
491template <typename ArgType, int Rows, int Cols, int Order>
492struct reshaped_evaluator<ArgType, Rows, Cols, Order, /* HasDirectAccess */ 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;
497
498 // Packets come from the mapbase_evaluator machinery, not from linear forwarding.
499 enum { ForwardLinearAccess = false };
500
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");
505 }
506};
507
508} // end namespace internal
509
510} // end namespace Eigen
511
512#endif // EIGEN_RESHAPED_H
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