#[repr(C, align(64))]pub struct Matrix {
pub n_rows: usize,
pub n_cols: usize,
pub stride: usize,
pub data: Buffer<f64>,
pub name: Option<String>,
}Expand description
§Matrix
Column-major dense matrix.
§Description
This struct is compatible with Arrow, LAPACK, BLAS, and all column-major numeric routines.
This is an optional extra enabled by the matrix feature,
and is not part of the Apache Arrow framework.
§Properties
n_rows: Logical number of rows.n_cols: Number of columns.stride: Physical elements per column in the buffer. Padded to 8-element (64-byte) boundaries so every column starts SIMD-aligned. This is the BLAS leading dimension (lda). Always>= n_rows.data: Flat buffer in column-major order with stride padding.name: Optional matrix name for diagnostics.
§Null handling
- It is dense - nulls can be represented through
f64::NAN - However this is not always reliable, as a single NaN can affect vectorised calculations when integrating with various frameworks.
§Layout trade-offs vs standard Arrow tables
Matrix is shaped for OLAP-style batch compute, designed to hand memory to
LAPACK without repacking. For streaming or append-heavy workloads, stay in
a Table and promote to Matrix only at the boundary where LAPACK access is
needed (see TableV::try_as_matrix_zc).
| Operation | Matrix | Arrow Table |
|---|---|---|
| Cross-column scan at a shared row index (e.g. dot product across columns) | Strided access across the flat buffer; recovering spatial locality requires a transpose | Each column lives in its own allocation, so cross-column access spans distinct arenas |
Hand the buffer to BLAS/LAPACK expecting (ptr, lda) | Native - pass (as_slice(), stride) directly to dgemm, dsyev, and their peers | Requires a repack into a contiguous stride-aligned buffer before dispatch |
| Grow the row count | Bounded by the pre-allocated stride budget; exceeding it triggers a full reallocation and re-layout of every column | Amortised O(1) per column via Buffer::push |
| Drop or reorder columns | Metadata-only while callers track which columns are live; a physical reorder rebuilds the flat buffer | Metadata change on the FieldArray list |
Both layouts deliver fast per-column SIMD scans. They differ on row-append flexibility and on how cheaply the buffer can be handed to LAPACK.
Fields§
§n_rows: usize§n_cols: usize§stride: usizePhysical column stride in elements, padded so each column is 64-byte aligned.
data: Buffer<f64>Backing storage. Buffer<f64> carries either an owned Vec64<f64> or a
shared view into an existing allocation. The shared variant enables
zero-copy construction from upstream TableV arenas via
TableV::try_as_matrix_zc without sacrificing the dense column-major
stride layout that BLAS/LAPACK expects.
name: Option<String>Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn new(n_rows: usize, n_cols: usize, name: Option<String>) -> Self
pub fn new(n_rows: usize, n_cols: usize, name: Option<String>) -> Self
Constructs a new dense Matrix with shape and optional name. Data buffer is zeroed. Columns are padded to 64-byte alignment.
Sourcepub fn from_f64_aligned(
data: Vec64<f64>,
n_rows: usize,
n_cols: usize,
name: Option<String>,
) -> Self
pub fn from_f64_aligned( data: Vec64<f64>, n_rows: usize, n_cols: usize, name: Option<String>, ) -> Self
Constructs a Matrix from a pre-padded Vec64 buffer.
The buffer must already have stride * n_cols elements with the correct
stride layout. Use from_f64_unaligned if your data is unpadded.
Sourcepub fn from_f64_unaligned(
src: &[f64],
n_rows: usize,
n_cols: usize,
name: Option<String>,
) -> Self
pub fn from_f64_unaligned( src: &[f64], n_rows: usize, n_cols: usize, name: Option<String>, ) -> Self
Constructs a Matrix from a flat column-major buffer without stride padding. The data is re-laid out with 64-byte aligned column padding.
Sourcepub fn try_from_cols(
cols: impl AsRef<[FloatArray<f64>]>,
name: Option<String>,
) -> Result<Self, MinarrowError>
pub fn try_from_cols( cols: impl AsRef<[FloatArray<f64>]>, name: Option<String>, ) -> Result<Self, MinarrowError>
Constructs a Matrix from a slice of FloatArray<f64> columns. Each
column becomes one Matrix column; null masks are rejected so Matrix
stays dense.
impl AsRef<[FloatArray<f64>]> accepts any contiguous layout at the
call-site: &[FloatArray<f64>], &Vec<FloatArray<f64>>,
[FloatArray<f64>; N], etc.
All columns must have the same length. Columns are copied into a 64-byte-aligned column-major buffer with stride padding.
For existing Table / TableV containers prefer Matrix::try_from.
This constructor targets direct construction from already-built column
arrays.
Sourcepub fn get(&self, row: usize, col: usize) -> f64
pub fn get(&self, row: usize, col: usize) -> f64
Returns the value at (row, col) (0-based). Panics if out of bounds.
Sourcepub fn set(&mut self, row: usize, col: usize, value: f64)
pub fn set(&mut self, row: usize, col: usize, value: f64)
Sets the value at (row, col) (0-based). Panics if out of bounds. Triggers copy-on-write if the backing buffer is currently shared.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the logical number of elements (n_rows * n_cols), not including padding.
Sourcepub fn as_slice(&self) -> &[f64]
pub fn as_slice(&self) -> &[f64]
Returns an immutable reference to the full flat buffer including padding.
Sourcepub fn as_mut_slice(&mut self) -> &mut [f64]
pub fn as_mut_slice(&mut self) -> &mut [f64]
Returns a mutable reference to the full flat buffer including padding. Triggers copy-on-write if the backing buffer is currently shared.
Sourcepub fn columns(&self) -> Vec<&[f64]>
pub fn columns(&self) -> Vec<&[f64]>
Returns a view of the matrix as a slice of columns (logical rows only, no padding).
Sourcepub fn columns_mut(&mut self) -> Vec<&mut [f64]>
pub fn columns_mut(&mut self) -> Vec<&mut [f64]>
Returns a vector of mutable slices, each corresponding to a column. Triggers copy-on-write if the backing buffer is currently shared.
Sourcepub fn col(&self, col: usize) -> &[f64]
pub fn col(&self, col: usize) -> &[f64]
Returns a single column as a slice, panics if col out of bounds.
Sourcepub fn col_mut(&mut self, col: usize) -> &mut [f64]
pub fn col_mut(&mut self, col: usize) -> &mut [f64]
Returns a single column as a mutable slice, panics if col out of bounds. Triggers copy-on-write if the backing buffer is currently shared.
Sourcepub fn transpose(&self) -> Self
pub fn transpose(&self) -> Self
Transpose this matrix, returning a new Matrix with rows and columns swapped. The result has stride-aligned columns for SIMD access.
Sourcepub fn extract_rows(&self, indices: &[usize]) -> Self
pub fn extract_rows(&self, indices: &[usize]) -> Self
Extract rows by index into a new Matrix. Column stride alignment is maintained in the result.
Sourcepub fn lda(&self) -> i32
pub fn lda(&self) -> i32
Leading dimension for BLAS - equals stride, which is n_rows padded to
64-byte alignment. Pass this as the lda parameter to all BLAS/LAPACK calls.
Sourcepub fn as_strided(&self) -> (&[f64], i32)
pub fn as_strided(&self) -> (&[f64], i32)
Borrow the matrix as a (data, lda) pair suitable for BLAS/LAPACK
routines. The leading dimension travels with the slice because the
two are meaningless apart - both describe the same stride-aligned
column-major layout.
Packaging them together also avoids a borrow-checker conflict when
handing a matrix to a function that wants both values: reading
self.stride produces a Copy i32, so no outstanding borrow of the
matrix lingers once the tuple is returned.
Sourcepub fn as_mut_strided(&mut self) -> (&mut [f64], i32)
pub fn as_mut_strided(&mut self) -> (&mut [f64], i32)
Mutable counterpart to [as_strided]. Returns (data, lda) where
data is a &mut [f64] view of the backing buffer (triggering
copy-on-write if the buffer is currently shared). The leading
dimension is read as a Copy value before the mutable borrow is
created, so the returned i32 carries no borrow of self.
Sourcepub fn to_table(self, fields: Vec<Field>) -> Result<Table, MinarrowError>
pub fn to_table(self, fields: Vec<Field>) -> Result<Table, MinarrowError>
Convert this Matrix into a Table with zero-copy column sharing.
The matrix data buffer is frozen into a SharedBuffer (or reused, if it was already shared), and each column becomes a FloatArray backed by a window into that shared allocation. No data is copied.
fields must have exactly n_cols entries, providing the name and
metadata for each column.
Sourcepub fn to_table_gen(self) -> Table
pub fn to_table_gen(self) -> Table
Convert this Matrix into a Table using auto-generated column names (col_0, col_1, …).
Trait Implementations§
Source§impl Concatenate for Matrix
impl Concatenate for Matrix
Source§fn concat(self, other: Self) -> Result<Self, MinarrowError>
fn concat(self, other: Self) -> Result<Self, MinarrowError>
Source§impl From<&[FloatArray<f64>]> for Matrix
impl From<&[FloatArray<f64>]> for Matrix
Source§fn from(columns: &[FloatArray<f64>]) -> Self
fn from(columns: &[FloatArray<f64>]) -> Self
Source§impl<'a> IntoIterator for &'a Matrix
impl<'a> IntoIterator for &'a Matrix
Source§impl<'a> IntoIterator for &'a mut Matrix
impl<'a> IntoIterator for &'a mut Matrix
Source§impl IntoIterator for Matrix
impl IntoIterator for Matrix
Source§impl Shape for Matrix
impl Shape for Matrix
impl StructuralPartialEq for Matrix
Source§impl TryFrom<&TableV> for Matrix
Available on crate feature views only.
impl TryFrom<&TableV> for Matrix
views only.Source§fn try_from(view: &TableV) -> Result<Self, Self::Error>
fn try_from(view: &TableV) -> Result<Self, Self::Error>
Materialise a TableV window into a column-major Matrix. Honours
active_col_selection and slices each column at the view’s offset, so
only the windowed rows are copied. Columns must be FloatArray<f64>;
callers needing other numeric types should convert before constructing
the view.
Source§type Error = MinarrowError
type Error = MinarrowError
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§
impl<T> Allocation for T
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,
Source§impl<T> CustomValue for T
impl<T> CustomValue for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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