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));
Singular Value Decomposition#
xyz.visualize_matrix(sla.svd(X), figsize=(8, 6));
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));
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));
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));
QR Decomposition#
xyz.visualize_matrix(sla.qr(X), figsize=(6, 4));
Polar Decomposition#
xyz.visualize_matrix(sla.polar(X), figsize=(6, 4));
LU Decomposition#
xyz.visualize_matrix(sla.lu(X), figsize=(8, 6));
Multiplying the left matrix in reorders the rows of the \(L\) factor:
xyz.visualize_matrix(sla.lu(X, permute_l=True), figsize=(6, 4));