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 
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>
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(); }
135  EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
140 
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 
156  {
157  EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
158  return m_data[0];
159  }
160 
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
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  }
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  }
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  }
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 
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
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  }
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  }
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  }
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:
323 };
324 
325 } // end namespace Eigen
326 
327 #endif // EIGEN_CXX11_TENSOR_TENSOR_MAP_H
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen::TensorMap::rank
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition: TensorMap.h:129
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen::TensorMap::Nested
Eigen::internal::nested< Self >::type Nested
Definition: TensorMap.h:37
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()()
Definition: TensorMap.h:243
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()() const
Definition: TensorMap.h:155
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
Definition: TensorMap.h:306
Eigen::TensorMap::StorageRefType
internal::conditional< bool(internal::is_lvalue< PlainObjectType >::value), Scalar &, const Scalar & >::type StorageRefType
Definition: TensorMap.h:65
Eigen::array
Definition: EmulateArray.h:21
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::internal::is_lvalue
Definition: XprHelper.h:659
Eigen::TensorMap::StorageKind
internal::traits< PlainObjectType >::StorageKind StorageKind
Definition: TensorMap.h:39
Eigen::TensorMap::PointerConstType
MakePointer_< Scalar >::ConstType PointerConstType
Definition: TensorMap.h:46
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index) const
Definition: TensorMap.h:162
Eigen::TensorMap::TensorMap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4)
Definition: TensorMap.h:106
type
Definition: pytypes.h:1525
Eigen::RowMajor
@ RowMajor
Definition: Constants.h:321
Eigen::TensorMap::TensorMap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4, Index dim5)
Definition: TensorMap.h:110
Eigen::TensorMap::NumIndices
static const Index NumIndices
Definition: TensorMap.h:69
eigen_internal_assert
#define eigen_internal_assert(x)
Definition: Macros.h:1043
Eigen::TensorMap::Options
static const int Options
Definition: TensorMap.h:67
test_eigen_tensor.indices
indices
Definition: test_eigen_tensor.py:33
Eigen::TensorMap::CoordAccess
@ CoordAccess
Definition: TensorMap.h:75
n
int n
Definition: BiCGSTAB_simple.cpp:1
Eigen::internal::true_type
Definition: Meta.h:96
Eigen::TensorMap::Layout
@ Layout
Definition: TensorMap.h:74
Eigen::TensorMap::TensorMap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(PlainObjectType &tensor)
Definition: TensorMap.h:124
Eigen::TensorMap::TensorMap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const array< Index, NumIndices > &dimensions)
Definition: TensorMap.h:115
Eigen::TensorMap::StoragePointerType
internal::conditional< bool(internal::is_lvalue< PlainObjectType >::value), PointerType, PointerConstType >::type StoragePointerType
Definition: TensorMap.h:56
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::all
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:171
Eigen::TensorMap::data
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data()
Definition: TensorMap.h:137
Eigen::Architecture::Type
Type
Definition: Constants.h:471
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3, Index i4) const
Definition: TensorMap.h:217
Eigen::TensorMap::CoeffReturnType
PlainObjectType::Base::CoeffReturnType CoeffReturnType
Definition: TensorMap.h:43
Eigen::TensorMap::Self
TensorMap< PlainObjectType, Options_, MakePointer_ > Self
Definition: TensorMap.h:32
Eigen::TensorMap::dimension
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const
Definition: TensorMap.h:131
Eigen::TensorMap::size
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: TensorMap.h:135
Eigen::TensorMap
A tensor expression mapping an existing array of data.
Definition: TensorForwardDeclarations.h:52
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index index)
Definition: TensorMap.h:250
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3)
Definition: TensorMap.h:295
Eigen::TensorMap::TensorMap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension)
Definition: TensorMap.h:93
Eigen::TensorMap::IsAligned
@ IsAligned
Definition: TensorMap.h:73
Eigen::TensorMap::TensorMap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2)
Definition: TensorMap.h:98
Eigen::TensorMap::Base
TensorBase< TensorMap< PlainObjectType, Options_, MakePointer_ > > Base
Definition: TensorMap.h:33
Eigen::TensorMap::Scalar
internal::traits< PlainObjectType >::Scalar Scalar
Definition: TensorMap.h:41
Eigen::TensorBase
The tensor base class.
Definition: TensorBase.h:973
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:490
Eigen::internal::remove_reference
Definition: Meta.h:114
i0
double i0(double x)
Definition: i0.c:149
array
Definition: numpy.h:821
EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS
#define EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: TensorMacros.h:94
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::TensorMap::TensorMap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const Dimensions &dimensions)
Definition: TensorMap.h:120
i1
double i1(double x)
Definition: i1.c:150
EIGEN_STATIC_ASSERT
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array< Index, NumIndices > &indices) const
Definition: TensorMap.h:142
Eigen::TensorMap::m_dimensions
Dimensions m_dimensions
Definition: TensorMap.h:322
Eigen::TensorMap::RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: TensorMap.h:42
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1)
Definition: TensorMap.h:273
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1) const
Definition: TensorMap.h:184
Eigen::TensorMap::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorMap.h:133
Eigen::internal::conditional
Definition: Meta.h:109
Eigen::TensorMap::RawAccess
@ RawAccess
Definition: TensorMap.h:76
Eigen::TensorMap::PointerType
MakePointer_< Scalar >::Type PointerType
Definition: TensorMap.h:45
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2)
Definition: TensorMap.h:284
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3) const
Definition: TensorMap.h:206
Eigen::TensorMap::TensorMap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr)
Definition: TensorMap.h:80
Eigen::TensorMap::data
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StoragePointerType data() const
Definition: TensorMap.h:139
Eigen::TensorMap::Dimensions
PlainObjectType::Dimensions Dimensions
Definition: TensorMap.h:70
Eigen::TensorMap::m_data
StoragePointerType m_data
Definition: TensorMap.h:321
Eigen::TensorMap::Index
internal::traits< PlainObjectType >::Index Index
Definition: TensorMap.h:40
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2) const
Definition: TensorMap.h:195
Eigen::TensorMap::TensorMap
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3)
Definition: TensorMap.h:102
Eigen::TensorMap::operator()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageRefType operator()(const array< Index, NumIndices > &indices)
Definition: TensorMap.h:230
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::Aligned
@ Aligned
Definition: Constants.h:240


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:15:08