Skip to content
Daniel Flassig edited this page Jul 17, 2026 · 8 revisions

The pymath library contains mathematical helper functions

Matrices and Vectors

Vectors are represented as arrays (Lua tables) of numbers, e.g. {1, 2, 3}. The pymath functions do not make a difference between row and column vectors.

A matrix is represented as an array of row-vectors, e.g.

local mat = {
    {1, 2}, 
    {3, 4}
}

with its layout as printed.

Even higher tensors may be formed as arrays of lower tensors (with the lower indices to the right)

Vector Geometry

Basic Linear Algebra

Inplace variants

For matrices or arrays-of-vectors, the Lua representation required a large number of tables. For enhanced performance, *_inplace variants are provided where applicable, that write the result back into the first parameter.

Linear systems

Overdetermined systems

Rotations

Rotation Matrices

pymath.rotation_matrix is a convenience constructor for rotation matrices, with the form selected by its arguments:

  • pymath.rotation_matrix(axis, angle) — 3×3 matrix for a rotation about axis (a vector or an axis keyword) by angle degrees
  • pymath.rotation_matrix(angle) — 2×2 matrix for a planar rotation by angle degrees

Quaternions

Unit quaternions are widely used in 3D CAD because of their tight correspondence with 3D rotation matrices. A quaternion is represented in Lua as an array of four numbers {x, y, z, w}.

Functions that require a unit quaternion (e.g. pymath.matrix_from_quat) normalize their input internally, so you rarely need to normalize by hand. See the individual pages for the exact behaviour.

and Quaternion operations

Euler angles

A rotation can also be described by three Euler angles. pymath uses the ZXZ convention

mat = rot(z, alpha) · rot(x, beta) · rot(z, gamma)

with all angles in degrees (like every PYTHA Lua API function)

Bézier curves

Bezier curves are a versatile tool to represent spatial splines and for interpolation:

Interpolation and Clamping

See also:

Math functions

Clone this wiki locally