42# ifndef MODULAR_LINALG_H
43# define MODULAR_LINALG_H
48# include <type_traits>
63 concept SparseMatrix =
requires(
const T &
m,
T & mut_m,
size_t r,
size_t c, uint64_t v)
65 {
m.rows() } -> std::convertible_to<size_t>;
66 {
m.cols() } -> std::convertible_to<size_t>;
67 {
m.read_ne(
r, c) } -> std::convertible_to<uint64_t>;
68 { mut_m.write(
r, c, v) };
69 { mut_m.traverse_allocated([](uint64_t &) {
return true; }) } -> std::convertible_to<bool>;
70 { mut_m.set_dimension(
r, c) };
77 concept DenseMatrix = std::is_same_v<T, Array<Array<uint64_t>>>;
93 template <DenseMatrix MatrixT>
94 [[nodiscard]]
constexpr std::pair<size_t, size_t>
102 template <SparseMatrix MatrixT>
103 [[nodiscard]]
constexpr std::pair<size_t, size_t>
106 return {
m.rows(),
m.cols()};
122 template <ModularMatrixBackend MatrixT>
129 static uint64_t
get_val(
const MatrixT &
m,
size_t r,
size_t c)
134 return m.read_ne(
r, c);
137 static void set_val(MatrixT &
m,
size_t r,
size_t c, uint64_t v)
145 if (v != 0 or
m.read_ne(
r, c) != 0)
153 std::swap(
m(r1),
m(r2));
156 const size_t cols =
m.cols();
157 for (
size_t j = 0; j <
cols; ++j)
159 const uint64_t v1 =
m.read_ne(r1, j);
160 const uint64_t v2 =
m.read_ne(r2, j);
186 <<
"Modular_Matrix: modulus " <<
mod <<
" must be prime";
193 for (
size_t i = 0; i < n_rows; ++i)
196 <<
"Modular_Matrix: ragged matrix input is not supported";
197 for (
size_t j = 0; j < n_cols; ++j)
204 mat.traverse_allocated([
this](uint64_t & val)
227 const MatrixT &
get() const noexcept {
return mat; }
243 <<
"Determinant requires a square matrix";
248 for (
size_t i = 0; i < n_rows; ++i)
253 for (
size_t j = i + 1; j < n_rows; ++j)
270 const uint64_t diag =
get_val(a, i, i);
274 for (
size_t j = i + 1; j < n_rows; ++j)
275 if (
const uint64_t val_ji =
get_val(a, j, i); val_ji != 0)
277 const uint64_t factor =
mod_mul(val_ji, inv,
mod);
278 for (
size_t k = i;
k < n_cols; ++
k)
279 if (
const uint64_t val_ik =
get_val(a, i,
k); val_ik != 0)
281 const uint64_t term =
mod_mul(factor, val_ik,
mod);
282 const uint64_t current =
get_val(a, j,
k);
283 const uint64_t res = (current >= term) ? (current - term) : (
mod - (term - current));
302 [[nodiscard]] std::optional<Modular_Matrix<MatrixT>>
inverse()
const
307 <<
"Inverse requires a square matrix";
315 for (
size_t i = 0; i < n_rows; ++i)
319 for (
size_t j = 0; j < n_rows; ++j)
320 row.append(i == j ? 1 : 0);
321 inv.append(std::move(
row));
326 inv.set_dimension(n_rows, n_rows);
327 for (
size_t i = 0; i < n_rows; ++i)
331 for (
size_t i = 0; i < n_rows; ++i)
335 for (
size_t j = i + 1; j < n_rows; ++j)
351 const uint64_t pivot_val =
get_val(a, i, i);
352 const uint64_t pivot_inv =
mod_inv(pivot_val,
mod);
353 for (
size_t j = 0; j < n_rows; ++j)
355 if (
const uint64_t val =
get_val(a, i, j); val != 0)
357 if (
const uint64_t val =
get_val(inv, i, j); val != 0)
361 for (
size_t j = 0; j < n_rows; ++j)
363 if (
const uint64_t factor =
get_val(a, j, i); factor != 0)
364 for (
size_t k = 0;
k < n_rows; ++
k)
366 if (
const uint64_t val_ik =
get_val(a, i,
k); val_ik != 0)
368 const uint64_t term_a =
mod_mul(factor, val_ik,
mod);
369 const uint64_t curr_a =
get_val(a, j,
k);
370 set_val(a, j,
k, (curr_a >= term_a) ? (curr_a - term_a) : (
mod - (term_a - curr_a)));
373 if (
const uint64_t val_inv_ik =
get_val(inv, i,
k); val_inv_ik != 0)
375 const uint64_t term_inv =
mod_mul(factor, val_inv_ik,
mod);
376 const uint64_t curr_inv =
get_val(inv, j,
k);
377 set_val(inv, j,
k, (curr_inv >= term_inv) ?
378 (curr_inv - term_inv) :
379 (
mod - (term_inv - curr_inv)));
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Simple dynamic array with automatic resizing and functional operations.
void reserve(size_t cap)
Reserves cap cells into the array.
Matrix operations modulo a prime.
MatrixT mat
The underlying matrix data.
static void swap_rows(MatrixT &m, size_t r1, size_t r2)
uint64_t mod
The prime modulus defining the finite field.
const MatrixT & get() const noexcept
Get a constant reference to the underlying matrix.
Modular_Matrix(MatrixT &&m, uint64_t p, skip_validation_t)
static void set_val(MatrixT &m, size_t r, size_t c, uint64_t v)
uint64_t determinant() const
Computes the determinant of the matrix modulo p.
Modular_Matrix(const MatrixT &m, const uint64_t p)
Construct a modular matrix from an existing matrix and a prime modulus.
static uint64_t get_val(const MatrixT &m, size_t r, size_t c)
std::optional< Modular_Matrix< MatrixT > > inverse() const
Computes the inverse of the matrix modulo p.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Expresses requirements for dense matrix backends like Array<Array<uint64_t>>.
Union of supported matrix backends for modular operations.
Expresses requirements for sparse matrix backends like DynMatrix.
Safe modular arithmetic, extended Euclidean algorithm, and Chinese Remainder Theorem.
Main namespace for Aleph-w library functions.
uint64_t mod_inv(const uint64_t a, const uint64_t m)
Modular Inverse.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
constexpr std::pair< size_t, size_t > matrix_dims(const MatrixT &m) noexcept
Get dimensions of a matrix (dense or sparse).
std::decay_t< typename HeadC::Item_Type > T
bool miller_rabin(uint64_t n) noexcept
Miller-Rabin primality test for 64-bit integers.
uint64_t mod_mul(uint64_t a, uint64_t b, uint64_t m)
Safe 64-bit modular multiplication.
Advanced primality testing algorithms.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
Dynamic matrix with lazy allocation.