18#include <initializer_list>
23#include <torch/torch.h>
29template <std::
size_t N,
typename T>
30inline std::array<T, N>
to_array(std::vector<T> &&vector) {
31 std::array<T, N> array;
32 std::move(vector.begin(), vector.end(), array.begin());
37template <
typename T, std::
size_t N>
38inline std::vector<T>
to_vector(std::array<T, N> &&array) {
39 std::vector<T> vector;
40 std::move(array.begin(), array.end(), vector.begin());
45template <
typename... Args>
inline auto to_array(Args &&...args) {
46 return std::array<
typename std::common_type<Args...>::type,
sizeof...(Args)>{
51template <
typename... Args>
inline auto to_vector(Args &&...args) {
52 return std::vector<
typename std::common_type<Args...>::type>{
58template <
typename T, std::
size_t N>
61 torch::IntArrayRef sizes = torch::IntArrayRef{-1},
63 if (options.
device() == torch::kCPU)
64 return torch::from_blob(
const_cast<T *
>(std::data(array)),
65 (sizes == torch::IntArrayRef{-1}) ? array.size()
70 .requires_grad_(options.requires_grad());
72 return torch::from_blob(
const_cast<T *
>(std::data(array)),
73 (sizes == torch::IntArrayRef{-1}) ? array.size()
75 options.device(
torch::kCPU))
79 .requires_grad_(options.requires_grad());
82template <
typename T, std::
size_t N>
85 if (options.
device() == torch::kCPU)
86 return torch::from_blob(
const_cast<T *
>(std::data(array)), array.size(),
92 return torch::from_blob(
const_cast<T *
>(std::data(array)), array.size(),
93 options.
device(torch::kCPU))
106 torch::IntArrayRef sizes = torch::IntArrayRef{-1},
108 if (options.
device() == torch::kCPU)
109 return torch::from_blob(
110 const_cast<T *
>(std::data(list)),
111 (sizes == torch::IntArrayRef{-1}) ? list.size() : sizes, options)
114 .requires_grad_(options.requires_grad());
116 return torch::from_blob(
const_cast<T *
>(std::data(list)),
117 (sizes == torch::IntArrayRef{-1}) ? list.size()
119 options.device(
torch::kCPU))
122 .to(options.device())
123 .requires_grad_(options.requires_grad());
129 if (options.
device() == torch::kCPU)
130 return torch::from_blob(
const_cast<T *
>(std::data(list)), list.size(),
136 return torch::from_blob(
const_cast<T *
>(std::data(list)), list.size(),
137 options.
device(torch::kCPU))
150 torch::IntArrayRef sizes = torch::IntArrayRef{-1},
152 if (options.
device() == torch::kCPU)
153 return torch::from_blob(
const_cast<T *
>(std::data(vector)),
154 (sizes == torch::IntArrayRef{-1}) ? vector.size()
159 .requires_grad_(options.requires_grad());
161 return torch::from_blob(
const_cast<T *
>(std::data(vector)),
162 (sizes == torch::IntArrayRef{-1}) ? vector.size()
164 options.device(
torch::kCPU))
167 .to(options.device())
168 .requires_grad_(options.requires_grad());
174 if (options.
device() == torch::kCPU)
175 return torch::from_blob(
const_cast<T *
>(std::data(vector)), vector.size(),
181 return torch::from_blob(
const_cast<T *
>(std::data(vector)), vector.size(),
182 options.
device(torch::kCPU))
191template <
typename T, std::
size_t N>
193 return at::ArrayRef<T>{array};
198template <
typename T, std::size_t... N>
199inline auto concat(
const std::array<T, N> &...arrays) {
200 std::array<T, (N + ...)> result;
203 ((std::copy_n(arrays.begin(), N, result.begin() + index), index += N), ...);
208template <
typename T, std::size_t... N>
209inline auto concat(std::array<T, N> &&...arrays) {
210 std::array<T, (N + ...)> result;
213 ((std::copy_n(std::make_move_iterator(arrays.begin()), N,
214 result.begin() + index),
224template <
typename... Ts>
225inline auto concat(
const std::vector<Ts> &...vectors) {
226 std::vector<
typename std::common_type<Ts...>::type> result;
228 (result.insert(result.end(), vectors.begin(), vectors.end()), ...);
233template <
typename... Ts>
inline auto concat(std::vector<Ts> &&...vectors) {
234 std::vector<
typename std::common_type<Ts...>::type> result;
236 (result.insert(result.end(), std::make_move_iterator(vectors.begin()),
237 std::make_move_iterator(vectors.end())),
246inline constexpr auto operator+(torch::ArrayRef<T> array, T data) {
247 std::vector<T> result{array.vec()};
248 result.push_back(data);
253template <
typename T, std::
size_t N>
254inline constexpr auto operator+(std::array<T, N> array, T data) {
255 std::array<T, N + 1> result;
256 for (std::size_t i = 0; i < N; ++i)
257 result[i] = array[i];
264inline constexpr auto operator+(std::vector<T> vector, T data) {
265 std::vector<T> result{vector};
266 result.push_back(data);
272inline constexpr auto operator+(T data, torch::ArrayRef<T> array) {
273 std::vector<T> result{array.vec()};
274 result.insert(result.begin(), data);
279template <
typename T, std::
size_t N>
280inline constexpr auto operator+(T data, std::array<T, N> array) {
281 std::array<T, N + 1> result;
283 for (std::size_t i = 0; i < N; ++i)
284 result[i + 1] = array[i];
290inline constexpr auto operator+(T data, std::vector<T> vector) {
291 std::vector<T> result{vector};
292 result.insert(result.begin(), data);
297template <
typename T, std::
size_t N>
inline constexpr auto make_array(T value) {
298 std::array<T, N> result;
304template <
typename T,
typename U, std::
size_t N>
305inline constexpr std::array<T, N>
make_array(std::array<U, N> array) {
306 std::array<T, N> result;
307 for (std::size_t i = 0; i < N; ++i)
308 result[i] =
static_cast<T
>(array[i]);
313template <
typename T, std::
size_t N>
314inline constexpr std::array<T, N>
operator-(std::array<T, N> array) {
315 std::array<T, N> result;
316 for (std::size_t i = 0; i < N; ++i)
317 result[i] = -array[i];
322template <
typename T, std::
size_t N>
323inline constexpr std::array<T, N>
operator+(std::array<T, N> lhs,
324 std::array<T, N> rhs) {
325 std::array<T, N> result;
326 for (std::size_t i = 0; i < N; ++i)
327 result[i] = lhs[i] + rhs[i];
332template <
typename T, std::
size_t N>
333inline constexpr std::array<T, N>
operator-(std::array<T, N> lhs,
334 std::array<T, N> rhs) {
335 std::array<T, N> result;
336 for (std::size_t i = 0; i < N; ++i)
337 result[i] = lhs[i] - rhs[i];
342template <
typename T, std::
size_t N>
343inline constexpr std::array<T, N>
operator*(std::array<T, N> lhs,
344 std::array<T, N> rhs) {
345 std::array<T, N> result;
346 for (std::size_t i = 0; i < N; ++i)
347 result[i] = lhs[i] * rhs[i];
352template <
typename T, std::
size_t N>
353inline constexpr std::array<T, N>
operator/(std::array<T, N> lhs,
354 std::array<T, N> rhs) {
355 std::array<T, N> result;
356 for (std::size_t i = 0; i < N; ++i)
357 result[i] = lhs[i] / rhs[i];
363template <
typename T, std::
size_t N, std::
size_t M = 1>
364inline constexpr std::array<T, N - M>
367 std::array<T, N - M> result;
368 for (std::size_t i = 0; i < N - M; ++i)
369 result[i] = array[i + M];
375template <
typename T, std::
size_t N, std::
size_t M = 1>
378 std::array<T, N - M> result;
379 for (std::size_t i = 0; i < N - M; ++i)
380 result[i] = array[i];
The Options class handles the automated determination of dtype from the template argument and the sel...
Definition options.hpp:107
torch::Device device() const noexcept
Returns the device property.
Definition options.hpp:140
bool requires_grad() const noexcept
Returns the requires_grad property.
Definition options.hpp:152
auto operator-(const BlockTensor< T, Dims... > &lhs, const BlockTensor< U, Dims... > &rhs)
Subtracts one compile-time block tensor from another and returns a new compile-time block tensor.
Definition blocktensor.hpp:1680
constexpr auto make_array(T value)
Creates an std::array object filled with a constant.
Definition container.hpp:297
auto to_tensor(const std::array< T, N > &array, torch::IntArrayRef sizes=torch::IntArrayRef{-1}, const iganet::Options< T > &options=iganet::Options< T >{})
Converts an std::array to torch::Tensor.
Definition container.hpp:60
auto concat(const std::array< T, N > &...arrays)
Concatenates multiple std::array objects.
Definition container.hpp:199
constexpr std::array< T, N - M > remove_from_front(std::array< T, N > array)
Derives an std::array object from a given std::array object dropping the first M entries.
Definition container.hpp:365
std::array< T, N > to_array(std::vector< T > &&vector)
Converts an std::vector object into std::array.
Definition container.hpp:30
std::vector< T > to_vector(std::array< T, N > &&array)
Converts an std::array object into std::vector.
Definition container.hpp:38
constexpr std::array< T, N > operator/(std::array< T, N > lhs, std::array< T, N > rhs)
Divides one std::array by another std::array.
Definition container.hpp:353
constexpr std::array< T, N - M > remove_from_back(std::array< T, N > array)
Derives an std::array object from a given std::array object dropping the last M entries.
Definition container.hpp:376
auto to_ArrayRef(const std::array< T, N > &array)
Converts an std::array<int64_t, N> to a at::IntArrayRef object.
Definition container.hpp:192
auto operator*(const BlockTensor< T, Rows, Common > &lhs, const BlockTensor< U, Common, Cols > &rhs)
Multiplies one compile-time rank-2 block tensor with another compile-time rank-2 block tensor.
Definition blocktensor.hpp:896
Definition boundary.hpp:22
constexpr auto operator+(deriv lhs, deriv rhs)
Adds two enumerators for specifying the derivative of B-spline evaluation.
Definition bspline.hpp:91
Definition optimizer.hpp:62