Skip to main content

Matrix

Struct Matrix 

Source
#[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).

OperationMatrixArrow 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 transposeEach 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 peersRequires a repack into a contiguous stride-aligned buffer before dispatch
Grow the row countBounded by the pre-allocated stride budget; exceeding it triggers a full reallocation and re-layout of every columnAmortised O(1) per column via Buffer::push
Drop or reorder columnsMetadata-only while callers track which columns are live; a physical reorder rebuilds the flat bufferMetadata 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: usize

Physical 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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get(&self, row: usize, col: usize) -> f64

Returns the value at (row, col) (0-based). Panics if out of bounds.

Source

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.

Source

pub fn is_empty(&self) -> bool

Returns true if the matrix is empty.

Source

pub fn len(&self) -> usize

Returns the logical number of elements (n_rows * n_cols), not including padding.

Source

pub fn as_slice(&self) -> &[f64]

Returns an immutable reference to the full flat buffer including padding.

Source

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.

Source

pub fn columns(&self) -> Vec<&[f64]>

Returns a view of the matrix as a slice of columns (logical rows only, no padding).

Source

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.

Source

pub fn col(&self, col: usize) -> &[f64]

Returns a single column as a slice, panics if col out of bounds.

Source

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.

Source

pub fn row(&self, row: usize) -> Vec<f64>

Returns a single row as an owned Vec.

Source

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.

Source

pub fn extract_rows(&self, indices: &[usize]) -> Self

Extract rows by index into a new Matrix. Column stride alignment is maintained in the result.

Source

pub fn set_name(&mut self, name: impl Into<String>)

Sets the matrix name.

Source

pub fn n_cols(&self) -> usize

Returns the number of columns.

Source

pub fn n_rows(&self) -> usize

Returns the number of rows.

Source

pub fn m(&self) -> i32

Number of rows as i32 for BLAS parameter passing.

Source

pub fn n(&self) -> i32

Number of columns as i32 for BLAS parameter passing.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 ByteSize for Matrix

Available on crate feature matrix only.
Source§

fn est_bytes(&self) -> usize

Returns the estimated byte size of this object in memory. Read more
Source§

impl Clone for Matrix

Source§

fn clone(&self) -> Matrix

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Concatenate for Matrix

Source§

fn concat(self, other: Self) -> Result<Self, MinarrowError>

Concatenates two matrices vertically (i.e., row-wise stacking).

§Requirements
  • Both matrices must have the same number of columns
§Returns

A new Matrix with rows from self followed by rows from other

§Errors
  • IncompatibleTypeError if column counts don’t match
Source§

impl Debug for Matrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&[FloatArray<f64>]> for Matrix

Source§

fn from(columns: &[FloatArray<f64>]) -> Self

Converts to this type from the input type.
Source§

impl From<&[Vec<f64>]> for Matrix

Source§

fn from(columns: &[Vec<f64>]) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<(&'a [f64], usize, usize, Option<String>)> for Matrix

Source§

fn from( (slice, n_rows, n_cols, name): (&'a [f64], usize, usize, Option<String>), ) -> Self

Converts to this type from the input type.
Source§

impl From<(Vec<FloatArray<f64>>, String)> for Matrix

Source§

fn from((columns, name): (Vec<FloatArray<f64>>, String)) -> Self

Converts to this type from the input type.
Source§

impl From<Matrix> for Value

Available on crate feature matrix only.
Source§

fn from(v: Matrix) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<FloatArray<f64>>> for Matrix

Source§

fn from(columns: Vec<FloatArray<f64>>) -> Self

Converts to this type from the input type.
Source§

impl<'a> IntoIterator for &'a Matrix

Source§

type Item = &'a f64

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, f64>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut Matrix

Source§

type Item = &'a mut f64

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, f64>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Matrix

Source§

type Item = f64

The type of the elements being iterated over.
Source§

type IntoIter = <Vec64<f64> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Matrix

Source§

fn eq(&self, other: &Matrix) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Shape for Matrix

Source§

fn shape(&self) -> ShapeDim

Returns arbitrary Shape dimension for any data shape
Source§

fn shape_1d(&self) -> usize

Returns the first dimension shape Read more
Source§

fn shape_2d(&self) -> (usize, usize)

Returns the first and second dimension shapes Read more
Source§

fn shape_3d(&self) -> (usize, usize, usize)

Returns the first, second and third dimension shapes Read more
Source§

fn shape_4d(&self) -> (usize, usize, usize, usize)

Returns the first, second, third and fourth dimension shapes Read more
Source§

impl StructuralPartialEq for Matrix

Source§

impl TryFrom<&Table> for Matrix

Source§

type Error = MinarrowError

The type returned in the event of a conversion error.
Source§

fn try_from(table: &Table) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&TableV> for Matrix

Available on crate feature views only.
Source§

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

The type returned in the event of a conversion error.
Source§

impl TryFrom<Table> for Matrix

Source§

type Error = MinarrowError

The type returned in the event of a conversion error.
Source§

fn try_from(table: Table) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Value> for Matrix

Available on crate feature matrix only.
Source§

type Error = MinarrowError

The type returned in the event of a conversion error.
Source§

fn try_from(v: Value) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CustomValue for T
where T: Any + Send + Sync + Clone + PartialEq + Debug,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Downcasts the type as Any
Source§

fn deep_clone(&self) -> Arc<dyn CustomValue>

Returns a deep clone of the object. Read more
Source§

fn eq_box(&self, other: &(dyn CustomValue + 'static)) -> bool

Performs semantic equality on the boxed object. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<I> IntoStreamingIterator for I
where I: IntoIterator,

Source§

fn into_streaming_iter(self) -> Convert<Self::IntoIter>

Source§

fn into_streaming_iter_ref<'a, T>(self) -> ConvertRef<'a, Self::IntoIter, T>
where Self: IntoIterator<Item = &'a T>, T: ?Sized,

Turns an IntoIterator of references into a StreamingIterator. Read more
Source§

fn into_streaming_iter_mut<'a, T>(self) -> ConvertMut<'a, Self::IntoIter, T>
where Self: IntoIterator<Item = &'a mut T>, T: ?Sized,

Turns an IntoIterator of mutable references into a StreamingIteratorMut. Read more
Source§

impl<T> Key for T
where T: Clone,

Source§

fn align() -> usize

The alignment necessary for the key. Must return a power of two.
Source§

fn size(&self) -> usize

The size of the key in bytes.
Source§

unsafe fn init(&self, ptr: *mut u8)

Initialize the key in the given memory location. Read more
Source§

unsafe fn get<'a>(ptr: *const u8) -> &'a T

Get a reference to the key from the given memory location. Read more
Source§

unsafe fn drop_in_place(ptr: *mut u8)

Drop the key in place. Read more
Source§

impl<T> PlanCallbackArgs for T

Source§

impl<T> PlanCallbackOut for T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more