18#include <initializer_list>
26template <std::
size_t N,
typename T>
27inline std::array<T, N>
to_array(std::vector<T> &&vector) {
28 std::array<T, N> array;
29 std::move(vector.begin(), vector.end(), array.begin());
34template <
typename T, std::
size_t N>
35inline std::vector<T>
to_vector(std::array<T, N> &&array) {
36 std::vector<T> vector;
37 std::move(array.begin(), array.end(), vector.begin());
42template <
typename... Args>
inline auto to_array(Args &&...args) {
43 return std::array<std::common_type_t<Args...>,
sizeof...(Args)>{
48template <
typename... Args>
inline auto to_vector(Args &&...args) {
49 return std::vector<std::common_type_t<Args...>>{std::move(args)...};
54template <
typename T, std::
size_t N>
57 torch::IntArrayRef sizes = torch::IntArrayRef{-1},
59 if (options.
device() == torch::kCPU)
60 return torch::from_blob(
const_cast<T *
>(std::data(array)),
61 (sizes == torch::IntArrayRef{-1}) ? array.size()
66 .requires_grad_(options.requires_grad());
68 return torch::from_blob(
const_cast<T *
>(std::data(array)),
69 (sizes == torch::IntArrayRef{-1}) ? array.size()
71 options.device(
torch::kCPU))
75 .requires_grad_(options.requires_grad());
78template <
typename T, std::
size_t N>
81 if (options.
device() == torch::kCPU)
82 return torch::from_blob(
const_cast<T *
>(std::data(array)), array.size(),
88 return torch::from_blob(
const_cast<T *
>(std::data(array)), array.size(),
89 options.
device(torch::kCPU))
102 torch::IntArrayRef sizes = torch::IntArrayRef{-1},
104 if (options.
device() == torch::kCPU)
105 return torch::from_blob(
106 const_cast<T *
>(std::data(list)),
107 (sizes == torch::IntArrayRef{-1}) ? list.size() : sizes, options)
110 .requires_grad_(options.requires_grad());
112 return torch::from_blob(
const_cast<T *
>(std::data(list)),
113 (sizes == torch::IntArrayRef{-1}) ? list.size()
115 options.device(
torch::kCPU))
118 .to(options.device())
119 .requires_grad_(options.requires_grad());
125 if (options.
device() == torch::kCPU)
126 return torch::from_blob(
const_cast<T *
>(std::data(list)), list.size(),
132 return torch::from_blob(
const_cast<T *
>(std::data(list)), list.size(),
133 options.
device(torch::kCPU))
146 torch::IntArrayRef sizes = torch::IntArrayRef{-1},
148 if (options.
device() == torch::kCPU)
149 return torch::from_blob(
const_cast<T *
>(std::data(vector)),
150 (sizes == torch::IntArrayRef{-1}) ? vector.size()
155 .requires_grad_(options.requires_grad());
157 return torch::from_blob(
const_cast<T *
>(std::data(vector)),
158 (sizes == torch::IntArrayRef{-1}) ? vector.size()
160 options.device(
torch::kCPU))
163 .to(options.device())
164 .requires_grad_(options.requires_grad());
170 if (options.
device() == torch::kCPU)
171 return torch::from_blob(
const_cast<T *
>(std::data(vector)), vector.size(),
177 return torch::from_blob(
const_cast<T *
>(std::data(vector)), vector.size(),
178 options.
device(torch::kCPU))
187template <
typename T, std::
size_t N>
189 return at::ArrayRef<T>{array};
194template <
typename T, std::size_t... N>
195inline auto concat(
const std::array<T, N> &...arrays) {
196 std::array<T, (N + ...)> result;
199 ((std::copy_n(arrays.begin(), N, result.begin() + index), index += N), ...);
204template <
typename T, std::size_t... N>
205inline auto concat(std::array<T, N> &&...arrays) {
206 std::array<T, (N + ...)> result;
209 ((std::copy_n(std::make_move_iterator(arrays.begin()), N,
210 result.begin() + index),
220template <
typename... Ts>
221inline auto concat(
const std::vector<Ts> &...vectors) {
222 std::vector<std::common_type_t<Ts...>> result;
224 (result.insert(result.end(), vectors.begin(), vectors.end()), ...);
229template <
typename... Ts>
inline auto concat(std::vector<Ts> &&...vectors) {
230 std::vector<std::common_type_t<Ts...>> result;
232 (result.insert(result.end(), std::make_move_iterator(vectors.begin()),
233 std::make_move_iterator(vectors.end())),
242inline constexpr auto operator+(torch::ArrayRef<T> array, T data) {
243 std::vector<T> result{array.vec()};
244 result.push_back(data);
249template <
typename T, std::
size_t N>
250inline constexpr auto operator+(std::array<T, N> array, T data) {
251 std::array<T, N + 1> result;
252 for (std::size_t i = 0; i < N; ++i)
253 result[i] = array[i];
260inline constexpr auto operator+(std::vector<T> vector, T data) {
261 std::vector<T> result{vector};
262 result.push_back(data);
268inline constexpr auto operator+(T data, torch::ArrayRef<T> array) {
269 std::vector<T> result{array.vec()};
270 result.insert(result.begin(), data);
275template <
typename T, std::
size_t N>
276inline constexpr auto operator+(T data, std::array<T, N> array) {
277 std::array<T, N + 1> result;
279 for (std::size_t i = 0; i < N; ++i)
280 result[i + 1] = array[i];
286inline constexpr auto operator+(T data, std::vector<T> vector) {
287 std::vector<T> result{vector};
288 result.insert(result.begin(), data);
293template <
typename T, std::
size_t N>
inline constexpr auto make_array(T value) {
294 std::array<T, N> result;
300template <
typename T,
typename U, std::
size_t N>
301inline constexpr std::array<T, N>
make_array(std::array<U, N> array) {
302 std::array<T, N> result;
303 for (std::size_t i = 0; i < N; ++i)
304 result[i] =
static_cast<T
>(array[i]);
309template <
typename T, std::
size_t N>
310inline constexpr std::array<T, N>
operator-(std::array<T, N> array) {
311 std::array<T, N> result;
312 for (std::size_t i = 0; i < N; ++i)
313 result[i] = -array[i];
318template <
typename T, std::
size_t N>
319inline constexpr std::array<T, N>
operator+(std::array<T, N> lhs,
320 std::array<T, N> rhs) {
321 std::array<T, N> result;
322 for (std::size_t i = 0; i < N; ++i)
323 result[i] = lhs[i] + rhs[i];
328template <
typename T, std::
size_t N>
329inline constexpr std::array<T, N>
operator-(std::array<T, N> lhs,
330 std::array<T, N> rhs) {
331 std::array<T, N> result;
332 for (std::size_t i = 0; i < N; ++i)
333 result[i] = lhs[i] - rhs[i];
338template <
typename T, std::
size_t N>
339inline constexpr std::array<T, N>
operator*(std::array<T, N> lhs,
340 std::array<T, N> rhs) {
341 std::array<T, N> result;
342 for (std::size_t i = 0; i < N; ++i)
343 result[i] = lhs[i] * rhs[i];
348template <
typename T, std::
size_t N>
349inline constexpr std::array<T, N>
operator/(std::array<T, N> lhs,
350 std::array<T, N> rhs) {
351 std::array<T, N> result;
352 for (std::size_t i = 0; i < N; ++i)
353 result[i] = lhs[i] / rhs[i];
359template <
typename T, std::
size_t N, std::
size_t M = 1>
360inline constexpr std::array<T, N - M>
363 std::array<T, N - M> result;
364 for (std::size_t i = 0; i < N - M; ++i)
365 result[i] = array[i + M];
371template <
typename T, std::
size_t N, std::
size_t M = 1>
374 std::array<T, N - M> result;
375 for (std::size_t i = 0; i < N - M; ++i)
376 result[i] = array[i];
The Options class handles the automated determination of dtype from the template argument and the sel...
Definition options.hpp:104
torch::Device device() const noexcept
Returns the device property.
Definition options.hpp:144
bool requires_grad() const noexcept
Returns the requires_grad property.
Definition options.hpp:160
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:1677
constexpr auto make_array(T value)
Creates a std::array object filled with a constant.
Definition container.hpp:293
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:56
auto concat(const std::array< T, N > &...arrays)
Concatenates multiple std::array objects.
Definition container.hpp:195
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:361
std::array< T, N > to_array(std::vector< T > &&vector)
Converts a std::vector object into std::array.
Definition container.hpp:27
std::vector< T > to_vector(std::array< T, N > &&array)
Converts a std::array object into std::vector.
Definition container.hpp:35
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:349
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:372
auto to_ArrayRef(const std::array< T, N > &array)
Converts a std::array<int64_t, N> to an at::IntArrayRef object.
Definition container.hpp:188
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:895
constexpr auto operator+(deriv lhs, deriv rhs)
Adds two enumerators for specifying the derivative of B-spline evaluation.
Definition bspline.hpp:89
Definition optimizer.hpp:61