Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Conversions

Conversions

Header: include/stratax/algorithms/Conversion.hpp. Functions live under stratax::conversion.

Shape contracts

Function Accepted shape Result
to_vector(arr) Rank one, or exactly one extent different from one Vector with source element count
to_matrix(arr) Rank two, or exactly two extents different from one Matrix with original rank-two shape or the two retained extents
to_tensor(arr) Any valid source shape Tensor with identical shape

Each conversion copies values in logical row-major order into independent storage, including for strided C++ views. Only singleton extents are removed: {1, 0, 3} converts to Matrix {0, 3}. A higher-rank shape containing only ones is not vector- or matrix-compatible under these rules. Rank-zero arrays cannot convert to Vector/Matrix through these helpers; flatten provides a Vector copy. Invalid conversion shapes raise Exceptions::ShapeError. Shape arithmetic and allocation errors propagate.

Dtype casts

astype<To>(arr) preserves the owning container family and shape and converts each element with static_cast<To>. Both dtypes must be supported and the cast must be valid. Narrowing and precision loss are not checked. The supplied RebindArray specializations cover owning containers, not ArrayView. Python does not currently bind astype.

Example

#include <stratax.h>
#include <cassert>
int main() {
auto vector = stratax::conversion::to_vector(source);
auto ints = stratax::conversion::astype<int>(vector);
assert(vector.size() == 3 && ints[0] == 2);
vector[0] = 8.0;
assert(source[0] == 2.5);
assert(stratax::conversion::to_matrix(empty).shape() == stratax::core::Shape({0, 3}));
}
Arbitrary-rank owning array of supported dtype values.
Definition Tensor.hpp:54
size_type size() const noexcept
Returns the number of stored elements.
Definition ArrayBase.hpp:73
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:36
Umbrella header for the Stratax array library.

Cost

Owning inputs take O(n + r) time; view inputs take O((n + 1)r), because logical access computes strided offsets. Output storage is O(n + r). Shape compatibility helpers in conversion::detail are implementation details. Python conversion functions accept the owning double Vector, Matrix, and Tensor classes; see Python API Reference.