-
Notifications
You must be signed in to change notification settings - Fork 0
/
algebra.py
60 lines (47 loc) · 1.95 KB
/
algebra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
from numpy.typing import NDArray
def has_leftinverse(matrix: NDArray) -> bool:
"""Returns whether the matrix is left-invertible. That is, it returns whether
a matrix A' exists to the given matrix A such that A'Ax=x for all x.
Args:
matrix (NDArray): Matrix A as numpy array.
Returns:
bool: Whether the given matrix A has a left inverse.
"""
# A matrix can only have a left-inverse if it is of full column rank.
m, n = matrix.shape # rows, columns
_, s, _ = np.linalg.svd(matrix)
rank = np.sum(s > np.finfo(matrix.dtype).eps)
return rank == n and n <= m
def random_orthogonal_matrix(
n: int, complex: bool = False, seed: int = None
) -> NDArray:
"""Random orthogonal matrix distributed with Haar measure.
Returns a random orthogonal matrix. To ensure randomness, we have to choose
from the distribution created by the Haar measure. The calculation follows
the results of:
Mezzadri, Francesco: How to generate random matrices from the classical
compact groups. In: NOTICES of the AMS, Vol. 54 (54 (2007).
URL: https://arxiv.org/pdf/math-ph/0609050.
Args:
n (int): Matrix returned has dimensions nxn.
complex (bool, optional): Whether or not the returned matrix contains complex numbers. Defaults to False.
seed (int, optional): If int the seed to generate reproducible results. Defaults to None.
Returns:
NDArray: Random orthogonal matrix distributed with Haar measure.
"""
if not seed is None:
np.random.seed(seed)
# The original algorithm provided by Mezzari's is only defined for complex
# initialization.
if complex:
z = (np.random.randn(n, n) + 1j * np.random.randn(n, n)) / np.lib.scimath.sqrt(
2.0
)
else:
z = np.random.randn(n, n)
q, r = np.linalg.qr(z)
d = np.diagonal(r)
ph = d / np.absolute(d)
q = np.multiply(q, ph, q)
return q