Visualizing Linear Algebra Decompositions
Contents
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]:

Singular Value Decomposition#
[3]:
xyz.visualize_matrix(sla.svd(X), gridsize=(1, 3), figsize=(6, 6))
[3]:

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]:

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]:

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]:

QR Decomposition#
[7]:
xyz.visualize_matrix(sla.qr(X), figsize=(4, 4))
[7]:

Polar Decomposition#
[8]:
xyz.visualize_matrix(sla.polar(X), figsize=(4, 4))
[8]:

LU Decomposition#
[9]:
xyz.visualize_matrix(sla.lu(X), figsize=(6, 6), gridsize=(1, 3))
[9]:

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]:
