Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Tensor.h
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// Copyright (C) 2013 Christian Seiler <christian@iwakd.de>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_TENSOR_TENSOR_H
13#define EIGEN_TENSOR_TENSOR_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
67
68template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
69class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexType_> > {
70 public:
71 using Self = Tensor<Scalar_, NumIndices_, Options_, IndexType_>;
73 using Nested = typename Eigen::internal::ref_selector<Self>::type;
74 using StorageKind = typename internal::traits<Self>::StorageKind;
75 using Index = typename internal::traits<Self>::Index;
76 using Scalar = Scalar_;
77 using RealScalar = typename NumTraits<Scalar>::Real;
78 using CoeffReturnType = typename Base::CoeffReturnType;
79
80 enum { IsAligned = (EIGEN_MAX_ALIGN_BYTES > 0) && !(Options_ & DontAlign), CoordAccess = true, RawAccess = true };
81
82 static constexpr int Layout = Options_ & RowMajor ? RowMajor : ColMajor;
83 static constexpr int Options = Options_;
84 static constexpr int NumIndices = NumIndices_;
85 using Dimensions = DSizes<Index, NumIndices_>;
86
87 protected:
88 TensorStorage<Scalar, Dimensions, Options> m_storage;
89
90 template <typename CustomIndices>
91 struct isOfNormalIndex {
92 static constexpr bool is_array = std::is_base_of<array<Index, NumIndices>, CustomIndices>::value;
93 static constexpr bool is_int = NumTraits<CustomIndices>::IsInteger;
94 static constexpr bool value = is_array | is_int;
95 };
96
97 public:
98 // Metadata
99 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const { return NumIndices; }
100 template <Index N>
101 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension() const {
102 EIGEN_STATIC_ASSERT(0 <= N && N < NumIndices, INVALID_DIMENSION);
103 return m_storage.dimensions()[N];
104 }
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const {
106 eigen_assert(0 <= n && n < NumIndices);
107 return m_storage.dimensions()[n];
108 }
109 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_storage.dimensions(); }
110 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_storage.size(); }
111 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar* data() { return m_storage.data(); }
112 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar* data() const { return m_storage.data(); }
113
114 // This makes EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
115 // work, because that uses base().coeffRef() - and we don't yet
116 // implement a similar class hierarchy
117 inline Self& base() { return *this; }
118 inline const Self& base() const { return *this; }
119
120 template <typename... IndexTypes>
121 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(Index firstIndex, Index secondIndex,
122 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) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
125 eigen_assert(internal::indices_fit<Index>(otherIndices...));
126 return coeff(array<Index, NumIndices>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
127 }
128
129 // normal indices
130 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(const array<Index, NumIndices>& indices) const {
131 eigen_internal_assert(checkIndexRange(indices));
132 return m_storage.data()[linearizedIndex(indices)];
133 }
134
135 // custom indices
136 template <typename CustomIndices, EIGEN_SFINAE_ENABLE_IF(!(isOfNormalIndex<CustomIndices>::value))>
137 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(CustomIndices& indices) const {
138 return coeff(internal::customIndices2Array<Index, NumIndices>(indices));
139 }
140
141 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff() const {
142 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
143 return m_storage.data()[0];
144 }
145
146 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(Index index) const {
147 eigen_internal_assert(index >= 0 && index < size());
148 return m_storage.data()[index];
149 }
150
151 template <typename... IndexTypes>
152 inline Scalar& coeffRef(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) {
153 // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
154 EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
155 eigen_assert(internal::indices_fit<Index>(otherIndices...));
156 return coeffRef(array<Index, NumIndices>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
157 }
158
159 // normal indices
160 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(const array<Index, NumIndices>& indices) {
161 eigen_internal_assert(checkIndexRange(indices));
162 return m_storage.data()[linearizedIndex(indices)];
163 }
164
165 // custom indices
166 template <typename CustomIndices, EIGEN_SFINAE_ENABLE_IF(!(isOfNormalIndex<CustomIndices>::value))>
167 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(CustomIndices& indices) {
168 return coeffRef(internal::customIndices2Array<Index, NumIndices>(indices));
169 }
170
171 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef() {
172 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
173 return m_storage.data()[0];
174 }
175
176 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
177 eigen_internal_assert(index >= 0 && index < size());
178 return m_storage.data()[index];
179 }
180
181 template <typename... IndexTypes>
182 inline const Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) const {
183 // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
184 EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
185 eigen_assert(internal::indices_fit<Index>(otherIndices...));
186 return this->operator()(array<Index, NumIndices>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
187 }
188
189 // custom indices
190 template <typename CustomIndices, EIGEN_SFINAE_ENABLE_IF(!(isOfNormalIndex<CustomIndices>::value))>
191 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(CustomIndices& indices) const {
192 return coeff(internal::customIndices2Array<Index, NumIndices>(indices));
193 }
194
195 // normal indices
196 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(const array<Index, NumIndices>& indices) const {
197 return coeff(indices);
198 }
199
200 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(Index index) const {
201 eigen_internal_assert(index >= 0 && index < size());
202 return coeff(index);
203 }
204
205 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()() const {
206 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
207 return coeff();
208 }
209
210 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator[](Index index) const {
211 // The bracket operator is only for vectors, use the parenthesis operator instead.
212 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE);
213 return coeff(index);
214 }
215
216 template <typename... IndexTypes>
217 inline Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) {
218 // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
219 EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
220 eigen_assert(internal::indices_fit<Index>(otherIndices...));
221 return operator()(array<Index, NumIndices>{{firstIndex, secondIndex, static_cast<Index>(otherIndices)...}});
222 }
223
224 // normal indices
225 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(const array<Index, NumIndices>& indices) {
226 return coeffRef(indices);
227 }
228
229 // custom indices
230 template <typename CustomIndices, EIGEN_SFINAE_ENABLE_IF(!(isOfNormalIndex<CustomIndices>::value))>
231 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(CustomIndices& indices) {
232 return coeffRef(internal::customIndices2Array<Index, NumIndices>(indices));
233 }
234
235 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index index) {
236 eigen_assert(index >= 0 && index < size());
237 return coeffRef(index);
238 }
239
240 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()() {
241 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
242 return coeffRef();
243 }
244
245 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator[](Index index) {
246 // The bracket operator is only for vectors, use the parenthesis operator instead.
247 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
248 return coeffRef(index);
249 }
250
251 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor() : m_storage() {}
252
253 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(const Self& other) = default;
254
255 template <typename... IndexTypes>
256 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(Index firstDimension, IndexTypes... otherDimensions)
257 : m_storage(firstDimension, otherDimensions...) {
258 // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
259 EIGEN_STATIC_ASSERT(sizeof...(otherDimensions) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
260 }
261
263 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit Tensor(const array<Index, NumIndices>& dimensions)
264 : m_storage(internal::array_prod(dimensions), dimensions) {
265 EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
266 }
267
268 template <typename OtherDerived>
269 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(const TensorBase<OtherDerived, ReadOnlyAccessors>& other) {
270 EIGEN_STATIC_ASSERT(OtherDerived::NumDimensions == Base::NumDimensions, Number_of_dimensions_must_match)
272 Assign assign(*this, other.derived());
273 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
274 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
275 }
276
277 template <typename OtherDerived>
278 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(const TensorBase<OtherDerived, WriteAccessors>& other) {
279 EIGEN_STATIC_ASSERT(OtherDerived::NumDimensions == Base::NumDimensions, Number_of_dimensions_must_match)
280 using Assign = TensorAssignOp<Tensor, const OtherDerived>;
281 Assign assign(*this, other.derived());
282 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
283 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
284 }
285
286 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(Self&& other) : m_storage(std::move(other.m_storage)) {}
287 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor& operator=(Self&& other) {
288 m_storage = std::move(other.m_storage);
289 return *this;
290 }
291
292 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor& operator=(const Tensor& other) {
293 using Assign = TensorAssignOp<Tensor, const Tensor>;
294 Assign assign(*this, other);
295 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
296 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
297 return *this;
298 }
299 template <typename OtherDerived>
300 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor& operator=(const OtherDerived& other) {
301 using Assign = TensorAssignOp<Tensor, const OtherDerived>;
302 Assign assign(*this, other);
303 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
304 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
305 return *this;
306 }
307
308 template <typename... IndexTypes>
309 EIGEN_DEVICE_FUNC void resize(Index firstDimension, IndexTypes... otherDimensions) {
310 // The number of dimensions used to resize a tensor must be equal to the rank of the tensor.
311 EIGEN_STATIC_ASSERT(sizeof...(otherDimensions) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
312 eigen_assert(internal::indices_fit<Index>(otherDimensions...));
313 resize(array<Index, NumIndices>{{firstDimension, static_cast<Index>(otherDimensions)...}});
314 }
315
317 EIGEN_DEVICE_FUNC void resize(const array<Index, NumIndices>& dimensions) {
318#ifndef EIGEN_NO_DEBUG
319 Index size = Index(1);
320 for (int i = 0; i < NumIndices; i++) {
321 internal::check_rows_cols_for_overflow<Dynamic, Dynamic, Dynamic>::run(size, dimensions[i]);
322 size *= dimensions[i];
323 }
324#else
325 Index size = internal::array_prod(dimensions);
326#endif
327
328#ifdef EIGEN_INITIALIZE_COEFFS
329 bool size_changed = size != this->size();
330 m_storage.resize(size, dimensions);
331 if (size_changed) EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
332#else
333 m_storage.resize(size, dimensions);
334#endif
335 }
336
337 EIGEN_DEVICE_FUNC void resize() {
338 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
339 // Nothing to do: rank 0 tensors have fixed size
340 }
341
342 template <typename FirstType, typename... OtherTypes>
343 EIGEN_DEVICE_FUNC void resize(const Eigen::IndexList<FirstType, OtherTypes...>& dimensions) {
344 array<Index, NumIndices> dims;
345 for (int i = 0; i < NumIndices; ++i) {
346 dims[i] = static_cast<Index>(dimensions[i]);
347 }
348 resize(dims);
349 }
350
352 template <typename CustomDimension, EIGEN_SFINAE_ENABLE_IF(!(isOfNormalIndex<CustomDimension>::value))>
353 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(CustomDimension& dimensions) {
354 resize(internal::customIndices2Array<Index, NumIndices>(dimensions));
355 }
356
357 template <std::ptrdiff_t... Indices>
358 EIGEN_DEVICE_FUNC void resize(const Sizes<Indices...>& dimensions) {
359 array<Index, NumIndices> dims;
360 for (int i = 0; i < NumIndices; ++i) {
361 dims[i] = static_cast<Index>(dimensions[i]);
362 }
363 resize(dims);
364 }
365
366#ifdef EIGEN_TENSOR_PLUGIN
367#include EIGEN_TENSOR_PLUGIN
368#endif
369
370 protected:
371 bool checkIndexRange(const array<Index, NumIndices>& indices) const {
372 for (std::size_t i = 0; i < NumIndices; ++i) {
373 if (indices[i] < 0 || indices[i] >= m_storage.dimensions()[i]) return false;
374 }
375 return true;
376 }
377
378 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index linearizedIndex(const array<Index, NumIndices>& indices) const {
379 EIGEN_IF_CONSTEXPR (Options & RowMajor) {
380 return m_storage.dimensions().IndexOfRowMajor(indices);
381 } else {
382 return m_storage.dimensions().IndexOfColMajor(indices);
383 }
384 }
385};
386
387#if EIGEN_COMP_CXXVER < 17
388template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
389constexpr int Tensor<Scalar_, NumIndices_, Options_, IndexType_>::NumIndices;
390template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
391constexpr int Tensor<Scalar_, NumIndices_, Options_, IndexType_>::Layout;
392template <typename Scalar_, int NumIndices_, int Options_, typename IndexType_>
393constexpr int Tensor<Scalar_, NumIndices_, Options_, IndexType_>::Options;
394#endif
395
396} // end namespace Eigen
397
398#endif // EIGEN_TENSOR_TENSOR_H
Definition TensorAssign.h:47
The tensor base class.
Definition TensorForwardDeclarations.h:69
The tensor class.
Definition Tensor.h:69
Tensor(const array< Index, NumIndices > &dimensions)
Definition Tensor.h:263
void resize(CustomDimension &dimensions)
Definition Tensor.h:353
void resize(const array< Index, NumIndices > &dimensions)
Definition Tensor.h:317
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47