typestar

Linear algebra in Python

scipy.linalg goes beyond numpy: decompositions, matrix functions, solvers.

import numpy as np
from scipy import linalg

A = np.array([[4.0, 1.0], [1.0, 3.0]])
b = np.array([1.0, 2.0])

print(linalg.solve(A, b).round(4))
print(round(linalg.det(A), 3), round(linalg.norm(A), 4))

values, vectors = linalg.eigh(A)
print(values.round(4), vectors.shape)

lower = linalg.cholesky(A, lower=True)
print(np.allclose(lower @ lower.T, A))
print(linalg.expm(np.zeros((2, 2))).round(1))

How it works

  1. lu, qr and cholesky are the standard decompositions.
  2. eigh is the symmetric case, and faster than eig.
  3. expm is the matrix exponential, not element-wise.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
398
Tokens
155
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 85 seconds.

Type this snippet

Step 1 of 3 in Linear algebra & signals, step 16 of 23 in Scientific computing with SciPy.

← Previous Next →