TensorFixedSize.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
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_CXX11_TENSOR_TENSOR_FIXED_SIZE_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_FIXED_SIZE_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
29 template <typename Scalar_, typename Dimensions_, int Options_, typename IndexType>
30 class TensorFixedSize : public TensorBase<TensorFixedSize<Scalar_, Dimensions_, Options_, IndexType> > {
31  public:
37  typedef Scalar_ Scalar;
40 
41  static constexpr int Options = Options_;
42  static constexpr int Layout = Options_ & RowMajor ? RowMajor : ColMajor;
43 
44  enum {
47  BlockAccess = false,
49  CoordAccess = true,
50  RawAccess = true
51  };
52 
53  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
55  //===--------------------------------------------------------------------===//
56 
57  typedef Dimensions_ Dimensions;
58  static constexpr std::size_t NumIndices = Dimensions::count;
59 
60  protected:
62 
63  public:
70 
71  // This makes EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
72  // work, because that uses base().coeffRef() - and we don't yet
73  // implement a similar class hierarchy
74  inline Self& base() { return *this; }
75  inline const Self& base() const { return *this; }
76 
77  template <typename... IndexTypes>
78  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(Index firstIndex, IndexTypes... otherIndices) const {
79  // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
80  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
81  return coeff(array<Index, NumIndices>{{firstIndex, otherIndices...}});
82  }
83 
86  return m_storage.data()[linearizedIndex(indices)];
87  }
88 
90  eigen_internal_assert(index >= 0 && index < size());
91  return m_storage.data()[index];
92  }
93 
95  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
96  return m_storage.data()[0];
97  }
98 
99  template <typename... IndexTypes>
100  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index firstIndex, IndexTypes... otherIndices) {
101  // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
102  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
103  return coeffRef(array<Index, NumIndices>{{firstIndex, otherIndices...}});
104  }
105 
108  return m_storage.data()[linearizedIndex(indices)];
109  }
110 
112  eigen_internal_assert(index >= 0 && index < size());
113  return m_storage.data()[index];
114  }
115 
117  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
118  return m_storage.data()[0];
119  }
120 
121  template <typename... IndexTypes>
122  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(Index firstIndex, IndexTypes... otherIndices) const {
123  // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
124  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
125  return this->operator()(array<Index, NumIndices>{{firstIndex, otherIndices...}});
126  }
127 
129  eigen_assert(checkIndexRange(indices));
130  return coeff(indices);
131  }
132 
134  eigen_internal_assert(index >= 0 && index < size());
135  return coeff(index);
136  }
137 
139  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
140  return coeff();
141  }
142 
144  // The bracket operator is only for vectors, use the parenthesis operator instead.
145  EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE);
146  return coeff(index);
147  }
148 
149  template <typename... IndexTypes>
150  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index firstIndex, IndexTypes... otherIndices) {
151  // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
152  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
153  return operator()(array<Index, NumIndices>{{firstIndex, otherIndices...}});
154  }
155 
157  eigen_assert(checkIndexRange(indices));
158  return coeffRef(indices);
159  }
160 
162  eigen_assert(index >= 0 && index < size());
163  return coeffRef(index);
164  }
165 
167  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
168  return coeffRef();
169  }
170 
172  // The bracket operator is only for vectors, use the parenthesis operator instead
173  EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
174  return coeffRef(index);
175  }
176 
178 
180 
182 
183  template <typename OtherDerived>
186  Assign assign(*this, other.derived());
188  }
189  template <typename OtherDerived>
192  Assign assign(*this, other.derived());
194  }
195 
196  // FIXME: check that the dimensions of other match the dimensions of *this.
197  // Unfortunately this isn't possible yet when the rhs is an expression.
199 
200  protected:
205  using internal::lesser_op;
207 
208  return true;
209  // check whether the indices are all >= 0
210  /* array_apply_and_reduce<logical_and_op, greater_equal_zero_op>(indices) &&
211  // check whether the indices fit in the dimensions
212  array_zip_and_reduce<logical_and_op, lesser_op>(indices, m_storage.dimensions());*/
213  }
214 
216  if (Options & RowMajor) {
217  return m_storage.dimensions().IndexOfRowMajor(indices);
218  } else {
219  return m_storage.dimensions().IndexOfColMajor(indices);
220  }
221  }
222 };
223 
224 } // end namespace Eigen
225 
226 #endif // EIGEN_CXX11_TENSOR_TENSOR_FIXED_SIZE_H
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
#define EIGEN_MAX_ALIGN_BYTES
Definition: ConfigureVectorization.h:163
#define eigen_internal_assert(x)
Definition: Macros.h:916
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_TENSOR_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived)
Definition: TensorMacros.h:72
Definition: TensorAssign.h:57
The tensor base class.
Definition: TensorBase.h:1026
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived & derived()
Definition: TensorBase.h:1220
The fixed sized version of the tensor class.
Definition: TensorFixedSize.h:30
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Definition: TensorFixedSize.h:111
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff() const
Definition: TensorFixedSize.h:94
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(Self &&other)
Definition: TensorFixedSize.h:181
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(const TensorBase< OtherDerived, WriteAccessors > &other)
Definition: TensorFixedSize.h:190
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef()
Definition: TensorFixedSize.h:116
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()()
Definition: TensorFixedSize.h:166
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()() const
Definition: TensorFixedSize.h:138
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()(const array< Index, NumIndices > &indices) const
Definition: TensorFixedSize.h:128
const Self & base() const
Definition: TensorFixedSize.h:75
TensorBase< TensorFixedSize< Scalar_, Dimensions_, Options_, IndexType > > Base
Definition: TensorFixedSize.h:33
Scalar_ Scalar
Definition: TensorFixedSize.h:37
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(const Self &other)
Definition: TensorFixedSize.h:179
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(Index firstIndex, IndexTypes... otherIndices) const
Definition: TensorFixedSize.h:78
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index firstIndex, IndexTypes... otherIndices)
Definition: TensorFixedSize.h:150
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()(Index firstIndex, IndexTypes... otherIndices) const
Definition: TensorFixedSize.h:122
TensorStorage< Scalar, Dimensions, Options > m_storage
Definition: TensorFixedSize.h:61
Eigen::internal::nested< Self >::type Nested
Definition: TensorFixedSize.h:34
internal::TensorBlockNotImplemented TensorBlock
Definition: TensorFixedSize.h:54
static constexpr std::size_t NumIndices
Definition: TensorFixedSize.h:58
Self & base()
Definition: TensorFixedSize.h:74
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(const array< Index, NumIndices > &indices)
Definition: TensorFixedSize.h:156
static constexpr int Options
Definition: TensorFixedSize.h:41
Base::CoeffReturnType CoeffReturnType
Definition: TensorFixedSize.h:39
TensorFixedSize< Scalar_, Dimensions_, Options_, IndexType > Self
Definition: TensorFixedSize.h:32
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()(Index index) const
Definition: TensorFixedSize.h:133
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(const array< Index, NumIndices > &indices)
Definition: TensorFixedSize.h:106
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: TensorFixedSize.h:67
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator[](Index index) const
Definition: TensorFixedSize.h:143
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize()
Definition: TensorFixedSize.h:177
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition: TensorFixedSize.h:64
internal::traits< Self >::Index Index
Definition: TensorFixedSize.h:36
Dimensions_ Dimensions
Definition: TensorFixedSize.h:57
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: TensorFixedSize.h:65
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(const TensorBase< OtherDerived, ReadOnlyAccessors > &other)
Definition: TensorFixedSize.h:184
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index firstIndex, IndexTypes... otherIndices)
Definition: TensorFixedSize.h:100
internal::traits< Self >::StorageKind StorageKind
Definition: TensorFixedSize.h:35
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index index)
Definition: TensorFixedSize.h:161
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool checkIndexRange(const array< Index, NumIndices > &) const
Definition: TensorFixedSize.h:201
NumTraits< Scalar >::Real RealScalar
Definition: TensorFixedSize.h:38
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index linearizedIndex(const array< Index, NumIndices > &indices) const
Definition: TensorFixedSize.h:215
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
Definition: TensorFixedSize.h:69
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: TensorFixedSize.h:68
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(Index index) const
Definition: TensorFixedSize.h:89
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions dimensions() const
Definition: TensorFixedSize.h:66
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator[](Index index)
Definition: TensorFixedSize.h:171
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(const array< Index, NumIndices > &indices) const
Definition: TensorFixedSize.h:84
@ BlockAccess
Definition: TensorFixedSize.h:47
@ PreferBlockAccess
Definition: TensorFixedSize.h:48
@ RawAccess
Definition: TensorFixedSize.h:50
@ IsAligned
Definition: TensorFixedSize.h:45
@ PacketAccess
Definition: TensorFixedSize.h:46
@ CoordAccess
Definition: TensorFixedSize.h:49
static constexpr int Layout
Definition: TensorFixedSize.h:42
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex size() const
Definition: TensorStorage.h:58
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const FixedDimensions dimensions() const
Definition: TensorStorage.h:56
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * data()
Definition: TensorStorage.h:53
Definition: TensorBlock.h:566
static EIGEN_STRONG_INLINE void run(const Expression &expr, const Device &device=DefaultDevice())
Definition: TensorExecutor.h:92
@ ColMajor
Definition: Constants.h:318
@ RowMajor
Definition: Constants.h:320
constexpr EIGEN_STRONG_INLINE auto array_zip_and_reduce(array< A, N > a, array< B, N > b) -> decltype(h_array_zip_and_reduce< Reducer, Op, A, B, N >(a, b, typename gen_numeric_list< int, N >::type()))
Definition: MoreMeta.h:536
constexpr EIGEN_STRONG_INLINE auto array_apply_and_reduce(array< A, N > a) -> decltype(h_array_apply_and_reduce< Reducer, Op, A, N >(a, typename gen_numeric_list< int, N >::type()))
Definition: MoreMeta.h:564
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
std::array< T, N > array
Definition: EmulateArray.h:231
Definition: TensorDeviceDefault.h:19
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: MoreMeta.h:422
Definition: MoreMeta.h:383
Definition: MoreMeta.h:358
ref_selector< T >::type type
Definition: TensorTraits.h:153
Definition: GenericPacketMath.h:108
Definition: ForwardDeclarations.h:21