pub struct Matrix<S: Semiring> {
pub rows: usize,
pub cols: usize,
pub data: Vec<S>,
}Expand description
A dense, row-major matrix over the semiring S.
§Examples
Build matrices over the counting semiring and multiply them; the identity acts as a multiplicative unit:
use sim_lib_discrete_algebra::{Counting, Matrix};
let a = Matrix::from_rows(vec![
vec![Counting::from_u64(1), Counting::from_u64(2)],
vec![Counting::from_u64(3), Counting::from_u64(4)],
])
.unwrap();
let id = Matrix::identity(2);
assert_eq!(a.matmul(&id).unwrap(), a);
assert_eq!(a.get(1, 0).unwrap(), &Counting::from_u64(3));Fields§
§rows: usizeNumber of rows.
cols: usizeNumber of columns.
data: Vec<S>Row-major entries; data.len() == rows * cols.
Implementations§
Source§impl<S: Semiring> Matrix<S>
impl<S: Semiring> Matrix<S>
Sourcepub fn closure(&self, limits: AlgebraLimits) -> Result<Self, AlgebraError>
pub fn closure(&self, limits: AlgebraLimits) -> Result<Self, AlgebraError>
Compute the Kleene closure A* = I + A + A^2 + ....
Uses the generalized Floyd-Warshall (Lehmann) asteration: for each
pivot k, paths may pass through k and loop there star(A[k][k])
times. The identity is added at the end so A* includes the empty path
on the diagonal (distance-0 for min-plus, reflexive for boolean).
Returns AlgebraError::NoStar when a pivot’s diagonal star does not
converge (e.g. a negative cycle in min-plus, any directed cycle in
counting, or a semiring with no star such as RealF64).
§Examples
Boolean closure of a directed chain 0 -> 1 -> 2 yields reflexive
reachability: node i reaches node j exactly when j >= i.
use sim_lib_discrete_algebra::{AlgebraLimits, BoolRing, Matrix};
let mut a = Matrix::new(3, 3);
a.set(0, 1, BoolRing(true)).unwrap();
a.set(1, 2, BoolRing(true)).unwrap();
let reach = a.closure(AlgebraLimits::default()).unwrap();
assert_eq!(reach.get(0, 2).unwrap(), &BoolRing(true)); // 0 reaches 2
assert_eq!(reach.get(1, 1).unwrap(), &BoolRing(true)); // reflexive
assert_eq!(reach.get(2, 0).unwrap(), &BoolRing(false)); // 2 cannot reach 0Source§impl<S: Semiring> Matrix<S>
impl<S: Semiring> Matrix<S>
Sourcepub fn new(rows: usize, cols: usize) -> Self
pub fn new(rows: usize, cols: usize) -> Self
A rows x cols matrix filled with the semiring zero.
Sourcepub fn try_new(rows: usize, cols: usize) -> Result<Self, AlgebraError>
pub fn try_new(rows: usize, cols: usize) -> Result<Self, AlgebraError>
Checked rows x cols matrix filled with the semiring zero.
Sourcepub fn try_new_with_limits(
rows: usize,
cols: usize,
limits: AlgebraLimits,
) -> Result<Self, AlgebraError>
pub fn try_new_with_limits( rows: usize, cols: usize, limits: AlgebraLimits, ) -> Result<Self, AlgebraError>
Checked rows x cols matrix filled with the semiring zero, guarded by
an explicit dimension limit.
Sourcepub fn filled(rows: usize, cols: usize, value: S) -> Self
pub fn filled(rows: usize, cols: usize, value: S) -> Self
A rows x cols matrix filled with value.
Sourcepub fn try_filled(
rows: usize,
cols: usize,
value: S,
) -> Result<Self, AlgebraError>
pub fn try_filled( rows: usize, cols: usize, value: S, ) -> Result<Self, AlgebraError>
Checked rows x cols matrix filled with value.
Sourcepub fn try_filled_with_limits(
rows: usize,
cols: usize,
value: S,
limits: AlgebraLimits,
) -> Result<Self, AlgebraError>
pub fn try_filled_with_limits( rows: usize, cols: usize, value: S, limits: AlgebraLimits, ) -> Result<Self, AlgebraError>
Checked rows x cols matrix filled with value, guarded by an explicit
dimension limit.
Sourcepub fn try_identity(n: usize) -> Result<Self, AlgebraError>
pub fn try_identity(n: usize) -> Result<Self, AlgebraError>
Checked n x n identity matrix.
Sourcepub fn try_identity_with_limits(
n: usize,
limits: AlgebraLimits,
) -> Result<Self, AlgebraError>
pub fn try_identity_with_limits( n: usize, limits: AlgebraLimits, ) -> Result<Self, AlgebraError>
Checked n x n identity matrix guarded by an explicit dimension limit.
Sourcepub fn from_rows(rows: Vec<Vec<S>>) -> Result<Self, AlgebraError>
pub fn from_rows(rows: Vec<Vec<S>>) -> Result<Self, AlgebraError>
Build from a vector of rows, rejecting ragged input.
Sourcepub fn validate(&self) -> Result<(), AlgebraError>
pub fn validate(&self) -> Result<(), AlgebraError>
Validate the public structural invariant before indexing by shape.
Sourcepub fn get(&self, r: usize, c: usize) -> Result<&S, AlgebraError>
pub fn get(&self, r: usize, c: usize) -> Result<&S, AlgebraError>
Bounds-checked read of entry (r, c).
Sourcepub fn set(&mut self, r: usize, c: usize, value: S) -> Result<(), AlgebraError>
pub fn set(&mut self, r: usize, c: usize, value: S) -> Result<(), AlgebraError>
Bounds-checked write of entry (r, c).
Sourcepub fn row(&self, r: usize) -> Result<&[S], AlgebraError>
pub fn row(&self, r: usize) -> Result<&[S], AlgebraError>
Immutable slice of row r, or an error if out of range.
Sourcepub fn transpose(&self) -> Result<Self, AlgebraError>
pub fn transpose(&self) -> Result<Self, AlgebraError>
The transpose (a fresh cols x rows matrix).
Sourcepub fn matmul(&self, other: &Self) -> Result<Self, AlgebraError>
pub fn matmul(&self, other: &Self) -> Result<Self, AlgebraError>
Semiring matrix multiply: self (m x p) by other (p x q).
Source§impl<S: Semiring> Matrix<S>
impl<S: Semiring> Matrix<S>
Sourcepub fn power(
&self,
k: usize,
limits: AlgebraLimits,
) -> Result<Self, AlgebraError>
pub fn power( &self, k: usize, limits: AlgebraLimits, ) -> Result<Self, AlgebraError>
Raise a square matrix to the k-th power over its semiring.
k == 0 returns the identity. Returns AlgebraError::ShapeMismatch
for non-square input and AlgebraError::LimitExceeded when the
dimension exceeds limits.max_dim.