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]]
34stratax::container::Tensor<typename A::value_type>
35reshape(const A& arr, const stratax::core::Shape& shape)
36{
37 if (arr.size() != shape.elements())
38 {
39 throw Exceptions::ShapeError(
40 "Reshape size must match original array size.");
41 }
42
43 stratax::container::Tensor<typename A::value_type> result(shape);
44 std::copy(arr.begin(), arr.end(), result.begin());
45
46 return result;
47}
48
61template<Array A>
62[[nodiscard]]
63stratax::container::Vector<typename A::value_type>
64flatten(const A& arr)
65{
66 stratax::container::Vector<typename A::value_type> result(arr.size());
67 std::copy(arr.begin(), arr.end(), result.begin());
68
69 return result;
70}
71
72} // namespace stratax::manipulation
size_type elements() const
Computes the total number of elements described by the shape.
Definition Shape.hpp:91