Linear Algebraο
Inverses & Solvesο
- cola.linalg.inv(A, alg=Auto())[source]ο
(lazily) computes the inverse of a linear operator, equivalent to solve.
- Parameters:
A (LinearOperator) β The linear operator to compute the inverse of.
alg (Algorithm, optional) β The algorithm to use for the solves.
- Returns:
The inverse of the linear operator.
- Return type:
Example
>>> A = MyLinearOperator() >>> x = cola.inverse(A, alg=CG(tol=1e-3)) @ b
- cola.linalg.solve(A, b, alg=Auto())[source]ο
Computes Linear solve of a linear operator. Equivalent to cola.inv
- Parameters:
A (LinearOperator) β The linear operator to compute the inverse of.
b (Array) β The right hand side of the linear system of shape (d, ) or (d, k)
alg (Algorithm, optional) β The algorithm to use for the solves.
- Returns:
The solution of the linear system of shape (d, ) or (d, k)
- Return type:
Array
Example
>>> A = MyLinearOperator() >>> x = cola.solve(A, b, alg=Auto(max_iters=10, pbar=True))
Algorithmsο
- class cola.linalg.Auto[source]ο
Bases:
SimpleNamespace
,Algorithm
if A is PSD and small, uses Cholesky
if A is not PSD and small, uses LU
if A is PSD and large, uses CG
if A is not PSD and large, uses GMRES
- class cola.linalg.CG(tol=1e-06, max_iters=1000, pbar=False, x0=None, P=None)[source]ο
Bases:
Algorithm
Conjugate gradients algorithm Solves Ax=b or AX=B (multiple rhs).
The runtime is bounded by \(O(\sqrt{\kappa})\) and it uses \(O(n)\) memory. Where \(\kappa\) is the condition number of the linear operator. Optionally, you can use a preconditioner (approx of Aβ»ΒΉ) to accelerate convergence.
- Parameters:
tol (float, optional) β Relative error tolerance.
max_iters (int, optional) β The maximum number of iterations to run.
pbar (bool, optional) β Whether to show progress bar.
x0 (Array, optional) β (n,) or (n, b) guess for initial solution.
P (LinearOperator, optional) β Preconditioner. Defaults to the identity.
- class cola.linalg.GMRES(tol=1e-06, max_iters=1000, pbar=False, x0=None, P=None)[source]ο
Bases:
Algorithm
Generalized Minimal Residual algorith (GMRES) for soving Ax=b or AX=B (multiple rhs).
The runtime is bounded by \(O(\sqrt{\kappa})\) and it uses \(O(m n)\) memory. Where \(\kappa\) is the condition number of the linear operator, n is the size of A and m represents the max iters. Optionally, you can use a preconditioner (approx of Aβ»ΒΉ) to accelerate convergence.
- Parameters:
tol (float, optional) β Relative error tolerance for Arnoldi.
max_iters (int, optional) β The maximum number of iterations to run in Arnoldi.
pbar (bool, optional) β Whether to show progress bar.
x0 (Array, optional) β (n,) or (n, b) guess for initial solution.
P (LinearOperator, optional) β Preconditioner. Defaults to the identity.
- class cola.linalg.LU[source]ο
Bases:
Algorithm
LU algorithm for decomposing a general square operator as \(A = PLU\), where \(P\) is a permutation operator, \(L\) is a lower triangular operator and \(U\) is an upper triangular operator.
Example
>>> A = MyLinearOperator() >>> P,L,U = cola.linalg.decompositions.LU()(A)
Eigenvalues and Eigenvectorsο
- cola.linalg.eig(A, k, which='LM', alg=Auto())[source]ο
Computes eigenvalues and eigenvectors of a linear operator.
- Parameters:
A (LinearOperator) β The linear operator for which eigenvalues and eigenvectors are computed.
k (int) β The desired number of eigenvalues and eigenvectors. Must be specified.
which (str) β From what part of the spectrum would de eigenvalues be fetched. Default is βLMβ (largest in magnitude) but alternatively you can use βSMβ (smallest in magnitude).
alg (Algorithm) β (Auto, Eig, Eigh, Arnoldi, Lanczos)
- Returns:
- A tuple containing eigenvalues and eigenvectors.
The eigenvalues are given by eig_vals and the eigenvectors are given by eig_vecs.
- Return type:
Tuple[Array, LinearOperator]
Example
>>> A = MyLinearOperator() >>> eig_vals, eig_vecs = eig(A, k=6, which='LM', alg=Auto(tol=1e-4))
Algorithmsο
- class cola.linalg.Auto[source]ο
Bases:
SimpleNamespace
,Algorithm
if A is Hermitian and small, uses Eigh
if A is not Hermitian and small, use Eig
if A is Hermitian and large, uses Lanczos
if A is not Hermitian and large, uses Arnoldi
- class cola.linalg.Arnoldi(start_vector=None, max_iters=1000, tol=1e-06, pbar=False, key=None)[source]ο
Bases:
Algorithm
Arnoldi decomposition for a general square operator, \(H \approx Q^{*} A Q\) where \(H\) is an upper Hessenberg operator. This algorithm is used to approximate eig(A) through eig(H).
- Parameters:
start_vector (Array, optional) β (n,) or (n, b) vector to start the algorithm.
max_iters (int, optional) β The maximum number of iterations to run.
tol (float, optional) β Relative error tolerance.
pbar (bool, optional) β Whether to show progress bar.
key (PRNGKey, optional) β Random key to use for the algorithm. PRNGKey for jax, long integer for numpy or pytorch.
Example
>>> A = MyLinearOperator() >>> Q,H,info = Arnoldi(max_iters=100,pbar=True)(A)
- class cola.linalg.Lanczos(start_vector=None, max_iters=1000, tol=1e-06, pbar=False, key=None)[source]ο
Bases:
Algorithm
Lanczos decomposition for Symmetric (or Hermitian) operators, \(T = Q^{*} A Q\) where \(T\) is a tridiagional operator. This algorithm is used to approximate eig(A) through eig(T).
- Parameters:
start_vector (Array, optional) β (n,) or (n, b) vector to start the algorithm.
max_iters (int, optional) β The maximum number of iterations to run.
tol (float, optional) β Relative error tolerance.
pbar (bool, optional) β Whether to show progress bar.
key (PRNGKey, optional) β Random key to use for the algorithm. PRNGKey for jax, long integer for numpy or pytorch.
Example
>>> A = MyLinearOperator() >>> Q,T,info = Lanczos(max_iters=100,pbar=True)(A)
- class cola.linalg.Eig[source]ο
Bases:
Algorithm
Uses a dense eigendecomposition for a general square operator.
- class cola.linalg.Eigh[source]ο
Bases:
Algorithm
Uses a dense eigendecomposition for a real symmetric or complex Hermitian operator.
Matrix Functions: exp, log, sqrt, isqrt, etc.ο
- cola.linalg.exp(A, alg=Auto())[source]ο
Computes the matrix exponential \(\exp(A)\) of the operator \(A\).
- Parameters:
f (Callable) β The function to apply.
A (LinearOperator) β The linear operator to compute f(A) with.
alg (Algorithm) β The algorithm to use (Auto, Eig, Eigh, Lanczos, Arnoldi).
- Returns:
a lazy instantiation of \(\exp(A)\)
- Return type:
- cola.linalg.exp(A: cola.ops.operators.KronSum, alg: cola.linalg.algorithm_base.Algorithm)[source]
- cola.linalg.log(A, alg=Auto())[source]ο
Computes the matrix logarithm \(log(A)\) of positive definite operator \(A\).
- Parameters:
A (LinearOperator) β The linear operator to compute f(A) with.
alg (Algorithm) β The algorithm to use (Auto, Eig, Eigh, Lanczos, Arnoldi).
- Returns:
a lazy instantiation of log(A)
- Return type:
- cola.linalg.sqrt(A, alg=Auto())[source]ο
Computes the square root, \(A^{1/2}\) of an operator \(A\) using the principal branch.
- Parameters:
A (LinearOperator) β The linear operator to compute f(A) with.
alg (Algorithm) β The algorithm to use (Auto, Eig, Eigh, Lanczos, Arnoldi).
- Returns:
a lazy instantiation of \(A^{1/2}\)
- Return type:
- cola.linalg.isqrt(A, alg=Auto())[source]ο
Computes the matrix inverse \(A^{-1/2}\) of an operator \(A\) using the principal branch.
- Parameters:
A (LinearOperator) β The linear operator to compute f(A) with.
alg (Algorithm) β The algorithm to use (Auto, Eig, Eigh, Lanczos, Arnoldi).
- Returns:
a lazy instantiation of \(A^{-1/2}\)
- Return type:
- cola.linalg.pow(A, alpha, alg=Auto())[source]ο
Computes the matrix power \(A^{\alpha}\) of an operator \(A\), where \(\alpha\) is the coefficient.
- Parameters:
A (LinearOperator) β The linear operator to compute f(A) with.
alg (Algorithm) β The algorithm to use (Auto, Eig, Eigh, Lanczos, Arnoldi).
- Returns:
a lazy instantiation of \(A^{\alpha}\)
- Return type:
- cola.linalg.pow(A: cola.ops.operators.Kronecker, alpha: numbers.Number, alg: cola.linalg.algorithm_base.Algorithm)[source]
- cola.linalg.apply_unary(f, A, alg=Auto())[source]ο
Generic apply a unary function \(f\) to a linear operator \(A\). That is, \(f(A)\), defined through the taylor expansion:
\(f(A) = \sum_{k=0}^\infty \frac{f^{(k)}(0)}{k!}A^k\).
- Parameters:
f (Callable) β The function to apply.
A (LinearOperator) β The linear operator to compute f(A) with.
alg (Algorithm) β The algorithm to use (Auto, Eig, Eigh, Lanczos, Arnoldi).
- Returns:
a lazy instantiation of \(f(A)\)
- Return type:
Algorithmsο
- class cola.linalg.Auto[source]ο
Bases:
SimpleNamespace
,Algorithm
if A is Hermitian and small, uses Eigh
if A is not Hermitian and small, uses Eig
if A is Hermitian and large, uses Lanczos
if A is not Hermitian and large, uses Arnoldi
- class cola.linalg.Lanczos(start_vector=None, max_iters=1000, tol=1e-06, pbar=False, key=None)[source]ο
Bases:
Algorithm
Lanczos decomposition for Symmetric (or Hermitian) operators, \(T = Q^{*} A Q\) where \(T\) is a tridiagional operator. This algorithm is used to approximate eig(A) through eig(T).
- Parameters:
start_vector (Array, optional) β (n,) or (n, b) vector to start the algorithm.
max_iters (int, optional) β The maximum number of iterations to run.
tol (float, optional) β Relative error tolerance.
pbar (bool, optional) β Whether to show progress bar.
key (PRNGKey, optional) β Random key to use for the algorithm. PRNGKey for jax, long integer for numpy or pytorch.
Example
>>> A = MyLinearOperator() >>> Q,T,info = Lanczos(max_iters=100,pbar=True)(A)
- class cola.linalg.Arnoldi(start_vector=None, max_iters=1000, tol=1e-06, pbar=False, key=None)[source]ο
Bases:
Algorithm
Arnoldi decomposition for a general square operator, \(H \approx Q^{*} A Q\) where \(H\) is an upper Hessenberg operator. This algorithm is used to approximate eig(A) through eig(H).
- Parameters:
start_vector (Array, optional) β (n,) or (n, b) vector to start the algorithm.
max_iters (int, optional) β The maximum number of iterations to run.
tol (float, optional) β Relative error tolerance.
pbar (bool, optional) β Whether to show progress bar.
key (PRNGKey, optional) β Random key to use for the algorithm. PRNGKey for jax, long integer for numpy or pytorch.
Example
>>> A = MyLinearOperator() >>> Q,H,info = Arnoldi(max_iters=100,pbar=True)(A)
Trace, Diagonal, Frobenius Normο
- cola.linalg.trace(A, alg=Auto())[source]ο
Compute the trace of a linear operator tr(A).
Can use either the \(O(\tfrac{1}{\delta^2})\) time stochastic estimation (alg=Hutch()) or a deterministic \(O(n)\) time algorithm (alg =Exact()).
If only unbiased estimates of the diagonal are needed, use the Hutchinson algorithm.
- Parameters:
A (LinearOperator) β The linear operator to compute the diagonal of.
alg (Algorithm, optional) β The algorithm to use for the diagonal computation (Hutch or Exact).
- Returns:
trace
- Return type:
Array
- cola.linalg.diag(A, k=0, alg=Auto())[source]ο
Extract the (kth) diagonal of a linear operator.
Can use either the \(O(\tfrac{1}{\delta^2})\) time stochastic estimation (alg=Hutch()) or a deterministic \(O(n)\) time algorithm (alg =Exact()).
If only unbiased estimates of the diagonal are needed, use the Hutchinson algorithm.
- Parameters:
A (LinearOperator) β The linear operator to compute the diagonal of.
k (int, optional) β Specify to compute the kth off diagonal diagonal.
alg (Algorithm, optional) β The algorithm to use for the diagonal computation (Hutch or Exact).
- Returns:
diag
- Return type:
Array
Algorithmsο
- class cola.linalg.Auto[source]
Bases:
SimpleNamespace
,Algorithm
if \(\tfrac{1}{\sqrt{10n}} < \epsilon\) use Hutch \(O(\tfrac{1}{\delta^2})\)
otherwise use Exact \(O(n)\)
- class cola.linalg.Hutch(tol=0.03, max_iters=10000, bs=100, rand='normal', pbar=False, key=None)[source]ο
Hutchinsonβs algorithm for estimating the trace of a matrix function. Basically, this algorithm uses random probes to approximate \(\text{tr}(f(A))\).
- Parameters:
tol (float, optional) β Approximation relative tolerance.
max_iters (int, optional) β The maximum number of iterations to run.
bs (int, optional) β Number of probes.
rand (str, optional) β type of random probes (either Normal or Rademacher)
pbar (bool, optional) β Whether to show progress bar.
key (xnp.PRNGKey, optional) β Random key (default None).
- class cola.linalg.Exact(bs=100, pbar=False)[source]ο
Exact algorithm to compute, element-by-element, the diagonal of the matrix. That is, each element of the diagoinal \(A_{i,i}\) is computed as \(e_{i}^{T} A(e_{i})\). For efficiency, this procedure is done through blocks of elements, \(A [e_{i_{1}}, \cdots, e_{i_{B}}]\) where \(B\) is the block size.
- class cola.linalg.HutchPP(tol=0.03, max_iters=10000, bs=100, rand='normal', pbar=False, key=None)[source]ο
Hutch++ is an improvement on the Hutchinsonβs estimator introduced in Meyer et al. 2020: Hutch++: Optimal Stochastic Trace Estimator.
- Parameters:
tol (float, optional) β Approximation relative tolerance.
max_iters (int, optional) β The maximum number of iterations to run.
bs (int, optional) β Number of probes.
rand (str, optional) β type of random probes (either Normal or Rademacher)
pbar (bool, optional) β Whether to show progress bar.
key (xnp.PRNGKey, optional) β Random key (default None).
Log Determinantsο
- cola.linalg.logdet(A, log_alg=Auto(), trace_alg=Auto())[source]ο
Computes log determinant of a linear operator.
For large inputs (or with method=βiterativeβ), uses either \(O(\tfrac{1}{\delta^2}\log(1/\epsilon))\) time stochastic algorithm (SLQ) where \(\epsilon=\) tol is the bias and \(\delta=\) vtol is the standard deviation of the estimate, or a deterministic \(O(n\log(1/\epsilon))\) time algorithm if \(\delta < 1/\sqrt{10n}\).
- Parameters:
A (LinearOperator) β The linear operator to compute the logdet of.
log_alg (Algorithm, optional) β The algorithm to use for the log. Specify LU() or Cholesky() for a dense approach.
trace_alg (Algorithm, optional) β The algorithm to use for the trace computation.
- Returns:
logdet
- Return type:
Array
- cola.linalg.slogdet(A, log_alg=Auto(), trace_alg=Auto())[source]ο
Computes sign and logdet of a linear operator. such that det(A) = sign(A) exp(logdet(A))
- Parameters:
A (LinearOperator) β The linear operator to compute the logdet of.
log_alg (Algorithm, optional) β The algorithm to use for the log. Specify LU() or Cholesky() for a dense approach.
trace_alg (Algorithm, optional) β The algorithm to use for the trace computation.
- Returns:
sign, logdet: logdet
- Return type:
Tuple[Array, Array]
#
Log Algsο
- class cola.linalg.Auto[source]ο
Bases:
SimpleNamespace
,Algorithm
if A is PSD and small, uses Cholesky
if A is not PSD and small, uses LU
if A is PSD and large, uses Lanczos
if A is not PSD and large, uses Arnoldi
- class cola.linalg.Lanczos(start_vector=None, max_iters=1000, tol=1e-06, pbar=False, key=None)[source]ο
Bases:
Algorithm
Lanczos decomposition for Symmetric (or Hermitian) operators, \(T = Q^{*} A Q\) where \(T\) is a tridiagional operator. This algorithm is used to approximate eig(A) through eig(T).
- Parameters:
start_vector (Array, optional) β (n,) or (n, b) vector to start the algorithm.
max_iters (int, optional) β The maximum number of iterations to run.
tol (float, optional) β Relative error tolerance.
pbar (bool, optional) β Whether to show progress bar.
key (PRNGKey, optional) β Random key to use for the algorithm. PRNGKey for jax, long integer for numpy or pytorch.
Example
>>> A = MyLinearOperator() >>> Q,T,info = Lanczos(max_iters=100,pbar=True)(A)
- class cola.linalg.Arnoldi(start_vector=None, max_iters=1000, tol=1e-06, pbar=False, key=None)[source]ο
Bases:
Algorithm
Arnoldi decomposition for a general square operator, \(H \approx Q^{*} A Q\) where \(H\) is an upper Hessenberg operator. This algorithm is used to approximate eig(A) through eig(H).
- Parameters:
start_vector (Array, optional) β (n,) or (n, b) vector to start the algorithm.
max_iters (int, optional) β The maximum number of iterations to run.
tol (float, optional) β Relative error tolerance.
pbar (bool, optional) β Whether to show progress bar.
key (PRNGKey, optional) β Random key to use for the algorithm. PRNGKey for jax, long integer for numpy or pytorch.
Example
>>> A = MyLinearOperator() >>> Q,H,info = Arnoldi(max_iters=100,pbar=True)(A)
- class cola.linalg.Cholesky[source]ο
Bases:
Algorithm
Cholesky algorithm for decomposing a positive definite operator as \(A = L L^{*}\), where \(L\) is a lower triangular operator.
Example
>>> A = MyLinearOperator() >>> L = cola.linalg.decompositions.Cholesky()(A)
- class cola.linalg.LU[source]ο
Bases:
Algorithm
LU algorithm for decomposing a general square operator as \(A = PLU\), where \(P\) is a permutation operator, \(L\) is a lower triangular operator and \(U\) is an upper triangular operator.
Example
>>> A = MyLinearOperator() >>> P,L,U = cola.linalg.decompositions.LU()(A)
Trace Algsο
if \(\tfrac{1}{\sqrt{10n}} < \epsilon\) use Hutch \(O(\tfrac{1}{\delta^2})\)
otherwise use Exact \(O(n)\)
- class cola.linalg.Hutch(tol=0.03, max_iters=10000, bs=100, rand='normal', pbar=False, key=None)[source]ο
Hutchinsonβs algorithm for estimating the trace of a matrix function. Basically, this algorithm uses random probes to approximate \(\text{tr}(f(A))\).
- Parameters:
tol (float, optional) β Approximation relative tolerance.
max_iters (int, optional) β The maximum number of iterations to run.
bs (int, optional) β Number of probes.
rand (str, optional) β type of random probes (either Normal or Rademacher)
pbar (bool, optional) β Whether to show progress bar.
key (xnp.PRNGKey, optional) β Random key (default None).
- class cola.linalg.Exact(bs=100, pbar=False)[source]ο
Exact algorithm to compute, element-by-element, the diagonal of the matrix. That is, each element of the diagoinal \(A_{i,i}\) is computed as \(e_{i}^{T} A(e_{i})\). For efficiency, this procedure is done through blocks of elements, \(A [e_{i_{1}}, \cdots, e_{i_{B}}]\) where \(B\) is the block size.
- class cola.linalg.HutchPP(tol=0.03, max_iters=10000, bs=100, rand='normal', pbar=False, key=None)[source]ο
Hutch++ is an improvement on the Hutchinsonβs estimator introduced in Meyer et al. 2020: Hutch++: Optimal Stochastic Trace Estimator.
- Parameters:
tol (float, optional) β Approximation relative tolerance.
max_iters (int, optional) β The maximum number of iterations to run.
bs (int, optional) β Number of probes.
rand (str, optional) β type of random probes (either Normal or Rademacher)
pbar (bool, optional) β Whether to show progress bar.
key (xnp.PRNGKey, optional) β Random key (default None).