Stratax
Scientific computing containers and operations
Toggle main menu visibility
Loading...
Searching...
No Matches
Conversions.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <cstddef>
4
5
#include <
stratax/containers/Matrix.hpp
>
6
#include <
stratax/containers/Tensor.hpp
>
7
#include <
stratax/containers/Vector.hpp
>
8
#include <
stratax/core/Concepts.hpp
>
9
#include <
stratax/core/Exceptions.hpp
>
10
#include <
stratax/core/Shape.hpp
>
11
#include <
stratax/core/Validation.hpp
>
12
23
template
<Array A>
24
stratax::container::Vector<typename A::value_type>
25
to_vector
(
const
A& arr)
26
{
27
stratax::core::validation::require_rank
(
28
arr,
29
1,
30
"Cannot convert non-rank-1 array to Vector"
);
31
32
stratax::container::Vector<typename A::value_type>
result(arr.shape());
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
52
template
<Array A>
53
stratax::container::Matrix<typename A::value_type>
54
to_matrix
(
const
A& arr)
55
{
56
stratax::core::validation::require_rank
(
57
arr,
58
2,
59
"Cannot convert non-rank-2 array to Matrix"
);
60
61
stratax::container::Matrix<typename A::value_type>
result(arr.shape());
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
79
template
<Array A>
80
stratax::container::Tensor<typename A::value_type>
81
to_tensor
(
const
A& arr)
82
{
83
stratax::container::Tensor<typename A::value_type>
result(arr.shape());
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
102
template
<
typename
To,
typename
From>
103
requires
Numeric<To>
&&
Numeric<From>
104
stratax::container::Vector<To>
105
astype
(
const
stratax::container::Vector<From>
& vec)
106
{
107
stratax::container::Vector<To>
result(vec.
shape
());
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
126
template
<
typename
To,
typename
From>
127
requires
Numeric<To>
&&
Numeric<From>
128
stratax::container::Matrix<To>
129
astype
(
const
stratax::container::Matrix<From>
& mat)
130
{
131
stratax::container::Matrix<To>
result(mat.
shape
());
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
150
template
<
typename
To,
typename
From>
151
requires
Numeric<To>
&&
Numeric<From>
152
stratax::container::Tensor<To>
153
astype
(
const
stratax::container::Tensor<From>
& tensor)
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
}
Concepts.hpp
to_tensor
stratax::container::Tensor< typename A::value_type > to_tensor(const A &arr)
Converts an array-like object to a tensor.
Definition
Conversions.hpp:81
astype
stratax::container::Vector< To > astype(const stratax::container::Vector< From > &vec)
Casts a vector to a different numeric value type.
Definition
Conversions.hpp:105
to_matrix
stratax::container::Matrix< typename A::value_type > to_matrix(const A &arr)
Converts an array-like object to a matrix.
Definition
Conversions.hpp:54
to_vector
stratax::container::Vector< typename A::value_type > to_vector(const A &arr)
Converts an array-like object to a vector.
Definition
Conversions.hpp:25
Exceptions.hpp
Matrix.hpp
Shape.hpp
Tensor.hpp
Validation.hpp
Shared runtime validation helpers.
Vector.hpp
stratax::container::Matrix
Definition
Matrix.hpp:29
stratax::container::Matrix::shape
const stratax::core::Shape & shape() const noexcept
Returns the matrix shape.
Definition
Matrix.hpp:201
stratax::container::Matrix::size
std::size_t size() const noexcept
Returns the total number of elements in the matrix.
Definition
Matrix.hpp:161
stratax::container::Tensor
Definition
Tensor.hpp:30
stratax::container::Tensor::shape
const core::Shape & shape() const noexcept
Returns the tensor shape.
Definition
Tensor.hpp:144
stratax::container::Tensor::size
std::size_t size() const noexcept
Returns the total number of elements in the tensor.
Definition
Tensor.hpp:114
stratax::container::Vector
Definition
Vector.hpp:27
stratax::container::Vector::shape
const stratax::core::Shape & shape() const noexcept
Returns the vector shape.
Definition
Vector.hpp:291
stratax::container::Vector::size
std::size_t size() const noexcept
Returns the number of stored elements.
Definition
Vector.hpp:261
Numeric
Matches all scalar types supported by Stratax numeric containers.
Definition
Concepts.hpp:152
stratax::core::validation::require_rank
void require_rank(std::size_t actual, std::size_t expected, const char *message)
Requires a rank value to match an expected rank.
Definition
Validation.hpp:177
include
stratax
containers
Conversions.hpp
Generated by
1.17.0