Skip to content

Mathematical notation

These equations restate established contracts. TeX in the source becomes native MathML at website build time; there is no external equation-rendering service.

Vectors and row matrices

For 𝐯=(vx,vy,vz), the Euclidean norm is

‖𝐯‖=vx2+vy2+vz2.

This defines the mathematics, not the floating-point algorithm: gem uses stable hypot-style lengths and scaled normalization. See numerical accuracy.

A homogeneous translation with row-vector inputs is

T=(100001000010txtytz1),𝐩′=𝐩T.

Thus [x,y,z,1]T=[x+tx,y+ty,z+tz,1]. Directions use w=0. The Matrix wrapper expression T * p evaluates this row product. A*B applies A before B. Transformation tutorial.

Quaternions

A unit axis 𝐮 and angle θ define

q=[cos(θ/2),𝐮sin(θ/2)],[0,𝐯′]=q[0,𝐯]q*.

Storage is [w,x,y,z], and products use Hamilton multiplication. API units remain function-specific; quat_from_axis_angle takes degrees. Quaternion reference.

For unit endpoints, shortest-path SLERP weights a spherical angle Ω:

S(q0,q1,t)=sin((1−t)Ω)sinΩq0+sin(tΩ)sinΩq1.

Signs are selected before interpolation. The implementation uses difference/sum norms to resolve tiny angles, preserves endpoints and uses a continuous limit for subnormal angles. The equation states the ideal calculation, rather than an instruction to divide by zero for identical rotations. Inspect the interpolation frames.

Bezier curves and subdivision

Quadratic and cubic curves use the Bernstein weights:

B2(t)=(1−t)2p0+2(1−t)tp1+t2p2,
B3(t)=(1−t)3p0+3(1−t)2tp1+3(1−t)t2p2+t3p3.

De Casteljau midpoint subdivision builds intermediate controls by repeated averaging. It preserves the curve while dividing its parameter interval in two. Adaptive sampling checks control distance to the endpoint segment and caps depth at 16; it may exhaust that depth before meeting the requested flatness. Equal parameter steps do not imply equal traveled distance. Inspect the control polygons and samples and derive a local tangent in the tutorial.

Legendre functions

Ordinary functions start with P0(x)=1 and P1(x)=x:

lPl(x)=(2l−1)xPl−1(x)−(l−1)Pl−2(x).

The associated functions retain the Condon–Shortley phase:

Pmm(x)=(−1)m(2m−1)!!(1−x2)m/2.

Ordinary Legendre polynomials P0 through P4 over minus one to one

Even degrees are even functions; odd degrees are odd functions. Every ordinary curve reaches 1 at x=1, with P_l(-1)=(-1)^l. These functions are unnormalized; the SH basis supplies its own normalization. Associated inputs require x in [-1,1], while ordinary functions permit extrapolation with documented numerical limits. Legendre contracts. The plot source evaluates the core API and checks independent analytical values before writing this SVG. Open the plot at full size to zoom into labels on a small screen.

Spherical harmonics

Canonical coefficient index is i=l(l+1)+m. For canonical real orthonormal basis functions, reconstruction and active rotation obey

f(𝐝)=∑l=02∑m=−llclmYlm(𝐝),fR(𝐝)=f(R−1𝐝).

Condon–Shortley signs are retained. The first-order XYZ functions are Y1,−1=−3/(4π)y, Y1,0=3/(4π)z, and Y1,1=−3/(4π)x. Coefficients may be RGB. Cosine convolution applies band factors π, 2π/3, π/4 exactly once for L0,L1,L2; reflected Lambertian radiance is Lo=ρE/π. Lighting tutorial.

import math
from gem.quaternion import quat_from_axis_angle, quat_rotate_vector
from gem.vector import Vector
q = quat_from_axis_angle([0, 0, 1], 90)
v = quat_rotate_vector(q, Vector(3, [1, 0, 0]))
assert math.isclose(v.vector[1], 1.0, abs_tol=1e-12)

The adjacent Python block follows the same active +Z convention. Equations, code and verified diagrams describe the same mathematics.