Stratax 0.3.1
Loading...
Searching...
No Matches
Reshape.hpp
1// TODO: Consider zero-copy reshape/flatten views once view support is implemented.
2
3#pragma once
4
5#include <algorithm>
6
7#include <stratax/core/dtypes/Concepts.hpp>
8#include <stratax/core/Shape.hpp>
9#include <stratax/exceptions/Exceptions.hpp>
10#include <stratax/containers/Tensor.hpp>
11#include <stratax/containers/Vector.hpp>
12
13namespace stratax::manipulation {
14
32template<Array A>
33[[nodiscard]]
35reshape(const A& arr, const stratax::core::Shape& shape)
36{
37 const auto target_size = shape.elements();
38 if (arr.size() != target_size)
39 {
41 "Reshape must preserve the number of elements.");
42 }
43
45 std::copy(arr.begin(), arr.end(), result.begin());
46
47 return result;
48}
49
62template<Array A>
63[[nodiscard]]
65flatten(const A& arr)
66{
68 std::copy(arr.begin(), arr.end(), result.begin());
69
70 return result;
71}
72
73} // namespace stratax::manipulation
Arbitrary-rank owning array of numeric values.
Definition Tensor.hpp:50
One-dimensional owning array of numeric values.
Definition Vector.hpp:44
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:33
size_type elements() const
Computes the total number of elements described by the shape.
Definition Shape.hpp:91