Stratax 0.2.0
Loading...
Searching...
No Matches
Conversions.hpp
1#pragma once
2
3#include <cstddef>
4
5#include <stratax/core/containers/Matrix.hpp>
6#include <stratax/core/containers/Tensor.hpp>
7#include <stratax/core/containers/Vector.hpp>
8#include <stratax/core/Concepts.hpp>
9#include <stratax/core/Exceptions.hpp>
10#include <stratax/core/containers/Shape.hpp>
12
13
14inline bool is_vector_shape(const stratax::core::Shape& shape)
15{
16 if (shape.rank() == 1)
17 {
18 return true;
19 }
20
21 std::size_t non_singleton = 0;
22
23 for (std::size_t dim : shape)
24 {
25 if (dim > 1)
26 {
27 ++non_singleton;
28 }
29 }
30
31 return non_singleton == 1;
32}
33
34inline bool is_matrix_shape(const stratax::core::Shape& shape)
35{
36 if (shape.rank() == 2)
37 {
38 return true;
39 }
40
41 std::size_t non_singleton = 0;
42
43 for (std::size_t dim : shape)
44 {
45 if (dim > 1)
46 {
47 ++non_singleton;
48 }
49 }
50
51 return non_singleton == 2;
52}
53
54inline stratax::core::Shape matrix_shape(const stratax::core::Shape& shape)
55{
56 if (shape.rank() == 2)
57 {
58 return shape;
59 }
60
61 std::vector<std::size_t> dims;
62 dims.reserve(shape.rank());
63
64 for (std::size_t dim : shape)
65 {
66 if (dim > 1)
67 {
68 dims.push_back(dim);
69 }
70 }
71
72 return stratax::core::Shape(dims);
73}
74
85template<Array A>
87to_vector(const A& arr)
88{
89 if (!is_vector_shape(arr.shape()))
90 {
92 "Array cannot be converted to a Vector."
93 );
94 }
95
97
98 for (std::size_t i = 0; i < arr.size(); ++i)
99 {
100 result[i] = arr[i];
101 }
102
103 return result;
104}
105
116template<Array A>
118to_matrix(const A& arr)
119{
120 if (!is_matrix_shape(arr.shape()))
121 {
123 "Array cannot be converted to a Matrix.");
124 }
125
126 const auto shape = matrix_shape(arr.shape());
128
129 for (std::size_t i = 0; i < arr.size(); ++i)
130 {
131 result[i] = arr[i];
132 }
133
134 return result;
135}
136
145template<Array A>
147to_tensor(const A& arr)
148{
150
151 for (std::size_t i = 0; i < arr.size(); ++i)
152 {
153 result[i] = arr[i];
154 }
155
156 return result;
157}
158
168template<typename To, typename From>
169requires Numeric<To> && Numeric<From>
171astype(const stratax::container::Vector<From>& vec)
172{
174
175 for (std::size_t i = 0; i < vec.size(); ++i)
176 {
177 result[i] = static_cast<To>(vec[i]);
178 }
179
180 return result;
181}
182
192template<typename To, typename From>
193requires Numeric<To> && Numeric<From>
195astype(const stratax::container::Matrix<From>& mat)
196{
198
199 for (std::size_t i = 0; i < mat.size(); ++i)
200 {
201 result[i] = static_cast<To>(mat[i]);
202 }
203
204 return result;
205}
206
216template<typename To, typename From>
217requires Numeric<To> && Numeric<From>
219astype(const stratax::container::Tensor<From>& tensor)
220{
221 stratax::container::Tensor<To> result(tensor.shape());
222
223 for (std::size_t i = 0; i < tensor.size(); ++i)
224 {
225 result[i] = static_cast<To>(tensor[i]);
226 }
227
228 return result;
229}
230
Shared runtime validation helpers.
Signals an invalid or incompatible shape.
Stores a rank-2 Stratax array in row-major order.
Definition Matrix.hpp:29
const stratax::core::Shape & shape() const noexcept
Returns the matrix shape.
Definition Matrix.hpp:215
std::size_t size() const noexcept
Returns the total number of elements in the matrix.
Definition Matrix.hpp:175
Stores an N-dimensional Stratax array in contiguous memory.
Definition Tensor.hpp:31
const core::Shape & shape() const noexcept
Returns the tensor shape.
Definition Tensor.hpp:159
std::size_t size() const noexcept
Returns the total number of elements in the tensor.
Definition Tensor.hpp:129
Stores a rank-1 Stratax array in contiguous memory.
Definition Vector.hpp:27
const stratax::core::Shape & shape() const noexcept
Returns the vector shape.
Definition Vector.hpp:307
std::size_t size() const noexcept
Returns the number of stored elements.
Definition Vector.hpp:277
Stores a list of dimension lengths for an array shape.
Definition Shape.hpp:22
std::size_t rank() const
Returns the number of stored dimensions.
Definition Shape.hpp:176
Matches all scalar types supported by Stratax numeric containers.
Definition Concepts.hpp:152