random_linear_system¶
- probnum.problems.zoo.linalg.random_linear_system(rng, matrix, solution_rv=None, **kwargs)[source]¶
Random linear system.
Generate a random linear system from a (random) matrix. If
matrix
is a callable instead of a matrix or linear operator, the system matrix is sampled by passing the random generator instancerng
. The solution of the linear system is set to a realization fromsolution_rv
. IfNone
the solution is drawn from a standard normal distribution with iid components.- Parameters
rng (np.random.Generator) – Random number generator.
matrix (Union[LinearOperatorLike, Callable[[np.random.Generator, Optional[Any]], Union[np.ndarray, scipy.sparse.spmatrix, linops.LinearOperator]]]) – Matrix, linear operator or callable returning either for a given random number generator instance.
solution_rv (Optional[randvars.RandomVariable]) – Random variable from which the solution of the linear system is sampled.
kwargs – Miscellaneous arguments passed onto the matrix-generating callable
matrix
.
- Return type
See also
random_spd_matrix
Generate a random symmetric positive-definite matrix.
random_sparse_spd_matrix
Generate a random sparse symmetric positive-definite matrix.
Examples
>>> import numpy as np >>> from probnum.problems.zoo.linalg import random_linear_system >>> rng = np.random.default_rng(42)
Linear system with given system matrix.
>>> import scipy.stats >>> unitary_matrix = scipy.stats.unitary_group.rvs(dim=5, random_state=rng) >>> linsys_unitary = random_linear_system(rng, unitary_matrix) >>> np.abs(np.linalg.det(linsys_unitary.A)) 1.0
Linear system with random symmetric positive-definite matrix.
>>> from probnum.problems.zoo.linalg import random_spd_matrix >>> linsys_spd = random_linear_system(rng, random_spd_matrix, dim=2) >>> linsys_spd LinearSystem(A=array([[ 9.62543582, 3.14955953], [ 3.14955953, 13.28720426]]), b=array([-2.7108139 , 1.10779288]), solution=array([-0.33488503, 0.16275307]))
Linear system with random sparse matrix.
>>> import scipy.sparse >>> random_sparse_matrix = lambda rng,m,n: scipy.sparse.random(m=m, n=n, random_state=rng) >>> linsys_sparse = random_linear_system(rng, random_sparse_matrix, m=4, n=2) >>> isinstance(linsys_sparse.A, scipy.sparse.spmatrix) True