Linear algebra in Python
Matrix products and the linalg toolkit.
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
product = a @ b
transposed = a.T
determinant = np.linalg.det(a)
inverse = np.linalg.inv(a)
vector = np.array([1, 2, 2])
length = np.linalg.norm(vector)
dot = np.dot(vector, vector)
How it works
@multiplies matrices;.Ttransposes.linalg.detandlinalg.invwork on matrices.linalg.normanddothandle vectors.
Keywords and builtins used here
as
The run, in numbers
- Lines
- 13
- Characters to type
- 264
- Tokens
- 108
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 59 seconds.
Step 4 of 4 in Math & shapes, step 6 of 22 in NumPy.