11 #ifndef EIGEN_CXX11_TENSOR_TENSOR_H
12 #define EIGEN_CXX11_TENSOR_TENSOR_H
14 #include "./InternalHeaderCheck.h"
65 template<
typename Scalar_,
int NumIndices_,
int Options_,
typename IndexType_>
71 typedef typename Eigen::internal::nested<Self>::type Nested;
72 typedef typename internal::traits<Self>::StorageKind StorageKind;
73 typedef typename internal::traits<Self>::Index Index;
74 typedef Scalar_ Scalar;
76 typedef typename Base::CoeffReturnType CoeffReturnType;
79 IsAligned = (EIGEN_MAX_ALIGN_BYTES>0) && !(Options_&
DontAlign),
85 static constexpr
int Options = Options_;
86 static constexpr
int NumIndices = NumIndices_;
87 typedef DSizes<Index, NumIndices_> Dimensions;
90 TensorStorage<Scalar, Dimensions, Options> m_storage;
92 template<
typename CustomIndices>
93 struct isOfNormalIndex{
94 static const bool is_array = internal::is_base_of<array<Index, NumIndices>, CustomIndices>::value;
96 static const bool value = is_array | is_int;
101 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank()
const {
return NumIndices; }
102 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n)
const {
return m_storage.dimensions()[n]; }
103 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_storage.dimensions(); }
104 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size()
const {
return m_storage.size(); }
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar *data() {
return m_storage.data(); }
106 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar *data()
const {
return m_storage.data(); }
111 inline Self& base() {
return *
this; }
112 inline const Self& base()
const {
return *
this; }
114 template<
typename... IndexTypes>
115 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
const
118 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
119 return coeff(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
123 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(
const array<Index, NumIndices>& indices)
const
125 eigen_internal_assert(checkIndexRange(indices));
126 return m_storage.data()[linearizedIndex(indices)];
130 template<
typename CustomIndices,
131 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
133 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(CustomIndices& indices)
const
135 return coeff(internal::customIndices2Array<Index,NumIndices>(indices));
138 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff()
const
140 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
141 return m_storage.data()[0];
144 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(Index index)
const
146 eigen_internal_assert(index >= 0 && index < size());
147 return m_storage.data()[index];
150 template<
typename... IndexTypes>
151 inline Scalar& coeffRef(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
154 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
155 return coeffRef(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
159 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)];
166 template<
typename CustomIndices,
167 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
169 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(CustomIndices& indices)
171 return coeffRef(internal::customIndices2Array<Index,NumIndices>(indices));
174 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef()
176 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
177 return m_storage.data()[0];
180 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
182 eigen_internal_assert(index >= 0 && index < size());
183 return m_storage.data()[index];
186 template<
typename... IndexTypes>
187 inline const Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
const
190 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
191 return this->operator()(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
195 template<
typename CustomIndices,
196 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
198 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(CustomIndices& indices)
const
200 return coeff(internal::customIndices2Array<Index,NumIndices>(indices));
204 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(
const array<Index, NumIndices>& indices)
const
206 return coeff(indices);
209 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(Index index)
const
211 eigen_internal_assert(index >= 0 && index < size());
215 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()()
const
217 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
221 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator[](Index index)
const
224 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE);
228 template<
typename... IndexTypes>
229 inline Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
232 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
233 return operator()(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
237 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(
const array<Index, NumIndices>& indices)
239 return coeffRef(indices);
243 template<
typename CustomIndices,
244 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
246 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(CustomIndices& indices)
248 return coeffRef(internal::customIndices2Array<Index,NumIndices>(indices));
251 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index index)
253 eigen_assert(index >= 0 && index < size());
254 return coeffRef(index);
257 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()()
259 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
263 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator[](Index index)
266 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
267 return coeffRef(index);
271 EIGEN_STRONG_INLINE
Tensor()
278 :
Base(other), m_storage(other.m_storage)
282 template<
typename... IndexTypes>
283 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Tensor(Index firstDimension, IndexTypes... otherDimensions)
284 : m_storage(firstDimension, otherDimensions...)
287 EIGEN_STATIC_ASSERT(
sizeof...(otherDimensions) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
291 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit Tensor(
const array<Index, NumIndices>& dimensions)
292 : m_storage(internal::array_prod(dimensions), dimensions)
294 EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
297 template<
typename OtherDerived>
301 typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
302 Assign assign(*
this, other.derived());
304 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
307 template<
typename OtherDerived>
309 EIGEN_STRONG_INLINE Tensor(
const TensorBase<OtherDerived, WriteAccessors>& other)
311 typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
312 Assign assign(*
this, other.derived());
313 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
314 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
318 EIGEN_STRONG_INLINE Tensor(Self&& other)
319 : m_storage(std::move(other.m_storage))
323 EIGEN_STRONG_INLINE Tensor& operator=(Self&& other)
325 m_storage = std::move(other.m_storage);
330 EIGEN_STRONG_INLINE Tensor& operator=(
const Tensor& other)
332 typedef TensorAssignOp<Tensor, const Tensor> Assign;
333 Assign assign(*
this, other);
334 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
335 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
338 template<
typename OtherDerived>
340 EIGEN_STRONG_INLINE Tensor& operator=(
const OtherDerived& other)
342 typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
343 Assign assign(*
this, other);
344 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
345 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
349 template<
typename... IndexTypes> EIGEN_DEVICE_FUNC
350 void resize(Index firstDimension, IndexTypes... otherDimensions)
353 EIGEN_STATIC_ASSERT(
sizeof...(otherDimensions) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
354 resize(array<Index, NumIndices>{{firstDimension, otherDimensions...}});
358 EIGEN_DEVICE_FUNC
void resize(
const array<Index, NumIndices>& dimensions)
361 Index size = Index(1);
362 for (i = 0; i < NumIndices; i++) {
363 internal::check_rows_cols_for_overflow<Dynamic>::run(size, dimensions[i]);
364 size *= dimensions[i];
366 #ifdef EIGEN_INITIALIZE_COEFFS
367 bool size_changed = size != this->size();
368 m_storage.resize(size, dimensions);
369 if(size_changed) EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
371 m_storage.resize(size, dimensions);
376 EIGEN_DEVICE_FUNC
void resize(
const DSizes<Index, NumIndices>& dimensions) {
377 array<Index, NumIndices> dims;
378 for (
int i = 0; i < NumIndices; ++i) {
379 dims[i] = dimensions[i];
387 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
391 template <
typename FirstType,
typename... OtherTypes>
393 void resize(
const Eigen::IndexList<FirstType, OtherTypes...>& dimensions) {
394 array<Index, NumIndices> dims;
395 for (
int i = 0; i < NumIndices; ++i) {
396 dims[i] =
static_cast<Index>(dimensions[i]);
402 template<
typename CustomDimension,
403 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomDimension>::value) )
405 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void resize(CustomDimension& dimensions)
407 resize(internal::customIndices2Array<Index,NumIndices>(dimensions));
410 #ifndef EIGEN_EMULATE_CXX11_META_H
411 template <
typename std::ptrdiff_t... Indices>
413 void resize(
const Sizes<Indices...>& dimensions) {
414 array<Index, NumIndices> dims;
415 for (
int i = 0; i < NumIndices; ++i) {
416 dims[i] =
static_cast<Index>(dimensions[i]);
421 template <std::
size_t V1, std::
size_t V2, std::
size_t V3, std::
size_t V4, std::
size_t V5>
423 void resize(
const Sizes<V1, V2, V3, V4, V5>& dimensions) {
424 array<Index, NumIndices> dims;
425 for (
int i = 0; i < NumIndices; ++i) {
426 dims[i] =
static_cast<Index>(dimensions[i]);
432 #ifdef EIGEN_TENSOR_PLUGIN
433 #include EIGEN_TENSOR_PLUGIN
438 bool checkIndexRange(
const array<Index, NumIndices>& indices)
const
440 using internal::array_apply_and_reduce;
441 using internal::array_zip_and_reduce;
442 using internal::greater_equal_zero_op;
443 using internal::logical_and_op;
444 using internal::lesser_op;
448 array_apply_and_reduce<logical_and_op, greater_equal_zero_op>(indices) &&
450 array_zip_and_reduce<logical_and_op, lesser_op>(indices, m_storage.dimensions());
453 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Index linearizedIndex(
const array<Index, NumIndices>& indices)
const
456 return m_storage.dimensions().IndexOfRowMajor(indices);
458 return m_storage.dimensions().IndexOfColMajor(indices);
The tensor base class.
Definition: TensorForwardDeclarations.h:58
The tensor class.
Definition: Tensor.h:67
Tensor(const array< Index, NumIndices > &dimensions)
Definition: Tensor.h:291
void resize(CustomDimension &dimensions)
Definition: Tensor.h:405
void resize(const array< Index, NumIndices > &dimensions)
Definition: Tensor.h:358
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:31