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 namespace Eigen {
14 
26 template<typename Scalar_, typename Dimensions_, int Options_, typename IndexType>
27 class TensorFixedSize : public TensorBase<TensorFixedSize<Scalar_, Dimensions_, Options_, IndexType> >
28 {
29  public:
35  typedef Scalar_ Scalar;
38 
39  static const int Options = Options_;
40 
41  enum {
44  BlockAccess = false,
46  Layout = Options_ & RowMajor ? RowMajor : ColMajor,
47  CoordAccess = true,
48  RawAccess = true
49  };
50 
51  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
53  //===--------------------------------------------------------------------===//
54 
55  typedef Dimensions_ Dimensions;
56  static const std::size_t NumIndices = Dimensions::count;
57 
58  protected:
60 
61  public:
64  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_storage.dimensions(); }
65  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_storage.size(); }
66  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar *data() { return m_storage.data(); }
67  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar *data() const { return m_storage.data(); }
68 
69  // This makes EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
70  // work, because that uses base().coeffRef() - and we don't yet
71  // implement a similar class hierarchy
72  inline Self& base() { return *this; }
73  inline const Self& base() const { return *this; }
74 
75 #if EIGEN_HAS_VARIADIC_TEMPLATES
76  template<typename... IndexTypes>
77  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(Index firstIndex, IndexTypes... otherIndices) const
78  {
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 #endif
84 
86  EIGEN_STRONG_INLINE const Scalar& coeff(const array<Index, NumIndices>& indices) const
87  {
89  return m_storage.data()[linearizedIndex(indices)];
90  }
91 
93  EIGEN_STRONG_INLINE const Scalar& coeff(Index index) const
94  {
95  eigen_internal_assert(index >= 0 && index < size());
96  return m_storage.data()[index];
97  }
98 
100  EIGEN_STRONG_INLINE const Scalar& coeff() const
101  {
102  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
103  return m_storage.data()[0];
104  }
105 
106 
107 #if EIGEN_HAS_VARIADIC_TEMPLATES
108  template<typename... IndexTypes>
109  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index firstIndex, IndexTypes... otherIndices)
110  {
111  // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
112  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
113  return coeffRef(array<Index, NumIndices>{{firstIndex, otherIndices...}});
114  }
115 #endif
116 
119  {
121  return m_storage.data()[linearizedIndex(indices)];
122  }
123 
125  EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
126  {
127  eigen_internal_assert(index >= 0 && index < size());
128  return m_storage.data()[index];
129  }
130 
133  {
134  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
135  return m_storage.data()[0];
136  }
137 
138 #if EIGEN_HAS_VARIADIC_TEMPLATES
139  template<typename... IndexTypes>
140  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(Index firstIndex, IndexTypes... otherIndices) const
141  {
142  // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
143  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
144  return this->operator()(array<Index, NumIndices>{{firstIndex, otherIndices...}});
145  }
146 #else
148  EIGEN_STRONG_INLINE const Scalar& operator()(Index i0, Index i1) const
149  {
150  if (Options&RowMajor) {
151  const Index index = i1 + i0 * m_storage.dimensions()[1];
152  return m_storage.data()[index];
153  } else {
154  const Index index = i0 + i1 * m_storage.dimensions()[0];
155  return m_storage.data()[index];
156  }
157  }
159  EIGEN_STRONG_INLINE const Scalar& operator()(Index i0, Index i1, Index i2) const
160  {
161  if (Options&RowMajor) {
162  const Index index = i2 + m_storage.dimensions()[2] * (i1 + m_storage.dimensions()[1] * i0);
163  return m_storage.data()[index];
164  } else {
165  const Index index = i0 + m_storage.dimensions()[0] * (i1 + m_storage.dimensions()[1] * i2);
166  return m_storage.data()[index];
167  }
168  }
170  EIGEN_STRONG_INLINE const Scalar& operator()(Index i0, Index i1, Index i2, Index i3) const
171  {
172  if (Options&RowMajor) {
173  const Index index = i3 + m_storage.dimensions()[3] * (i2 + m_storage.dimensions()[2] * (i1 + m_storage.dimensions()[1] * i0));
174  return m_storage.data()[index];
175  } else {
176  const Index index = i0 + m_storage.dimensions()[0] * (i1 + m_storage.dimensions()[1] * (i2 + m_storage.dimensions()[2] * i3));
177  return m_storage.data()[index];
178  }
179  }
181  EIGEN_STRONG_INLINE const Scalar& operator()(Index i0, Index i1, Index i2, Index i3, Index i4) const
182  {
183  if (Options&RowMajor) {
184  const Index index = i4 + m_storage.dimensions()[4] * (i3 + m_storage.dimensions()[3] * (i2 + m_storage.dimensions()[2] * (i1 + m_storage.dimensions()[1] * i0)));
185  return m_storage.data()[index];
186  } else {
187  const Index index = i0 + m_storage.dimensions()[0] * (i1 + m_storage.dimensions()[1] * (i2 + m_storage.dimensions()[2] * (i3 + m_storage.dimensions()[3] * i4)));
188  return m_storage.data()[index];
189  }
190  }
191 #endif
192 
193 
195  EIGEN_STRONG_INLINE const Scalar& operator()(const array<Index, NumIndices>& indices) const
196  {
197  eigen_assert(checkIndexRange(indices));
198  return coeff(indices);
199  }
200 
202  EIGEN_STRONG_INLINE const Scalar& operator()(Index index) const
203  {
204  eigen_internal_assert(index >= 0 && index < size());
205  return coeff(index);
206  }
207 
209  EIGEN_STRONG_INLINE const Scalar& operator()() const
210  {
211  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
212  return coeff();
213  }
214 
216  EIGEN_STRONG_INLINE const Scalar& operator[](Index index) const
217  {
218  // The bracket operator is only for vectors, use the parenthesis operator instead.
219  EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE);
220  return coeff(index);
221  }
222 
223 #if EIGEN_HAS_VARIADIC_TEMPLATES
224  template<typename... IndexTypes>
225  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index firstIndex, IndexTypes... otherIndices)
226  {
227  // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
228  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
229  return operator()(array<Index, NumIndices>{{firstIndex, otherIndices...}});
230  }
231 #else
233  EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1)
234  {
235  if (Options&RowMajor) {
236  const Index index = i1 + i0 * m_storage.dimensions()[1];
237  return m_storage.data()[index];
238  } else {
239  const Index index = i0 + i1 * m_storage.dimensions()[0];
240  return m_storage.data()[index];
241  }
242  }
244  EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2)
245  {
246  if (Options&RowMajor) {
247  const Index index = i2 + m_storage.dimensions()[2] * (i1 + m_storage.dimensions()[1] * i0);
248  return m_storage.data()[index];
249  } else {
250  const Index index = i0 + m_storage.dimensions()[0] * (i1 + m_storage.dimensions()[1] * i2);
251  return m_storage.data()[index];
252  }
253  }
255  EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2, Index i3)
256  {
257  if (Options&RowMajor) {
258  const Index index = i3 + m_storage.dimensions()[3] * (i2 + m_storage.dimensions()[2] * (i1 + m_storage.dimensions()[1] * i0));
259  return m_storage.data()[index];
260  } else {
261  const Index index = i0 + m_storage.dimensions()[0] * (i1 + m_storage.dimensions()[1] * (i2 + m_storage.dimensions()[2] * i3));
262  return m_storage.data()[index];
263  }
264  }
266  EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
267  {
268  if (Options&RowMajor) {
269  const Index index = i4 + m_storage.dimensions()[4] * (i3 + m_storage.dimensions()[3] * (i2 + m_storage.dimensions()[2] * (i1 + m_storage.dimensions()[1] * i0)));
270  return m_storage.data()[index];
271  } else {
272  const Index index = i0 + m_storage.dimensions()[0] * (i1 + m_storage.dimensions()[1] * (i2 + m_storage.dimensions()[2] * (i3 + m_storage.dimensions()[3] * i4)));
273  return m_storage.data()[index];
274  }
275  }
276 #endif
277 
280  {
281  eigen_assert(checkIndexRange(indices));
282  return coeffRef(indices);
283  }
284 
286  EIGEN_STRONG_INLINE Scalar& operator()(Index index)
287  {
288  eigen_assert(index >= 0 && index < size());
289  return coeffRef(index);
290  }
291 
294  {
295  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
296  return coeffRef();
297  }
298 
300  EIGEN_STRONG_INLINE Scalar& operator[](Index index)
301  {
302  // The bracket operator is only for vectors, use the parenthesis operator instead
303  EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
304  return coeffRef(index);
305  }
306 
309  : m_storage()
310  {
311  }
312 
315  : m_storage(other.m_storage)
316  {
317  }
318 
319 #if EIGEN_HAS_RVALUE_REFERENCES
321  : m_storage(other.m_storage)
322  {
323  }
324 #endif
325 
326  template<typename OtherDerived>
329  {
331  Assign assign(*this, other.derived());
333  }
334  template<typename OtherDerived>
337  {
339  Assign assign(*this, other.derived());
341  }
342 
343  // FIXME: check that the dimensions of other match the dimensions of *this.
344  // Unfortunately this isn't possible yet when the rhs is an expression.
346 
347 
348  protected:
351  {
356  using internal::lesser_op;
357 
358  return true;
359  // check whether the indices are all >= 0
360  /* array_apply_and_reduce<logical_and_op, greater_equal_zero_op>(indices) &&
361  // check whether the indices fit in the dimensions
362  array_zip_and_reduce<logical_and_op, lesser_op>(indices, m_storage.dimensions());*/
363  }
364 
367  {
368  if (Options&RowMajor) {
369  return m_storage.dimensions().IndexOfRowMajor(indices);
370  } else {
371  return m_storage.dimensions().IndexOfColMajor(indices);
372  }
373  }
374 };
375 
376 
377 } // end namespace Eigen
378 
379 #endif // EIGEN_CXX11_TENSOR_TENSOR_FIXED_SIZE_H
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()(Index i0, Index i1, Index i2, Index i3) const
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: CXX11Meta.h:462
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(const array< Index, NumIndices > &indices)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
internal::TensorBlockNotImplemented TensorBlock
internal::traits< Self >::Index Index
Eigen::internal::nested< Self >::type Nested
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(const TensorBase< OtherDerived, WriteAccessors > &other)
int n
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()(Index i0, Index i1, Index i2, Index i3, Index i4) const
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool checkIndexRange(const array< Index, NumIndices > &) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()(const array< Index, NumIndices > &indices) const
internal::traits< Self >::StorageKind StorageKind
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()(Index i0, Index i1, Index i2) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived & derived()
Definition: TensorBase.h:1169
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
static const int Options
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: CXX11Meta.h:434
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef()
TensorBase< TensorFixedSize< Scalar_, Dimensions_, Options_, IndexType > > Base
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index i0, Index i1, Index i2)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator[](Index index)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(const TensorBase< OtherDerived, ReadOnlyAccessors > &other)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()(Index i0, Index i1) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index index)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()() const
#define eigen_assert(x)
Definition: Macros.h:1037
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
static const std::size_t NumIndices
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index i0, Index i1)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(const Self &other)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
The fixed sized version of the tensor class.
The tensor base class.
Definition: TensorBase.h:973
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator()(Index index) const
NumTraits< Scalar >::Real RealScalar
#define EIGEN_MAX_ALIGN_BYTES
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const FixedDimensions & dimensions()
Definition: TensorStorage.h:59
const Self & base() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(const array< Index, NumIndices > &indices) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(Index index) const
TensorFixedSize< Scalar_, Dimensions_, Options_, IndexType > Self
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index linearizedIndex(const array< Index, NumIndices > &indices) const
TensorStorage< Scalar, Dimensions, Options > m_storage
#define eigen_internal_assert(x)
Definition: Macros.h:1043
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index i0, Index i1, Index i2, Index i3)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex size() const
Definition: TensorStorage.h:66
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * data()
Definition: TensorStorage.h:54
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(const array< Index, NumIndices > &indices)
#define EIGEN_TENSOR_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived)
Definition: TensorMacros.h:84
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(const Expression &expr, const Device &device=Device())
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff() const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()()
Base::CoeffReturnType CoeffReturnType
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & operator[](Index index) const


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:36:58