-
Notifications
You must be signed in to change notification settings - Fork 18
pymath
The pymath library contains mathematical helper functions
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)
pymath.dotpymath.transposepymath.inversepymath.solve_linearpymath.determinantpymath.identitypymath.trace
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.
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},
...
}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 aboutaxis(a vector or an axis keyword) byangledegrees -
pymath.rotation_matrix(angle)— 2×2 matrix for a planar rotation byangledegrees
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.
-
pymath.quat_from_rotation— from an axis and angle -
pymath.quat_from_matrix— from a 3×3 rotation matrix -
pymath.rotation_from_quat— to an axis and angle -
pymath.matrix_from_quat— to a 3×3 rotation matrix
-
pymath.multiply_quat— compose two rotations -
pymath.invert_quat— invert a rotation -
pymath.interpolate_quat— spherical linear interpolation (SLERP) -
pymath.rotate_by_quat— rotate a 3-vector with the rotation given by a quaternion
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)
-
pymath.matrix_from_euler_angles— 3×3 rotation matrix fromalpha, beta, gamma -
pymath.euler_angles_from_matrix—alpha, beta, gammafrom a 3×3 rotation matrix
with all angles in degrees (like every PYTHA Lua API function)
The pymath namespace evaluates Bézier curves from their control points (scalars or points of any dimension):
-
pymath.eval_bezier— the curve point at a parametert -
pymath.eval_bezier_deriv— the curve derivative (tangent) att