Skip to content

pymath.solve_linear

Daniel Flassig edited this page Jun 24, 2026 · 1 revision

Solves the square linear system A . x == b, returning the solution vector x, or nil if A is (close to) singular.

x = pymath.solve_linear(A, b)
Parameter Type Description
A matrix A square matrix (n × n), represented as an array of n row-vectors.
b vector The right-hand side, a table read as a vector of length n (missing entries read as 0).

Return value

Type Description
x A new vector of length n solving A . x == b, or nil if A is singular (not solvable).

Notes:

  • A must be square, otherwise an error is raised.
  • You should prefer pymath.solve_linear to calculating the inverse explicitly, because it is more numerically accurate (and faster)

Example:

local A = {{2, 1},
           {1, 3}}
local b = {3, 5}
local x = pymath.solve_linear(A, b)
-- x == {0.8, 1.4}        -- A . x == b

local S = {{1, 2},
           {2, 4}}        -- singular
local xs = pymath.solve_linear(S, {1, 1})
-- xs == nil

Version Support:

Minimum PYTHA Version: V27

See also:

pymath, pymath.dot, pymath.inverse, pymath.determinant

Clone this wiki locally