11#ifndef NUMERICAL_MATRIX_HPP
12#define NUMERICAL_MATRIX_HPP
19#include <initializer_list>
46 matrix(std::initializer_list<std::initializer_list<T>>
rows)
50 for (
const std::initializer_list<T>&
row :
rows) {
120 std::size_t
col)
const
134 return data_.begin();
149 return std::move(
data_);
160 template <
typename T>
168 std::transform(a.
begin(), a.
end(), b.
begin(),
out.begin(), std::plus<T> {});
173 template <
typename T>
181 std::transform(a.
begin(), a.
end(), b.
begin(),
out.begin(), std::minus<T> {});
186 template <
typename T>
192 [&scalar](
const T&
element) { return scalar * element; });
197 template <
typename T>
206 template <
typename T>
211 throw dimension_error(
"matrix–vector product: inner dimensions differ");
215 std::fill(
out.begin(),
out.end(),
T {});
224 template <
typename T>
229 throw dimension_error(
"matrix–matrix product: inner dimensions differ");
235 if (!
out.empty() && a.
cols() != 0) {
249 template <
typename T>
252 const std::size_t rows {m.
rows()};
253 const std::size_t cols {m.
cols()};
260 for (std::size_t j {
j0}; j <
j1; ++j) {
261 for (std::size_t i {
i0}; i <
i1; ++i) {
262 out[(j * rows) + i] = m(i, j);
277 template <
typename T>
285 for (std::size_t i {0}; i < a.
rows(); ++i) {
286 for (std::size_t j {0}; j < a.
cols(); ++j) {
289 if (!(a(i, j) == b(i, j)) && !(std::abs(a(i, j) - b(i, j)) <=
tolerance)) {
Thrown when an operation's operands have incompatible shapes.
A dense, row-major matrix of T with value semantics.
bool operator==(const matrix &other) const
Exact equality (same shape and equal elements).
matrix()=default
An empty matrix (0×0).
buffer< T >::const_iterator end() const noexcept
Const iterator past the last element.
matrix(std::size_t rows, std::size_t cols, buffer< T > elements)
From dimensions and a flat row-major buffer.
matrix(std::initializer_list< std::initializer_list< T > > rows)
From a braced list of rows; every row must have the same length.
buffer< T >::const_iterator begin() const noexcept
Const iterator to the first element (row-major).
buffer< T > data_
The elements, row-major.
std::size_t rows() const noexcept
The number of rows.
std::size_t rows_
The number of rows.
buffer< T > elements() &&
The elements, row-major, handed over by a matrix about to expire, which is left 0 × 0: a large result...
const T & operator()(std::size_t row, std::size_t col) const
The element at (row, col).
std::size_t cols_
The number of columns.
std::size_t cols() const noexcept
The number of columns.
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)...
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.
A dense vector of T with value semantics.
std::size_t size() const noexcept
The number of elements.
The matrix product kernel behind matrix * matrix and the einsum matmul path.
void parallel_zero(T *out, std::size_t count)
Sets the count elements at out to zero, split across the threads the work pays for: a fresh result's ...
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 += A·B, every element of C summed in ascending order of the depth.
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).
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...
void matrix_times_vector(const T *a, std::size_t rows, std::size_t cols, const T *x, T *y)
y = A x for the row-major rows × cols a: each element the dot product of its row with x,...
The NumericAL public API.
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...
matrix< T > operator+(const matrix< T > &a, const matrix< T > &b)
Element-wise sum.
matrix< T > operator*(const T &scalar, const matrix< T > &m)
Scalar times matrix.
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...
matrix< T > transpose(const matrix< T > &m)
The transpose, by blocks of transpose_tile, the result's blocks of rows shared among the threads.
@ 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.
bool approx_equal(const matrix< T > &a, const matrix< T > &b, double tolerance=1e-9)
Element-wise closeness within a tolerance (for inexact scalars).
A minimal std::thread parallel-for for the hot kernels.
A dense, value-semantic vector over a scalar type.