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 <vector>
18#include <tuple>
19
20namespace iganet {
21namespace utils {
22
25template <typename... Tensors>
26torch::Tensor cat_tuple_into_tensor(const std::tuple<Tensors...>& tensors, int64_t dim = 0) {
27 std::vector<torch::Tensor> vec;
28 vec.reserve(sizeof...(Tensors));
29 std::apply([&](const auto&... tensor) {
30 (vec.emplace_back(tensor), ...);
31 }, tensors);
32
33 return torch::cat(vec, dim);
34}
35
39 template <typename... Tensors, typename Func>
40 torch::Tensor cat_tuple_into_tensor(const std::tuple<Tensors...>& tensors, Func&& func, int64_t dim = 0) {
41 std::vector<torch::Tensor> vec;
42 vec.reserve(sizeof...(Tensors));
43 std::apply([&](const auto&... tensor) {
44 (vec.emplace_back(std::invoke(func, tensor)), ...);
45 }, tensors);
46
47 return torch::cat(vec, dim);
48}
49
51template <std::size_t N, typename T>
52constexpr auto repeat_tuple(const T& value) {
53 return [&]<std::size_t... Is>(std::index_sequence<Is...>) {
54 return std::tuple{((void)Is, value)...};
55 }(std::make_index_sequence<N>{});
56}
57
60template <std::size_t I = 0, typename... Tensors, typename FuncSize, typename FuncAssign>
61void slice_tensor_into_tuple(std::tuple<Tensors...>& tuple,
62 const torch::Tensor& tensor,
63 FuncSize&& funcSize,
64 FuncAssign&& funcAssign,
65 int64_t& offset, int64_t dim = 0) {
66 if constexpr (I < sizeof...(Tensors)) {
67 auto& t = std::get<I>(tuple);
68 auto size = std::forward<FuncSize>(funcSize)(t);
69 std::forward<FuncAssign>(funcAssign)(t, tensor.slice(dim, offset, offset + size));
70 offset += size;
71 slice_tensor_into_tuple<I + 1>(tuple, tensor, funcSize, funcAssign, offset, dim);
72 }
73}
74
75template <typename... Tensors, typename FuncSize, typename FuncAssign>
76void slice_tensor_into_tuple(std::tuple<Tensors...>& tuple,
77 const torch::Tensor& tensor,
78 FuncSize&& funcSize,
79 FuncAssign&& funcAssign,
80 int64_t dim = 0) {
81 int64_t offset = 0;
82 slice_tensor_into_tuple(tuple, tensor, funcSize, funcAssign, offset, dim);
83}
85
86} // namespace utils
87} // namespace iganet
constexpr auto repeat_tuple(const T &value)
Returns an std::tuple object with N replications of the given value.
Definition tuple.hpp:52
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:61
torch::Tensor cat_tuple_into_tensor(const std::tuple< Tensors... > &tensors, int64_t dim=0)
Concatenates the entries of an std::tuple object into a single Torch tensor along the given dimension...
Definition tuple.hpp:26
Definition boundary.hpp:22