11 #ifndef EIGEN_CXX11_TENSOR_TENSOR_H
12 #define EIGEN_CXX11_TENSOR_TENSOR_H
62 template<
typename Scalar_,
int NumIndices_,
int Options_,
typename IndexType_>
68 typedef typename Eigen::internal::nested<Self>::type Nested;
69 typedef typename internal::traits<Self>::StorageKind StorageKind;
70 typedef typename internal::traits<Self>::Index Index;
71 typedef Scalar_ Scalar;
72 typedef typename internal::packet_traits<Scalar>::type Packet;
73 typedef typename NumTraits<Scalar>::Real RealScalar;
74 typedef typename Base::CoeffReturnType CoeffReturnType;
75 typedef typename Base::PacketReturnType PacketReturnType;
78 IsAligned = bool(EIGEN_MAX_ALIGN_BYTES>0) & !(Options_&DontAlign),
79 PacketAccess = (internal::packet_traits<Scalar>::size > 1),
80 Layout = Options_ & RowMajor ? RowMajor : ColMajor,
84 static const int Options = Options_;
85 static const int NumIndices = NumIndices_;
86 typedef DSizes<Index, NumIndices_> Dimensions;
89 TensorStorage<Scalar, Dimensions, Options> m_storage;
91 #ifdef EIGEN_HAS_SFINAE
92 template<
typename CustomIndices>
93 struct isOfNormalIndex{
94 static const bool is_array = internal::is_base_of<array<Index, NumIndices>, CustomIndices>::value;
95 static const bool is_int = NumTraits<CustomIndices>::IsInteger;
96 static const bool value = is_array | is_int;
102 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank()
const {
return NumIndices; }
103 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n)
const {
return m_storage.dimensions()[n]; }
104 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_storage.dimensions(); }
105 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size()
const {
return m_storage.size(); }
106 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar *data() {
return m_storage.data(); }
107 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar *data()
const {
return m_storage.data(); }
112 inline Self& base() {
return *
this; }
113 inline const Self& base()
const {
return *
this; }
115 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
116 template<
typename... IndexTypes>
117 EIGEN_DEVICE_FUNC
inline const Scalar& coeff(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
const
120 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
121 return coeff(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
126 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(
const array<Index, NumIndices>& indices)
const
128 eigen_internal_assert(checkIndexRange(indices));
129 return m_storage.data()[linearizedIndex(indices)];
133 #ifdef EIGEN_HAS_SFINAE
134 template<
typename CustomIndices,
135 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
137 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(CustomIndices& indices)
const
139 return coeff(internal::customIndices2Array<Index,NumIndices>(indices));
143 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff()
const
145 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
146 return m_storage.data()[0];
149 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(Index index)
const
151 eigen_internal_assert(index >= 0 && index < size());
152 return m_storage.data()[index];
155 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
156 template<
typename... IndexTypes>
157 inline Scalar& coeffRef(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
160 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
161 return coeffRef(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
166 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(
const array<Index, NumIndices>& indices)
168 eigen_internal_assert(checkIndexRange(indices));
169 return m_storage.data()[linearizedIndex(indices)];
173 #ifdef EIGEN_HAS_SFINAE
174 template<
typename CustomIndices,
175 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
177 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(CustomIndices& indices)
179 return coeffRef(internal::customIndices2Array<Index,NumIndices>(indices));
183 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef()
185 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
186 return m_storage.data()[0];
189 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
191 eigen_internal_assert(index >= 0 && index < size());
192 return m_storage.data()[index];
195 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
196 template<
typename... IndexTypes>
197 inline const Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
const
200 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
201 return this->operator()(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
205 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1)
const
207 return coeff(array<Index, 2>(i0, i1));
210 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2)
const
212 return coeff(array<Index, 3>(i0, i1, i2));
215 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2, Index i3)
const
217 return coeff(array<Index, 4>(i0, i1, i2, i3));
220 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
const
222 return coeff(array<Index, 5>(i0, i1, i2, i3, i4));
227 #ifdef EIGEN_HAS_SFINAE
228 template<
typename CustomIndices,
229 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
231 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(CustomIndices& indices)
const
233 return coeff(internal::customIndices2Array<Index,NumIndices>(indices));
238 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(
const array<Index, NumIndices>& indices)
const
240 return coeff(indices);
243 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(Index index)
const
245 eigen_internal_assert(index >= 0 && index < size());
249 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()()
const
251 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
255 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator[](Index index)
const
258 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE);
262 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
263 template<
typename... IndexTypes>
264 inline Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
267 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
268 return operator()(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
272 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1)
274 return coeffRef(array<Index, 2>(i0, i1));
277 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2)
279 return coeffRef(array<Index, 3>(i0, i1, i2));
282 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2, Index i3)
284 return coeffRef(array<Index, 4>(i0, i1, i2, i3));
287 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
289 return coeffRef(array<Index, 5>(i0, i1, i2, i3, i4));
294 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(
const array<Index, NumIndices>& indices)
296 return coeffRef(indices);
300 #ifdef EIGEN_HAS_SFINAE
301 template<
typename CustomIndices,
302 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
304 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(CustomIndices& indices)
306 return coeffRef(internal::customIndices2Array<Index,NumIndices>(indices));
310 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index index)
312 eigen_assert(index >= 0 && index < size());
313 return coeffRef(index);
316 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()()
318 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
322 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator[](Index index)
325 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
326 return coeffRef(index);
330 EIGEN_STRONG_INLINE
Tensor()
336 EIGEN_STRONG_INLINE Tensor(
const Self& other)
337 : m_storage(other.m_storage)
341 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
342 template<
typename... IndexTypes>
343 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(Index firstDimension, IndexTypes... otherDimensions)
344 : m_storage(internal::array_prod(array<Index, NumIndices>{{firstDimension, otherDimensions...}}), array<Index, NumIndices>{{firstDimension, otherDimensions...}})
347 EIGEN_STATIC_ASSERT(
sizeof...(otherDimensions) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
350 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit Tensor(Index dim1)
351 : m_storage(dim1, array<Index, 1>(dim1))
353 EIGEN_STATIC_ASSERT(1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
355 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit Tensor(Index dim1, Index dim2)
356 : m_storage(dim1*dim2, array<Index, 2>(dim1, dim2))
358 EIGEN_STATIC_ASSERT(2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
360 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit Tensor(Index dim1, Index dim2, Index dim3)
361 : m_storage(dim1*dim2*dim3, array<Index, 3>(dim1, dim2, dim3))
363 EIGEN_STATIC_ASSERT(3 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
365 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit Tensor(Index dim1, Index dim2, Index dim3, Index dim4)
366 : m_storage(dim1*dim2*dim3*dim4, array<Index, 4>(dim1, dim2, dim3, dim4))
368 EIGEN_STATIC_ASSERT(4 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
370 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit Tensor(Index dim1, Index dim2, Index dim3, Index dim4, Index dim5)
371 : m_storage(dim1*dim2*dim3*dim4*dim5, array<Index, 5>(dim1, dim2, dim3, dim4, dim5))
373 EIGEN_STATIC_ASSERT(5 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
378 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit Tensor(
const array<Index, NumIndices>& dimensions)
379 : m_storage(internal::array_prod(dimensions), dimensions)
381 EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
384 template<
typename OtherDerived>
388 typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
389 Assign assign(*
this, other.derived());
391 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
393 template<
typename OtherDerived>
395 EIGEN_STRONG_INLINE Tensor(
const TensorBase<OtherDerived, WriteAccessors>& other)
397 typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
398 Assign assign(*
this, other.derived());
399 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
400 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
404 EIGEN_STRONG_INLINE Tensor& operator=(
const Tensor& other)
406 typedef TensorAssignOp<Tensor, const Tensor> Assign;
407 Assign assign(*
this, other);
408 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
409 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
412 template<
typename OtherDerived>
414 EIGEN_STRONG_INLINE Tensor& operator=(
const OtherDerived& other)
416 typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
417 Assign assign(*
this, other);
418 resize(TensorEvaluator<const Assign, DefaultDevice>(assign, DefaultDevice()).dimensions());
419 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
423 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
424 template<
typename... IndexTypes> EIGEN_DEVICE_FUNC
425 void resize(Index firstDimension, IndexTypes... otherDimensions)
428 EIGEN_STATIC_ASSERT(
sizeof...(otherDimensions) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
429 resize(array<Index, NumIndices>{{firstDimension, otherDimensions...}});
434 EIGEN_DEVICE_FUNC
void resize(
const array<Index, NumIndices>& dimensions)
437 Index size = Index(1);
438 for (i = 0; i < NumIndices; i++) {
439 internal::check_rows_cols_for_overflow<Dynamic>::run(size, dimensions[i]);
440 size *= dimensions[i];
442 #ifdef EIGEN_INITIALIZE_COEFFS
443 bool size_changed = size != this->size();
444 m_storage.resize(size, dimensions);
445 if(size_changed) EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
447 m_storage.resize(size, dimensions);
452 EIGEN_DEVICE_FUNC
void resize(
const DSizes<Index, NumIndices>& dimensions) {
453 array<Index, NumIndices> dims;
454 for (
int i = 0; i < NumIndices; ++i) {
455 dims[i] = dimensions[i];
463 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
468 #ifdef EIGEN_HAS_SFINAE
469 template<
typename CustomDimension,
470 EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomDimension>::value) )
472 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void resize(CustomDimension& dimensions)
474 resize(internal::customIndices2Array<Index,NumIndices>(dimensions));
478 #ifndef EIGEN_EMULATE_CXX11_META_H
479 template <
typename std::ptrdiff_t... Indices>
481 void resize(
const Sizes<Indices...>& dimensions) {
482 array<Index, NumIndices> dims;
483 for (
int i = 0; i < NumIndices; ++i) {
484 dims[i] =
static_cast<Index
>(dimensions[i]);
489 template <std::
size_t V1, std::
size_t V2, std::
size_t V3, std::
size_t V4, std::
size_t V5>
491 void resize(
const Sizes<V1, V2, V3, V4, V5>& dimensions) {
492 array<Index, NumIndices> dims;
493 for (
int i = 0; i < NumIndices; ++i) {
494 dims[i] =
static_cast<Index
>(dimensions[i]);
502 bool checkIndexRange(
const array<Index, NumIndices>& indices)
const
504 using internal::array_apply_and_reduce;
505 using internal::array_zip_and_reduce;
506 using internal::greater_equal_zero_op;
507 using internal::logical_and_op;
508 using internal::lesser_op;
512 array_apply_and_reduce<logical_and_op, greater_equal_zero_op>(indices) &&
514 array_zip_and_reduce<logical_and_op, lesser_op>(indices, m_storage.dimensions());
517 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index linearizedIndex(
const array<Index, NumIndices>& indices)
const
519 if (Options&RowMajor) {
520 return m_storage.dimensions().IndexOfRowMajor(indices);
522 return m_storage.dimensions().IndexOfColMajor(indices);
529 #endif // EIGEN_CXX11_TENSOR_TENSOR_H
void resize(const array< Index, NumIndices > &dimensions)
Definition: Tensor.h:434
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13
The tensor evaluator classes.
Definition: TensorEvaluator.h:28
Tensor(const array< Index, NumIndices > &dimensions)
Definition: Tensor.h:378
The tensor base class.
Definition: TensorForwardDeclarations.h:19
void resize(const Sizes< Indices...> &dimensions)
Definition: Tensor.h:481
The tensor class.
Definition: Tensor.h:63