42# ifndef MODULAR_LINALG_H
43# define MODULAR_LINALG_H
48# include <type_traits>
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>;
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>
134 return m.read_ne(
r, c);
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)
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)
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)
274 for (
size_t j = i + 1; j <
n_rows; ++j)
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));
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)
353 for (
size_t j = 0; j <
n_rows; ++j)
361 for (
size_t j = 0; j <
n_rows; ++j)
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.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
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
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.