Skip to main content

Matrix

Struct Matrix 

Source
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: usize

Number of rows.

§cols: usize

Number of columns.

§data: Vec<S>

Row-major entries; data.len() == rows * cols.

Implementations§

Source§

impl<S: Semiring> Matrix<S>

Source

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 0
Source§

impl<S: Semiring> Matrix<S>

Source

pub fn new(rows: usize, cols: usize) -> Self

A rows x cols matrix filled with the semiring zero.

Source

pub fn try_new(rows: usize, cols: usize) -> Result<Self, AlgebraError>

Checked rows x cols matrix filled with the semiring zero.

Source

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.

Source

pub fn filled(rows: usize, cols: usize, value: S) -> Self

A rows x cols matrix filled with value.

Source

pub fn try_filled( rows: usize, cols: usize, value: S, ) -> Result<Self, AlgebraError>

Checked rows x cols matrix filled with value.

Source

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.

Source

pub fn identity(n: usize) -> Self

The n x n identity: one on the diagonal, zero elsewhere.

Source

pub fn try_identity(n: usize) -> Result<Self, AlgebraError>

Checked n x n identity matrix.

Source

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.

Source

pub fn from_rows(rows: Vec<Vec<S>>) -> Result<Self, AlgebraError>

Build from a vector of rows, rejecting ragged input.

Source

pub fn is_square(&self) -> bool

Whether the matrix is square.

Source

pub fn row_count(&self) -> usize

Number of matrix rows.

Source

pub fn col_count(&self) -> usize

Number of matrix columns.

Source

pub fn data(&self) -> &[S]

Read-only row-major data slice.

Source

pub fn validate(&self) -> Result<(), AlgebraError>

Validate the public structural invariant before indexing by shape.

Source

pub fn get(&self, r: usize, c: usize) -> Result<&S, AlgebraError>

Bounds-checked read of entry (r, c).

Source

pub fn set(&mut self, r: usize, c: usize, value: S) -> Result<(), AlgebraError>

Bounds-checked write of entry (r, c).

Source

pub fn row(&self, r: usize) -> Result<&[S], AlgebraError>

Immutable slice of row r, or an error if out of range.

Source

pub fn transpose(&self) -> Result<Self, AlgebraError>

The transpose (a fresh cols x rows matrix).

Source

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>

Source

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.

Trait Implementations§

Source§

impl<S: Clone + Semiring> Clone for Matrix<S>

Source§

fn clone(&self) -> Matrix<S>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + Semiring> Debug for Matrix<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: PartialEq + Semiring> PartialEq for Matrix<S>

Source§

fn eq(&self, other: &Matrix<S>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<S: PartialEq + Semiring> StructuralPartialEq for Matrix<S>

Auto Trait Implementations§

§

impl<S> Freeze for Matrix<S>

§

impl<S> RefUnwindSafe for Matrix<S>
where S: RefUnwindSafe,

§

impl<S> Send for Matrix<S>
where S: Send,

§

impl<S> Sync for Matrix<S>
where S: Sync,

§

impl<S> Unpin for Matrix<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for Matrix<S>

§

impl<S> UnwindSafe for Matrix<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.