Skip to main content

Cholesky

Struct Cholesky 

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

The upper Cholesky factor R of a symmetric positive definite A = RᵀR, kept up to date under A ← A ± x xᵀ (Chapter 12.2: Algorithms F, U, C).

use xingo_statla::{CholOptions, Cholesky, MatRef};
// A = [[4, 2], [2, 3]] (upper triangle read).
let a = [4.0, 0.0, 2.0, 3.0];
let mut c = Cholesky::factor(MatRef::new(2, 2, 2, &a)?, &CholOptions::default())?;
c.update(&[1.0, 1.0])?; // A + x xᵀ = [[5, 3], [3, 4]]
let info = c.downdate(&[1.0, 1.0])?; // back to A
assert!(info.margin > 0.0 && info.margin <= 1.0);
let mut b = [6.0, 5.0]; // A · (1, 1)
c.solve(&mut b)?;
assert!((b[0] - 1.0).abs() < 1e-14 && (b[1] - 1.0).abs() < 1e-14);
assert!(c.downdate(&[3.0, 0.0]).is_err()); // A − 9 e₀e₀ᵀ is indefinite

Fields§

§r: DenseBlock<T>§kernel: K§opts: CholOptions

Implementations§

Source§

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

Source

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

Factor the symmetric positive definite A (upper triangle read) with the reference kernel.

§Errors

StatlaError::InvalidArgument (not square, invalid options), StatlaError::NonFinite, StatlaError::NotPositiveDefinite.

Source§

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

Source

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

As Cholesky::factor with the dense kernel kernel.

§Errors

As Cholesky::factor.

Source

pub fn from_upper( r: DenseBlock<T>, kernel: K, opts: &CholOptions, ) -> Result<Self, StatlaError>

Adopt an existing upper factor (for instance the R of a crate::Qr: the Q-less QR of streaming regression is this type).

§Errors

StatlaError::InvalidArgument if r is not square upper triangular with positive diagonal (or the options are invalid); StatlaError::NonFinite.

Source

pub fn n(&self) -> usize

Order n.

Source

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

R (the strict lower triangle of the view is zero).

Source

pub fn into_upper(self) -> DenseBlock<T>

R, owned.

Source

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

A ← A + x xᵀ (Algorithm U).

§Errors

StatlaError::Dimension, StatlaError::NonFinite; the factor is unchanged on error (H3).

Source

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

A ← A − x xᵀ (Algorithm C).

§Errors

StatlaError::Dimension, StatlaError::NonFinite, StatlaError::DowndateIndefinite; unchanged on error (H3).

Source

fn forward(&self, b: &mut [T])

b ← R⁻ᵀ b without checks (b.len() = n).

Source

fn backward(&self, b: &mut [T])

b ← R⁻¹ b without checks.

Source

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

b ← A⁻¹ b.

§Errors

StatlaError::Dimension.

Source

pub fn solve_block(&self, b: MatMut<'_, T>) -> Result<(), StatlaError>

B ← A⁻¹ B for a block of right-hand sides.

§Errors

StatlaError::Dimension.

Source

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

b ← R⁻ᵀ b (whitening: ‖R⁻ᵀ b‖² = bᵀA⁻¹b, a Mahalanobis distance).

§Errors

StatlaError::Dimension.

Source

pub fn log2_det_parts(&self) -> (T, i64)

ln det A = 2 Σ ln r_jj, returned as (m, e) with det R = Π r_jj = m · 2^e exactly split, m ∈ [1, 2) ((1, 0) for n = 0): no ln in Real, so the caller forms ln det A = 2 (ln m + e ln 2) in its own precision (log₂ det R = log₂ m + e).

Source

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

Invariant (H1).

§Errors

The text of the violated invariant.

Trait Implementations§

Source§

impl<T: Clone + Real, K: Clone + DenseKernel<T>> Clone for Cholesky<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 Cholesky<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 Cholesky<T, K>
where K: Freeze,

§

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

§

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

§

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

§

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

§

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

§

impl<T, K> UnwindSafe for Cholesky<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.