RandomMatrix#
- class xesn.RandomMatrix(n_rows, n_cols, factor, distribution, normalization='multiply', random_seed=None)#
Creates a random numpy or cupy based matrix via
__call__(). Use input arguments to control the shape, generator distribution, normalization, and the random number generator seed.- Parameters:
n_rows (int) – number of rows in the matrix
n_cols (int) – number of columns in the matrix
distribution (str) – distribution to draw elements of the matrix from, either “uniform”, or “gaussian” and “normal” are recognized
normalization (str, optional) – method used to rescale the matrix, see
normalize()factor (float, optional) – factor to rescale the matrix with after it’s been normalized
random_seed (int, optional) – used to control the RNG for matrix generation
Example
Create a 2x2 random matrix with entries from a uniform distribution ranging from -5 to 5
>>> from xesn import RandomMatrix >>> mat = RandomMatrix(2, 2, factor=5, distribution="uniform", random_seed=0) >>> A = mat() >>> A array([[0.48813504, 2.15189366], [1.02763376, 0.44883183]])
Note that every time the object is called, a new matrix is generated
>>> B = mat() >>> B array([[-0.76345201, 1.45894113], [-0.62412789, 3.91773001]])
Example
Create a 2x2 random matrix with entries from a Gaussian distribution with its spectral radius set to 0.9
>>> from xesn import RandomMatrix ; from scipy.linalg import eigvals >>> mat = RandomMatrix(2, 2, factor=0.9, distribution="gaussian", normalization="eig", random_seed=0)
or equivalently…
>>> mat = RandomMatrix(2, 2, factor=0.9, distribution="normal", normalization="eig", random_seed=0) >>> A = mat() >>> A array([[0.59414168, 0.13477495], [0.32964386, 0.75474407]]) >>> max(abs(eigvals(A))) 0.9000000000000001
Example
Create a 2x2 random matrix with entries from a Gaussian distribution with its induced 2-norm set to 1.0
>>> from xesn import RandomMatrix ; from scipy.linalg import svd >>> mat = RandomMatrix(2, 2, factor=1.0, distribution="gaussian", normalization="svd", random_seed=0) >>> A = mat() >>> A array([[0.64082821, 0.14536532], [0.35554665, 0.81405043]]) >>> max(abs(svd(A, compute_uv=False, full_matrices=False))) 1.0
Methods
The main routine to use.
Create either a uniform or normal random matrix.
Rescale a random matrix either through simple multiplication, or by first normalizing by the matrix's spectral radius or induced 2 norm.
Attributes
RandomMatrix.n_rowsRandomMatrix.n_colsRandomMatrix.distributionRandomMatrix.normalizationRandomMatrix.factorRandomMatrix.random_seedRandomMatrix.random_state