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 numpy as np
import scipy.linalg as sla

import xyzpy as xyz

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/974ef38e80e6bce2f2ce78de11515a1c1f9f8a64ec48af49210a3e4529047d2f.svg

Singular Value Decomposition

xyz.visualize_matrix(sla.svd(X), figsize=(8, 6));
../_images/c7624841adc30976a5b7ad8e3b8e3cc36fa3dcb8989ae391d7db87ea906815d4.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/d31d91e0ac04becddfab6482c12d6f56b2bf256da6ba0fbbf0741cc0d5d4089e.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/44dacaae61855919e4da4740d33fd2cc9266c1a9ef87901f08c688e2b639a998.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/c7d62d905a332efc27ecb9aaed33d9ddcda544dcd79ea0be663935bfc01e296e.svg

QR Decomposition

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

Polar Decomposition

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

LU Decomposition

xyz.visualize_matrix(sla.lu(X), figsize=(8, 6));
../_images/08e49b2f9f003a1a1fa7e72f06ec38e319864ab01dd6b674b7ee55462d3fdb69.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/3c9737b60fbb2f1a6a976a23f2161bf3053298ec26cbf068ceef4fac180c3a77.svg