69class Tensor :
public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexType_> > {
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_;
78 using CoeffReturnType =
typename Base::CoeffReturnType;
80 enum { IsAligned = (EIGEN_MAX_ALIGN_BYTES > 0) && !(Options_ &
DontAlign), CoordAccess =
true, RawAccess =
true };
83 static constexpr int Options = Options_;
84 static constexpr int NumIndices = NumIndices_;
85 using Dimensions = DSizes<Index, NumIndices_>;
88 TensorStorage<Scalar, Dimensions, Options> m_storage;
90 template <
typename CustomIndices>
91 struct isOfNormalIndex {
92 static constexpr bool is_array = std::is_base_of<array<Index, NumIndices>, CustomIndices>::value;
94 static constexpr bool value = is_array | is_int;
99 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank()
const {
return NumIndices; }
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];
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];
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(); }
117 inline Self& base() {
return *
this; }
118 inline const Self& base()
const {
return *
this; }
120 template <
typename... IndexTypes>
121 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& coeff(Index firstIndex, Index secondIndex,
122 IndexTypes... otherIndices)
const {
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)...}});
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)];
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));
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];
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];
151 template <
typename... IndexTypes>
152 inline Scalar& coeffRef(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) {
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)...}});
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)];
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));
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];
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];
181 template <
typename... IndexTypes>
182 inline const Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
const {
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)...}});
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));
196 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(
const array<Index, NumIndices>& indices)
const {
197 return coeff(indices);
200 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()(Index index)
const {
201 eigen_internal_assert(index >= 0 && index < size());
205 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator()()
const {
206 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
210 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar& operator[](Index index)
const {
212 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE);
216 template <
typename... IndexTypes>
217 inline Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) {
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)...}});
225 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(
const array<Index, NumIndices>& indices) {
226 return coeffRef(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));
235 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index index) {
236 eigen_assert(index >= 0 && index < size());
237 return coeffRef(index);
240 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()() {
241 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
245 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator[](Index index) {
247 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
248 return coeffRef(index);
251 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor() : m_storage() {}
253 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(
const Self& other) =
default;
255 template <
typename... IndexTypes>
256 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(Index firstDimension, IndexTypes... otherDimensions)
257 : m_storage(firstDimension, otherDimensions...) {
259 EIGEN_STATIC_ASSERT(
sizeof...(otherDimensions) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
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
268 template <
typename OtherDerived>
270 EIGEN_STATIC_ASSERT(OtherDerived::NumDimensions == Base::NumDimensions, Number_of_dimensions_must_match)
272 Assign assign(*
this, other.derived());
274 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
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());
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);
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());
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());
308 template <
typename... IndexTypes>
309 EIGEN_DEVICE_FUNC
void resize(Index firstDimension, IndexTypes... otherDimensions) {
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)...}});
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];
325 Index size = internal::array_prod(dimensions);
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
333 m_storage.resize(size, dimensions);
337 EIGEN_DEVICE_FUNC
void resize() {
338 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
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]);
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));
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]);
366#ifdef EIGEN_TENSOR_PLUGIN
367#include EIGEN_TENSOR_PLUGIN
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;
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);
382 return m_storage.dimensions().IndexOfColMajor(indices);