mojolearn.Cholesky#

class mojolearn.Cholesky(jitter=None)[source]#

Bases: NumericModeMixin

Cholesky factorization A + jitter * I = L @ L.T of a symmetric positive-definite matrix, then solves.

fit factors a given float32 matrix once; solve answers A X = B from the factor and logdet_ gives the log-determinant. A failed factorization is not an exception: check info_ (0 means success); a failed factor’s solve and logdet_ refuse. The factor can be saved with save and loaded with load, including on a CPU-only install, where fit and solve also run.

Parameters:

jitter (float or None, default None) – Ridge added to the diagonal before factoring. None uses the profile’s fixed ridge (see profile_jitter); 0.0 adds none. Any other value is refused, because the identical mode fixes the ridge to those two values. A non-number raises TypeError.

Variables:
  • L_ (mojolearn.Array, shape (n, n), float32) – Lower-triangular factor, strict upper triangle zero. Meaningful only when info_ == 0.

  • info_ (int) – LAPACK-style status: 0 for a complete factor; k > 0 means the leading minor of order k is not positive definite and L_ is partial.

  • nb_ (int) – Panel width used by the factorization.

  • logdet_ (float) – log |A + jitter * I|. Raises ValueError after a failed fit.

  • jitter_ (float) – The ridge that was added.

  • n_ (int) – Matrix size.

Notes

A non-square A or a right-hand side whose row count is not n raises ValueError; non-finite or non-symmetric matrices are refused before any work.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> M = rng.random((64, 64), dtype=np.float32)
>>> A = (M @ M.T + 64 * np.eye(64, dtype=np.float32)).astype(np.float32)
>>> chol = mojolearn.Cholesky(jitter=0.0).fit(A)
>>> x = np.asarray(chol.solve(np.ones(64, dtype=np.float32)))
__init__(jitter=None)[source]#

Methods

fit(A)

Factor A + jitter * I; returns self.

get_params([deep])

load(path)

Load a factor written by save.

numeric_mode_used()

The numeric mode this estimator runs in.

profile_jitter()

The profile's fixed ridge, used when jitter=None.

save(path)

Write the factor to path as an .npz file.

set_params(**params)

Set constructor parameters.

solve(B)

Solve A X = B from the factor.

vendor_used()

Accelerator API of the loaded binary, read back from it; "cpu" on a CPU-only install.

Attributes

logdet_

log |A + jitter * I|; raises ValueError after a failed fit.

vendor_used()[source]#

Accelerator API of the loaded binary, read back from it; “cpu” on a CPU-only install.

profile_jitter()[source]#

The profile’s fixed ridge, used when jitter=None.

fit(A)[source]#

Factor A + jitter * I; returns self.

A is a square 2-D float32 matrix, otherwise ValueError. A failed factorization sets info_ != 0 instead of raising.

property logdet_#

log |A + jitter * I|; raises ValueError after a failed fit.

solve(B)[source]#

Solve A X = B from the factor.

Parameters:

B (float32 array, shape (n,) or (n, nrhs))

Returns:

X (float32 array, the shape of B)

Raises:

ValueError – Before fit, or when B has the wrong row count or no columns. Solving against a failed factor is also refused.

save(path)[source]#

Write the factor to path as an .npz file.

Stores the factor, info_, nb_, logdet_, the jitter and the numeric mode. A failed factor saves too; its solve still refuses. Raises RuntimeError before fit.

classmethod load(path)[source]#

Load a factor written by save.

The result can solve and report logdet_; it holds no matrix and does not refactor. A file saved by another estimator, or with mismatched shapes or dtypes, raises ValueError.