Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
TensorDimensions.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//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_DIMENSIONS_H
12#define EIGEN_TENSOR_TENSOR_DIMENSIONS_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19// Boilerplate code
20namespace internal {
21
22template <std::ptrdiff_t n, typename Dimension>
23struct dget;
24
25template <std::ptrdiff_t n, typename T, T first, T... rest>
26struct dget<n, std::integer_sequence<T, first, rest...>> : dget<n - 1, std::integer_sequence<T, rest...>> {};
27
28template <typename T, T first, T... rest>
29struct dget<0, std::integer_sequence<T, first, rest...>> {
30 static constexpr T value = first;
31};
32
33template <typename Index, std::ptrdiff_t NumIndices, std::ptrdiff_t n, bool RowMajor>
34struct fixed_size_tensor_index_linearization_helper {
35 template <typename Dimensions>
36 constexpr EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Index run(array<Index, NumIndices> const& indices,
37 const Dimensions& dimensions) {
38 return array_get < RowMajor ? n - 1
39 : (NumIndices - n) > (indices) + dget < RowMajor ? n - 1
40 : (NumIndices - n),
41 Dimensions > ::value * fixed_size_tensor_index_linearization_helper<Index, NumIndices, n - 1, RowMajor>::run(
42 indices, dimensions);
43 }
44};
45
46template <typename Index, std::ptrdiff_t NumIndices, bool RowMajor>
47struct fixed_size_tensor_index_linearization_helper<Index, NumIndices, 0, RowMajor> {
48 template <typename Dimensions>
49 constexpr EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Index run(array<Index, NumIndices> const&, const Dimensions&) {
50 return 0;
51 }
52};
53
54template <typename Index, std::ptrdiff_t n>
55struct fixed_size_tensor_index_extraction_helper {
56 template <typename Dimensions>
57 constexpr EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Index run(const Index index, const Dimensions& dimensions) {
58 const Index mult = (index == n - 1) ? 1 : 0;
59 return dget<n - 1, Dimensions>::value * mult +
60 fixed_size_tensor_index_extraction_helper<Index, n - 1>::run(index, dimensions);
61 }
62};
63
64template <typename Index>
65struct fixed_size_tensor_index_extraction_helper<Index, 0> {
66 template <typename Dimensions>
67 constexpr EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Index run(const Index, const Dimensions&) {
68 return 0;
69 }
70};
71
72} // end namespace internal
73
86template <std::ptrdiff_t... Indices>
87struct Sizes {
88 using Base = std::integer_sequence<std::ptrdiff_t, Indices...>;
89 const Base t = Base();
90 static constexpr std::ptrdiff_t total_size = internal::arg_prod(Indices...);
91 static constexpr ptrdiff_t count = sizeof...(Indices);
92
93 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t rank() const { return count; }
94
95 static constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t TotalSize() {
96 return internal::arg_prod(Indices...);
97 }
98
99 constexpr EIGEN_DEVICE_FUNC Sizes() = default;
100 template <typename DenseIndex>
101 explicit constexpr EIGEN_DEVICE_FUNC Sizes(const array<DenseIndex, count>& /*indices*/) {
102 // TODO: Add assertion.
103 }
104 template <typename... DenseIndex>
105 constexpr EIGEN_DEVICE_FUNC Sizes(DenseIndex...) {}
106 explicit EIGEN_DEVICE_FUNC Sizes(std::initializer_list<std::ptrdiff_t> /*l*/) {
107 // TODO: Add assertion.
108 }
109
110 template <typename T>
111 Sizes& operator=(const T& /*other*/) {
112 // add assertion failure if the size of other is different
113 return *this;
114 }
115
116 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t operator[](const std::ptrdiff_t index) const {
117 return internal::fixed_size_tensor_index_extraction_helper<std::ptrdiff_t, count>::run(index, t);
118 }
119
120 template <typename DenseIndex>
121 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t
122 IndexOfColMajor(const array<DenseIndex, count>& indices) const {
123 return internal::fixed_size_tensor_index_linearization_helper<DenseIndex, count, count, false>::run(indices, t);
124 }
125 template <typename DenseIndex>
126 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t
127 IndexOfRowMajor(const array<DenseIndex, count>& indices) const {
128 return internal::fixed_size_tensor_index_linearization_helper<DenseIndex, count, count, true>::run(indices, t);
129 }
130};
131
132#if EIGEN_COMP_CXXVER < 17
133template <std::ptrdiff_t... Indices>
134constexpr std::ptrdiff_t Sizes<Indices...>::total_size;
135template <std::ptrdiff_t... Indices>
136constexpr std::ptrdiff_t Sizes<Indices...>::count;
137#endif
138
139namespace internal {
140template <std::ptrdiff_t... Indices>
141constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes<Indices...>&) {
142 return Sizes<Indices...>::total_size;
143}
144} // namespace internal
145
146// Boilerplate
147namespace internal {
148template <typename Index, std::ptrdiff_t NumIndices, std::ptrdiff_t n, bool RowMajor>
149struct tensor_index_linearization_helper {
150 static constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(array<Index, NumIndices> const& indices,
151 array<Index, NumIndices> const& dimensions) {
152 return array_get < RowMajor ? n
153 : (NumIndices - n - 1) > (indices) + array_get < RowMajor
154 ? n
155 : (NumIndices - n - 1) >
156 (dimensions)*tensor_index_linearization_helper<Index, NumIndices, n - 1, RowMajor>::run(
157 indices, dimensions);
158 }
159};
160
161template <typename Index, std::ptrdiff_t NumIndices, bool RowMajor>
162struct tensor_index_linearization_helper<Index, NumIndices, 0, RowMajor> {
163 static constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(array<Index, NumIndices> const& indices,
164 array<Index, NumIndices> const&) {
165 return array_get < RowMajor ? 0 : NumIndices - 1 > (indices);
166 }
167};
168} // end namespace internal
169
181template <typename DenseIndex, int NumDims>
182struct DSizes : array<DenseIndex, NumDims> {
183 using Base = array<DenseIndex, NumDims>;
184 static constexpr int count = NumDims;
185
186 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const { return NumDims; }
187
188 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex TotalSize() const {
189 return (NumDims == 0) ? 1 : internal::array_prod(*static_cast<const Base*>(this));
190 }
191
192 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DSizes() {
193 for (int i = 0; i < NumDims; ++i) {
194 (*this)[i] = 0;
195 }
196 }
197 EIGEN_DEVICE_FUNC explicit DSizes(const array<DenseIndex, NumDims>& a) : Base(a) {}
198
199 EIGEN_DEVICE_FUNC explicit DSizes(const DenseIndex i0) {
200 eigen_assert(NumDims == 1);
201 (*this)[0] = i0;
202 }
203
204 EIGEN_DEVICE_FUNC DSizes(const DimensionList<DenseIndex, NumDims>& a) {
205 for (int i = 0; i < NumDims; ++i) {
206 (*this)[i] = a[i];
207 }
208 }
209
210 // Enable DSizes index type promotion only if we are promoting to the
211 // larger type, e.g. allow to promote dimensions of type int to long.
212 template <typename OtherIndex,
213 std::enable_if_t<
214 std::is_same<DenseIndex, typename internal::promote_index_type<DenseIndex, OtherIndex>::type>::value,
215 int> = 0>
216 EIGEN_DEVICE_FUNC explicit DSizes(const array<OtherIndex, NumDims>& other) {
217 for (int i = 0; i < NumDims; ++i) {
218 (*this)[i] = static_cast<DenseIndex>(other[i]);
219 }
220 }
221
222 template <typename OtherIndex>
223 EIGEN_DEPRECATED_WITH_REASON("Omit the implementation-only second argument.")
224 EIGEN_DEVICE_FUNC explicit DSizes(
225 const array<OtherIndex, NumDims>& other,
226 std::enable_if_t<
227 std::is_same<DenseIndex, typename internal::promote_index_type<DenseIndex, OtherIndex>::type>::value, void*>)
228 : DSizes(other) {}
229
230 template <typename FirstType, typename... OtherTypes>
231 EIGEN_DEVICE_FUNC explicit DSizes(const Eigen::IndexList<FirstType, OtherTypes...>& dimensions) {
232 for (int i = 0; i < dimensions.count; ++i) {
233 (*this)[i] = dimensions[i];
234 }
235 }
236
237 template <std::ptrdiff_t... Indices>
238 EIGEN_DEVICE_FUNC DSizes(const Sizes<Indices...>& a) {
239 for (int i = 0; i < NumDims; ++i) {
240 (*this)[i] = a[i];
241 }
242 }
243
244 template <typename... IndexTypes>
245 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit DSizes(DenseIndex firstDimension, DenseIndex secondDimension,
246 IndexTypes... otherDimensions)
247 : Base({{firstDimension, secondDimension, static_cast<DenseIndex>(otherDimensions)...}}) {
248 EIGEN_STATIC_ASSERT(sizeof...(otherDimensions) + 2 == NumDims, YOU_MADE_A_PROGRAMMING_MISTAKE)
249 eigen_assert(internal::indices_fit<DenseIndex>(otherDimensions...));
250 }
251
252 EIGEN_DEVICE_FUNC DSizes& operator=(const array<DenseIndex, NumDims>& other) {
253 *static_cast<Base*>(this) = other;
254 return *this;
255 }
256
257 // A constexpr would be so much better here
258 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex IndexOfColMajor(const array<DenseIndex, NumDims>& indices) const {
259 return internal::tensor_index_linearization_helper<DenseIndex, NumDims, NumDims - 1, false>::run(
260 indices, *static_cast<const Base*>(this));
261 }
262 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex IndexOfRowMajor(const array<DenseIndex, NumDims>& indices) const {
263 return internal::tensor_index_linearization_helper<DenseIndex, NumDims, NumDims - 1, true>::run(
264 indices, *static_cast<const Base*>(this));
265 }
266};
267
268#if EIGEN_COMP_CXXVER < 17
269template <typename DenseIndex, int NumDims>
270constexpr int DSizes<DenseIndex, NumDims>::count;
271#endif
272
273template <typename IndexType, int NumDims>
274std::ostream& operator<<(std::ostream& os, const DSizes<IndexType, NumDims>& dims) {
275 os << "[";
276 for (int i = 0; i < NumDims; ++i) {
277 if (i > 0) os << ", ";
278 os << dims[i];
279 }
280 os << "]";
281 return os;
282}
283
284namespace internal {
285
286template <typename DenseIndex, int NumDims>
287struct array_size<const DSizes<DenseIndex, NumDims>> {
288 static constexpr ptrdiff_t value = NumDims;
289};
290template <typename DenseIndex, int NumDims>
291struct array_size<DSizes<DenseIndex, NumDims>> {
292 static constexpr ptrdiff_t value = NumDims;
293};
294template <std::ptrdiff_t... Indices>
295struct array_size<const Sizes<Indices...>> {
296 static constexpr std::ptrdiff_t value = Sizes<Indices...>::count;
297};
298template <std::ptrdiff_t... Indices>
299struct array_size<Sizes<Indices...>> {
300 static constexpr std::ptrdiff_t value = Sizes<Indices...>::count;
301};
302template <std::ptrdiff_t n, std::ptrdiff_t... Indices>
303constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_get(const Sizes<Indices...>&) {
304 return dget<n, typename Sizes<Indices...>::Base>::value;
305}
306template <std::ptrdiff_t n>
307EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_get(const Sizes<>&) {
308 eigen_assert(false && "should never be called");
309 return -1;
310}
311
312// Fixed rank (DSizes) does not imply fixed extents (Sizes).
313template <typename Dims1, typename Dims2>
314struct tensor_dimension_relation {
315 static constexpr bool Compatible = array_size<Dims1>::value == array_size<Dims2>::value;
316 static constexpr bool Known = !Compatible;
317 static constexpr bool Proven = false;
318};
319
320template <std::ptrdiff_t... Left, std::ptrdiff_t... Right>
321struct tensor_dimension_relation<Sizes<Left...>, Sizes<Right...>> {
322 static constexpr bool Known = true;
323 static constexpr bool Compatible = std::is_same<Sizes<Left...>, Sizes<Right...>>::value;
324 static constexpr bool Proven = Compatible;
325};
326
327template <typename Dims1, typename Dims2, ptrdiff_t n, ptrdiff_t m>
328struct sizes_match_below_dim {
329 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(const Dims1&, const Dims2&) { return false; }
330};
331template <typename Dims1, typename Dims2, ptrdiff_t n>
332struct sizes_match_below_dim<Dims1, Dims2, n, n> {
333 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(const Dims1& dims1, const Dims2& dims2) {
334 return numext::equal_strict(array_get<n - 1>(dims1), array_get<n - 1>(dims2)) &&
335 sizes_match_below_dim<Dims1, Dims2, n - 1, n - 1>::run(dims1, dims2);
336 }
337};
338template <typename Dims1, typename Dims2>
339struct sizes_match_below_dim<Dims1, Dims2, 0, 0> {
340 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(const Dims1&, const Dims2&) { return true; }
341};
342
343} // end namespace internal
344
345template <typename Dims1, typename Dims2>
346EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool dimensions_match(const Dims1& dims1, const Dims2& dims2) {
347 return internal::sizes_match_below_dim<Dims1, Dims2, internal::array_size<Dims1>::value,
348 internal::array_size<Dims2>::value>::run(dims1, dims2);
349}
350
351} // end namespace Eigen
352
353#define EIGEN_INTERNAL_ASSERT_TENSOR_SHAPE(Lhs, Rhs) \
354 EIGEN_INTERNAL_CHECK_SHAPE( \
355 Eigen::dimensions_match((Lhs).dimensions(), (Rhs).dimensions()) && "Both tensors must have the same dimensions", \
356 YOU_MIXED_TENSORS_OF_DIFFERENT_SIZES, \
357 Eigen::internal::tensor_dimension_relation<Eigen::internal::remove_all_t<decltype((Lhs).dimensions())>, \
358 Eigen::internal::remove_all_t<decltype((Rhs).dimensions())>>)
359
360#endif // EIGEN_TENSOR_TENSOR_DIMENSIONS_H
Namespace containing all symbols from the Eigen library.