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.

[1]:
import xyzpy as xyz
import numpy as np
import scipy.linalg as sla

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

[2]:
X = np.random.randn(20, 20) + 0.01j * np.random.rand(20, 20)
xyz.visualize_matrix(X, figsize=(2, 2))
[2]:
../_images/examples_visualize_matrix_3_0.png

Singular Value Decomposition#

[3]:
xyz.visualize_matrix(sla.svd(X), gridsize=(1, 3), figsize=(6, 6))
[3]:
../_images/examples_visualize_matrix_5_0.png

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

Eigen-decomposition#

[4]:
xyz.visualize_matrix(sla.eig(X), figsize=(4, 4))
[4]:
../_images/examples_visualize_matrix_8_0.png

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

Schur decomposition#

[5]:
xyz.visualize_matrix(sla.schur(X), figsize=(4, 4))
[5]:
../_images/examples_visualize_matrix_11_0.png

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

[6]:
xyz.visualize_matrix(sla.schur(X.real), figsize=(4, 4))
[6]:
../_images/examples_visualize_matrix_13_0.png

QR Decomposition#

[7]:
xyz.visualize_matrix(sla.qr(X), figsize=(4, 4))
[7]:
../_images/examples_visualize_matrix_15_0.png

Polar Decomposition#

[8]:
xyz.visualize_matrix(sla.polar(X), figsize=(4, 4))
[8]:
../_images/examples_visualize_matrix_17_0.png

LU Decomposition#

[9]:
xyz.visualize_matrix(sla.lu(X), figsize=(6, 6), gridsize=(1, 3))
[9]:
../_images/examples_visualize_matrix_19_0.png

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

[10]:
xyz.visualize_matrix(sla.lu(X, permute_l=True), figsize=(4, 4))
[10]:
../_images/examples_visualize_matrix_21_0.png