Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Conversion.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <algorithm>
7#include <cstddef>
8#include <vector>
9
17
18namespace stratax::conversion {
19
20namespace detail {
21
34inline bool is_vector_shape(const stratax::core::Shape& shape)
35{
36 if (shape.rank() == 1)
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 == 1;
52}
53
66inline bool is_matrix_shape(const stratax::core::Shape& shape)
67{
68 if (shape.rank() == 2)
69 {
70 return true;
71 }
72
73 std::size_t non_singleton = 0;
74
75 for (std::size_t dim : shape)
76 {
77 if (dim != 1)
78 {
79 ++non_singleton;
80 }
81 }
82
83 return non_singleton == 2;
84}
85
101{
102 if (shape.rank() == 2)
103 {
104 return shape;
105 }
106
107 std::vector<std::size_t> dims;
108 dims.reserve(2);
109
110 for (std::size_t dim : shape)
111 {
112 if (dim != 1)
113 {
114 dims.push_back(dim);
115 }
116 }
117
118 return stratax::core::Shape(dims);
119}
120
121} // namespace detail
122
137template<Array A>
138[[nodiscard]]
140to_vector(const A& arr)
141{
142 if (!detail::is_vector_shape(arr.shape()))
143 {
145 "Array shape cannot be converted to a Vector.");
146 }
147
149
150 std::copy(
151 arr.begin(),
152 arr.end(),
153 result.begin());
154
155 return result;
156}
157
174template<Array A>
175[[nodiscard]]
177to_matrix(const A& arr)
178{
179 if (!detail::is_matrix_shape(arr.shape()))
180 {
182 "Array shape cannot be converted to a Matrix.");
183 }
184
185 const stratax::core::Shape shape =
186 detail::matrix_shape(arr.shape());
187
189
190 std::copy(
191 arr.begin(),
192 arr.end(),
193 result.begin());
194
195 return result;
196}
197
211template<Array A>
212[[nodiscard]]
214to_tensor(const A& arr)
215{
217
218 std::copy(
219 arr.begin(),
220 arr.end(),
221 result.begin());
222
223 return result;
224}
225
245template<DType To, Array A>
246[[nodiscard]]
248astype(const A& arr)
249{
250 using result_type =
252
253 result_type result(arr.shape());
254
255 std::transform(
256 arr.begin(),
257 arr.end(),
258 result.begin(),
259 [](const auto& value)
260 {
261 return static_cast<To>(value);
262 });
263
264 return result;
265}
266
267} // namespace stratax::conversion
Result-container rebinding and promotion for owning arrays.
typename RebindArray< std::remove_cvref_t< A >, std::remove_cvref_t< T > >::type rebind_array_t
Convenience alias for rebinding an array to a new value type.
Dtype and array interface concepts.
bool is_matrix_shape(const stratax::core::Shape &shape)
Determines whether a shape can be represented by a Matrix.
stratax::container::Matrix< typename A::value_type > to_matrix(const A &arr)
Copies a matrix-compatible Stratax array into a Matrix.
stratax::container::Vector< typename A::value_type > to_vector(const A &arr)
Copies a vector-compatible Stratax array into a Vector.
stratax::core::Shape matrix_shape(const stratax::core::Shape &shape)
Produces the rank-two shape used by a matrix conversion.
stratax::container::Tensor< typename A::value_type > to_tensor(const A &arr)
Copies a Stratax array into a Tensor of the same shape.
stratax::core::rebind_array_t< A, To > astype(const A &arr)
Casts every element of a Stratax array to another dtype.
bool is_vector_shape(const stratax::core::Shape &shape)
Determines whether a shape can be represented by a Vector.
Stratax exception types derived from std::runtime_error.
Rank-two owning arrays.
Immutable dimension metadata and checked row-major strides.
Arbitrary-rank owning arrays; rank zero is empty.
Rank-one owning arrays.
Shape incompatible with the requested operation or container.
Two-dimensional owning array of supported dtype values.
Definition Matrix.hpp:49
Arbitrary-rank owning array of supported dtype values.
Definition Tensor.hpp:54
One-dimensional owning array of supported dtype values.
Definition Vector.hpp:47
iterator begin() noexcept
Returns a mutable iterator to the first element.
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:36
size_type rank() const noexcept
Returns the number of dimensions.
Definition Shape.hpp:124