$darkmode
Eigen-unsupported  5.0.1-dev
CoherentPadOp.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2020 The Eigen Team.
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_COHERENT_PAD_OP_H
11 #define EIGEN_COHERENT_PAD_OP_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 // Pads a vector with zeros to a given size.
20 template <typename XprType, int SizeAtCompileTime_>
21 struct CoherentPadOp;
22 
23 template <typename XprType, int SizeAtCompileTime_>
24 struct traits<CoherentPadOp<XprType, SizeAtCompileTime_>> : public traits<XprType> {
25  typedef typename internal::remove_all<XprType>::type PlainXprType;
26  typedef typename internal::ref_selector<XprType>::type XprNested;
27  typedef typename std::remove_reference_t<XprNested> XprNested_;
28  enum : int {
29  IsRowMajor = traits<PlainXprType>::Flags & RowMajorBit,
30  SizeAtCompileTime = SizeAtCompileTime_,
31  RowsAtCompileTime = IsRowMajor ? 1 : SizeAtCompileTime,
32  ColsAtCompileTime = IsRowMajor ? SizeAtCompileTime : 1,
33  MaxRowsAtCompileTime = RowsAtCompileTime,
34  MaxColsAtCompileTime = ColsAtCompileTime,
35  Flags = traits<XprType>::Flags & ~NestByRefBit,
36  };
37 };
38 
39 // Pads a vector with zeros to a given size.
40 template <typename XprType, int SizeAtCompileTime_>
41 struct CoherentPadOp : public dense_xpr_base<CoherentPadOp<XprType, SizeAtCompileTime_>>::type {
42  typedef typename internal::generic_xpr_base<CoherentPadOp<XprType, SizeAtCompileTime_>>::type Base;
43  EIGEN_GENERIC_PUBLIC_INTERFACE(CoherentPadOp)
44 
45  using XprNested = typename traits<CoherentPadOp>::XprNested;
46  using XprNested_ = typename traits<CoherentPadOp>::XprNested_;
47  using NestedExpression = XprNested_;
48 
49  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoherentPadOp() = delete;
50  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoherentPadOp(const CoherentPadOp&) = default;
51  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoherentPadOp(CoherentPadOp&& other) = default;
52 
53  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoherentPadOp(const XprType& xpr, Index size) : xpr_(xpr), size_(size) {
54  static_assert(XprNested_::IsVectorAtCompileTime, "input type must be a vector");
55  }
56 
57  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const XprNested_& nestedExpression() const { return xpr_; }
58 
59  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return size_.value(); }
60 
61  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rows() const {
62  return traits<CoherentPadOp>::IsRowMajor ? Index(1) : size();
63  }
64 
65  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index cols() const {
66  return traits<CoherentPadOp>::IsRowMajor ? size() : Index(1);
67  }
68 
69  private:
70  XprNested xpr_;
71  const internal::variable_if_dynamic<Index, SizeAtCompileTime> size_;
72 };
73 
74 // Adapted from the Replicate evaluator.
75 template <typename ArgType, int SizeAtCompileTime>
76 struct unary_evaluator<CoherentPadOp<ArgType, SizeAtCompileTime>>
77  : evaluator_base<CoherentPadOp<ArgType, SizeAtCompileTime>> {
78  typedef CoherentPadOp<ArgType, SizeAtCompileTime> XprType;
79  typedef typename internal::remove_all_t<typename XprType::CoeffReturnType> CoeffReturnType;
80  typedef typename internal::nested_eval<ArgType, 1>::type ArgTypeNested;
81  typedef internal::remove_all_t<ArgTypeNested> ArgTypeNestedCleaned;
82 
83  enum {
84  CoeffReadCost = evaluator<ArgTypeNestedCleaned>::CoeffReadCost,
85  LinearAccessMask = XprType::IsVectorAtCompileTime ? LinearAccessBit : 0,
86  Flags = evaluator<ArgTypeNestedCleaned>::Flags & (HereditaryBits | LinearAccessMask | RowMajorBit),
87  Alignment = evaluator<ArgTypeNestedCleaned>::Alignment
88  };
89 
90  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit unary_evaluator(const XprType& pad)
91  : m_arg(pad.nestedExpression()), m_argImpl(m_arg), m_size(pad.nestedExpression().size()) {}
92 
93  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const {
94  EIGEN_IF_CONSTEXPR(XprType::IsRowMajor) {
95  if (col < m_size.value()) {
96  return m_argImpl.coeff(1, col);
97  }
98  }
99  else {
100  if (row < m_size.value()) {
101  return m_argImpl.coeff(row, 1);
102  }
103  }
104  return CoeffReturnType(0);
105  }
106 
107  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
108  if (index < m_size.value()) {
109  return m_argImpl.coeff(index);
110  }
111  return CoeffReturnType(0);
112  }
113 
114  template <int LoadMode, typename PacketType>
115  EIGEN_STRONG_INLINE PacketType packet(Index row, Index col) const {
116  // AutoDiff scalar's derivative must be a vector, which is enforced by static assert.
117  // Defer to linear access for simplicity.
118  EIGEN_IF_CONSTEXPR(XprType::IsRowMajor) { return packet(col); }
119  return packet(row);
120  }
121 
122  template <int LoadMode, typename PacketType>
123  EIGEN_STRONG_INLINE PacketType packet(Index index) const {
124  constexpr int kPacketSize = unpacket_traits<PacketType>::size;
125  if (index + kPacketSize <= m_size.value()) {
126  return m_argImpl.template packet<LoadMode, PacketType>(index);
127  } else if (index < m_size.value()) {
128  // Partial packet.
129  EIGEN_ALIGN_MAX std::remove_const_t<CoeffReturnType> values[kPacketSize];
130  const int partial = m_size.value() - index;
131  for (int i = 0; i < partial && i < kPacketSize; ++i) {
132  values[i] = m_argImpl.coeff(index + i);
133  }
134  for (int i = partial; i < kPacketSize; ++i) {
135  values[i] = CoeffReturnType(0);
136  }
137  return pload<PacketType>(values);
138  }
139  return pset1<PacketType>(CoeffReturnType(0));
140  }
141 
142  protected:
143  const ArgTypeNested m_arg;
144  evaluator<ArgTypeNestedCleaned> m_argImpl;
145  const variable_if_dynamic<Index, ArgTypeNestedCleaned::SizeAtCompileTime> m_size;
146 };
147 
148 } // namespace internal
149 
150 } // namespace Eigen
151 
152 #endif // EIGEN_CWISE_BINARY_OP_H
Namespace containing all symbols from the Eigen library.
const unsigned int RowMajorBit
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
const unsigned int LinearAccessBit