Visualizing Linear Algebra Decompositions#

In this notebook we just demonstrate the utility function xyzpy.visualize_matrix on various linear algebra decompositions taken from scipy. This function plots matrices with the values of numbers directly mapped to color. By default, complex phase gives the hue, with

  • real positive = blue

  • real negative = orange

  • imaginary positive = purple

  • imaginary negative = green

whereas the magnitude gives the saturation, such that \(|z| \sim 0\) gives white.

%config InlineBackend.figure_formats = ['svg']
import xyzpy as xyz
import numpy as np
import scipy.linalg as sla

rng = np.random.default_rng(42)

First we’ll start with a non-symmetric random matrix with some small complex parts:

X = (
    0.1 * rng.normal(size=(20, 20)) +
    0.01j * rng.normal(size=(20, 20))
)
xyz.visualize_matrix(X, figsize=(2, 2));
../_images/c087f394c2f6f595ccf253ca1a75e0c9a4ea94678bcb79817def692032fd7647.svg

Singular Value Decomposition#

xyz.visualize_matrix(sla.svd(X), figsize=(8, 6));
../_images/70567b76ff019add8f52989ebf74ef847292cf915a4f34de2ac22350b6aa1564.svg

The 1D array of real singular values in decreasing magnitude is shown as a diagonal.

Eigen-decomposition#

xyz.visualize_matrix(sla.eig(X), figsize=(6, 4));
../_images/3addbf024c257b51ba2a7279c887620223c05c249738f7018d8822499000a0e2.svg

Here we see the introduction of many complex numbers far from the real axis.

Schur decomposition#

xyz.visualize_matrix(sla.schur(X), figsize=(6, 4));
../_images/0d84b18263f4ddb38975ea384d9d32d237c9927d989a0b7cfec58d8b7379e1b1.svg

If you look closely here at the color sequence of the left diagonal it follows the eigen decomposition.

xyz.visualize_matrix(sla.schur(X.real), figsize=(6, 4));
../_images/2c24fe1d838741929032f4dfb800469c38c1ab5c99732fe48e54f8ee52478428.svg

QR Decomposition#

xyz.visualize_matrix(sla.qr(X), figsize=(6, 4));
../_images/57475e98ae2ea3927db2f43d050ddb1dc06c55207c7181cfcdeb698244fc81bd.svg

Polar Decomposition#

xyz.visualize_matrix(sla.polar(X), figsize=(6, 4));
../_images/9017af080e40e7161e6cc926f4f34d759329d172a3f88affdf4dbc863ddd2b4e.svg

LU Decomposition#

xyz.visualize_matrix(sla.lu(X), figsize=(8, 6));
../_images/217aec840ff7d3e6719e948f0d434c23f9772037facee1a97183b2bb9a28fb00.svg

Multiplying the left matrix in reorders the rows of the \(L\) factor:

xyz.visualize_matrix(sla.lu(X, permute_l=True), figsize=(6, 4));
../_images/7ae2ad63dacac7ae0f31616c1ffecb695156543017b14e4a27b049e3d7bb271d.svg