pub struct Mat<S: TropicalSemiring> { /* private fields */ }Expand description
Owned matrix storing semiring values.
The matrix stores values in column-major order (Fortran/BLAS convention). Use factory methods to create matrices:
use tropical_gemm::{Mat, MaxPlus, TropicalSemiring};
let zeros = Mat::<MaxPlus<f32>>::zeros(3, 4);
let identity = Mat::<MaxPlus<f32>>::identity(3);
let custom = Mat::<MaxPlus<f32>>::from_fn(2, 2, |i, j| {
MaxPlus::<f32>::from_scalar((i + j) as f32)
});Implementations§
Source§impl<S: TropicalSemiring> Mat<S>
impl<S: TropicalSemiring> Mat<S>
Sourcepub fn zeros(nrows: usize, ncols: usize) -> Self
pub fn zeros(nrows: usize, ncols: usize) -> Self
Create a matrix filled with tropical zeros.
For MaxPlus, this fills with -∞. For MinPlus, this fills with +∞.
Sourcepub fn identity(n: usize) -> Self
pub fn identity(n: usize) -> Self
Create a tropical identity matrix.
Diagonal elements are tropical one (0 for MaxPlus/MinPlus). Off-diagonal elements are tropical zero (-∞ for MaxPlus, +∞ for MinPlus).
Sourcepub fn from_fn<F>(nrows: usize, ncols: usize, f: F) -> Self
pub fn from_fn<F>(nrows: usize, ncols: usize, f: F) -> Self
Create a matrix from a function.
The function is called with (row, col) indices. Data is stored in column-major order internally.
Sourcepub fn from_col_major(data: &[S::Scalar], nrows: usize, ncols: usize) -> Self
pub fn from_col_major(data: &[S::Scalar], nrows: usize, ncols: usize) -> Self
Create a matrix from column-major scalar data.
Each scalar is wrapped in the semiring type. Data should be in column-major order: first column, then second column, etc.
Sourcepub fn from_row_major(data: &[S::Scalar], nrows: usize, ncols: usize) -> Self
👎Deprecated since 0.4.0: use from_col_major instead for direct column-major input; this method has O(m×n) transpose overhead
pub fn from_row_major(data: &[S::Scalar], nrows: usize, ncols: usize) -> Self
Create a matrix from row-major scalar data.
This is a convenience method that converts row-major input to column-major storage.
§Performance Warning
This method performs an O(m×n) transpose operation. For performance-critical code,
provide data in column-major order and use [from_col_major] instead.
Sourcepub fn from_vec(data: Vec<S>, nrows: usize, ncols: usize) -> Self
pub fn from_vec(data: Vec<S>, nrows: usize, ncols: usize) -> Self
Create a matrix from a vector of semiring values.
Sourcepub fn as_mut_slice(&mut self) -> &mut [S]
pub fn as_mut_slice(&mut self) -> &mut [S]
Get the underlying data as a mutable slice.
Sourcepub fn get_value(&self, i: usize, j: usize) -> S::Scalar
pub fn get_value(&self, i: usize, j: usize) -> S::Scalar
Get the scalar value at position (i, j).
This is a convenience method that extracts the underlying scalar without requiring a trait import.
§Example
use tropical_gemm::{Mat, MaxPlus};
let m = Mat::<MaxPlus<f64>>::from_row_major(&[1.0, 2.0, 3.0, 4.0], 2, 2);
assert_eq!(m.get_value(0, 0), 1.0);
assert_eq!(m.get_value(1, 1), 4.0);Sourcepub fn as_ref(&self) -> MatRef<'_, S>
pub fn as_ref(&self) -> MatRef<'_, S>
Convert to an immutable matrix reference.
The returned reference views the scalar values.
Sourcepub fn as_mut_ptr(&mut self) -> *mut S
pub fn as_mut_ptr(&mut self) -> *mut S
Get a mutable pointer to the data.
Source§impl<S> Mat<S>
impl<S> Mat<S>
Sourcepub fn matmul(&self, b: &Mat<S>) -> Mat<S>
pub fn matmul(&self, b: &Mat<S>) -> Mat<S>
Perform tropical matrix multiplication: C = A ⊗ B.
Computes C[i,j] = ⊕_k (A[i,k] ⊗ B[k,j])
§Panics
Panics if dimensions don’t match (self.ncols != b.nrows).
§Example
use tropical_gemm::{Mat, MaxPlus, TropicalSemiring};
let a = Mat::<MaxPlus<f64>>::from_row_major(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 2, 3);
let b = Mat::<MaxPlus<f64>>::from_row_major(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2);
let c = a.matmul(&b);
// C[0,0] = max(1+1, 2+3, 3+5) = 8
assert_eq!(c[(0, 0)].value(), 8.0);Sourcepub fn matmul_ref(&self, b: &MatRef<'_, S>) -> Mat<S>
pub fn matmul_ref(&self, b: &MatRef<'_, S>) -> Mat<S>
Perform tropical matrix multiplication with a MatRef.
This allows mixing owned and reference matrices.
Source§impl<S> Mat<S>
impl<S> Mat<S>
Sourcepub fn matmul_argmax(&self, b: &Mat<S>) -> MatWithArgmax<S>
pub fn matmul_argmax(&self, b: &Mat<S>) -> MatWithArgmax<S>
Perform tropical matrix multiplication with argmax tracking.
Returns both the result matrix and the argmax indices indicating which k-index produced each optimal value.
§Example
use tropical_gemm::{Mat, MaxPlus, TropicalSemiring};
let a = Mat::<MaxPlus<f64>>::from_row_major(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 2, 3);
let b = Mat::<MaxPlus<f64>>::from_row_major(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2);
let result = a.matmul_argmax(&b);
assert_eq!(result.get(0, 0).value(), 8.0);
assert_eq!(result.get_argmax(0, 0), 2); // k=2 gave maxSourcepub fn matmul_batched_with_argmax(
a_batch: &[Mat<S>],
b_batch: &[Mat<S>],
) -> Vec<MatWithArgmax<S>>
pub fn matmul_batched_with_argmax( a_batch: &[Mat<S>], b_batch: &[Mat<S>], ) -> Vec<MatWithArgmax<S>>
Batched tropical matrix multiplication with argmax tracking.
Computes C[i] = A[i] ⊗ B[i] for each pair of matrices in the batch, tracking which k-index produced each optimal value.
All matrices in a_batch must have the same dimensions, and all
matrices in b_batch must have the same dimensions.
§Panics
Panics if:
a_batchandb_batchhave different lengths- Matrices in
a_batchhave different dimensions - Matrices in
b_batchhave different dimensions - Inner dimensions don’t match (A.ncols != B.nrows)
§Example
use tropical_gemm::{Mat, MaxPlus};
let a1 = Mat::<MaxPlus<f32>>::from_row_major(&[1.0, 2.0, 3.0, 4.0], 2, 2);
let a2 = Mat::<MaxPlus<f32>>::from_row_major(&[5.0, 6.0, 7.0, 8.0], 2, 2);
let b1 = Mat::<MaxPlus<f32>>::from_row_major(&[1.0, 0.0, 0.0, 1.0], 2, 2);
let b2 = Mat::<MaxPlus<f32>>::from_row_major(&[1.0, 2.0, 3.0, 4.0], 2, 2);
let results = Mat::matmul_batched_with_argmax(&[a1, a2], &[b1, b2]);
assert_eq!(results.len(), 2);Source§impl<S> Mat<S>
impl<S> Mat<S>
Sourcepub fn matmul_batched(a_batch: &[Mat<S>], b_batch: &[Mat<S>]) -> Vec<Mat<S>>
pub fn matmul_batched(a_batch: &[Mat<S>], b_batch: &[Mat<S>]) -> Vec<Mat<S>>
Batched tropical matrix multiplication.
Computes C[i] = A[i] ⊗ B[i] for each pair of matrices in the batch.
All matrices in a_batch must have the same dimensions, and all
matrices in b_batch must have the same dimensions.
§Panics
Panics if:
a_batchandb_batchhave different lengths- Matrices in
a_batchhave different dimensions - Matrices in
b_batchhave different dimensions - Inner dimensions don’t match (A.ncols != B.nrows)
§Example
use tropical_gemm::{Mat, MaxPlus};
let a1 = Mat::<MaxPlus<f32>>::from_row_major(&[1.0, 2.0, 3.0, 4.0], 2, 2);
let a2 = Mat::<MaxPlus<f32>>::from_row_major(&[5.0, 6.0, 7.0, 8.0], 2, 2);
let b1 = Mat::<MaxPlus<f32>>::from_row_major(&[1.0, 0.0, 0.0, 1.0], 2, 2);
let b2 = Mat::<MaxPlus<f32>>::from_row_major(&[1.0, 2.0, 3.0, 4.0], 2, 2);
let results = Mat::matmul_batched(&[a1, a2], &[b1, b2]);
assert_eq!(results.len(), 2);Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for Mat<S>
impl<S> RefUnwindSafe for Mat<S>where
S: RefUnwindSafe,
impl<S> Send for Mat<S>
impl<S> Sync for Mat<S>
impl<S> Unpin for Mat<S>where
S: Unpin,
impl<S> UnwindSafe for Mat<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more