11#ifndef EIGEN_TENSOR_TENSOR_DIMENSIONS_H
12#define EIGEN_TENSOR_TENSOR_DIMENSIONS_H
15#include "./InternalHeaderCheck.h"
22template <std::ptrdiff_t n,
typename Dimension>
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...>> {};
28template <
typename T, T first, T... rest>
29struct dget<0, std::integer_sequence<T, first, rest...>> {
30 static constexpr T value = first;
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) {
39 : (NumIndices - n) > (indices) + dget <
RowMajor ? n - 1
41 Dimensions > ::value * fixed_size_tensor_index_linearization_helper<Index, NumIndices, n - 1, RowMajor>::run(
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&) {
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);
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&) {
86template <std::ptrdiff_t... Indices>
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);
93 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t rank()
const {
return count; }
95 static constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t TotalSize() {
96 return internal::arg_prod(Indices...);
99 constexpr EIGEN_DEVICE_FUNC Sizes() =
default;
100 template <
typename DenseIndex>
101 explicit constexpr EIGEN_DEVICE_FUNC Sizes(
const array<DenseIndex, count>& ) {
104 template <
typename... DenseIndex>
105 constexpr EIGEN_DEVICE_FUNC Sizes(DenseIndex...) {}
106 explicit EIGEN_DEVICE_FUNC Sizes(std::initializer_list<std::ptrdiff_t> ) {
110 template <
typename T>
111 Sizes& operator=(
const T& ) {
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);
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);
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);
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;
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;
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) {
153 : (NumIndices - n - 1) > (indices) + array_get <
RowMajor
155 : (NumIndices - n - 1) >
156 (dimensions)*tensor_index_linearization_helper<Index, NumIndices, n - 1, RowMajor>::run(
157 indices, dimensions);
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);
181template <
typename DenseIndex,
int NumDims>
182struct DSizes : array<DenseIndex, NumDims> {
183 using Base = array<DenseIndex, NumDims>;
184 static constexpr int count = NumDims;
186 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank()
const {
return NumDims; }
188 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex TotalSize()
const {
189 return (NumDims == 0) ? 1 : internal::array_prod(*
static_cast<const Base*
>(
this));
192 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DSizes() {
193 for (
int i = 0; i < NumDims; ++i) {
197 EIGEN_DEVICE_FUNC
explicit DSizes(
const array<DenseIndex, NumDims>& a) : Base(a) {}
199 EIGEN_DEVICE_FUNC
explicit DSizes(
const DenseIndex i0) {
200 eigen_assert(NumDims == 1);
204 EIGEN_DEVICE_FUNC DSizes(
const DimensionList<DenseIndex, NumDims>& a) {
205 for (
int i = 0; i < NumDims; ++i) {
212 template <
typename OtherIndex,
214 std::is_same<DenseIndex, typename internal::promote_index_type<DenseIndex, OtherIndex>::type>::value,
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]);
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,
227 std::is_same<DenseIndex, typename internal::promote_index_type<DenseIndex, OtherIndex>::type>::value,
void*>)
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];
237 template <std::ptrdiff_t... Indices>
238 EIGEN_DEVICE_FUNC DSizes(
const Sizes<Indices...>& a) {
239 for (
int i = 0; i < NumDims; ++i) {
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...));
252 EIGEN_DEVICE_FUNC DSizes& operator=(
const array<DenseIndex, NumDims>& other) {
253 *
static_cast<Base*
>(
this) = other;
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));
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));
268#if EIGEN_COMP_CXXVER < 17
269template <
typename DenseIndex,
int NumDims>
270constexpr int DSizes<DenseIndex, NumDims>::count;
273template <
typename IndexType,
int NumDims>
274std::ostream& operator<<(std::ostream& os,
const DSizes<IndexType, NumDims>& dims) {
276 for (
int i = 0; i < NumDims; ++i) {
277 if (i > 0) os <<
", ";
286template <
typename DenseIndex,
int NumDims>
287struct array_size<const DSizes<DenseIndex, NumDims>> {
288 static constexpr ptrdiff_t value = NumDims;
290template <
typename DenseIndex,
int NumDims>
291struct array_size<DSizes<DenseIndex, NumDims>> {
292 static constexpr ptrdiff_t value = NumDims;
294template <std::ptrdiff_t... Indices>
295struct array_size<const Sizes<Indices...>> {
296 static constexpr std::ptrdiff_t value = Sizes<Indices...>::count;
298template <std::ptrdiff_t... Indices>
299struct array_size<Sizes<Indices...>> {
300 static constexpr std::ptrdiff_t value = Sizes<Indices...>::count;
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;
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");
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;
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;
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; }
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);
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; }
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);
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())>>)
Namespace containing all symbols from the Eigen library.