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>where
T: IncrementalQrScalar,
impl<T> IncrementalQr<T>where
T: IncrementalQrScalar,
Sourcepub fn from_factors(
q: Matrix<T>,
r: Matrix<T>,
) -> Result<Self, BackendLinalgError>
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-majorm × pthin factor.r- Existing column-majorp × nupper-trapezoidal factor, wheren >= 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);Sourcepub fn new(input: Matrix<T>) -> Result<Self, BackendLinalgError>
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-majorm × nmatrix withm >= nandn > 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);Sourcepub fn append(
&mut self,
new_columns: &Matrix<T>,
) -> Result<(), BackendLinalgError>
pub fn append( &mut self, new_columns: &Matrix<T>, ) -> Result<(), BackendLinalgError>
Append a column block using the existing QR state.
§Arguments
new_columns- Column-majorm × kblock with the same row count as the initial matrix andk > 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);Sourcepub fn q(&self) -> Matrix<T>
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);Sourcepub fn rank(&self) -> usize
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);Sourcepub fn q_columns(
&self,
start: usize,
count: usize,
) -> Result<Matrix<T>, BackendLinalgError>
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 currentQfactor.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);Sourcepub fn r(&self) -> Matrix<T>
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);Sourcepub fn error_estimate(&self) -> Result<SrcErrorEstimate, BackendLinalgError>
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>
impl<T: Clone> Clone for IncrementalQr<T>
Source§fn clone(&self) -> IncrementalQr<T>
fn clone(&self) -> IncrementalQr<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto 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> 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,
impl<T, U> Imply<T> for U
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