random_spd_matrix¶
- probnum.problems.zoo.linalg.random_spd_matrix(rng, dim, spectrum=None)[source]¶
Random symmetric positive definite matrix.
Constructs a random symmetric positive definite matrix from a given spectrum. An orthogonal matrix \(Q\) with \(\operatorname{det}(Q)\) (a rotation) is sampled with respect to the Haar measure and the diagonal matrix containing the eigenvalues is rotated accordingly resulting in \(A=Q \operatorname{diag}(\lambda_1, \dots, \lambda_n)Q^\top\). If no spectrum is provided, one is randomly drawn from a Gamma distribution.
- Parameters
- Return type
See also
random_sparse_spd_matrix
Generate a random sparse symmetric positive definite matrix.
Examples
>>> import numpy as np >>> from probnum.problems.zoo.linalg import random_spd_matrix >>> rng = np.random.default_rng(1) >>> mat = random_spd_matrix(rng, dim=5) >>> mat array([[10.24394619, 0.05484236, 0.39575826, -0.70032495, -0.75482692], [ 0.05484236, 11.31516868, 0.6968935 , -0.13877394, 0.52783063], [ 0.39575826, 0.6968935 , 11.5728974 , 0.21214568, 1.07692458], [-0.70032495, -0.13877394, 0.21214568, 9.88674751, -1.09750511], [-0.75482692, 0.52783063, 1.07692458, -1.09750511, 10.193655 ]])
Check for symmetry and positive definiteness.
>>> np.all(mat == mat.T) True >>> np.linalg.eigvals(mat) array([ 8.09147328, 12.7635956 , 10.84504988, 10.73086331, 10.78143272])