Mat

Struct Mat 

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

Source

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 +∞.

Source

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).

Source

pub fn from_fn<F>(nrows: usize, ncols: usize, f: F) -> Self
where F: FnMut(usize, usize) -> S,

Create a matrix from a function.

The function is called with (row, col) indices. Data is stored in column-major order internally.

Source

pub fn from_col_major(data: &[S::Scalar], nrows: usize, ncols: usize) -> Self
where S::Scalar: Copy,

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.

Source

pub fn from_row_major(data: &[S::Scalar], nrows: usize, ncols: usize) -> Self
where S::Scalar: Copy,

👎Deprecated since 0.4.0: use from_col_major instead for direct column-major input; this method has O(m×n) transpose overhead

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.

Source

pub fn from_vec(data: Vec<S>, nrows: usize, ncols: usize) -> Self

Create a matrix from a vector of semiring values.

Source

pub fn nrows(&self) -> usize

Number of rows.

Source

pub fn ncols(&self) -> usize

Number of columns.

Source

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

Get the underlying data as a slice.

Source

pub fn as_mut_slice(&mut self) -> &mut [S]

Get the underlying data as a mutable slice.

Source

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);
Source

pub fn as_ref(&self) -> MatRef<'_, S>
where S::Scalar: Copy,

Convert to an immutable matrix reference.

The returned reference views the scalar values.

Source

pub fn as_mut_ptr(&mut self) -> *mut S

Get a mutable pointer to the data.

Source§

impl<S> Mat<S>

Source

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);
Source

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>
where S: TropicalWithArgmax<Index = u32> + KernelDispatch, S::Scalar: Copy,

Source

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 max
Source

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_batch and b_batch have different lengths
  • Matrices in a_batch have different dimensions
  • Matrices in b_batch have 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>

Source

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_batch and b_batch have different lengths
  • Matrices in a_batch have different dimensions
  • Matrices in b_batch have 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§

Source§

impl<S: Clone + TropicalSemiring> Clone for Mat<S>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + TropicalSemiring> Debug for Mat<S>

Source§

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

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

impl<S: TropicalSemiring> Index<(usize, usize)> for Mat<S>

Source§

type Output = S

The returned type after indexing.
Source§

fn index(&self, (i, j): (usize, usize)) -> &S

Performs the indexing (container[index]) operation. Read more
Source§

impl<S: TropicalSemiring> IndexMut<(usize, usize)> for Mat<S>

Source§

fn index_mut(&mut self, (i, j): (usize, usize)) -> &mut S

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<S> Mul<&Mat<S>> for &Mat<S>

Source§

type Output = Mat<S>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Mat<S>) -> Mat<S>

Performs the * operation. Read more
Source§

impl<'a, S> Mul<&'a MatRef<'a, S>> for &Mat<S>

Source§

type Output = Mat<S>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &'a MatRef<'a, S>) -> Mat<S>

Performs the * operation. Read more
Source§

impl<S> Mul for Mat<S>

Source§

type Output = Mat<S>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Mat<S>) -> Mat<S>

Performs the * operation. Read more

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.