IgANet
IGAnets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
tuple.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <concepts>
18#include <functional>
19#include <tuple>
20#include <type_traits>
21#include <vector>
22
23namespace iganet::utils {
24
27template <class T> struct is_tuple : std::false_type {};
28
29template <class... Ts> struct is_tuple<std::tuple<Ts...>> : std::true_type {};
31
32template <class T> inline constexpr bool is_tuple_v = is_tuple<T>::value;
33
36template <class T> struct is_tuple_of_tuples : std::false_type {};
37
38template <class... Ts>
39struct is_tuple_of_tuples<std::tuple<Ts...>>
40 : std::conjunction<is_tuple<Ts>...> {};
42
45
47template <class T>
49
52template <typename... Tuples> struct tuple_cat;
53
54template <> struct tuple_cat<> {
55 using type = std::tuple<>;
56};
57
58template <typename... Ts, typename... Tuples>
59struct tuple_cat<std::tuple<Ts...>, Tuples...> {
60 using type = decltype(std::tuple_cat(
61 std::declval<std::tuple<Ts...>>(),
62 std::declval<typename tuple_cat<Tuples...>::type>()));
63};
64
65template <typename T, typename... Tuples> struct tuple_cat<T, Tuples...> {
66 using type = decltype(std::tuple_cat(
67 std::declval<std::tuple<T>>(),
68 std::declval<typename tuple_cat<Tuples...>::type>()));
69};
71
73template <typename... Tuples> using tuple_cat_t = tuple_cat<Tuples...>::type;
74
76template <typename... Tuples>
77inline constexpr auto tuple_cat_v = tuple_cat<Tuples...>::value;
78
86template <typename... Tensors>
87torch::Tensor cat_tuple_into_tensor(const std::tuple<Tensors...> &tensors,
88 int64_t dim = 0) {
89 std::vector<torch::Tensor> vec;
90 vec.reserve(sizeof...(Tensors));
91 std::apply([&](const auto &...tensor) { (vec.emplace_back(tensor), ...); },
92 tensors);
93
94 return torch::cat(vec, dim);
95}
96
103template <typename... Tensors, typename Func>
104 requires(std::invocable<Func, const Tensors &> && ...)
105torch::Tensor cat_tuple_into_tensor(const std::tuple<Tensors...> &tensors,
106 Func &&func, int64_t dim = 0) {
107 std::vector<torch::Tensor> vec;
108 vec.reserve(sizeof...(Tensors));
109 std::apply(
110 [&](const auto &...tensor) {
111 (vec.emplace_back(std::invoke(func, tensor)), ...);
112 },
113 tensors);
114
115 return torch::cat(vec, dim);
116}
117
123template <std::size_t N, typename T>
124constexpr auto repeat_tuple(const T &value) {
125 return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
126 return std::tuple{((void)Is, value)...};
127 }(std::make_index_sequence<N>{});
128}
129
138template <std::size_t I = 0, typename... Tensors, typename FuncSize,
139 typename FuncAssign>
140void slice_tensor_into_tuple(std::tuple<Tensors...> &tuple,
141 const torch::Tensor &tensor, FuncSize &&funcSize,
142 FuncAssign &&funcAssign, int64_t &offset,
143 int64_t dim = 0) {
144 if constexpr (I < sizeof...(Tensors)) {
145 auto &t = std::get<I>(tuple);
146 auto size = std::forward<FuncSize>(funcSize)(t);
147 std::forward<FuncAssign>(funcAssign)(
148 t, tensor.slice(dim, offset, offset + size));
149 offset += size;
150 slice_tensor_into_tuple<I + 1>(tuple, tensor, funcSize, funcAssign, offset,
151 dim);
152 }
153}
154
164template <typename... Tensors, typename FuncSize, typename FuncAssign>
165void slice_tensor_into_tuple(std::tuple<Tensors...> &tuple,
166 const torch::Tensor &tensor, FuncSize &&funcSize,
167 FuncAssign &&funcAssign, int64_t dim = 0) {
168 int64_t offset = 0;
169 slice_tensor_into_tuple(tuple, tensor, funcSize, funcAssign, offset, dim);
170}
172
173} // namespace iganet::utils
Definition blocktensor.hpp:24
std::tuple<> type
Definition tuple.hpp:55
decltype(std::tuple_cat(std::declval< std::tuple< Ts... > >(), std::declval< typename tuple_cat< Tuples... >::type >())) type
Definition tuple.hpp:62
constexpr auto is_tuple_of_tuples_v
Alias for is_tuple_of_tuples::value.
Definition tuple.hpp:48
constexpr auto repeat_tuple(const T &value)
Returns a std::tuple object with N replications of the given value.
Definition tuple.hpp:124
void slice_tensor_into_tuple(std::tuple< Tensors... > &tuple, const torch::Tensor &tensor, FuncSize &&funcSize, FuncAssign &&funcAssign, int64_t &offset, int64_t dim=0)
Slices the given tensor into the objects of the std::tuple.
Definition tuple.hpp:140
constexpr auto tuple_cat_v
Alias for tuple_cat::value.
Definition tuple.hpp:77
torch::Tensor cat_tuple_into_tensor(const std::tuple< Tensors... > &tensors, int64_t dim=0)
Concatenates the entries of a std::tuple object into a single Torch tensor along the given dimension.
Definition tuple.hpp:87
decltype(std::tuple_cat(std::declval< std::tuple< T > >(), std::declval< typename tuple_cat< Tuples... >::type >())) type
Definition tuple.hpp:68
tuple_cat< Tuples... >::type tuple_cat_t
Alias for tuple_cat::type.
Definition tuple.hpp:73
is_tuple_of_tuples< T >::type is_tuple_of_tuples_t
Alias for is_tuple_of_tuples::type.
Definition tuple.hpp:44
constexpr bool is_tuple_v
Definition tuple.hpp:32
Type trait for concatenating std::tuples.
Definition tuple.hpp:52
STL namespace.
Definition optimizer.hpp:61
Type trait for std::tuple<std::tuple> type.
Definition tuple.hpp:36
Type trait for std::tuple type.
Definition tuple.hpp:27