-def random_matrix(dims):
- """
- Generate a random square matrix.
-
- Parameters
- ----------
-
- dims : int
- The number of rows/columns you want in the returned matrix.
-
- Returns
- -------
-
- matrix
- A new matrix whose entries are random floats chosen uniformly from
- the interval [-RANDOM_MAX, RANDOM_MAX].
-
- Examples
- --------
-
- >>> A = random_matrix(3)
- >>> A.size
- (3, 3)
-
- """
- return matrix([[uniform(-RANDOM_MAX, RANDOM_MAX) for _ in range(dims)]
- for _ in range(dims)])
-
-
-def random_nonnegative_matrix(dims):
- """
- Generate a random square matrix with nonnegative entries.
-
- Parameters
- ----------
-
- dims : int
- The number of rows/columns you want in the returned matrix.
-
- Returns
- -------
-
- matrix
- A new matrix whose entries are random floats chosen uniformly from
- the interval [0, RANDOM_MAX].
-
- Examples
- --------
-
- >>> A = random_nonnegative_matrix(3)
- >>> A.size
- (3, 3)
- >>> all([entry >= 0 for entry in A])
- True
-
- """
- L = random_matrix(dims)
- return matrix([abs(entry) for entry in L], (dims, dims))
-
-
-def random_diagonal_matrix(dims):
- """
- Generate a random square matrix with zero off-diagonal entries.
-
- These matrices are Lyapunov-like on the nonnegative orthant, as is
- fairly easy to see.
-
- Parameters
- ----------
-
- dims : int
- The number of rows/columns you want in the returned matrix.
-
- Returns
- -------
-
- matrix
- A new matrix whose diagonal entries are random floats chosen
- uniformly from the interval [-RANDOM_MAX, RANDOM_MAX] and whose
- off-diagonal entries are zero.
-
- Examples
- --------
-
- >>> A = random_diagonal_matrix(3)
- >>> A.size
- (3, 3)
- >>> A[0,1] == A[0,2] == A[1,0] == A[2,0] == A[1,2] == A[2,1] == 0
- True
-
- """
- return matrix([[uniform(-RANDOM_MAX, RANDOM_MAX)*int(i == j)
- for i in range(dims)]
- for j in range(dims)])
-
-
-def random_skew_symmetric_matrix(dims):