pub struct FdMatrix { /* private fields */ }Expand description
Column-major matrix for functional data.
Stores data in a flat Vec<f64> with column-major (Fortran) layout:
element (row, col) is at index row + col * nrows.
§Conventions
For functional data, rows typically represent observations and columns
represent evaluation points. For 2D surfaces with m1 x m2 grids,
the surface is flattened into m1 * m2 columns.
§Examples
use fdars_core::matrix::FdMatrix;
// 3 observations, 4 evaluation points
let data = vec![
1.0, 2.0, 3.0, // column 0 (all obs at point 0)
4.0, 5.0, 6.0, // column 1
7.0, 8.0, 9.0, // column 2
10.0, 11.0, 12.0, // column 3
];
let mat = FdMatrix::from_column_major(data, 3, 4).unwrap();
assert_eq!(mat[(0, 0)], 1.0); // obs 0 at point 0
assert_eq!(mat[(1, 2)], 8.0); // obs 1 at point 2
assert_eq!(mat.column(0), &[1.0, 2.0, 3.0]);Implementations§
Source§impl FdMatrix
impl FdMatrix
Sourcepub fn from_column_major(
data: Vec<f64>,
nrows: usize,
ncols: usize,
) -> Result<Self, FdarError>
pub fn from_column_major( data: Vec<f64>, nrows: usize, ncols: usize, ) -> Result<Self, FdarError>
Create from flat column-major data with dimension validation.
Returns Err if data.len() != nrows * ncols.
Sourcepub fn from_slice(
data: &[f64],
nrows: usize,
ncols: usize,
) -> Result<Self, FdarError>
pub fn from_slice( data: &[f64], nrows: usize, ncols: usize, ) -> Result<Self, FdarError>
Create from a borrowed slice (copies the data).
Returns Err if data.len() != nrows * ncols.
Sourcepub fn column_mut(&mut self, col: usize) -> &mut [f64]
pub fn column_mut(&mut self, col: usize) -> &mut [f64]
Sourcepub fn row(&self, row: usize) -> Vec<f64>
pub fn row(&self, row: usize) -> Vec<f64>
Extract a single row as a new Vec<f64>.
This is an O(ncols) operation because rows are not contiguous in column-major layout.
Sourcepub fn row_to_buf(&self, row: usize, buf: &mut [f64])
pub fn row_to_buf(&self, row: usize, buf: &mut [f64])
Copy a single row into a pre-allocated buffer (zero allocation).
§Panics
Panics if buf.len() < ncols or row >= nrows.
Sourcepub fn row_dot(&self, row_a: usize, other: &FdMatrix, row_b: usize) -> f64
pub fn row_dot(&self, row_a: usize, other: &FdMatrix, row_b: usize) -> f64
Compute the dot product of two rows without materializing either one.
The rows may come from different matrices (which must have the same ncols).
§Panics
Panics (in debug) if row_a >= self.nrows, row_b >= other.nrows,
or self.ncols != other.ncols.
Sourcepub fn row_l2_sq(&self, row_a: usize, other: &FdMatrix, row_b: usize) -> f64
pub fn row_l2_sq(&self, row_a: usize, other: &FdMatrix, row_b: usize) -> f64
Compute the squared L2 distance between two rows without allocation.
Equivalent to ||self.row(row_a) - other.row(row_b)||^2 but without
materializing either row vector.
§Panics
Panics (in debug) if row_a >= self.nrows, row_b >= other.nrows,
or self.ncols != other.ncols.
Sourcepub fn rows(&self) -> Vec<Vec<f64>>
pub fn rows(&self) -> Vec<Vec<f64>>
Extract all rows as Vec<Vec<f64>>.
Equivalent to the former extract_curves function.
Sourcepub fn to_row_major(&self) -> Vec<f64>
pub fn to_row_major(&self) -> Vec<f64>
Produce a single contiguous flat buffer in row-major order.
Row i occupies buf[i * ncols..(i + 1) * ncols]. This is a single
allocation versus nrows allocations from rows(), and gives better
cache locality for row-oriented iteration.
Sourcepub fn as_mut_slice(&mut self) -> &mut [f64]
pub fn as_mut_slice(&mut self) -> &mut [f64]
Mutable flat slice of the underlying column-major data.
Sourcepub fn to_dmatrix(&self) -> DMatrix<f64>
pub fn to_dmatrix(&self) -> DMatrix<f64>
Convert to a nalgebra DMatrix<f64>.
This copies the data into nalgebra’s storage. Both use column-major layout, so the copy is a simple memcpy.
Sourcepub fn from_dmatrix(mat: &DMatrix<f64>) -> Self
pub fn from_dmatrix(mat: &DMatrix<f64>) -> Self
Create from a nalgebra DMatrix<f64>.
Both use column-major layout so this is a direct copy.
Trait Implementations§
impl StructuralPartialEq for FdMatrix
Auto Trait Implementations§
impl Freeze for FdMatrix
impl RefUnwindSafe for FdMatrix
impl Send for FdMatrix
impl Sync for FdMatrix
impl Unpin for FdMatrix
impl UnsafeUnpin for FdMatrix
impl UnwindSafe for FdMatrix
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.