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 instance rng. The solution of the linear system is set to a realization from solution_rv. If None the solution is drawn from a standard normal distribution with iid components.

  • rng (Generator) – Random number generator.

  • matrix (LinearOperatorLike | Callable[[Generator, Any | None], ndarray | spmatrix | LinearOperator]) – Matrix, linear operator or callable returning either for a given random number generator instance.

  • solution_rv (RandomVariable | None) – 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


Generate a random symmetric positive-definite matrix.


Generate a random sparse symmetric positive-definite matrix.


>>> 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))

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)