Skip to content

math.linalg

vectors and matrices: products, determinants, inverses and solving systems of equations

Vectors and matrices: products, determinants, inverses, and solving systems of linear equations.

A vector is a list of numbers, like [3, 4], and a matrix is a list of rows, like [[1, 2], [3, 4]] for the grid with 1 2 on top and 3 4 below.

Given integers and fractions, every function here gives an exact answer.

Tags: algebra

  • dot the dot product: multiply matching items of two equal-length vectors and add the results
  • cross the cross product of two 3D vectors: a vector at right angles to both
  • norm the length of a vector: the square root of the sum of its squared items
  • transpose a matrix with its rows and columns swapped
  • matmul the matrix product of a and b, where a must have as many columns as b has rows
  • det the determinant of a square matrix
  • inv the inverse of a square matrix: the matrix that multiplies with it to give the identity
  • identity the n by n identity matrix: 1 down the diagonal and 0 elsewhere
  • matrix a matrix of the given number of rows and columns, filled with fill (0 if left out)
  • reshape the items of xs laid out as a matrix of the given rows and columns, filling row by row
  • row row i of matrix m, counting from 0
  • col column j of matrix m, counting from 0
  • hcat two matrices side by side, which need the same number of rows
  • vcat two matrices one above the other, which need the same number of columns
  • triple the scalar triple product of three 3D vectors, the signed volume of the box they span
  • rref the reduced row echelon form of a matrix: the simplest matrix its row operations can reach
  • rank the rank of a matrix: how many of its rows are independent of the others
  • cofactor the cofactor of m at row i and column j: the determinant of what is left without them, signed
  • adjugate the adjugate of a square matrix: the transposed cofactors, which divided by the determinant give the inverse
  • kron the Kronecker product of two matrices: every item of a times the whole of b, in blocks
  • permanent the permanent of a square matrix: the determinant without the alternating signs
  • solve solve linear equations: the list x for which matmul(a, x) gives b
dot(a: list, b: list) -> num|quantity

the dot product: multiply matching items of two equal-length vectors and add the results

1
dot([1, 2, 3], [4, 5, 6])32
Example outputCtrl+Enter to run

See also cross, norm

cross(a: list, b: list) -> list

the cross product of two 3D vectors: a vector at right angles to both

1
cross([1, 0, 0], [0, 1, 0])[0, 0, 1]
Example outputCtrl+Enter to run

See also dot

norm(v: list, p?: num) -> num|quantity

the length of a vector: the square root of the sum of its squared items

With p, the p-norm instead: the p-th root of the sum of each item’s absolute value to the power p. 1 adds the absolute values, inf takes the largest

1
norm([3, 4])5
2
norm([1 m, 1 m])1.41421 m
3
norm([3, -4], 1)7
4
norm([3, -4], inf)4
Example outputCtrl+Enter to run

See also dot, hypot

transpose(m: list) -> list

a matrix with its rows and columns swapped

1
[[1, 2, 3], [4, 5, 6]].transpose[[1, 4], [2, 5], [3, 6]]
Example outputCtrl+Enter to run

See also matmul

matmul(a: list, b: list) -> list

the matrix product of a and b, where a must have as many columns as b has rows

1
matmul([[1, 2], [3, 4]], [[5], [6]])[[17], [39]]
Example outputCtrl+Enter to run

See also transpose, inv

det(m: list) -> num

the determinant of a square matrix

0 means it has no inverse

1
det([[1, 2], [3, 4]])-2
Example outputCtrl+Enter to run

See also inv, solve

inv(m: list) -> list

the inverse of a square matrix: the matrix that multiplies with it to give the identity

1
inv([[4, 7], [2, 6]])[[0.6, -0.7], [-0.2, 0.4]]
Example outputCtrl+Enter to run

See also det, solve

identity(n: int) -> list

the n by n identity matrix: 1 down the diagonal and 0 elsewhere

1
identity(3)[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Example outputCtrl+Enter to run

See also matrix

matrix(r: int, c: int, fill?: num) -> list

a matrix of the given number of rows and columns, filled with fill (0 if left out)

1
matrix(2, 3)[[0, 0, 0], [0, 0, 0]]
2
matrix(2, 2, 7)[[7, 7], [7, 7]]
Example outputCtrl+Enter to run

See also identity, reshape

reshape(xs: list, r: int, c: int) -> list

the items of xs laid out as a matrix of the given rows and columns, filling row by row

1
reshape([1, 2, 3, 4, 5, 6], 2, 3)[[1, 2, 3], [4, 5, 6]]
Example outputCtrl+Enter to run

See also matrix, transpose

row(m: list, i: int) -> list

row i of matrix m, counting from 0

1
row([[1, 2], [3, 4]], 1)[3, 4]
Example outputCtrl+Enter to run

See also col

col(m: list, j: int) -> list

column j of matrix m, counting from 0

1
col([[1, 2], [3, 4]], 1)[2, 4]
Example outputCtrl+Enter to run

See also row

hcat(a: list, b: list) -> list

two matrices side by side, which need the same number of rows

1
hcat([[1, 2], [3, 4]], [[5], [6]])[[1, 2, 5], [3, 4, 6]]
Example outputCtrl+Enter to run

See also vcat

vcat(a: list, b: list) -> list

two matrices one above the other, which need the same number of columns

1
vcat([[1, 2]], [[3, 4]])[[1, 2], [3, 4]]
Example outputCtrl+Enter to run

See also hcat

triple(a: list, b: list, c: list) -> num|quantity

the scalar triple product of three 3D vectors, the signed volume of the box they span

1
triple([1, 0, 0], [0, 1, 0], [0, 0, 1])1
Example outputCtrl+Enter to run

See also dot, cross

rref(m: list) -> list

the reduced row echelon form of a matrix: the simplest matrix its row operations can reach

1
rref([[1, 2, 3], [4, 5, 6], [7, 8, 9]])[[1, 0, -1], [0, 1, 2], [0, 0, 0]]
Example outputCtrl+Enter to run

See also rank, solve

rank(m: list) -> int

the rank of a matrix: how many of its rows are independent of the others

1
rank([[1, 2], [2, 4]])1
2
rank([[1, 0], [0, 1]])2
Example outputCtrl+Enter to run

See also rref, det

cofactor(m: list, i: int, j: int) -> num

the cofactor of m at row i and column j: the determinant of what is left without them, signed

1
cofactor([[1, 2], [3, 4]], 0, 1)-3
Example outputCtrl+Enter to run

See also adjugate, det

adjugate(m: list) -> list

the adjugate of a square matrix: the transposed cofactors, which divided by the determinant give the inverse

1
adjugate([[1, 2], [3, 4]])[[4, -2], [-3, 1]]
Example outputCtrl+Enter to run

See also cofactor, inv

kron(a: list, b: list) -> list

the Kronecker product of two matrices: every item of a times the whole of b, in blocks

1
kron([[1, 2], [3, 4]], [[1, 1], [1, 1]])[[1, 1, 2, 2], [1, 1, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]]
Example outputCtrl+Enter to run

See also matmul

permanent(m: list) -> num

the permanent of a square matrix: the determinant without the alternating signs

1
permanent([[1, 2], [3, 4]])10
Example outputCtrl+Enter to run

See also det

solve(a: list, b: list) -> list

solve linear equations: the list x for which matmul(a, x) gives b

Each row of the square matrix a holds one equation’s coefficients, and b the right-hand sides, so 2x + y = 3 and x + 3y = 5 is solve([[2, 1], [1, 3]], [3, 5])

1
solve([[2, 1], [1, 3]], [3, 5])[0.8, 1.4]
2
solve([[1, 1, 1], [0, 2, 5], [2, 5, -1]], [6, -4, 27])[5, 3, -2]
Example outputCtrl+Enter to run

See also inv, det

1
# solve 2x + y = 3, x + 3y = 5
2
solve([[2, 1], [1, 3]], [3, 5]).map(frac)[4/5, 7/5]
3
# exact inverse
4
inv([[2, 1], [1, 1]])[[1, -1], [-1, 2]]
5
# rotate the point (1, 0) by 90°
6
matmul([[cos(90 deg), -sin(90 deg)], [sin(90 deg), cos(90 deg)]], [[1], [0]])[[6.12323e-17], [1]]
7
# the distance from 0 to (3 m, 4 m)
8
norm([3 m, 4 m])5 m
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.