pub trait Matrix<T: Send + Sync + Clone>: Send + Sync {
Show 26 methods
// Required methods
fn width(&self) -> usize;
fn height(&self) -> usize;
// Provided methods
fn dimensions(&self) -> Dimensions { ... }
fn get(&self, r: usize, c: usize) -> Option<T> { ... }
unsafe fn get_unchecked(&self, r: usize, c: usize) -> T { ... }
fn row(
&self,
r: usize,
) -> Option<impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>> { ... }
unsafe fn row_unchecked(
&self,
r: usize,
) -> impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync> { ... }
unsafe fn row_subseq_unchecked(
&self,
r: usize,
start: usize,
end: usize,
) -> impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync> { ... }
fn row_slice(&self, r: usize) -> Option<impl Deref<Target = [T]>> { ... }
unsafe fn row_slice_unchecked(&self, r: usize) -> impl Deref<Target = [T]> { ... }
unsafe fn row_subslice_unchecked(
&self,
r: usize,
start: usize,
end: usize,
) -> impl Deref<Target = [T]> { ... }
fn rows(
&self,
) -> impl Iterator<Item = impl Iterator<Item = T>> + Send + Sync { ... }
fn par_rows(
&self,
) -> impl IndexedParallelIterator<Item = impl Iterator<Item = T>> + Send + Sync { ... }
fn wrapping_row_slices(
&self,
r: usize,
c: usize,
) -> Vec<impl Deref<Target = [T]>> { ... }
fn first_row(
&self,
) -> Option<impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>> { ... }
fn last_row(
&self,
) -> Option<impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>> { ... }
fn to_row_major_matrix(self) -> RowMajorMatrix<T>
where Self: Sized,
T: Clone { ... }
fn horizontally_packed_row<'a, P>(
&'a self,
r: usize,
) -> (impl Iterator<Item = P> + Send + Sync, impl Iterator<Item = T> + Send + Sync)
where P: PackedValue<Value = T>,
T: Clone + 'a { ... }
fn padded_horizontally_packed_row<'a, P>(
&'a self,
r: usize,
) -> impl Iterator<Item = P> + Send + Sync
where P: PackedValue<Value = T>,
T: Clone + Default + 'a { ... }
fn par_horizontally_packed_rows<'a, P>(
&'a self,
) -> impl IndexedParallelIterator<Item = (impl Iterator<Item = P> + Send + Sync, impl Iterator<Item = T> + Send + Sync)>
where P: PackedValue<Value = T>,
T: Clone + 'a { ... }
fn par_padded_horizontally_packed_rows<'a, P>(
&'a self,
) -> impl IndexedParallelIterator<Item = impl Iterator<Item = P> + Send + Sync>
where P: PackedValue<Value = T>,
T: Clone + Default + 'a { ... }
fn vertically_packed_row<P>(&self, r: usize) -> impl Iterator<Item = P>
where T: Copy,
P: PackedValue<Value = T> { ... }
fn vertically_packed_row_pair<P>(&self, r: usize, step: usize) -> Vec<P>
where T: Copy,
P: PackedValue<Value = T> { ... }
fn vertically_strided(
self,
stride: usize,
offset: usize,
) -> VerticallyStridedMatrixView<Self>
where Self: Sized { ... }
fn columnwise_dot_product<EF>(&self, v: &[EF]) -> Vec<EF>
where T: Field,
EF: ExtensionField<T> { ... }
fn rowwise_packed_dot_product<EF>(
&self,
vec: &[EF::ExtensionPacking],
) -> impl IndexedParallelIterator<Item = EF>
where T: Field,
EF: ExtensionField<T> { ... }
}
Expand description
A generic trait for two-dimensional matrix-like data structures.
The Matrix
trait provides a uniform interface for accessing rows, elements,
and computing with matrices in both sequential and parallel contexts. It supports
packing strategies for SIMD optimizations and interaction with extension fields.
Required Methods§
Provided Methods§
Sourcefn dimensions(&self) -> Dimensions
fn dimensions(&self) -> Dimensions
Returns the dimensions (width, height) of the matrix.
Sourcefn get(&self, r: usize, c: usize) -> Option<T>
fn get(&self, r: usize, c: usize) -> Option<T>
Returns the element at the given row and column.
Returns None
if either r >= height()
or c >= width()
.
Sourceunsafe fn get_unchecked(&self, r: usize, c: usize) -> T
unsafe fn get_unchecked(&self, r: usize, c: usize) -> T
Returns the element at the given row and column.
For a safe alternative, see [get
].
§Safety
The caller must ensure that r < self.height()
and c < self.width()
.
Breaking any of these assumptions is considered undefined behaviour.
Sourcefn row(
&self,
r: usize,
) -> Option<impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>>
fn row( &self, r: usize, ) -> Option<impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>>
Returns an iterator over the elements of the r
-th row.
The iterator will have self.width()
elements.
Returns None
if r >= height()
.
Sourceunsafe fn row_unchecked(
&self,
r: usize,
) -> impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>
unsafe fn row_unchecked( &self, r: usize, ) -> impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>
Returns an iterator over the elements of the r
-th row.
The iterator will have self.width()
elements.
For a safe alternative, see [row
].
§Safety
The caller must ensure that r < self.height()
.
Breaking this assumption is considered undefined behaviour.
Sourceunsafe fn row_subseq_unchecked(
&self,
r: usize,
start: usize,
end: usize,
) -> impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>
unsafe fn row_subseq_unchecked( &self, r: usize, start: usize, end: usize, ) -> impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>
Returns an iterator over the elements of the r
-th row from position start
to end
.
When start = 0
and end = width()
, this is equivalent to [row_unchecked
].
For a safe alternative, use [row
], along with the skip
and take
iterator methods.
§Safety
The caller must ensure that r < self.height()
and start <= end <= self.width()
.
Breaking any of these assumptions is considered undefined behaviour.
Sourcefn row_slice(&self, r: usize) -> Option<impl Deref<Target = [T]>>
fn row_slice(&self, r: usize) -> Option<impl Deref<Target = [T]>>
Returns the elements of the r
-th row as something which can be coerced to a slice.
Returns None
if r >= height()
.
Sourceunsafe fn row_slice_unchecked(&self, r: usize) -> impl Deref<Target = [T]>
unsafe fn row_slice_unchecked(&self, r: usize) -> impl Deref<Target = [T]>
Returns the elements of the r
-th row as something which can be coerced to a slice.
For a safe alternative, see [row_slice
].
§Safety
The caller must ensure that r < self.height()
.
Breaking this assumption is considered undefined behaviour.
Sourceunsafe fn row_subslice_unchecked(
&self,
r: usize,
start: usize,
end: usize,
) -> impl Deref<Target = [T]>
unsafe fn row_subslice_unchecked( &self, r: usize, start: usize, end: usize, ) -> impl Deref<Target = [T]>
Returns a subset of elements of the r
-th row as something which can be coerced to a slice.
When start = 0
and end = width()
, this is equivalent to [row_slice_unchecked
].
For a safe alternative, see [row_slice
].
§Safety
The caller must ensure that r < self.height()
and start <= end <= self.width()
.
Breaking any of these assumptions is considered undefined behaviour.
Sourcefn rows(&self) -> impl Iterator<Item = impl Iterator<Item = T>> + Send + Sync
fn rows(&self) -> impl Iterator<Item = impl Iterator<Item = T>> + Send + Sync
Returns an iterator over all rows in the matrix.
Sourcefn par_rows(
&self,
) -> impl IndexedParallelIterator<Item = impl Iterator<Item = T>> + Send + Sync
fn par_rows( &self, ) -> impl IndexedParallelIterator<Item = impl Iterator<Item = T>> + Send + Sync
Returns a parallel iterator over all rows in the matrix.
Sourcefn wrapping_row_slices(
&self,
r: usize,
c: usize,
) -> Vec<impl Deref<Target = [T]>>
fn wrapping_row_slices( &self, r: usize, c: usize, ) -> Vec<impl Deref<Target = [T]>>
Collect the elements of the rows r
through r + c
. If anything is larger than self.height()
simply wrap around to the beginning of the matrix.
Sourcefn first_row(
&self,
) -> Option<impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>>
fn first_row( &self, ) -> Option<impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>>
Returns an iterator over the first row of the matrix.
Returns None if height() == 0
.
Sourcefn last_row(
&self,
) -> Option<impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>>
fn last_row( &self, ) -> Option<impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync>>
Returns an iterator over the last row of the matrix.
Returns None if height() == 0
.
Sourcefn to_row_major_matrix(self) -> RowMajorMatrix<T>
fn to_row_major_matrix(self) -> RowMajorMatrix<T>
Converts the matrix into a RowMajorMatrix
by collecting all rows into a single vector.
Sourcefn horizontally_packed_row<'a, P>(
&'a self,
r: usize,
) -> (impl Iterator<Item = P> + Send + Sync, impl Iterator<Item = T> + Send + Sync)where
P: PackedValue<Value = T>,
T: Clone + 'a,
fn horizontally_packed_row<'a, P>(
&'a self,
r: usize,
) -> (impl Iterator<Item = P> + Send + Sync, impl Iterator<Item = T> + Send + Sync)where
P: PackedValue<Value = T>,
T: Clone + 'a,
Get a packed iterator over the r
-th row.
If the row length is not divisible by the packing width, the final elements
are returned as a base iterator with length <= P::WIDTH - 1
.
§Panics
Panics if r >= height()
.
Sourcefn padded_horizontally_packed_row<'a, P>(
&'a self,
r: usize,
) -> impl Iterator<Item = P> + Send + Sync
fn padded_horizontally_packed_row<'a, P>( &'a self, r: usize, ) -> impl Iterator<Item = P> + Send + Sync
Get a packed iterator over the r
-th row.
If the row length is not divisible by the packing width, the final entry will be zero-padded.
§Panics
Panics if r >= height()
.
Sourcefn par_horizontally_packed_rows<'a, P>(
&'a self,
) -> impl IndexedParallelIterator<Item = (impl Iterator<Item = P> + Send + Sync, impl Iterator<Item = T> + Send + Sync)>where
P: PackedValue<Value = T>,
T: Clone + 'a,
fn par_horizontally_packed_rows<'a, P>(
&'a self,
) -> impl IndexedParallelIterator<Item = (impl Iterator<Item = P> + Send + Sync, impl Iterator<Item = T> + Send + Sync)>where
P: PackedValue<Value = T>,
T: Clone + 'a,
Get a parallel iterator over all packed rows of the matrix.
If the matrix width is not divisible by the packing width, the final elements
of each row are returned as a base iterator with length <= P::WIDTH - 1
.
Sourcefn par_padded_horizontally_packed_rows<'a, P>(
&'a self,
) -> impl IndexedParallelIterator<Item = impl Iterator<Item = P> + Send + Sync>
fn par_padded_horizontally_packed_rows<'a, P>( &'a self, ) -> impl IndexedParallelIterator<Item = impl Iterator<Item = P> + Send + Sync>
Get a parallel iterator over all packed rows of the matrix.
If the matrix width is not divisible by the packing width, the final entry of each row will be zero-padded.
Sourcefn vertically_packed_row<P>(&self, r: usize) -> impl Iterator<Item = P>where
T: Copy,
P: PackedValue<Value = T>,
fn vertically_packed_row<P>(&self, r: usize) -> impl Iterator<Item = P>where
T: Copy,
P: PackedValue<Value = T>,
Pack together a collection of adjacent rows from the matrix.
Returns an iterator whose i’th element is packing of the i’th element of the rows r through r + P::WIDTH - 1. If we exceed the height of the matrix, wrap around and include initial rows.
Sourcefn vertically_packed_row_pair<P>(&self, r: usize, step: usize) -> Vec<P>where
T: Copy,
P: PackedValue<Value = T>,
fn vertically_packed_row_pair<P>(&self, r: usize, step: usize) -> Vec<P>where
T: Copy,
P: PackedValue<Value = T>,
Pack together a collection of rows and “next” rows from the matrix.
Returns a vector corresponding to 2 packed rows. The i’th element of the first row contains the packing of the i’th element of the rows r through r + P::WIDTH - 1. The i’th element of the second row contains the packing of the i’th element of the rows r + step through r + step + P::WIDTH - 1. If at some point we exceed the height of the matrix, wrap around and include initial rows.
Sourcefn vertically_strided(
self,
stride: usize,
offset: usize,
) -> VerticallyStridedMatrixView<Self>where
Self: Sized,
fn vertically_strided(
self,
stride: usize,
offset: usize,
) -> VerticallyStridedMatrixView<Self>where
Self: Sized,
Returns a view over a vertically strided submatrix.
The view selects rows using r = offset + i * stride
for each i
.
Sourcefn columnwise_dot_product<EF>(&self, v: &[EF]) -> Vec<EF>where
T: Field,
EF: ExtensionField<T>,
fn columnwise_dot_product<EF>(&self, v: &[EF]) -> Vec<EF>where
T: Field,
EF: ExtensionField<T>,
Compute Mᵀv, aka premultiply this matrix by the given vector,
aka scale each row by the corresponding entry in v
and take the sum across rows.
v
can be a vector of extension elements.
Sourcefn rowwise_packed_dot_product<EF>(
&self,
vec: &[EF::ExtensionPacking],
) -> impl IndexedParallelIterator<Item = EF>where
T: Field,
EF: ExtensionField<T>,
fn rowwise_packed_dot_product<EF>(
&self,
vec: &[EF::ExtensionPacking],
) -> impl IndexedParallelIterator<Item = EF>where
T: Field,
EF: ExtensionField<T>,
Compute the matrix vector product M . vec
, aka take the dot product of each
row of M
by vec
. If the length of vec
is longer than the width of M
,
vec
is truncated to the first width()
elements.
We make use of PackedFieldExtension
to speed up computations. Thus vec
is passed in as
a slice of PackedFieldExtension
elements.
§Panics
This function panics if the length of vec
is less than self.width().div_ceil(T::Packing::WIDTH)
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.