TensorMap.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_MAP_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_MAP_H
12 
13 namespace Eigen {
14 
15 // FIXME use proper doxygen documentation (e.g. \tparam MakePointer_)
16 
23 template<typename PlainObjectType, int Options_, template <class> class MakePointer_> class TensorMap : public TensorBase<TensorMap<PlainObjectType, Options_, MakePointer_> >
30 {
31  public:
34  #ifdef EIGEN_USE_SYCL
36  #else
38  #endif
43  typedef typename PlainObjectType::Base::CoeffReturnType CoeffReturnType;
44 
46  typedef typename MakePointer_<Scalar>::ConstType PointerConstType;
47 
48  // WARN: PointerType still can be a pointer to const (const Scalar*), for
49  // example in TensorMap<Tensor<const Scalar, ...>> expression. This type of
50  // expression should be illegal, but adding this restriction is not possible
51  // in practice (see https://bitbucket.org/eigen/eigen/pull-requests/488).
52  typedef typename internal::conditional<
54  PointerType, // use simple pointer in lvalue expressions
55  PointerConstType // use const pointer in rvalue expressions
57 
58  // If TensorMap was constructed over rvalue expression (e.g. const Tensor),
59  // we should return a reference to const from operator() (and others), even
60  // if TensorMap itself is not const.
61  typedef typename internal::conditional<
63  Scalar&,
64  const Scalar&
66 
67  static const int Options = Options_;
68 
69  static const Index NumIndices = PlainObjectType::NumIndices;
70  typedef typename PlainObjectType::Dimensions Dimensions;
71 
72  enum {
73  IsAligned = ((int(Options_)&Aligned)==Aligned),
74  Layout = PlainObjectType::Layout,
75  CoordAccess = true,
76  RawAccess = true
77  };
78 
80  EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr) : m_data(dataPtr), m_dimensions() {
81  // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
82  EIGEN_STATIC_ASSERT((0 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
83  }
84 
85 #if EIGEN_HAS_VARIADIC_TEMPLATES
86  template<typename... IndexTypes> EIGEN_DEVICE_FUNC
87  EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension, IndexTypes... otherDimensions) : m_data(dataPtr), m_dimensions(firstDimension, otherDimensions...) {
88  // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
89  EIGEN_STATIC_ASSERT((sizeof...(otherDimensions) + 1 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
90  }
91 #else
93  EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension) : m_data(dataPtr), m_dimensions(firstDimension) {
94  // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
95  EIGEN_STATIC_ASSERT((1 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
96  }
98  EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2) : m_data(dataPtr), m_dimensions(dim1, dim2) {
99  EIGEN_STATIC_ASSERT(2 == NumIndices || NumIndices == Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
100  }
102  EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3) : m_data(dataPtr), m_dimensions(dim1, dim2, dim3) {
103  EIGEN_STATIC_ASSERT(3 == NumIndices || NumIndices == Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
104  }
106  EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4) : m_data(dataPtr), m_dimensions(dim1, dim2, dim3, dim4) {
107  EIGEN_STATIC_ASSERT(4 == NumIndices || NumIndices == Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
108  }
110  EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4, Index dim5) : m_data(dataPtr), m_dimensions(dim1, dim2, dim3, dim4, dim5) {
111  EIGEN_STATIC_ASSERT(5 == NumIndices || NumIndices == Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
112  }
113 #endif
114 
116  : m_data(dataPtr), m_dimensions(dimensions)
117  { }
118 
119  template <typename Dimensions>
120  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const Dimensions& dimensions)
121  : m_data(dataPtr), m_dimensions(dimensions)
122  { }
123 
125  : m_data(tensor.data()), m_dimensions(tensor.dimensions())
126  { }
127 
129  EIGEN_STRONG_INLINE Index rank() const { return m_dimensions.rank(); }
131  EIGEN_STRONG_INLINE Index dimension(Index n) const { return m_dimensions[n]; }
133  EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
135  EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
137  EIGEN_STRONG_INLINE StoragePointerType data() { return m_data; }
139  EIGEN_STRONG_INLINE StoragePointerType data() const { return m_data; }
140 
142  EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices) const
143  {
144  // eigen_assert(checkIndexRange(indices));
145  if (PlainObjectType::Options&RowMajor) {
146  const Index index = m_dimensions.IndexOfRowMajor(indices);
147  return m_data[index];
148  } else {
149  const Index index = m_dimensions.IndexOfColMajor(indices);
150  return m_data[index];
151  }
152  }
153 
155  EIGEN_STRONG_INLINE StorageRefType operator()() const
156  {
157  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
158  return m_data[0];
159  }
160 
162  EIGEN_STRONG_INLINE StorageRefType operator()(Index index) const
163  {
164  eigen_internal_assert(index >= 0 && index < size());
165  return m_data[index];
166  }
167 
168 #if EIGEN_HAS_VARIADIC_TEMPLATES
169  template<typename... IndexTypes> EIGEN_DEVICE_FUNC
170  EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) const
171  {
172  EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
174  if (PlainObjectType::Options&RowMajor) {
175  const Index index = m_dimensions.IndexOfRowMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
176  return m_data[index];
177  } else {
178  const Index index = m_dimensions.IndexOfColMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
179  return m_data[index];
180  }
181  }
182 #else
184  EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1) const
185  {
186  if (PlainObjectType::Options&RowMajor) {
187  const Index index = i1 + i0 * m_dimensions[1];
188  return m_data[index];
189  } else {
190  const Index index = i0 + i1 * m_dimensions[0];
191  return m_data[index];
192  }
193  }
195  EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2) const
196  {
197  if (PlainObjectType::Options&RowMajor) {
198  const Index index = i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0);
199  return m_data[index];
200  } else {
201  const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * i2);
202  return m_data[index];
203  }
204  }
206  EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3) const
207  {
208  if (PlainObjectType::Options&RowMajor) {
209  const Index index = i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0));
210  return m_data[index];
211  } else {
212  const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * i3));
213  return m_data[index];
214  }
215  }
217  EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3, Index i4) const
218  {
219  if (PlainObjectType::Options&RowMajor) {
220  const Index index = i4 + m_dimensions[4] * (i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0)));
221  return m_data[index];
222  } else {
223  const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * (i3 + m_dimensions[3] * i4)));
224  return m_data[index];
225  }
226  }
227 #endif
228 
231  {
232  // eigen_assert(checkIndexRange(indices));
233  if (PlainObjectType::Options&RowMajor) {
234  const Index index = m_dimensions.IndexOfRowMajor(indices);
235  return m_data[index];
236  } else {
237  const Index index = m_dimensions.IndexOfColMajor(indices);
238  return m_data[index];
239  }
240  }
241 
244  {
245  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
246  return m_data[0];
247  }
248 
250  EIGEN_STRONG_INLINE StorageRefType operator()(Index index)
251  {
252  eigen_internal_assert(index >= 0 && index < size());
253  return m_data[index];
254  }
255 
256 #if EIGEN_HAS_VARIADIC_TEMPLATES
257  template<typename... IndexTypes> EIGEN_DEVICE_FUNC
258  EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
259  {
260  static_assert(sizeof...(otherIndices) + 2 == NumIndices || NumIndices == Dynamic, "Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
262  const std::size_t NumDims = sizeof...(otherIndices) + 2;
263  if (PlainObjectType::Options&RowMajor) {
264  const Index index = m_dimensions.IndexOfRowMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
265  return m_data[index];
266  } else {
267  const Index index = m_dimensions.IndexOfColMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
268  return m_data[index];
269  }
270  }
271 #else
273  EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1)
274  {
275  if (PlainObjectType::Options&RowMajor) {
276  const Index index = i1 + i0 * m_dimensions[1];
277  return m_data[index];
278  } else {
279  const Index index = i0 + i1 * m_dimensions[0];
280  return m_data[index];
281  }
282  }
284  EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2)
285  {
286  if (PlainObjectType::Options&RowMajor) {
287  const Index index = i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0);
288  return m_data[index];
289  } else {
290  const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * i2);
291  return m_data[index];
292  }
293  }
295  EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3)
296  {
297  if (PlainObjectType::Options&RowMajor) {
298  const Index index = i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0));
299  return m_data[index];
300  } else {
301  const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * i3));
302  return m_data[index];
303  }
304  }
306  EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
307  {
308  if (PlainObjectType::Options&RowMajor) {
309  const Index index = i4 + m_dimensions[4] * (i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0)));
310  return m_data[index];
311  } else {
312  const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * (i3 + m_dimensions[3] * i4)));
313  return m_data[index];
314  }
315  }
316 #endif
317 
319 
320  private:
321  StoragePointerType m_data;
322  Dimensions m_dimensions;
323 };
324 
325 } // end namespace Eigen
326 
327 #endif // EIGEN_CXX11_TENSOR_TENSOR_MAP_H
PlainObjectType::Base::CoeffReturnType CoeffReturnType
Definition: TensorMap.h:43
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const
Definition: TensorMap.h:131
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorMap.h:133
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr)
Definition: TensorMap.h:80
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Dimensions m_dimensions
Definition: TensorMap.h:322
static const Eigen::internal::all_t all
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
Definition: TensorMap.h:306
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()()
Definition: TensorMap.h:243
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3)
Definition: TensorMap.h:102
PlainObjectType::Dimensions Dimensions
Definition: TensorMap.h:70
int n
internal::traits< PlainObjectType >::Index Index
Definition: TensorMap.h:40
TensorMap< PlainObjectType, Options_, MakePointer_ > Self
Definition: TensorMap.h:32
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index) const
Definition: TensorMap.h:162
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 StorageRefType operator()(Index i0, Index i1) const
Definition: TensorMap.h:184
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array< Index, NumIndices > &indices)
Definition: TensorMap.h:230
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(PlainObjectType &tensor)
Definition: TensorMap.h:124
static const int Options
Definition: TensorMap.h:67
MakePointer_< Scalar >::ConstType PointerConstType
Definition: TensorMap.h:46
internal::traits< PlainObjectType >::StorageKind StorageKind
Definition: TensorMap.h:39
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data()
Definition: TensorMap.h:137
MakePointer_< Scalar >::Type PointerType
Definition: TensorMap.h:45
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2) const
Definition: TensorMap.h:195
A tensor expression mapping an existing array of data.
StoragePointerType m_data
Definition: TensorMap.h:321
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1)
Definition: TensorMap.h:273
#define eigen_assert(x)
Definition: Macros.h:1037
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2)
Definition: TensorMap.h:98
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4)
Definition: TensorMap.h:106
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3)
Definition: TensorMap.h:295
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data() const
Definition: TensorMap.h:139
NumTraits< Scalar >::Real RealScalar
Definition: TensorMap.h:42
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const Dimensions &dimensions)
Definition: TensorMap.h:120
The tensor base class.
Definition: TensorBase.h:973
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
TensorBase< TensorMap< PlainObjectType, Options_, MakePointer_ > > Base
Definition: TensorMap.h:33
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3, Index i4) const
Definition: TensorMap.h:217
internal::conditional< bool(internal::is_lvalue< PlainObjectType >::value), Scalar &, const Scalar &>::type StorageRefType
Definition: TensorMap.h:65
Eigen::internal::nested< Self >::type Nested
Definition: TensorMap.h:37
#define EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: TensorMacros.h:94
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array< Index, NumIndices > &indices) const
Definition: TensorMap.h:142
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension)
Definition: TensorMap.h:93
static const Index NumIndices
Definition: TensorMap.h:69
const int Dynamic
Definition: Constants.h:22
#define eigen_internal_assert(x)
Definition: Macros.h:1043
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()() const
Definition: TensorMap.h:155
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index)
Definition: TensorMap.h:250
internal::traits< PlainObjectType >::Scalar Scalar
Definition: TensorMap.h:41
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2)
Definition: TensorMap.h:284
internal::conditional< bool(internal::is_lvalue< PlainObjectType >::value), PointerType, PointerConstType >::type StoragePointerType
Definition: TensorMap.h:56
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3) const
Definition: TensorMap.h:206
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const array< Index, NumIndices > &dimensions)
Definition: TensorMap.h:115
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: TensorMap.h:135
Definition: pytypes.h:1370
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition: TensorMap.h:129
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4, Index dim5)
Definition: TensorMap.h:110


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:06