Stratax
Scientific computing containers and operations
Loading...
Searching...
No Matches
Conversions.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4
12
23template<Array A>
25to_vector(const A& arr)
26{
28 arr,
29 1,
30 "Cannot convert non-rank-1 array to Vector");
31
33
34 for (std::size_t i = 0; i < arr.size(); ++i)
35 {
36 result[i] = arr[i];
37 }
38
39 return result;
40}
41
52template<Array A>
54to_matrix(const A& arr)
55{
57 arr,
58 2,
59 "Cannot convert non-rank-2 array to Matrix");
60
62
63 for (std::size_t i = 0; i < arr.size(); ++i)
64 {
65 result[i] = arr[i];
66 }
67
68 return result;
69}
70
79template<Array A>
81to_tensor(const A& arr)
82{
84
85 for (std::size_t i = 0; i < arr.size(); ++i)
86 {
87 result[i] = arr[i];
88 }
89
90 return result;
91}
92
102template<typename To, typename From>
103requires Numeric<To> && Numeric<From>
106{
108
109 for (std::size_t i = 0; i < vec.size(); ++i)
110 {
111 result[i] = static_cast<To>(vec[i]);
112 }
113
114 return result;
115}
116
126template<typename To, typename From>
127requires Numeric<To> && Numeric<From>
130{
132
133 for (std::size_t i = 0; i < mat.size(); ++i)
134 {
135 result[i] = static_cast<To>(mat[i]);
136 }
137
138 return result;
139}
140
150template<typename To, typename From>
151requires Numeric<To> && Numeric<From>
154{
155 stratax::container::Tensor<To> result(tensor.shape());
156
157 for (std::size_t i = 0; i < tensor.size(); ++i)
158 {
159 result[i] = static_cast<To>(tensor[i]);
160 }
161
162 return result;
163}
stratax::container::Tensor< typename A::value_type > to_tensor(const A &arr)
Converts an array-like object to a tensor.
stratax::container::Vector< To > astype(const stratax::container::Vector< From > &vec)
Casts a vector to a different numeric value type.
stratax::container::Matrix< typename A::value_type > to_matrix(const A &arr)
Converts an array-like object to a matrix.
stratax::container::Vector< typename A::value_type > to_vector(const A &arr)
Converts an array-like object to a vector.
Shared runtime validation helpers.
const stratax::core::Shape & shape() const noexcept
Returns the matrix shape.
Definition Matrix.hpp:201
std::size_t size() const noexcept
Returns the total number of elements in the matrix.
Definition Matrix.hpp:161
const core::Shape & shape() const noexcept
Returns the tensor shape.
Definition Tensor.hpp:144
std::size_t size() const noexcept
Returns the total number of elements in the tensor.
Definition Tensor.hpp:114
const stratax::core::Shape & shape() const noexcept
Returns the vector shape.
Definition Vector.hpp:291
std::size_t size() const noexcept
Returns the number of stored elements.
Definition Vector.hpp:261
Matches all scalar types supported by Stratax numeric containers.
Definition Concepts.hpp:152
void require_rank(std::size_t actual, std::size_t expected, const char *message)
Requires a rank value to match an expected rank.