Skip to content
Daniel Flassig edited this page Jul 16, 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)

Basic Vector and Matrix Functions

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.

Sparse Matrices

Some functions are specifically designed and documented to take advantage of sparse matrices. Sparse matrices are represented in a similar way, but have to declare their size up-font. The entries are defined (sparsely) for all rows, taking advantage of Lua's table implementation:

local sparse_mat = {
    size={1000,1000},
    {[17]=1, [202]=2, [998]=2}, 
    {[5]=3, [368]=4},
    ...
}

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 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.

Conversions

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

The pymath namespace evaluates Bézier curves from their control points (scalars or points of any dimension):

See also:

Math functions

Clone this wiki locally