41# ifndef MODULAR_LINALG_H
42# define MODULAR_LINALG_H
47# include <type_traits>
64 {
m.rows() } -> std::convertible_to<size_t>;
65 {
m.cols() } -> std::convertible_to<size_t>;
66 {
m.read_ne(
r, c) } -> std::convertible_to<uint64_t>;
68 {
mut_m.traverse_allocated([](
uint64_t &) {
return true; }) } -> std::convertible_to<bool>;
69 {
mut_m.set_dimension(
r, c) };
76 concept DenseMatrix = std::is_same_v<T, Array<Array<uint64_t>>>;
92 template <DenseMatrix MatrixT>
93 [[
nodiscard]]
constexpr std::pair<size_t, size_t>
96 const size_t rows =
m.
size();
97 return {rows, rows > 0 ?
m[0].
size() : 0};
101 template <SparseMatrix MatrixT>
102 [[
nodiscard]]
constexpr std::pair<size_t, size_t>
105 return {
m.rows(),
m.cols()};
121 template <ModularMatrixBackend MatrixT>
133 return m.read_ne(
r, c);
144 if (v != 0
or m.read_ne(
r, c) != 0)
152 std::swap(
m(
r1),
m(r2));
155 const size_t cols =
m.cols();
156 for (
size_t j = 0; j < cols; ++j)
185 <<
"Modular_Matrix: modulus " <<
mod <<
" must be prime";
192 for (
size_t i = 0; i <
n_rows; ++i)
195 <<
"Modular_Matrix: ragged matrix input is not supported";
196 for (
size_t j = 0; j <
n_cols; ++j)
242 <<
"Determinant requires a square matrix";
247 for (
size_t i = 0; i <
n_rows; ++i)
252 for (
size_t j = i + 1; j <
n_rows; ++j)
273 for (
size_t j = i + 1; j <
n_rows; ++j)
306 <<
"Inverse requires a square matrix";
314 for (
size_t i = 0; i <
n_rows; ++i)
318 for (
size_t j = 0; j <
n_rows; ++j)
319 row.
append(i == j ? 1 : 0);
320 inv.append(std::move(row));
326 for (
size_t i = 0; i <
n_rows; ++i)
330 for (
size_t i = 0; i <
n_rows; ++i)
334 for (
size_t j = i + 1; j <
n_rows; ++j)
352 for (
size_t j = 0; j <
n_rows; ++j)
360 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.
T & append(const T &data)
Append a copy of data
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
constexpr std::pair< size_t, size_t > matrix_dims(const MatrixT &m) noexcept
Get dimensions of a matrix (dense or sparse).
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.