NumericAL
A header-only C++20 lexer built on REAL
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1
11#ifndef NUMERICAL_MATRIX_HPP
12#define NUMERICAL_MATRIX_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 <memory>
21#include <utility>
22#include <vector>
23
24#include "gemm.hpp"
25#include "parallel.hpp"
26#include "vector.hpp"
27
28namespace numerical {
29
34 template <typename T>
35 class matrix
36 {
37 public:
38
40 matrix() = default;
41
47 matrix(std::initializer_list<std::initializer_list<T>> rows)
48 : rows_(rows.size())
49 {
50 std::size_t r {0};
51 for (const std::initializer_list<T>& row : rows) {
52 if (r == 0) {
53 cols_ = row.size();
54 }
55 else if (row.size() != cols_) {
56 throw dimension_error("matrix rows have unequal lengths");
57 }
58 data_.insert(data_.end(), row.begin(), row.end());
59 ++r;
60 }
61 }
62
69 explicit matrix(std::size_t rows,
70 std::size_t cols,
73 {
74 if (data_.size() != detail::checked_mul(rows_, cols_)) {
75 throw dimension_error("matrix buffer size does not match dimensions");
76 }
77 }
78
85 explicit matrix(std::size_t rows,
86 std::size_t cols,
87 const std::vector<T>& elements)
88 : matrix(rows,
89 cols,
91 {}
92
99 explicit matrix(std::size_t rows,
100 std::size_t cols,
101 std::initializer_list<T> elements)
102 : matrix(rows,
103 cols,
104 buffer<T>(elements))
105 {}
106
108 [[nodiscard]] std::size_t rows() const noexcept
109 {
110 return rows_;
111 }
112
114 [[nodiscard]] std::size_t cols() const noexcept
115 {
116 return cols_;
117 }
118
120 [[nodiscard]] const T& operator()(std::size_t row,
121 std::size_t col) const
122 {
123 return data_[(row * cols_) + col];
124 }
125
127 [[nodiscard]] bool operator==(const matrix& other) const
128 {
129 return rows_ == other.rows_ && cols_ == other.cols_ && data_ == other.data_;
130 }
131
134 {
135 return data_.begin();
136 }
137
140 {
141 return data_.end();
142 }
143
147 {
148 rows_ = 0;
149 cols_ = 0;
150 return std::move(data_);
151 }
152
153 private:
154
155 std::size_t rows_ {0};
156 std::size_t cols_ {0};
158 };
159
161 template <typename T>
163 const matrix<T>& b)
164 {
165 if (a.rows() != b.rows() || a.cols() != b.cols()) {
166 throw dimension_error("matrix shapes differ in '+'");
167 }
168 buffer<T> out(a.rows() * a.cols());
169 std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::plus<T> {});
170 return matrix<T> {a.rows(), a.cols(), std::move(out)};
171 }
172
174 template <typename T>
176 const matrix<T>& b)
177 {
178 if (a.rows() != b.rows() || a.cols() != b.cols()) {
179 throw dimension_error("matrix shapes differ in '-'");
180 }
181 buffer<T> out(a.rows() * a.cols());
182 std::transform(a.begin(), a.end(), b.begin(), out.begin(), std::minus<T> {});
183 return matrix<T> {a.rows(), a.cols(), std::move(out)};
184 }
185
187 template <typename T>
188 [[nodiscard]] matrix<T> operator*(const T& scalar,
189 const matrix<T>& m)
190 {
191 buffer<T> out(m.rows() * m.cols());
192 std::transform(m.begin(), m.end(), out.begin(),
193 [&scalar](const T& element) { return scalar * element; });
194 return matrix<T> {m.rows(), m.cols(), std::move(out)};
195 }
196
198 template <typename T>
200 const T& scalar)
201 {
202 return scalar * m;
203 }
204
207 template <typename T>
209 const vector<T>& v)
210 {
211 if (a.cols() != v.size()) {
212 throw dimension_error("matrix–vector product: inner dimensions differ");
213 }
214 buffer<T> out(a.rows());
215 if (a.cols() == 0) {
216 std::fill(out.begin(), out.end(), T {}); // each element an empty sum
217 }
218 else {
219 detail::matrix_times_vector(&*a.begin(), a.cols(), a.rows(), a.cols(), &v[0], out.data());
220 }
221 return vector<T> {std::move(out)};
222 }
223
225 template <typename T>
227 const matrix<T>& b)
228 {
229 if (a.cols() != b.rows()) {
230 throw dimension_error("matrix–matrix product: inner dimensions differ");
231 }
232 // detail::gemm sums every element in ascending order of the inner index, whatever the blocking and the number of
233 // threads (see gemm.hpp), from zero: each part of `out` zeroed by the thread that computes it. An empty operand
234 // gives its address, which a product of an empty dimension never reads.
235 buffer<T> out(detail::checked_mul(a.rows(), b.cols())); // guard the cross-product alloc
236 detail::gemm(a.rows(), b.cols(), a.cols(), std::to_address(a.begin()), a.cols(), std::to_address(b.begin()), b.cols(), out.data(),
238 return matrix<T> {a.rows(), b.cols(), std::move(out)};
239 }
240
243 inline constexpr std::size_t transpose_tile {32};
244
250 template <typename T>
252 {
253 const std::size_t rows {m.rows()};
254 const std::size_t cols {m.cols()};
255 buffer<T> out(rows * cols);
256 const std::size_t piece {detail::transpose_piece(cols * sizeof(T), rows * sizeof(T))};
257 detail::parallel_for((cols + transpose_tile - 1) / transpose_tile, transpose_tile * rows, [&](std::size_t block) {
258 const std::size_t j0 {block * transpose_tile};
259 const std::size_t j1 {std::min(j0 + transpose_tile, cols)};
260 for (std::size_t i0 {0}; i0 < rows; i0 += transpose_tile) {
261 detail::transpose_walk(j0, j1, i0, std::min(i0 + transpose_tile, rows), piece,
262 [&](std::size_t j, std::size_t i) { out[(j * rows) + i] = m(i, j); });
263 }
264 });
265 return matrix<T> {cols, rows, std::move(out)};
266 }
267
275 template <typename T>
277 const matrix<T>& b,
278 double tolerance = 1e-9)
279 {
280 if (a.rows() != b.rows() || a.cols() != b.cols()) {
281 return false;
282 }
283 for (std::size_t i {0}; i < a.rows(); ++i) {
284 for (std::size_t j {0}; j < a.cols(); ++j) {
285 // Equal, or within the tolerance: a NaN is neither, so it is never close, and two equal
286 // infinities are equal although their difference is NaN.
287 if (!(a(i, j) == b(i, j)) && !(std::abs(a(i, j) - b(i, j)) <= tolerance)) {
288 return false;
289 }
290 }
291 }
292 return true;
293 }
294} // namespace numerical
295
296#endif // NUMERICAL_MATRIX_HPP
Thrown when an operation's operands have incompatible shapes.
Definition vector.hpp:76
A dense, row-major matrix of T with value semantics.
Definition matrix.hpp:36
bool operator==(const matrix &other) const
Exact equality (same shape and equal elements).
Definition matrix.hpp:127
matrix()=default
An empty matrix (0×0).
buffer< T >::const_iterator end() const noexcept
Const iterator past the last element.
Definition matrix.hpp:139
matrix(std::size_t rows, std::size_t cols, buffer< T > elements)
From dimensions and a flat row-major buffer.
Definition matrix.hpp:69
matrix(std::initializer_list< std::initializer_list< T > > rows)
From a braced list of rows; every row must have the same length.
Definition matrix.hpp:47
buffer< T >::const_iterator begin() const noexcept
Const iterator to the first element (row-major).
Definition matrix.hpp:133
buffer< T > data_
The elements, row-major.
Definition matrix.hpp:157
std::size_t rows() const noexcept
The number of rows.
Definition matrix.hpp:108
std::size_t rows_
The number of rows.
Definition matrix.hpp:155
buffer< T > elements() &&
The elements, row-major, handed over by a matrix about to expire, which is left 0 × 0: a large result...
Definition matrix.hpp:146
const T & operator()(std::size_t row, std::size_t col) const
The element at (row, col).
Definition matrix.hpp:120
std::size_t cols_
The number of columns.
Definition matrix.hpp:156
std::size_t cols() const noexcept
The number of columns.
Definition matrix.hpp:114
matrix(std::size_t rows, std::size_t cols, const std::vector< T > &elements)
From dimensions and a flat row-major std::vector, copied into the storage (a buffer moves in instead)...
Definition matrix.hpp:85
matrix(std::size_t rows, std::size_t cols, std::initializer_list< T > elements)
From dimensions and a braced list of the elements, row-major.
Definition matrix.hpp:99
A dense vector of T with value semantics.
Definition vector.hpp:171
std::size_t size() const noexcept
The number of elements.
Definition vector.hpp:194
The matrix product kernel behind matrix * matrix and the einsum matmul path.
void transpose_walk(std::size_t i0, std::size_t i1, std::size_t j0, std::size_t j1, std::size_t piece, const Copy &copy)
transpose_pieces of side piece (transpose_piece's 8 or the whole tile), each side its own instance: a...
Definition simd.hpp:142
void parallel_for(std::size_t count, std::size_t work_per_index, const Body &body)
Runs body(i) for every i in [0, count).
Definition parallel.hpp:790
void matrix_times_vector(const T *a, std::size_t lda, std::size_t rows, std::size_t cols, const T *x, T *y)
y = A x for the row-major rows × cols a, its rows lda apart: each element the dot product of its row ...
Definition gemm.hpp:5153
std::size_t transpose_piece(std::size_t from_bytes, std::size_t to_bytes) noexcept
The side of the square pieces a transposing copy walks a tile by, from the bytes between rows of its ...
Definition simd.hpp:100
void gemm(std::size_t m, std::size_t n, std::size_t k, const T *a, std::size_t lda, const T *b, std::size_t ldb, T *c, std::size_t ldc, c_start start=c_start::kept)
C += A·B, every element of C summed in ascending order of the depth.
Definition gemm.hpp:4583
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
@ zero
C = A·B: C's elements before the product are not read.
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
matrix< T > operator*(const T &scalar, const matrix< T > &m)
Scalar times matrix.
Definition matrix.hpp:188
constexpr std::size_t transpose_tile
The square blocks transpose copies one at a time: a block's rows of the source and of the result both...
Definition matrix.hpp:243
@ 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
A minimal std::thread parallel-for for the hot kernels.
A dense, value-semantic vector over a scalar type.