Skip to main content

IncrementalQr

Struct IncrementalQr 

Source
pub struct IncrementalQr<T> { /* private fields */ }
Expand description

Thin QR state that can append columns without refactorizing the old block.

The state stores an explicit thin Q factor and an upper-trapezoidal R factor. Appending a full-rank block uses two backend matrix-product projection passes, factorizes only the residual block through the configured QR backend, and updates the block-triangular R.

The matrix layout is column-major throughout. The current state must have at least as many rows as columns, and appends are accepted only while the resulting factorization remains thin.

§Examples

use tensor4all_tensorbackend::{IncrementalQr, Matrix, mat_mul};

let first = Matrix::from_col_major_vec(3, 1, vec![1.0_f64, 2.0, 3.0]);
let appended = Matrix::from_col_major_vec(3, 1, vec![2.0, 0.0, 1.0]);
let mut qr = IncrementalQr::new(first).unwrap();
qr.append(&appended).unwrap();
let reconstructed = mat_mul(&qr.q(), &qr.r()).unwrap();
assert!(reconstructed
    .as_col_major_slice()
    .iter()
    .zip([1.0, 2.0, 3.0, 2.0, 0.0, 1.0])
    .all(|(actual, expected)| (actual - expected).abs() < 1.0e-12));

Implementations§

Source§

impl<T> IncrementalQr<T>

Source

pub fn from_factors( q: Matrix<T>, r: Matrix<T>, ) -> Result<Self, BackendLinalgError>

Resume an incremental QR update from compatible thin factors.

§Arguments
  • q - Existing column-major m × p thin factor.
  • r - Existing column-major p × n upper-trapezoidal factor, where n >= p.
§Returns

An update state whose next append extends the represented factorization.

§Errors

Returns a backend error when the factors are empty, have incompatible dimensions, are not thin, or backend QR/multiplication fails.

§Examples
use tensor4all_tensorbackend::{IncrementalQr, Matrix};

let state = IncrementalQr::from_factors(
    Matrix::from_col_major_vec(2, 1, vec![1.0_f64, 0.0]),
    Matrix::from_col_major_vec(1, 1, vec![2.0]),
)
.unwrap();
assert_eq!(state.q().ncols(), 1);
assert_eq!(state.r().nrows(), 1);
Source

pub fn new(input: Matrix<T>) -> Result<Self, BackendLinalgError>

Factorize a non-empty tall-or-square matrix into thin Q and square R.

§Arguments
  • input - Column-major m × n matrix with m >= n and n > 0.
§Returns

A state containing factors satisfying input = Q R up to backend floating-point error.

§Errors

Returns a backend error when the input dimensions are invalid because the matrix is empty or wide, or when backend QR conversion or factorization fails.

§Examples
use tensor4all_tensorbackend::{IncrementalQr, Matrix};

let qr = IncrementalQr::new(Matrix::from_col_major_vec(
    2,
    1,
    vec![1.0_f64, 2.0],
))
.unwrap();
assert_eq!(qr.q().nrows(), 2);
assert_eq!(qr.r().ncols(), 1);
Source

pub fn append( &mut self, new_columns: &Matrix<T>, ) -> Result<(), BackendLinalgError>

Append a column block using the existing QR state.

§Arguments
  • new_columns - Column-major m × k block with the same row count as the initial matrix and k > 0.
§Returns

Updates this state in place so that Q R represents the original matrix followed by new_columns.

§Errors

Returns a backend error when the input dimensions are invalid because row counts differ, the append is empty, or the resulting matrix would be wide; when a rank or column count overflows; or when backend matrix multiplication, conversion, or QR factorization fails.

§Examples
use tensor4all_tensorbackend::{IncrementalQr, Matrix};

let mut qr = IncrementalQr::new(Matrix::from_col_major_vec(
    3,
    1,
    vec![1.0_f64, 2.0, 3.0],
))
.unwrap();
qr.append(&Matrix::from_col_major_vec(
    3,
    1,
    vec![3.0_f64, 2.0, 1.0],
))
.unwrap();
assert_eq!(qr.r().ncols(), 2);
Source

pub fn q(&self) -> Matrix<T>

Return a copy of the current thin Q factor.

§Examples
use tensor4all_tensorbackend::{IncrementalQr, Matrix};

let qr = IncrementalQr::new(Matrix::from_col_major_vec(
    2,
    1,
    vec![1.0_f64, 0.0],
))
.unwrap();
assert_eq!(qr.q().ncols(), 1);
Source

pub fn rank(&self) -> usize

Return the current thin factor width.

This is the number of columns in both Q and the row count of R.

§Examples
use tensor4all_tensorbackend::{IncrementalQr, Matrix};

let qr = IncrementalQr::new(Matrix::from_col_major_vec(
    2,
    1,
    vec![1.0_f64, 0.0],
))
.unwrap();
assert_eq!(qr.rank(), 1);
Source

pub fn q_columns( &self, start: usize, count: usize, ) -> Result<Matrix<T>, BackendLinalgError>

Return a contiguous range of columns from the current thin Q factor.

§Arguments
  • start - Zero-based column in the current Q factor.
  • count - Number of columns to materialize.
§Returns

The requested column-major m × count block of Q.

§Errors

Returns a backend error when the requested range overflows or is out of bounds for the current thin-factor width, or when the output shape is invalid because its element count overflows.

§Examples
use tensor4all_tensorbackend::{IncrementalQr, Matrix};

let qr = IncrementalQr::new(Matrix::from_col_major_vec(
    3,
    2,
    vec![1.0_f64, 0.0, 0.0, 0.0, 1.0, 0.0],
))
.unwrap();
let second = qr.q_columns(1, 1).unwrap();
assert_eq!(second.nrows(), 3);
assert_eq!(second.ncols(), 1);
assert!((second[[1, 0]].abs() - 1.0).abs() < 1.0e-12);
Source

pub fn r(&self) -> Matrix<T>

Return a copy of the current upper-trapezoidal R factor.

§Examples
use tensor4all_tensorbackend::{IncrementalQr, Matrix};

let qr = IncrementalQr::new(Matrix::from_col_major_vec(
    2,
    1,
    vec![1.0_f64, 0.0],
))
.unwrap();
assert_eq!(qr.r().nrows(), 1);
Source

pub fn error_estimate(&self) -> Result<SrcErrorEstimate, BackendLinalgError>

Compute the Appendix C SRC estimate from the current R factor.

§Returns

The randomized residual and norm estimates associated with the current sketch width.

§Errors

Returns a backend error when the current factor is singular or contains invalid values.

§Examples
use tensor4all_tensorbackend::{IncrementalQr, Matrix};

let qr = IncrementalQr::new(Matrix::from_col_major_vec(
    2,
    1,
    vec![1.0_f64, 0.0],
))
.unwrap();
let estimate = qr.error_estimate().unwrap();
assert!(estimate.error.is_finite());
assert!(estimate.norm.is_finite());

Trait Implementations§

Source§

impl<T: Clone> Clone for IncrementalQr<T>

Source§

fn clone(&self) -> IncrementalQr<T>

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<T: Debug> Debug for IncrementalQr<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for IncrementalQr<T>

§

impl<T> RefUnwindSafe for IncrementalQr<T>
where T: RefUnwindSafe,

§

impl<T> Send for IncrementalQr<T>
where T: Send,

§

impl<T> Sync for IncrementalQr<T>
where T: Sync,

§

impl<T> Unpin for IncrementalQr<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for IncrementalQr<T>

§

impl<T> UnwindSafe for IncrementalQr<T>
where T: UnwindSafe,

Blanket Implementations§

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
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

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

§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

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
§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

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.
§

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

§

fn vzip(self) -> V