Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Conversion.hpp File Reference

Copying container conversions and dtype casts. More...

#include <algorithm>
#include <cstddef>
#include <vector>
#include <stratax/containers/Matrix.hpp>
#include <stratax/containers/Tensor.hpp>
#include <stratax/containers/Vector.hpp>
#include <stratax/core/ArrayTraits.hpp>
#include <stratax/core/dtypes/Concepts.hpp>
#include <stratax/core/Shape.hpp>
#include <stratax/exceptions/Exceptions.hpp>
Include dependency graph for Conversion.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool stratax::conversion::detail::is_vector_shape (const stratax::core::Shape &shape)
 Determines whether a shape can be represented by a Vector.
 
bool stratax::conversion::detail::is_matrix_shape (const stratax::core::Shape &shape)
 Determines whether a shape can be represented by a Matrix.
 
stratax::core::Shape stratax::conversion::detail::matrix_shape (const stratax::core::Shape &shape)
 Produces the rank-two shape used by a matrix conversion.
 
template<Array A>
stratax::container::Vector< typename A::value_type > stratax::conversion::to_vector (const A &arr)
 Copies a vector-compatible Stratax array into a Vector.
 
template<Array A>
stratax::container::Matrix< typename A::value_type > stratax::conversion::to_matrix (const A &arr)
 Copies a matrix-compatible Stratax array into a Matrix.
 
template<Array A>
stratax::container::Tensor< typename A::value_type > stratax::conversion::to_tensor (const A &arr)
 Copies a Stratax array into a Tensor of the same shape.
 
template<DType To, Array A>
stratax::core::rebind_array_t< A, To > stratax::conversion::astype (const A &arr)
 Casts every element of a Stratax array to another dtype.
 

Detailed Description

Copying container conversions and dtype casts.

Definition in file Conversion.hpp.

Function Documentation

◆ astype()

template<DType To, Array A>
stratax::core::rebind_array_t< A, To > stratax::conversion::astype ( const A &  arr)

Casts every element of a Stratax array to another dtype.

The result preserves the source container category and shape while replacing its element type with To. Each element is converted with static_cast<To>, so the standard C++ rules for narrowing, truncation, signedness conversion, boolean conversion, and precision loss apply.

The returned array owns storage independent of arr.

Template Parameters
ToDestination Stratax dtype for which static_cast from each source value is valid.
AOwning Stratax array with a RebindArray specialization.
Parameters
arrArray whose elements are converted.
Returns
Owning array with the same container category and shape as arr, with element type To.
Exceptions
Exceptions::DimensionErrorIf result shape or stride arithmetic overflows.
std::bad_allocIf result allocation fails.
Complexity
O(arr.size() + arr.rank()).

Definition at line 248 of file Conversion.hpp.

◆ is_matrix_shape()

bool stratax::conversion::detail::is_matrix_shape ( const stratax::core::Shape &  shape)
inline

Determines whether a shape can be represented by a Matrix.

Every rank-two shape is accepted. Shapes of any other rank are accepted only when exactly two dimensions are different from one. Only dimensions of size one are removed; zero extents are retained.

Parameters
shapeShape to inspect.
Returns
true when shape is matrix-compatible; otherwise false.
Complexity
O(shape.rank()).

Definition at line 66 of file Conversion.hpp.

◆ is_vector_shape()

bool stratax::conversion::detail::is_vector_shape ( const stratax::core::Shape &  shape)
inline

Determines whether a shape can be represented by a Vector.

Every rank-one shape is accepted. Higher-rank shapes are accepted only when exactly one dimension is different from one. Only dimensions of size one are removed; zero extents are retained.

Parameters
shapeShape to inspect.
Returns
true when shape is vector-compatible; otherwise false.
Complexity
O(shape.rank()).

Definition at line 34 of file Conversion.hpp.

◆ matrix_shape()

stratax::core::Shape stratax::conversion::detail::matrix_shape ( const stratax::core::Shape &  shape)
inline

Produces the rank-two shape used by a matrix conversion.

Rank-two shapes are returned unchanged. For other ranks, dimensions different from one are retained in their original order while singleton dimensions are removed. Callers must first establish matrix compatibility with is_matrix_shape().

Parameters
shapeMatrix-compatible source shape.
Returns
Two-dimensional shape used by the resulting Matrix.
Exceptions
std::bad_allocIf storage for the filtered dimensions cannot be allocated.
Complexity
O(shape.rank()).

Definition at line 100 of file Conversion.hpp.

◆ to_matrix()

template<Array A>
stratax::container::Matrix< typename A::value_type > stratax::conversion::to_matrix ( const A &  arr)

Copies a matrix-compatible Stratax array into a Matrix.

Rank-two sources retain their shape. For other ranks, singleton dimensions are removed and the two remaining non-singleton dimensions form the result shape. Values retain flat row-major iterator order, and the result owns independent storage.

Template Parameters
AStratax array type satisfying Array.
Parameters
arrSource array to convert.
Returns
Owning Matrix with the same value type as arr.
Exceptions
Exceptions::ShapeErrorIf the source shape is not matrix-compatible.
Exceptions::DimensionErrorIf result shape arithmetic overflows.
std::bad_allocIf result or shape allocation fails.
Complexity
O(arr.size() + arr.rank()) for owning arrays; O((arr.size() + 1) * arr.rank()) for views.

Definition at line 177 of file Conversion.hpp.

◆ to_tensor()

template<Array A>
stratax::container::Tensor< typename A::value_type > stratax::conversion::to_tensor ( const A &  arr)

Copies a Stratax array into a Tensor of the same shape.

Values retain flat row-major iterator order. The returned tensor preserves the source shape exactly and owns storage independent of arr.

Template Parameters
AStratax array type satisfying Array.
Parameters
arrSource array to convert.
Returns
Owning Tensor with the same value type, shape, and values as arr.
Exceptions
Exceptions::DimensionErrorIf result stride arithmetic overflows.
std::bad_allocIf result allocation fails.
Complexity
O(arr.size() + arr.rank()) for owning arrays; O((arr.size() + 1) * arr.rank()) for views.

Definition at line 214 of file Conversion.hpp.

◆ to_vector()

template<Array A>
stratax::container::Vector< typename A::value_type > stratax::conversion::to_vector ( const A &  arr)

Copies a vector-compatible Stratax array into a Vector.

The result contains the source elements in flat row-major iterator order and owns storage independent of arr. Rank-one sources are always compatible; other ranks must contain exactly one dimension different from one.

Template Parameters
AStratax array type satisfying Array.
Parameters
arrSource array to convert.
Returns
Owning Vector with the same value type and element count as arr.
Exceptions
Exceptions::ShapeErrorIf the source shape is not vector-compatible.
std::bad_allocIf result allocation fails.
Complexity
O(arr.size() + arr.rank()) for owning arrays; O((arr.size() + 1) * arr.rank()) for views.

Definition at line 140 of file Conversion.hpp.