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.
writing
Section titled “writing”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.
good to know
Section titled “good to know”Given integers and fractions, every function here gives an exact answer.
Tags: algebra
dotthe dot product: multiply matching items of two equal-length vectors and add the resultscrossthe cross product of two 3D vectors: a vector at right angles to bothnormthe length of a vector: the square root of the sum of its squared itemstransposea matrix with its rows and columns swappedmatmulthe matrix product of a and b, where a must have as many columns as b has rowsdetthe determinant of a square matrixinvthe inverse of a square matrix: the matrix that multiplies with it to give the identityidentitythe n by n identity matrix: 1 down the diagonal and 0 elsewherematrixa matrix of the given number of rows and columns, filled with fill (0 if left out)reshapethe items of xs laid out as a matrix of the given rows and columns, filling row by rowrowrow i of matrix m, counting from 0colcolumn j of matrix m, counting from 0hcattwo matrices side by side, which need the same number of rowsvcattwo matrices one above the other, which need the same number of columnstriplethe scalar triple product of three 3D vectors, the signed volume of the box they spanrrefthe reduced row echelon form of a matrix: the simplest matrix its row operations can reachrankthe rank of a matrix: how many of its rows are independent of the otherscofactorthe cofactor of m at row i and column j: the determinant of what is left without them, signedadjugatethe adjugate of a square matrix: the transposed cofactors, which divided by the determinant give the inversekronthe Kronecker product of two matrices: every item of a times the whole of b, in blockspermanentthe permanent of a square matrix: the determinant without the alternating signssolvesolve linear equations: the list x for which matmul(a, x) gives b
Functions
Section titled “Functions”dot(a: list, b: list) -> num|quantity
the dot product: multiply matching items of two equal-length vectors and add the results
1dot([1, 2, 3], [4, 5, 6])32cross(a: list, b: list) -> list
the cross product of two 3D vectors: a vector at right angles to both
1cross([1, 0, 0], [0, 1, 0])[0, 0, 1]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
1norm([3, 4])52norm([1 m, 1 m])1.41421 m3norm([3, -4], 1)74norm([3, -4], inf)4transpose
Section titled “transpose”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]]See also matmul
matmul
Section titled “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
1matmul([[1, 2], [3, 4]], [[5], [6]])[[17], [39]]det(m: list) -> num
the determinant of a square matrix
0 means it has no inverse
1det([[1, 2], [3, 4]])-2inv(m: list) -> list
the inverse of a square matrix: the matrix that multiplies with it to give the identity
1inv([[4, 7], [2, 6]])[[0.6, -0.7], [-0.2, 0.4]]identity
Section titled “identity”identity(n: int) -> list
the n by n identity matrix: 1 down the diagonal and 0 elsewhere
1identity(3)[[1, 0, 0], [0, 1, 0], [0, 0, 1]]See also matrix
matrix
Section titled “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)
1matrix(2, 3)[[0, 0, 0], [0, 0, 0]]2matrix(2, 2, 7)[[7, 7], [7, 7]]reshape
Section titled “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
1reshape([1, 2, 3, 4, 5, 6], 2, 3)[[1, 2, 3], [4, 5, 6]]row(m: list, i: int) -> list
row i of matrix m, counting from 0
1row([[1, 2], [3, 4]], 1)[3, 4]See also col
col(m: list, j: int) -> list
column j of matrix m, counting from 0
1col([[1, 2], [3, 4]], 1)[2, 4]See also row
hcat(a: list, b: list) -> list
two matrices side by side, which need the same number of rows
1hcat([[1, 2], [3, 4]], [[5], [6]])[[1, 2, 5], [3, 4, 6]]See also vcat
vcat(a: list, b: list) -> list
two matrices one above the other, which need the same number of columns
1vcat([[1, 2]], [[3, 4]])[[1, 2], [3, 4]]See also hcat
triple
Section titled “triple”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
1triple([1, 0, 0], [0, 1, 0], [0, 0, 1])1rref(m: list) -> list
the reduced row echelon form of a matrix: the simplest matrix its row operations can reach
1rref([[1, 2, 3], [4, 5, 6], [7, 8, 9]])[[1, 0, -1], [0, 1, 2], [0, 0, 0]]rank(m: list) -> int
the rank of a matrix: how many of its rows are independent of the others
1rank([[1, 2], [2, 4]])12rank([[1, 0], [0, 1]])2cofactor
Section titled “cofactor”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
1cofactor([[1, 2], [3, 4]], 0, 1)-3adjugate
Section titled “adjugate”adjugate(m: list) -> list
the adjugate of a square matrix: the transposed cofactors, which divided by the determinant give the inverse
1adjugate([[1, 2], [3, 4]])[[4, -2], [-3, 1]]kron(a: list, b: list) -> list
the Kronecker product of two matrices: every item of a times the whole of b, in blocks
1kron([[1, 2], [3, 4]], [[1, 1], [1, 1]])[[1, 1, 2, 2], [1, 1, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]]See also matmul
permanent
Section titled “permanent”permanent(m: list) -> num
the permanent of a square matrix: the determinant without the alternating signs
1permanent([[1, 2], [3, 4]])10See 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])
1solve([[2, 1], [1, 3]], [3, 5])[0.8, 1.4]2solve([[1, 1, 1], [0, 2, 5], [2, 5, -1]], [6, -4, 27])[5, 3, -2]More examples
Section titled “More examples”linalg
Section titled “linalg”1# solve 2x + y = 3, x + 3y = 52solve([[2, 1], [1, 3]], [3, 5]).map(frac)[4/5, 7/5]3# exact inverse4inv([[2, 1], [1, 1]])[[1, -1], [-1, 2]]5# rotate the point (1, 0) by 90°6matmul([[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)8norm([3 m, 4 m])5 mExample results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.