18#include <initializer_list>
30template <std::
size_t N,
typename T>
31inline std::array<T, N>
to_array(std::vector<T> &&vector) {
32 if (vector.size() != N)
33 throw std::invalid_argument(
"Cannot convert std::vector to std::array: size mismatch");
35 std::array<T, N> array;
36 std::move(vector.begin(), vector.end(), array.begin());
45template <
typename T, std::
size_t N>
46inline std::vector<T>
to_vector(std::array<T, N> &&array) {
47 std::vector<T> vector(N);
48 std::move(array.begin(), array.end(), vector.begin());
56template <
typename... Args>
inline auto to_array(Args &&...args) {
57 return std::array<std::common_type_t<Args...>,
sizeof...(Args)>{
65template <
typename... Args>
inline auto to_vector(Args &&...args) {
66 return std::vector<std::common_type_t<Args...>>{std::move(args)...};
77template <
typename T, std::
size_t N>
80 torch::IntArrayRef sizes = torch::IntArrayRef{-1},
82 if (options.
device() == torch::kCPU)
83 return torch::from_blob(
const_cast<T *
>(std::data(array)),
84 (sizes == torch::IntArrayRef{-1}) ? array.size()
89 .requires_grad_(options.requires_grad());
91 return torch::from_blob(
const_cast<T *
>(std::data(array)),
92 (sizes == torch::IntArrayRef{-1}) ? array.size()
94 options.device(
torch::kCPU))
98 .requires_grad_(options.requires_grad());
107template <
typename T, std::
size_t N>
110 if (options.
device() == torch::kCPU)
111 return torch::from_blob(
const_cast<T *
>(std::data(array)), array.size(),
117 return torch::from_blob(
const_cast<T *
>(std::data(array)), array.size(),
118 options.
device(torch::kCPU))
136 torch::IntArrayRef sizes = torch::IntArrayRef{-1},
138 if (options.
device() == torch::kCPU)
139 return torch::from_blob(
140 const_cast<T *
>(std::data(list)),
141 (sizes == torch::IntArrayRef{-1}) ? list.size() : sizes, options)
144 .requires_grad_(options.requires_grad());
146 return torch::from_blob(
const_cast<T *
>(std::data(list)),
147 (sizes == torch::IntArrayRef{-1}) ? list.size()
149 options.device(
torch::kCPU))
152 .to(options.device())
153 .requires_grad_(options.requires_grad());
164 if (options.
device() == torch::kCPU)
165 return torch::from_blob(
const_cast<T *
>(std::data(list)), list.size(),
171 return torch::from_blob(
const_cast<T *
>(std::data(list)), list.size(),
172 options.
device(torch::kCPU))
190 torch::IntArrayRef sizes = torch::IntArrayRef{-1},
192 if (options.
device() == torch::kCPU)
193 return torch::from_blob(
const_cast<T *
>(std::data(vector)),
194 (sizes == torch::IntArrayRef{-1}) ? vector.size()
199 .requires_grad_(options.requires_grad());
201 return torch::from_blob(
const_cast<T *
>(std::data(vector)),
202 (sizes == torch::IntArrayRef{-1}) ? vector.size()
204 options.device(
torch::kCPU))
207 .to(options.device())
208 .requires_grad_(options.requires_grad());
219 if (options.
device() == torch::kCPU)
220 return torch::from_blob(
const_cast<T *
>(std::data(vector)), vector.size(),
226 return torch::from_blob(
const_cast<T *
>(std::data(vector)), vector.size(),
227 options.
device(torch::kCPU))
240template <
typename T, std::
size_t N>
242 return at::ArrayRef<T>{array};
251template <
typename T, std::size_t... N>
252inline auto concat(
const std::array<T, N> &...arrays) {
253 std::array<T, (N + ...)> result;
256 ((std::copy_n(arrays.begin(), N, result.begin() + index), index += N), ...);
266template <
typename T, std::size_t... N>
267inline auto concat(std::array<T, N> &&...arrays) {
268 std::array<T, (N + ...)> result;
271 ((std::copy_n(std::make_move_iterator(arrays.begin()), N,
272 result.begin() + index),
285template <
typename... Ts>
286inline auto concat(
const std::vector<Ts> &...vectors) {
287 std::vector<std::common_type_t<Ts...>> result;
289 (result.insert(result.end(), vectors.begin(), vectors.end()), ...);
298template <
typename... Ts>
inline auto concat(std::vector<Ts> &&...vectors) {
299 std::vector<std::common_type_t<Ts...>> result;
301 (result.insert(result.end(), std::make_move_iterator(vectors.begin()),
302 std::make_move_iterator(vectors.end())),
315inline constexpr auto operator+(torch::ArrayRef<T> array, T data) {
316 std::vector<T> result{array.vec()};
317 result.push_back(data);
327template <
typename T, std::
size_t N>
328inline constexpr auto operator+(std::array<T, N> array, T data) {
329 std::array<T, N + 1> result;
330 for (std::size_t i = 0; i < N; ++i)
331 result[i] = array[i];
342inline constexpr auto operator+(std::vector<T> vector, T data) {
343 std::vector<T> result{vector};
344 result.push_back(data);
354inline constexpr auto operator+(T data, torch::ArrayRef<T> array) {
355 std::vector<T> result{array.vec()};
356 result.insert(result.begin(), data);
366template <
typename T, std::
size_t N>
367inline constexpr auto operator+(T data, std::array<T, N> array) {
368 std::array<T, N + 1> result;
370 for (std::size_t i = 0; i < N; ++i)
371 result[i + 1] = array[i];
381inline constexpr auto operator+(T data, std::vector<T> vector) {
382 std::vector<T> result{vector};
383 result.insert(result.begin(), data);
392template <
typename T, std::
size_t N>
inline constexpr auto make_array(T value) {
393 std::array<T, N> result;
404template <
typename T,
typename U, std::
size_t N>
405inline constexpr std::array<T, N>
make_array(std::array<U, N> array) {
406 std::array<T, N> result;
407 for (std::size_t i = 0; i < N; ++i)
408 result[i] =
static_cast<T
>(array[i]);
417template <
typename T, std::
size_t N>
418inline constexpr std::array<T, N>
operator-(std::array<T, N> array) {
419 std::array<T, N> result;
420 for (std::size_t i = 0; i < N; ++i)
421 result[i] = -array[i];
431template <
typename T, std::
size_t N>
432inline constexpr std::array<T, N>
operator+(std::array<T, N> lhs,
433 std::array<T, N> rhs) {
434 std::array<T, N> result;
435 for (std::size_t i = 0; i < N; ++i)
436 result[i] = lhs[i] + rhs[i];
446template <
typename T, std::
size_t N>
447inline constexpr std::array<T, N>
operator-(std::array<T, N> lhs,
448 std::array<T, N> rhs) {
449 std::array<T, N> result;
450 for (std::size_t i = 0; i < N; ++i)
451 result[i] = lhs[i] - rhs[i];
461template <
typename T, std::
size_t N>
462inline constexpr std::array<T, N>
operator*(std::array<T, N> lhs,
463 std::array<T, N> rhs) {
464 std::array<T, N> result;
465 for (std::size_t i = 0; i < N; ++i)
466 result[i] = lhs[i] * rhs[i];
476template <
typename T, std::
size_t N>
477inline constexpr std::array<T, N>
operator/(std::array<T, N> lhs,
478 std::array<T, N> rhs) {
479 std::array<T, N> result;
480 for (std::size_t i = 0; i < N; ++i)
481 result[i] = lhs[i] / rhs[i];
492template <
typename T, std::
size_t N, std::
size_t M = 1>
493inline constexpr std::array<T, N - M>
496 std::array<T, N - M> result;
497 for (std::size_t i = 0; i < N - M; ++i)
498 result[i] = array[i + M];
509template <
typename T, std::
size_t N, std::
size_t M = 1>
512 std::array<T, N - M> result;
513 for (std::size_t i = 0; i < N - M; ++i)
514 result[i] = array[i];
The Options class handles the automated determination of dtype from the template argument and the sel...
Definition options.hpp:47
torch::Device device() const noexcept
Returns the device property.
Definition options.hpp:90
bool requires_grad() const noexcept
Returns the requires_grad property.
Definition options.hpp:110
Definition blocktensor.hpp:24
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:1810
constexpr auto make_array(T value)
Creates a std::array object filled with a constant.
Definition container.hpp:392
auto to_tensor(const std::array< T, N > &array, torch::IntArrayRef sizes=torch::IntArrayRef{-1}, const iganet::Options< T > &options=iganet::Options< T >{})
Converts a std::array to torch::Tensor.
Definition container.hpp:79
auto concat(const std::array< T, N > &...arrays)
Concatenates multiple std::array objects.
Definition container.hpp:252
constexpr std::array< T, N - M > remove_from_front(std::array< T, N > array)
Derives a std::array object from a given std::array object dropping the first M entries.
Definition container.hpp:494
std::array< T, N > to_array(std::vector< T > &&vector)
Converts a std::vector object into std::array.
Definition container.hpp:31
std::vector< T > to_vector(std::array< T, N > &&array)
Converts a std::array object into std::vector.
Definition container.hpp:46
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:477
constexpr std::array< T, N - M > remove_from_back(std::array< T, N > array)
Derives a std::array object from a given std::array object dropping the last M entries.
Definition container.hpp:510
auto to_ArrayRef(const std::array< T, N > &array)
Converts a std::array<int64_t, N> to an at::IntArrayRef object.
Definition container.hpp:241
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:958
constexpr auto operator+(deriv lhs, deriv rhs)
Adds two enumerators for specifying the derivative of B-spline evaluation.
Definition bspline.hpp:93
Definition optimizer.hpp:61