typestar

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

  1. @ multiplies matrices; .T transposes.
  2. linalg.det and linalg.inv work on matrices.
  3. linalg.norm and dot handle vectors.

Keywords and builtins used here

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.

Type this snippet

Step 4 of 4 in Math & shapes, step 6 of 22 in NumPy.

← Previous Next →