NumericAL
A header-only C++20 lexer built on REAL
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1
11#ifndef NUMERICAL_VECTOR_HPP
12#define NUMERICAL_VECTOR_HPP
13
14#include <algorithm>
15#include <cmath>
16#include <complex> // so std::abs resolves for a complex scalar, regardless of include order
17#include <cstddef>
18#include <functional>
19#include <initializer_list>
20#include <limits>
21#include <memory>
22#include <numeric>
23#include <stdexcept>
24#include <string>
25#include <type_traits>
26#include <utility>
27#include <vector>
28
29#include "buffer.hpp"
30
31namespace numerical {
32
39 class error
40 {
41 public:
42
43 virtual ~error() = default;
44
46 [[nodiscard]] virtual const char* what() const noexcept = 0;
47
49
55 };
56
57 namespace detail {
59 template <typename Std>
60 class tagged : public Std, public error
61 {
62 public:
63
64 using Std::Std;
65
67 [[nodiscard]] const char* what() const noexcept override
68 {
69 return Std::what();
70 }
71 };
72 } // namespace detail
73
75 class dimension_error : public detail::tagged<std::runtime_error>
76 {
77 public:
78
80 explicit dimension_error(const std::string& message)
81 : tagged(message)
82 {}
83 };
84
86 class argument_error : public detail::tagged<std::invalid_argument>
87 {
88 public:
89
91 explicit argument_error(const std::string& message)
92 : tagged(message)
93 {}
94 };
95
97 class index_error : public detail::tagged<std::out_of_range>
98 {
99 public:
100
102 explicit index_error(const std::string& message)
103 : tagged(message)
104 {}
105 };
106
107 namespace detail {
108
115 [[nodiscard]] constexpr bool range_within(std::size_t first,
116 std::size_t count,
117 std::ptrdiff_t step,
118 std::size_t extent) noexcept
119 {
120 if (count == 0) {
121 return true;
122 }
123 if (first >= extent) {
124 return false;
125 }
126 const std::size_t room {step >= 0 ? extent - 1 - first : first}; // the indices past first on the step's side
127 const std::size_t magnitude {step >= 0 ? static_cast<std::size_t>(step) : std::size_t {0} - static_cast<std::size_t>(step)};
128 return magnitude == 0 || count - 1 <= room / magnitude;
129 }
130
135 [[nodiscard]] inline std::size_t checked_mul(std::size_t a,
136 std::size_t b)
137 {
138 if (a != 0 && b > (std::numeric_limits<std::size_t>::max() / a)) {
139 throw dimension_error("dimension product overflows std::size_t");
140 }
141 return a * b;
142 }
143
150 template <typename It>
151 [[nodiscard]] std::size_t element_count(It first,
152 It last)
153 {
154 if (std::find(first, last, std::size_t {0}) != last) {
155 return 0;
156 }
157 std::size_t count {1};
158 for (; first != last; ++first) {
159 count = checked_mul(count, *first);
160 }
161 return count;
162 }
163 } // namespace detail
164
169 template <typename T>
170 class vector
171 {
172 public:
173
175 vector() = default;
176
178 vector(std::initializer_list<T> elements)
179 : data_(elements)
180 {}
181
183 explicit vector(buffer<T> elements)
184 : data_(std::move(elements))
185 {}
186
189 explicit vector(const std::vector<T>& elements)
190 : data_(elements.begin(), elements.end())
191 {}
192
194 [[nodiscard]] std::size_t size() const noexcept
195 {
196 return data_.size();
197 }
198
200 [[nodiscard]] const T& operator[](std::size_t index) const
201 {
202 return data_[index];
203 }
204
206 [[nodiscard]] typename buffer<T>::const_iterator begin() const noexcept
207 {
208 return data_.begin();
209 }
210
212 [[nodiscard]] typename buffer<T>::const_iterator end() const noexcept
213 {
214 return data_.end();
215 }
216
218 [[nodiscard]] bool operator==(const vector& other) const
219 {
220 return data_ == other.data_;
221 }
222
223 private:
224
226 };
227
229 template <typename T>
231 const vector<T>& b)
232 {
233 if (a.size() != b.size()) {
234 throw dimension_error("vector sizes differ in '+'");
235 }
236 buffer<T> out(a.size());
237 std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::plus<T> {});
238 return vector<T> {std::move(out)};
239 }
240
242 template <typename T>
244 const vector<T>& b)
245 {
246 if (a.size() != b.size()) {
247 throw dimension_error("vector sizes differ in '-'");
248 }
249 buffer<T> out(a.size());
250 std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::minus<T> {});
251 return vector<T> {std::move(out)};
252 }
253
255 template <typename T>
256 [[nodiscard]] vector<T> operator*(const T& scalar,
257 const vector<T>& v)
258 {
259 buffer<T> out(v.size());
260 std::transform(v.begin(), v.end(), out.begin(),
261 [&scalar](const T& element) { return scalar * element; });
262 return vector<T> {std::move(out)};
263 }
264
266 template <typename T>
268 const T& scalar)
269 {
270 return scalar * v;
271 }
272
274 template <typename T>
275 [[nodiscard]] T dot(const vector<T>& a,
276 const vector<T>& b)
277 {
278 if (a.size() != b.size()) {
279 throw dimension_error("vector sizes differ in 'dot'");
280 }
281 // std::inner_product is sequential (left-to-right), so the reduction order —
282 // and thus the floating-point result — stays deterministic.
283 return std::inner_product(a.begin(), a.end(), b.begin(), T {});
284 }
285
293 template <typename T>
295 const vector<T>& b,
296 double tolerance = 1e-9)
297 {
298 if (a.size() != b.size()) {
299 return false;
300 }
301 for (std::size_t i {0}; i < a.size(); ++i) {
302 // Equal, or within the tolerance: a NaN is neither, so it is never close, and two equal
303 // infinities are equal although their difference is NaN.
304 if (!(a[i] == b[i]) && !(std::abs(a[i] - b[i]) <= tolerance)) {
305 return false;
306 }
307 }
308 return true;
309 }
310} // namespace numerical
311
312#endif // NUMERICAL_VECTOR_HPP
numerical::buffer, the storage of the library's vectors, matrices and tensors: a std::vector whose el...
Thrown when an argument is outside what an operation takes (a tolerance below 0, an FFT type past 4).
Definition vector.hpp:87
argument_error(const std::string &message)
Builds the error.
Definition vector.hpp:91
A standard exception Std that numerical::error tags too, its one what answering for both bases.
Definition vector.hpp:61
const char * what() const noexcept override
The message.
Definition vector.hpp:67
Thrown when an operation's operands have incompatible shapes.
Definition vector.hpp:76
dimension_error(const std::string &message)
Builds the error.
Definition vector.hpp:80
What every exception NumericAL throws is, beside its standard base (all but std::bad_alloc and std::s...
Definition vector.hpp:40
virtual const char * what() const noexcept=0
The message, as std::exception::what gives it.
virtual ~error()=default
Thrown when an index or a range leaves its axis.
Definition vector.hpp:98
index_error(const std::string &message)
Builds the error.
Definition vector.hpp:102
A dense vector of T with value semantics.
Definition vector.hpp:171
vector(buffer< T > elements)
From an existing buffer.
Definition vector.hpp:183
buffer< T >::const_iterator end() const noexcept
Const iterator past the last element.
Definition vector.hpp:212
const T & operator[](std::size_t index) const
The element at index.
Definition vector.hpp:200
vector(const std::vector< T > &elements)
From a std::vector, copied into the storage (a buffer moves in instead).
Definition vector.hpp:189
bool operator==(const vector &other) const
Exact element-wise equality (same size, equal elements).
Definition vector.hpp:218
buffer< T > data_
The elements.
Definition vector.hpp:225
std::size_t size() const noexcept
The number of elements.
Definition vector.hpp:194
vector(std::initializer_list< T > elements)
From a braced list of elements.
Definition vector.hpp:178
buffer< T >::const_iterator begin() const noexcept
Const iterator to the first element.
Definition vector.hpp:206
vector()=default
An empty vector (size 0).
constexpr bool range_within(std::size_t first, std::size_t count, std::ptrdiff_t step, std::size_t extent) noexcept
Whether count indices from first, step apart, all lie in [0, extent): none is out for a count of 0....
Definition vector.hpp:115
std::size_t checked_mul(std::size_t a, std::size_t b)
a * b, throwing numerical::dimension_error if the product overflows std::size_t. Guards dimension/sha...
Definition vector.hpp:135
std::size_t element_count(It first, It last)
The elements a shape's extents [first, last) hold: 0 where one of them is 0, whatever the others,...
Definition vector.hpp:151
The NumericAL public API.
Definition amx.hpp:44
std::vector< T, detail::default_init_allocator< T > > buffer
The storage of vector, matrix and tensor, namely a std::vector whose buffer<T>(n) and resize(n) leave...
Definition buffer.hpp:443
matrix< T > operator+(const matrix< T > &a, const matrix< T > &b)
Element-wise sum.
Definition matrix.hpp:162
T dot(const vector< T > &a, const vector< T > &b)
Dot product.
Definition vector.hpp:275
matrix< T > operator*(const T &scalar, const matrix< T > &m)
Scalar times matrix.
Definition matrix.hpp:188
@ amx
The Apple AMX coprocessor, fused multiply-adds; the products it does not suit on the NEON path's tile...
matrix< T > operator-(const matrix< T > &a, const matrix< T > &b)
Element-wise difference.
Definition matrix.hpp:175
bool approx_equal(const matrix< T > &a, const matrix< T > &b, double tolerance=1e-9)
Element-wise closeness within a tolerance (for inexact scalars).
Definition matrix.hpp:276