Skip to main content

Qr

Struct Qr 

Source
pub struct Qr<T: Real = f64, K: DenseKernel<T> = NaiveDense> {
    q: DenseBlock<T>,
    r: DenseBlock<T>,
    kernel: K,
    opts: QrOptions,
}
Expand description

Thin QR A = QR, Q ∈ R^{m×n} explicit with orthonormal columns, R ∈ R^{n×n} upper triangular with r_jj > 0, m ≥ n, full column rank (Chapter 12.3: Algorithms F, I, D, C, E, R).

use xingo_statla::{MatRef, Qr, QrOptions};
// A = [1 0; 1 1; 1 2] (column-major), b = (1, 2, 4).
let a = [1.0, 1.0, 1.0, 0.0, 1.0, 2.0];
let mut f = Qr::factor(MatRef::new(3, 2, 3, &a)?, &QrOptions::default())?;
let mut x = [0.0; 2];
let res = f.solve_least_squares(&[1.0, 2.0, 4.0], &mut x)?;
assert!((x[0] - 5.0 / 6.0).abs() < 1e-14 && (x[1] - 1.5).abs() < 1e-14);
assert!((res - (1.0f64 / 6.0).sqrt()).abs() < 1e-14);
f.insert_row(3, &[1.0, 3.0])?; // a fourth observation
f.delete_row(0)?; // and the first leaves
assert_eq!((f.nrows(), f.ncols()), (3, 2));
assert!(f.check_invariants(16.0).is_ok());

Fields§

§q: DenseBlock<T>§r: DenseBlock<T>§kernel: K§opts: QrOptions

Implementations§

Source§

impl<T: Real> Qr<T, NaiveDense>

Source

pub fn factor(a: MatRef<'_, T>, opts: &QrOptions) -> Result<Self, StatlaError>

Householder QR of A (m ≥ n), Q formed explicitly.

§Errors

StatlaError::InvalidArgument (m < n, invalid options), StatlaError::NonFinite, StatlaError::RankDeficient.

Source§

impl<T: Real, K: DenseKernel<T>> Qr<T, K>

Source

pub fn factor_with( a: MatRef<'_, T>, kernel: K, opts: &QrOptions, ) -> Result<Self, StatlaError>

As Qr::factor with the dense kernel kernel.

§Errors

As Qr::factor.

Source

pub fn nrows(&self) -> usize

m.

Source

pub fn ncols(&self) -> usize

n.

Source

pub fn q(&self) -> MatRef<'_, T>

Q.

Source

pub fn r(&self) -> MatRef<'_, T>

R.

Source

pub fn insert_row(&mut self, i: usize, w: &[T]) -> Result<(), StatlaError>

Insert the row w (length n) before row i (i = m appends) (Algorithm I).

§Errors

StatlaError::Dimension, StatlaError::InvalidArgument, StatlaError::NonFinite; unchanged on error (Q3).

Source

pub fn delete_row(&mut self, i: usize) -> Result<(), StatlaError>

Delete row i (requires m > n) (Algorithm D).

§Errors

StatlaError::InvalidArgument, StatlaError::RankDeficient (with column = i: the rows left are rank deficient); unchanged on error (Q3).

Source

pub fn insert_col(&mut self, j: usize, c: &[T]) -> Result<(), StatlaError>

Insert the column c (length m) before column j (requires m > n) (Algorithm C).

§Errors

StatlaError::Dimension, StatlaError::InvalidArgument, StatlaError::NonFinite, StatlaError::RankDeficient; unchanged on error.

Source

pub fn delete_col(&mut self, j: usize) -> Result<(), StatlaError>

Delete column j (Algorithm E).

§Errors

StatlaError::InvalidArgument.

Source

pub fn rank1_update(&mut self, u: &[T], v: &[T]) -> Result<(), StatlaError>

A ← A + u vᵀ (u of length m, v of length n) (Algorithm R).

§Errors

StatlaError::Dimension, StatlaError::NonFinite, StatlaError::RankDeficient (the result lost rank); unchanged on error.

Source

pub fn solve_least_squares( &self, b: &[T], x: &mut [T], ) -> Result<T, StatlaError>

x = argmin ‖A x − b‖ (x ← R⁻¹ Qᵀ b); returns ‖b − Q Qᵀ b‖, the residual norm.

§Errors

StatlaError::Dimension.

Source

pub fn to_cholesky(&self) -> Result<Cholesky<T, K>, StatlaError>
where K: Clone,

The Q-less view: a Cholesky of AᵀA holding a copy of R (with the default CholOptions).

§Errors

As Cholesky::from_upper.

Source

fn check_shape(&self) -> Result<(), &'static str>

The shape part of (Q2): consistent blocks, R upper triangular with finite entries and positive diagonal, Q finite. O(mn).

Source

pub fn check_invariants(&self, c_q: f64) -> Result<(), &'static str>

Invariants (Q1) and the shape part of (Q2), with c_Q given: ‖QᵀQ − I‖_F ≤ c_Q · m · u (computed in T, O(mn²)).

§Errors

The text of the violated invariant.

Trait Implementations§

Source§

impl<T: Clone + Real, K: Clone + DenseKernel<T>> Clone for Qr<T, K>

Source§

fn clone(&self) -> Self

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 + Real, K: Debug + DenseKernel<T>> Debug for Qr<T, K>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, K> Freeze for Qr<T, K>
where K: Freeze,

§

impl<T, K> RefUnwindSafe for Qr<T, K>

§

impl<T, K> Send for Qr<T, K>
where K: Send, T: Send,

§

impl<T, K> Sync for Qr<T, K>
where K: Sync, T: Sync,

§

impl<T, K> Unpin for Qr<T, K>
where K: Unpin, T: Unpin,

§

impl<T, K> UnsafeUnpin for Qr<T, K>
where K: UnsafeUnpin,

§

impl<T, K> UnwindSafe for Qr<T, K>
where K: UnwindSafe, 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
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.

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.