pub struct Matrix<T> { /* private fields */ }Expand description
A dense 2D matrix in column-major layout.
Access elements with m[[row, col]] syntax. Data is stored contiguously
in column-major order, so flat buffers use row + nrows * col.
§Examples
use tensor4all_tensorbackend::Matrix;
let mut m = Matrix::zeros(2, 3);
m[[0, 1]] = 5.0_f64;
assert_eq!(m[[0, 1]], 5.0);
assert_eq!(m[[0, 0]], 0.0);
assert_eq!(m.nrows(), 2);
assert_eq!(m.ncols(), 3);Implementations§
Source§impl<T> Matrix<T>
impl<T> Matrix<T>
Sourcepub fn try_from_col_major_vec(
nrows: usize,
ncols: usize,
data: Vec<T>,
) -> Result<Self, MatrixShapeError>
pub fn try_from_col_major_vec( nrows: usize, ncols: usize, data: Vec<T>, ) -> Result<Self, MatrixShapeError>
Fallibly create a matrix from column-major data after checked shape validation.
§Errors
Returns MatrixShapeError::ShapeOverflow when the shape exceeds
usize, or MatrixShapeError::DataLengthMismatch when the payload
length does not match the shape.
Sourcepub fn from_col_major_vec(nrows: usize, ncols: usize, data: Vec<T>) -> Self
pub fn from_col_major_vec(nrows: usize, ncols: usize, data: Vec<T>) -> Self
Create a matrix from raw column-major data.
§Panics
Panics if nrows * ncols overflows or if data.len() != nrows * ncols.
§Examples
use tensor4all_tensorbackend::Matrix;
let m = Matrix::from_col_major_vec(2, 2, vec![1.0, 3.0, 2.0, 4.0]);
assert_eq!(m[[0, 0]], 1.0);
assert_eq!(m[[0, 1]], 2.0);
assert_eq!(m[[1, 0]], 3.0);
assert_eq!(m[[1, 1]], 4.0);Sourcepub fn as_col_major_slice(&self) -> &[T]
pub fn as_col_major_slice(&self) -> &[T]
View the underlying column-major data as a contiguous slice.
§Examples
use tensor4all_tensorbackend::Matrix;
let m = Matrix::from_col_major_vec(2, 2, vec![1, 3, 2, 4]);
assert_eq!(m.as_col_major_slice(), &[1, 3, 2, 4]);Sourcepub fn as_col_major_mut_slice(&mut self) -> &mut [T]
pub fn as_col_major_mut_slice(&mut self) -> &mut [T]
View the underlying column-major data as a mutable contiguous slice.
The slice uses row + nrows * col ordering. This is useful for kernels
that validate dimensions once and then operate over contiguous columns.
§Examples
use tensor4all_tensorbackend::Matrix;
let mut m = Matrix::from_col_major_vec(2, 2, vec![1, 3, 2, 4]);
m.as_col_major_mut_slice()[1] = 30;
assert_eq!(m[[1, 0]], 30);Sourcepub fn into_col_major_vec(self) -> Vec<T>
pub fn into_col_major_vec(self) -> Vec<T>
Consume the matrix and return its owned column-major buffer.
The returned buffer uses row + nrows * col ordering. Use this when
transferring matrix storage to another column-major dense container
without cloning.
§Examples
use tensor4all_tensorbackend::Matrix;
let m = Matrix::from_col_major_vec(2, 2, vec![1.0, 3.0, 2.0, 4.0]);
let data = m.into_col_major_vec();
assert_eq!(data, vec![1.0, 3.0, 2.0, 4.0]);Sourcepub fn to_typed_tensor(&self) -> TypedTensor<T>where
T: TensorScalar,
pub fn to_typed_tensor(&self) -> TypedTensor<T>where
T: TensorScalar,
Borrow this matrix as an owned tenferro [TypedTensor].
This clones the matrix buffer and preserves column-major layout. Use
Matrix::into_typed_tensor when the matrix can be consumed.
§Examples
use tensor4all_tensorbackend::Matrix;
let m = Matrix::from_col_major_vec(2, 2, vec![1.0_f64, 3.0, 2.0, 4.0]);
let tensor = m.to_typed_tensor();
assert_eq!(tensor.shape(), &[2, 2]);
assert_eq!(tensor.as_slice()?, &[1.0, 3.0, 2.0, 4.0]);
assert_eq!(m.as_col_major_slice(), &[1.0, 3.0, 2.0, 4.0]);Sourcepub fn into_typed_tensor(self) -> TypedTensor<T>where
T: TensorScalar,
pub fn into_typed_tensor(self) -> TypedTensor<T>where
T: TensorScalar,
Consume this matrix as a tenferro [TypedTensor] without cloning.
The tensor shape is [nrows, ncols], and the owned data remains in
column-major layout.
§Examples
use tensor4all_tensorbackend::Matrix;
let m = Matrix::from_col_major_vec(2, 2, vec![1.0_f64, 3.0, 2.0, 4.0]);
let tensor = m.into_typed_tensor();
assert_eq!(tensor.shape(), &[2, 2]);
assert_eq!(tensor.as_slice()?, &[1.0, 3.0, 2.0, 4.0]);Sourcepub fn try_from_typed_tensor(
tensor: TypedTensor<T>,
) -> Result<Self, MatrixTensorConversionError>where
T: TensorScalar + Clone,
pub fn try_from_typed_tensor(
tensor: TypedTensor<T>,
) -> Result<Self, MatrixTensorConversionError>where
T: TensorScalar + Clone,
Consume a rank-2 tenferro [TypedTensor] as a Matrix.
The input tensor must have shape [nrows, ncols] and an owned host
buffer. The buffer is reused without cloning and interpreted as
column-major matrix storage.
§Errors
Returns MatrixTensorConversionError::Rank if the tensor is not
rank-2, or MatrixTensorConversionError::HostBuffer if tenferro
cannot export the tensor as an owned host buffer.
§Examples
use tenferro::TypedTensor;
use tensor4all_tensorbackend::Matrix;
let tensor = TypedTensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 3.0, 2.0, 4.0])?;
let m = Matrix::try_from_typed_tensor(tensor)?;
assert_eq!(m.nrows(), 2);
assert_eq!(m.ncols(), 2);
assert_eq!(m[[0, 1]], 2.0);Source§impl<T: Clone + Zero> Matrix<T>
impl<T: Clone + Zero> Matrix<T>
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Matrix<T>
impl<T> RefUnwindSafe for Matrix<T>where
T: RefUnwindSafe,
impl<T> Send for Matrix<T>where
T: Send,
impl<T> Sync for Matrix<T>where
T: Sync,
impl<T> Unpin for Matrix<T>where
T: Unpin,
impl<T> UnsafeUnpin for Matrix<T>
impl<T> UnwindSafe for Matrix<T>where
T: 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T, U> Imply<T> for U
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