pub struct Matrix { /* private fields */ }Expand description
Row-major dense matrix of f64 backed by a single contiguous Vec<f64>.
The flat layout (one allocation, stride-1 row traversal) keeps every numeric
hot loop cache-friendly and auto-vectorizable — a substantial speedup over
the previous Vec<Vec<f64>> representation on large dense inputs.
§Examples
use datarust::Matrix;
let m = Matrix::new(vec![vec![1.0, 2.0], vec![3.0, 4.0]]).unwrap();
assert_eq!(m.nrows(), 2);
assert_eq!(m.ncols(), 2);
assert_eq!(m.get(0, 0), 1.0);Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn new(data: Vec<Vec<f64>>) -> Result<Self>
pub fn new(data: Vec<Vec<f64>>) -> Result<Self>
Creates a matrix from a nested vector, validating a rectangular shape.
Sourcepub fn from_rows(rows: Vec<Vec<f64>>) -> Result<Self>
pub fn from_rows(rows: Vec<Vec<f64>>) -> Result<Self>
Creates a matrix from a nested vector of rows.
Sourcepub fn from_flat(rows: usize, cols: usize, flat: Vec<f64>) -> Result<Self>
pub fn from_flat(rows: usize, cols: usize, flat: Vec<f64>) -> Result<Self>
Creates a matrix from row-major flat data of the given shape.
With the flat internal representation this stores the buffer directly, with no per-row chunking or copy.
use datarust::Matrix;
let m = Matrix::from_flat(2, 3, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
assert_eq!(m.nrows(), 2);
assert_eq!(m.ncols(), 3);
assert_eq!(m.get(1, 2), 6.0);Sourcepub fn zeros(rows: usize, cols: usize) -> Result<Self>
pub fn zeros(rows: usize, cols: usize) -> Result<Self>
Creates a matrix filled with zeros of the given shape.
Sourcepub fn as_slice(&self) -> &[f64]
pub fn as_slice(&self) -> &[f64]
Returns the underlying flat row-major data as a slice.
Elements are laid out so that element (i, j) is at index i * ncols + j.
Use this for cache-friendly, auto-vectorizable numeric loops in preference
to per-element get calls.
Sourcepub fn as_mut_slice(&mut self) -> &mut [f64]
pub fn as_mut_slice(&mut self) -> &mut [f64]
Returns the underlying flat row-major data as a mutable slice.
Sourcepub fn get(&self, i: usize, j: usize) -> f64
pub fn get(&self, i: usize, j: usize) -> f64
Returns the element at row i, column j.
§Panics
Panics if i >= nrows() or j >= ncols(). Use checked_get
for a safe alternative that returns None on out-of-bounds access.
Sourcepub fn checked_get(&self, i: usize, j: usize) -> Option<f64>
pub fn checked_get(&self, i: usize, j: usize) -> Option<f64>
Returns Some(element) at row i, column j, or None if the indices
are out of bounds.
Sourcepub fn validate_no_nan(&self) -> Result<()>
pub fn validate_no_nan(&self) -> Result<()>
Returns Ok(()) if the matrix contains no NaN values.
Sourcepub fn validate_finite(&self) -> Result<()>
pub fn validate_finite(&self) -> Result<()>
Returns Ok(()) if every matrix element is finite.
Unlike validate_no_nan, this also rejects
positive and negative infinity. Algorithms whose numerical kernels do
not define missing-value semantics should call this before fitting or
predicting.
Sourcepub fn validate_no_infinite(&self) -> Result<()>
pub fn validate_no_infinite(&self) -> Result<()>
Returns Ok(()) if the matrix contains no positive or negative
infinity. NaN values are allowed for missing-value-aware algorithms
such as imputers.
Sourcepub fn row(&self, i: usize) -> &[f64]
pub fn row(&self, i: usize) -> &[f64]
Returns the row at index i as a contiguous slice.
use datarust::Matrix;
let m = Matrix::new(vec![vec![1.0, 2.0], vec![3.0, 4.0]]).unwrap();
assert_eq!(m.row(0), &[1.0, 2.0]);Sourcepub fn col(&self, j: usize) -> Vec<f64>
pub fn col(&self, j: usize) -> Vec<f64>
Returns column j as a new vector.
use datarust::Matrix;
let m = Matrix::new(vec![vec![1.0, 2.0], vec![3.0, 4.0]]).unwrap();
assert_eq!(m.col(1), vec![2.0, 4.0]);Sourcepub fn iter_rows(&self) -> impl Iterator<Item = &[f64]>
pub fn iter_rows(&self) -> impl Iterator<Item = &[f64]>
Iterates over the rows as contiguous slices.
Sourcepub fn transpose(&self) -> Matrix
pub fn transpose(&self) -> Matrix
Returns the transpose of the matrix.
use datarust::Matrix;
let m = Matrix::new(vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]]).unwrap();
let t = m.transpose();
assert_eq!(t.nrows(), 3);
assert_eq!(t.ncols(), 2);
assert_eq!(t.get(0, 1), 4.0);Sourcepub fn matmul(&self, other: &Matrix) -> Result<Matrix>
pub fn matmul(&self, other: &Matrix) -> Result<Matrix>
Multiplies two matrices and returns the product.
use datarust::Matrix;
let a = Matrix::new(vec![vec![1.0, 2.0], vec![3.0, 4.0]]).unwrap();
let b = Matrix::new(vec![vec![5.0, 6.0], vec![7.0, 8.0]]).unwrap();
let c = a.matmul(&b).unwrap();
assert_eq!(c.get(0, 0), 19.0); // 1*5 + 2*7
assert_eq!(c.get(0, 1), 22.0); // 1*6 + 2*8Sourcepub fn column_mean(&self) -> Vec<f64>
pub fn column_mean(&self) -> Vec<f64>
Returns the mean of each column.
Sourcepub fn from_columns(cols: Vec<Vec<f64>>) -> Result<Self>
pub fn from_columns(cols: Vec<Vec<f64>>) -> Result<Self>
Creates a matrix from a vector of columns.
Sourcepub fn select_columns(&self, indices: &[usize]) -> Result<Self>
pub fn select_columns(&self, indices: &[usize]) -> Result<Self>
Select a subset of columns by index (0-based).
use datarust::Matrix;
let m = Matrix::new(vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 5.0, 6.0],
])?;
let sub = m.select_columns(&[0, 2])?;
assert_eq!(sub.ncols(), 2);
assert_eq!(sub.get(0, 0), 1.0);
assert_eq!(sub.get(0, 1), 3.0);Sourcepub fn select_rows(&self, indices: &[usize]) -> Result<Self>
pub fn select_rows(&self, indices: &[usize]) -> Result<Self>
Select a subset of rows by index (0-based).
use datarust::Matrix;
let m = Matrix::new(vec![
vec![10.0],
vec![20.0],
vec![30.0],
])?;
let sub = m.select_rows(&[0, 2])?;
assert_eq!(sub.nrows(), 2);
assert_eq!(sub.get(0, 0), 10.0);
assert_eq!(sub.get(1, 0), 30.0);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Matrix
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Matrix
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl StructuralPartialEq for Matrix
Source§impl TryFrom<Vec<Vec<f64>>> for Matrix
impl TryFrom<Vec<Vec<f64>>> for Matrix
Source§fn try_from(data: Vec<Vec<f64>>) -> Result<Self>
fn try_from(data: Vec<Vec<f64>>) -> Result<Self>
Fallibly construct a Matrix from a nested vector.
Returns an error if the rows are empty or jagged. This replaces the
previous panicking From impl to keep validation consistent with
Matrix::new.
Source§type Error = DatarustError
type Error = DatarustError
Auto Trait Implementations§
impl Freeze for Matrix
impl RefUnwindSafe for Matrix
impl Send for Matrix
impl Sync for Matrix
impl Unpin for Matrix
impl UnsafeUnpin for Matrix
impl UnwindSafe for Matrix
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,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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