Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Reshape and Flatten

Reshape and Flatten

Header: include/stratax/algorithms/Reshape.hpp.

stratax::manipulation::reshape(arr, shape) copies into Tensor with the requested shape. stratax::manipulation::flatten(arr) copies into a rank-one Vector. Both preserve logical row-major order and dtype, including for C++ strided view inputs.

Invariants and failures

Output storage is independent. Reshape requires shape.elements() == arr.size() and throws Exceptions::ShapeError on mismatch. Shape/stride overflow and allocation failures propagate. Dimensions are explicit unsigned extents; there is no inferred -1 axis and no in-place metadata change. Rank-zero output is valid only for an empty source. Flattening any empty input produces shape {0}. Shape-changing views are a separate, unimplemented API.

#include <stratax.h>
#include <cassert>
int main() {
stratax::container::Vector<int> source{0, 1, 2, 3, 4, 5};
auto selected = stratax::indexing::slice(source, stratax::core::Slice{0, 6, 2});
auto tensor = stratax::manipulation::reshape(selected, stratax::core::Shape{1, 3});
assert(tensor(0, 2) == 4);
auto flat = stratax::manipulation::flatten(tensor);
flat[0] = 9;
assert(source[0] == 0 && tensor[0] == 0);
}
stratax::container::Tensor< typename A::value_type > reshape(const A &arr, const stratax::core::Shape &shape)
Copies a Stratax array or view into a tensor with a new shape.
Definition Reshape.hpp:39
stratax::container::Vector< typename A::value_type > flatten(const A &arr)
Copies a Stratax array or view into a rank-one vector.
Definition Reshape.hpp:69
One-dimensional owning array of supported dtype values.
Definition Vector.hpp:47
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:36
Describes a signed, strided half-open index range.
Definition Slice.hpp:33
Umbrella header for the Stratax array library.

For n elements, source rank r, and target rank q, reshape takes O(n + q) on owning inputs and O((n + 1)r + q) on views. Flatten takes O(n) on owning inputs and O((n + 1)r) on views. Both allocate storage for n elements and result metadata. Python exposes reshape and flatten on owning containers, not on ArrayView.

See Conversions and ArrayView.