module documentation

Implementation of Singular Value Decomposition

Mostly consists of helper functions as numpy does heavy lifting. Run the module as __main__ to see and example of it in action.

Function decompose_to_matricies Using the results of a singular value decomposition (M = U S V*), returns the matrix M_i = SUM(s_i * u_i * v_i)
Function plot Visualisation of the SVD process, where a matrix M is decomposed into components M = U S V*
Function rect_diag Diagonalise vector a into a matrix of non-rectangular shape
Function svd Perform single value decomposition on array a, see https://en.wikipedia.org/wiki/Singular_value_decomposition
Variable _lgr Undocumented
def decompose_to_matricies(u: np.ndarray, s: np.ndarray, v_star: np.ndarray, small: bool = True) -> np.ndarray:

Using the results of a singular value decomposition (M = U S V*), returns the matrix M_i = SUM(s_i * u_i * v_i)

def plot(u: np.ndarray, s: np.ndarray, v_star: np.ndarray, inmat: np.ndarray, decomp: np.ndarray, recomp_n=None, f1=None, a1=None):

Visualisation of the SVD process, where a matrix M is decomposed into components M = U S V*

# ARGUMENTS #
u : np.ndarray
left singular vectors of some matrix M
s : np.ndarray
singular values of some matrix M
v : np.ndarray
right singular vectors of some matrix M
inmat : np.ndarray
The input matrix, M that u, s, v come from
decom : np.ndarray
The M_i decomposition of matrix M, where M = SUM(M_i) = SUM(s_i * u_i * v_i)
recomp_n: int | None
The number of decomposed matrices M_i to recombine for visualisation purposes
f1 : matplotlib.figure.Figure | None
Figure to put the plot axes in
a1 : Sequence[matplotlib.axes.Axes] | None
6 Axes to plot the data in
def rect_diag(a, shape):

Diagonalise vector a into a matrix of non-rectangular shape

def svd(a: np.ndarray, n: None | int = None) -> tuple[np.ndarray, np.ndarray, np.ndarray]:

Perform single value decomposition on array a, see https://en.wikipedia.org/wiki/Singular_value_decomposition

# ARGUMENTS #
a : np.ndarray
Array to perform SVD on
n : int | None
Number of SVD components to report
# RETURNS #
evecs : np.ndarray
"Left side" singular vectors, "U" in A = U S V*
singular_values : np.ndarray
Singular values of "left side" and "right side" singular vectors, S in A = U S V*
fvecs : np.ndarray
"Right side" singular vectors, V in A = U S V*
_lgr =

Undocumented