\(\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}\)

Diagonal matrices

We often want to make matrices with all zeros except on the diagonal – diagonal matrices.

Numpy does not fail us:

>>> import numpy as np
>>> np.diag([3, 4, 5, 6])
array([[3, 0, 0, 0],
       [0, 4, 0, 0],
       [0, 0, 5, 0],
       [0, 0, 0, 6]])
>>> np.diag([7, 8, 9, 10, 11])
array([[ 7,  0,  0,  0,  0],
       [ 0,  8,  0,  0,  0],
       [ 0,  0,  9,  0,  0],
       [ 0,  0,  0, 10,  0],
       [ 0,  0,  0,  0, 11]])