11 #ifndef EIGEN_CXX11_TENSOR_TENSOR_H
12 #define EIGEN_CXX11_TENSOR_TENSOR_H
62 template<
typename Scalar_, std::
size_t 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 std::size_t NumIndices = NumIndices_;
86 typedef DSizes<Index, NumIndices_> Dimensions;
89 TensorStorage<Scalar, Dimensions, Options> m_storage;
93 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank()
const {
return NumIndices; }
94 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n)
const {
return m_storage.dimensions()[n]; }
95 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_storage.dimensions(); }
96 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size()
const {
return m_storage.size(); }
97 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar *data() {
return m_storage.data(); }
98 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar *data()
const {
return m_storage.data(); }
103 inline Self& base() {
return *
this; }
104 inline const Self& base()
const {
return *
this; }
106 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
107 template<
typename... IndexTypes>
108 EIGEN_DEVICE_FUNC
inline const Scalar& coeff(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
const
111 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
112 return coeff(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
116 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(
const array<Index, NumIndices>& indices)
const
118 eigen_internal_assert(checkIndexRange(indices));
119 return m_storage.data()[linearizedIndex(indices)];
122 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(Index index)
const
124 eigen_internal_assert(index >= 0 && index < size());
125 return m_storage.data()[index];
128 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
129 template<
typename... IndexTypes>
130 inline Scalar& coeffRef(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
133 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
134 return coeffRef(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
138 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(
const array<Index, NumIndices>& indices)
140 eigen_internal_assert(checkIndexRange(indices));
141 return m_storage.data()[linearizedIndex(indices)];
144 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
146 eigen_internal_assert(index >= 0 && index < size());
147 return m_storage.data()[index];
150 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
151 template<
typename... IndexTypes>
152 inline const Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
const
155 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
156 return this->operator()(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
160 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1)
const
162 return coeff(array<Index, 2>(i0, i1));
165 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2)
const
167 return coeff(array<Index, 3>(i0, i1, i2));
170 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2, Index i3)
const
172 return coeff(array<Index, 4>(i0, i1, i2, i3));
175 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
const
177 return coeff(array<Index, 5>(i0, i1, i2, i3, i4));
181 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(
const array<Index, NumIndices>& indices)
const
183 eigen_assert(checkIndexRange(indices));
184 return coeff(indices);
187 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(Index index)
const
189 eigen_internal_assert(index >= 0 && index < size());
193 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator[](Index index)
const
196 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE);
200 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
201 template<
typename... IndexTypes>
202 inline Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
205 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
206 return operator()(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
210 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1)
212 return coeffRef(array<Index, 2>(i0, i1));
215 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2)
217 return coeffRef(array<Index, 3>(i0, i1, i2));
220 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2, Index i3)
222 return coeffRef(array<Index, 4>(i0, i1, i2, i3));
225 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
227 return coeffRef(array<Index, 5>(i0, i1, i2, i3, i4));
231 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(
const array<Index, NumIndices>& indices)
233 eigen_assert(checkIndexRange(indices));
234 return coeffRef(indices);
237 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index index)
239 eigen_assert(index >= 0 && index < size());
240 return coeffRef(index);
243 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator[](Index index)
246 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
247 return coeffRef(index);
251 EIGEN_STRONG_INLINE
Tensor()
257 EIGEN_STRONG_INLINE Tensor(
const Self& other)
258 : m_storage(other.m_storage)
262 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
263 template<
typename... IndexTypes>
264 inline Tensor(Index firstDimension, IndexTypes... otherDimensions)
265 : m_storage(internal::array_prod(array<Index, NumIndices>{{firstDimension, otherDimensions...}}), array<Index, NumIndices>{{firstDimension, otherDimensions...}})
268 EIGEN_STATIC_ASSERT(
sizeof...(otherDimensions) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
271 inline explicit Tensor(Index dim1)
272 : m_storage(dim1, array<Index, 1>(dim1))
274 EIGEN_STATIC_ASSERT(1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
276 inline explicit Tensor(Index dim1, Index dim2)
277 : m_storage(dim1*dim2, array<Index, 2>(dim1, dim2))
279 EIGEN_STATIC_ASSERT(2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
281 inline explicit Tensor(Index dim1, Index dim2, Index dim3)
282 : m_storage(dim1*dim2*dim3, array<Index, 3>(dim1, dim2, dim3))
284 EIGEN_STATIC_ASSERT(3 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
286 inline explicit Tensor(Index dim1, Index dim2, Index dim3, Index dim4)
287 : m_storage(dim1*dim2*dim3*dim4, array<Index, 4>(dim1, dim2, dim3, dim4))
289 EIGEN_STATIC_ASSERT(4 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
291 inline explicit Tensor(Index dim1, Index dim2, Index dim3, Index dim4, Index dim5)
292 : m_storage(dim1*dim2*dim3*dim4*dim5, array<Index, 4>(dim1, dim2, dim3, dim4, dim5))
294 EIGEN_STATIC_ASSERT(5 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
298 inline explicit Tensor(
const array<Index, NumIndices>& dimensions)
299 : m_storage(internal::array_prod(dimensions), dimensions)
301 EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
304 template<
typename OtherDerived>
308 typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
309 Assign assign(*
this, other.derived());
311 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
313 template<
typename OtherDerived>
317 typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
318 Assign assign(*
this, other.derived());
320 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
324 EIGEN_STRONG_INLINE Tensor& operator=(
const Tensor& other)
326 typedef TensorAssignOp<Tensor, const Tensor> Assign;
327 Assign assign(*
this, other);
329 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
332 template<
typename OtherDerived>
334 EIGEN_STRONG_INLINE Tensor& operator=(
const OtherDerived& other)
336 typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
337 Assign assign(*
this, other);
339 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
343 #ifdef EIGEN_HAS_VARIADIC_TEMPLATES
344 template<
typename... IndexTypes> EIGEN_DEVICE_FUNC
345 void resize(Index firstDimension, IndexTypes... otherDimensions)
348 EIGEN_STATIC_ASSERT(
sizeof...(otherDimensions) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
349 resize(array<Index, NumIndices>{{firstDimension, otherDimensions...}});
353 EIGEN_DEVICE_FUNC
void resize(
const array<Index, NumIndices>& dimensions)
356 Index size = Index(1);
357 for (i = 0; i < NumIndices; i++) {
358 internal::check_rows_cols_for_overflow<Dynamic>::run(size, dimensions[i]);
359 size *= dimensions[i];
361 #ifdef EIGEN_INITIALIZE_COEFFS
362 bool size_changed = size != this->size();
363 m_storage.resize(size, dimensions);
364 if(size_changed) EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
366 m_storage.resize(size, dimensions);
370 EIGEN_DEVICE_FUNC
void resize(
const DSizes<Index, NumIndices>& dimensions) {
371 array<Index, NumIndices> dims;
372 for (std::size_t i = 0; i < NumIndices; ++i) {
373 dims[i] = dimensions[i];
378 #ifndef EIGEN_EMULATE_CXX11_META_H
379 template <
typename std::ptrdiff_t... Indices>
381 void resize(
const Sizes<Indices...>& dimensions) {
382 array<Index, NumIndices> dims;
383 for (std::size_t i = 0; i < NumIndices; ++i) {
384 dims[i] =
static_cast<Index
>(dimensions[i]);
389 template <std::
size_t V1, std::
size_t V2, std::
size_t V3, std::
size_t V4, std::
size_t V5>
391 void resize(
const Sizes<V1, V2, V3, V4, V5>& dimensions) {
392 array<Index, NumIndices> dims;
393 for (std::size_t i = 0; i < NumIndices; ++i) {
394 dims[i] =
static_cast<Index
>(dimensions[i]);
402 bool checkIndexRange(
const array<Index, NumIndices>& indices)
const
404 using internal::array_apply_and_reduce;
405 using internal::array_zip_and_reduce;
406 using internal::greater_equal_zero_op;
407 using internal::logical_and_op;
408 using internal::lesser_op;
412 array_apply_and_reduce<logical_and_op, greater_equal_zero_op>(indices) &&
414 array_zip_and_reduce<logical_and_op, lesser_op>(indices, m_storage.dimensions());
417 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index linearizedIndex(
const array<Index, NumIndices>& indices)
const
419 if (Options&RowMajor) {
420 return m_storage.dimensions().IndexOfRowMajor(indices);
422 return m_storage.dimensions().IndexOfColMajor(indices);
429 #endif // EIGEN_CXX11_TENSOR_TENSOR_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13
The tensor evaluator classes.
Definition: TensorEvaluator.h:28
The tensor base class.
Definition: TensorForwardDeclarations.h:19
The tensor class.
Definition: Tensor.h:63