OpenGL Mathematics (GLM) library for Python providing comprehensive vector and matrix manipulation capabilities for graphics programming.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Complete GLSL-compatible mathematical function library covering trigonometry, exponentials, common functions, and interpolation. These functions work with both scalar values and vectors, applying operations component-wise for vector inputs.
Fundamental mathematical operations including absolute value, rounding, and comparison functions.
def abs(x):
"""
Calculate absolute value.
Args:
x: Scalar or vector input
Returns:
Absolute value(s) of the input
Example:
abs(-5.0) # 5.0
abs(glm.vec3(-1, 2, -3)) # vec3(1, 2, 3)
"""
def sign(x):
"""
Extract the sign of the input.
Args:
x: Scalar or vector input
Returns:
-1.0 for negative, 0.0 for zero, 1.0 for positive
Example:
sign(-2.5) # -1.0
sign(glm.vec2(3, -1)) # vec2(1.0, -1.0)
"""
def floor(x):
"""
Find the largest integer less than or equal to x.
Args:
x: Scalar or vector input
Returns:
Floor value(s)
Example:
floor(3.7) # 3.0
floor(glm.vec2(2.1, -1.8)) # vec2(2.0, -2.0)
"""
def ceil(x):
"""
Find the smallest integer greater than or equal to x.
Args:
x: Scalar or vector input
Returns:
Ceiling value(s)
Example:
ceil(3.2) # 4.0
ceil(glm.vec2(2.1, -1.2)) # vec2(3.0, -1.0)
"""
def fract(x):
"""
Extract the fractional part of x.
Args:
x: Scalar or vector input
Returns:
Fractional part (x - floor(x))
Example:
fract(3.7) # 0.7
fract(glm.vec2(2.3, -1.7)) # vec2(0.3, 0.3)
"""
def trunc(x):
"""
Truncate x to remove fractional part.
Args:
x: Scalar or vector input
Returns:
Integer part of x
Example:
trunc(3.7) # 3.0
trunc(-2.8) # -2.0
"""
def round(x):
"""
Round x to the nearest integer.
Args:
x: Scalar or vector input
Returns:
Rounded value(s)
Example:
round(3.6) # 4.0
round(glm.vec2(2.3, 2.7)) # vec2(2.0, 3.0)
"""
def roundEven(x):
"""
Round x to the nearest even integer.
Args:
x: Scalar or vector input
Returns:
Rounded value(s) (ties go to even numbers)
Example:
roundEven(2.5) # 2.0 (even)
roundEven(3.5) # 4.0 (even)
"""Functions for constraining values within ranges and finding extremes.
def min(x, y):
"""
Return the smaller of two values.
Args:
x: First value (scalar or vector)
y: Second value (same type as x, or scalar)
Returns:
Component-wise minimum
Example:
min(3.0, 5.0) # 3.0
min(glm.vec3(1, 4, 2), glm.vec3(3, 2, 5)) # vec3(1, 2, 2)
min(glm.vec3(1, 4, 2), 2.5) # vec3(1, 2.5, 2)
"""
def max(x, y):
"""
Return the larger of two values.
Args:
x: First value (scalar or vector)
y: Second value (same type as x, or scalar)
Returns:
Component-wise maximum
Example:
max(3.0, 5.0) # 5.0
max(glm.vec3(1, 4, 2), glm.vec3(3, 2, 5)) # vec3(3, 4, 5)
"""
def clamp(x, minVal, maxVal):
"""
Constrain x to lie between minVal and maxVal.
Args:
x: Value to clamp (scalar or vector)
minVal: Minimum value (same type as x, or scalar)
maxVal: Maximum value (same type as x, or scalar)
Returns:
Clamped value(s)
Example:
clamp(7.0, 2.0, 5.0) # 5.0
clamp(glm.vec3(-1, 3, 8), 0.0, 5.0) # vec3(0, 3, 5)
"""
def fmin(x, y):
"""
Return the minimum of two values (handles NaN differently than min).
Args:
x: First value (scalar or vector)
y: Second value (same type as x, or scalar)
Returns:
Component-wise minimum, with NaN handling
"""
def fmax(x, y):
"""
Return the maximum of two values (handles NaN differently than max).
Args:
x: First value (scalar or vector)
y: Second value (same type as x, or scalar)
Returns:
Component-wise maximum, with NaN handling
"""
def fma(a, b, c):
"""
Fused multiply-add operation: (a * b) + c.
Args:
a: First multiplicand (scalar or vector)
b: Second multiplicand (same type as a)
c: Addend (same type as a)
Returns:
Fused multiply-add result with higher precision
"""Complete set of trigonometric functions including basic, inverse, and hyperbolic variants.
def radians(degrees):
"""
Convert degrees to radians.
Args:
degrees: Angle in degrees (scalar or vector)
Returns:
Angle in radians
Example:
radians(90.0) # ~1.5708 (π/2)
radians(glm.vec3(0, 90, 180)) # vec3(0, π/2, π)
"""
def degrees(radians):
"""
Convert radians to degrees.
Args:
radians: Angle in radians (scalar or vector)
Returns:
Angle in degrees
Example:
degrees(glm.pi() / 2) # 90.0
"""
def sin(angle):
"""
Calculate sine of angle.
Args:
angle: Angle in radians (scalar or vector)
Returns:
Sine value(s)
Example:
sin(glm.pi() / 2) # 1.0
sin(glm.vec3(0, glm.pi()/2, glm.pi())) # vec3(0, 1, 0)
"""
def cos(angle):
"""
Calculate cosine of angle.
Args:
angle: Angle in radians (scalar or vector)
Returns:
Cosine value(s)
Example:
cos(0.0) # 1.0
cos(glm.pi() / 2) # ~0.0
"""
def tan(angle):
"""
Calculate tangent of angle.
Args:
angle: Angle in radians (scalar or vector)
Returns:
Tangent value(s)
Example:
tan(glm.pi() / 4) # 1.0
"""
def asin(x):
"""
Calculate arcsine (inverse sine).
Args:
x: Input value in range [-1, 1] (scalar or vector)
Returns:
Angle in radians [-π/2, π/2]
Example:
asin(1.0) # π/2
asin(0.5) # π/6
"""
def acos(x):
"""
Calculate arccosine (inverse cosine).
Args:
x: Input value in range [-1, 1] (scalar or vector)
Returns:
Angle in radians [0, π]
Example:
acos(0.0) # π/2
acos(-1.0) # π
"""
def atan(y, x=None):
"""
Calculate arctangent.
Args:
y: Y coordinate or y/x ratio (scalar or vector)
x: X coordinate (optional, scalar or vector)
Returns:
Angle in radians
- atan(y): range [-π/2, π/2]
- atan(y, x): range [-π, π] (two-argument version)
Example:
atan(1.0) # π/4
atan(1.0, 1.0) # π/4
atan(-1.0, 1.0) # -π/4
"""
def sinh(x):
"""
Calculate hyperbolic sine.
Args:
x: Input value (scalar or vector)
Returns:
Hyperbolic sine value(s)
"""
def cosh(x):
"""
Calculate hyperbolic cosine.
Args:
x: Input value (scalar or vector)
Returns:
Hyperbolic cosine value(s)
"""
def tanh(x):
"""
Calculate hyperbolic tangent.
Args:
x: Input value (scalar or vector)
Returns:
Hyperbolic tangent value(s)
"""
def asinh(x):
"""
Calculate inverse hyperbolic sine.
Args:
x: Input value (scalar or vector)
Returns:
Inverse hyperbolic sine value(s)
"""
def acosh(x):
"""
Calculate inverse hyperbolic cosine.
Args:
x: Input value >= 1 (scalar or vector)
Returns:
Inverse hyperbolic cosine value(s)
"""
def atanh(x):
"""
Calculate inverse hyperbolic tangent.
Args:
x: Input value in range (-1, 1) (scalar or vector)
Returns:
Inverse hyperbolic tangent value(s)
"""Additional trigonometric functions including secant, cosecant, and cotangent variants.
def sec(angle):
"""
Calculate secant (1/cos).
Args:
angle: Angle in radians (scalar or vector)
Returns:
Secant value(s)
"""
def csc(angle):
"""
Calculate cosecant (1/sin).
Args:
angle: Angle in radians (scalar or vector)
Returns:
Cosecant value(s)
"""
def cot(angle):
"""
Calculate cotangent (1/tan).
Args:
angle: Angle in radians (scalar or vector)
Returns:
Cotangent value(s)
"""
def asec(x):
"""
Calculate inverse secant.
Args:
x: Input value |x| >= 1 (scalar or vector)
Returns:
Inverse secant value(s) in radians
"""
def acsc(x):
"""
Calculate inverse cosecant.
Args:
x: Input value |x| >= 1 (scalar or vector)
Returns:
Inverse cosecant value(s) in radians
"""
def acot(x):
"""
Calculate inverse cotangent.
Args:
x: Input value (scalar or vector)
Returns:
Inverse cotangent value(s) in radians
"""
def sech(x):
"""
Calculate hyperbolic secant.
Args:
x: Input value (scalar or vector)
Returns:
Hyperbolic secant value(s)
"""
def csch(x):
"""
Calculate hyperbolic cosecant.
Args:
x: Input value != 0 (scalar or vector)
Returns:
Hyperbolic cosecant value(s)
"""
def coth(x):
"""
Calculate hyperbolic cotangent.
Args:
x: Input value != 0 (scalar or vector)
Returns:
Hyperbolic cotangent value(s)
"""
def asech(x):
"""
Calculate inverse hyperbolic secant.
Args:
x: Input value in range (0, 1] (scalar or vector)
Returns:
Inverse hyperbolic secant value(s)
"""
def acsch(x):
"""
Calculate inverse hyperbolic cosecant.
Args:
x: Input value != 0 (scalar or vector)
Returns:
Inverse hyperbolic cosecant value(s)
"""
def acoth(x):
"""
Calculate inverse hyperbolic cotangent.
Args:
x: Input value |x| > 1 (scalar or vector)
Returns:
Inverse hyperbolic cotangent value(s)
"""Power, exponential, and logarithmic functions for scientific calculations.
def pow(x, y):
"""
Calculate x raised to the power of y.
Args:
x: Base value (scalar or vector)
y: Exponent (scalar or vector, same type as x)
Returns:
x^y
Example:
pow(2.0, 3.0) # 8.0
pow(glm.vec3(2, 3, 4), 2.0) # vec3(4, 9, 16)
"""
def exp(x):
"""
Calculate e raised to the power of x.
Args:
x: Exponent (scalar or vector)
Returns:
e^x
Example:
exp(1.0) # e (~2.718)
exp(0.0) # 1.0
"""
def exp2(x):
"""
Calculate 2 raised to the power of x.
Args:
x: Exponent (scalar or vector)
Returns:
2^x
Example:
exp2(3.0) # 8.0
exp2(glm.vec2(1, 4)) # vec2(2, 16)
"""
def log(x):
"""
Calculate natural logarithm of x.
Args:
x: Input value > 0 (scalar or vector)
Returns:
ln(x)
Example:
log(glm.e()) # 1.0
log(1.0) # 0.0
"""
def log2(x):
"""
Calculate base-2 logarithm of x.
Args:
x: Input value > 0 (scalar or vector)
Returns:
log₂(x)
Example:
log2(8.0) # 3.0
log2(glm.vec3(2, 4, 8)) # vec3(1, 2, 3)
"""
def sqrt(x):
"""
Calculate square root of x.
Args:
x: Input value >= 0 (scalar or vector)
Returns:
√x
Example:
sqrt(9.0) # 3.0
sqrt(glm.vec2(4, 25)) # vec2(2, 5)
"""
def inversesqrt(x):
"""
Calculate inverse square root (1/√x).
Args:
x: Input value > 0 (scalar or vector)
Returns:
1/√x
Example:
inversesqrt(4.0) # 0.5
inversesqrt(glm.vec2(1, 9)) # vec2(1, 1/3)
"""
def modf(x, i):
"""
Extract integer and fractional parts of x.
Args:
x: Input value (scalar or vector)
i: Output parameter for integer part
Returns:
Fractional part of x (x - floor(x))
"""
def frexp(x, exp):
"""
Extract mantissa and exponent from floating-point number.
Args:
x: Input value (scalar or vector)
exp: Output parameter for exponent
Returns:
Mantissa (significand) in range [0.5, 1.0) or 0
"""
def ldexp(x, exp):
"""
Construct floating-point number from mantissa and exponent.
Args:
x: Mantissa (scalar or vector)
exp: Exponent (scalar or integer vector)
Returns:
x * 2^exp
"""Functions for smooth blending and transitions between values.
def mix(x, y, a):
"""
Linear interpolation between x and y.
Args:
x: Start value (scalar or vector)
y: End value (same type as x)
a: Interpolation factor (scalar or vector)
Returns:
x * (1 - a) + y * a
Example:
mix(0.0, 10.0, 0.5) # 5.0
mix(glm.vec3(0), glm.vec3(10), 0.3) # vec3(3, 3, 3)
"""
def step(edge, x):
"""
Step function: 0.0 if x < edge, 1.0 otherwise.
Args:
edge: Threshold value (scalar or vector)
x: Input value (scalar or vector)
Returns:
0.0 or 1.0 based on comparison
Example:
step(0.5, 0.3) # 0.0
step(0.5, 0.7) # 1.0
step(0.5, glm.vec3(0.2, 0.6, 0.8)) # vec3(0, 1, 1)
"""
def smoothstep(edge0, edge1, x):
"""
Smooth Hermite interpolation between 0 and 1.
Args:
edge0: Lower edge of the transition (scalar or vector)
edge1: Upper edge of the transition (scalar or vector)
x: Input value (scalar or vector)
Returns:
Smooth transition from 0.0 to 1.0
Example:
smoothstep(0.0, 1.0, 0.5) # 0.5 with smooth curve
smoothstep(0.0, 10.0, glm.vec3(2, 5, 8)) # Smooth transitions
"""from pyglm import glm
import math
# === Basic Math Operations ===
# Scalar operations
value = -3.7
abs_val = glm.abs(value) # 3.7
floor_val = glm.floor(value) # -4.0
ceil_val = glm.ceil(value) # -3.0
fract_val = glm.fract(value) # 0.3
# Component-wise vector operations
vec = glm.vec3(-2.3, 4.7, -1.1)
abs_vec = glm.abs(vec) # vec3(2.3, 4.7, 1.1)
floor_vec = glm.floor(vec) # vec3(-3.0, 4.0, -2.0)
# === Trigonometry ===
# Convert degrees to radians
angle_deg = 45.0
angle_rad = glm.radians(angle_deg) # π/4
# Basic trig functions
sin_val = glm.sin(angle_rad) # ~0.707
cos_val = glm.cos(angle_rad) # ~0.707
tan_val = glm.tan(angle_rad) # 1.0
# Vector trigonometry
angles = glm.vec3(0.0, glm.pi()/4, glm.pi()/2)
sin_values = glm.sin(angles) # vec3(0, ~0.707, 1)
# Inverse trigonometry
asin_val = glm.asin(0.5) # π/6 (30 degrees)
atan2_val = glm.atan(1.0, 1.0) # π/4 (45 degrees)
# === Exponentials and Logarithms ===
# Power functions
power_result = glm.pow(2.0, 3.0) # 8.0
exp_result = glm.exp(1.0) # e (~2.718)
exp2_result = glm.exp2(3.0) # 8.0
# Logarithms
log_result = glm.log(glm.e()) # 1.0
log2_result = glm.log2(8.0) # 3.0
# Square roots
sqrt_result = glm.sqrt(16.0) # 4.0
inv_sqrt = glm.inversesqrt(4.0) # 0.5
# Vector exponentials
bases = glm.vec3(2, 3, 4)
powers = glm.vec3(2, 2, 2)
powered = glm.pow(bases, powers) # vec3(4, 9, 16)
# === Min, Max, and Clamping ===
# Scalar operations
min_val = glm.min(3.0, 7.0) # 3.0
max_val = glm.max(3.0, 7.0) # 7.0
clamped = glm.clamp(15.0, 0.0, 10.0) # 10.0
# Vector operations
vec1 = glm.vec3(1, 8, 3)
vec2 = glm.vec3(5, 2, 7)
min_vec = glm.min(vec1, vec2) # vec3(1, 2, 3)
max_vec = glm.max(vec1, vec2) # vec3(5, 8, 7)
# Clamp vector to range
input_vec = glm.vec3(-2, 5, 12)
clamped_vec = glm.clamp(input_vec, 0.0, 10.0) # vec3(0, 5, 10)
# === Interpolation ===
# Linear interpolation
start = 0.0
end = 100.0
t = 0.3
interpolated = glm.mix(start, end, t) # 30.0
# Vector interpolation
start_vec = glm.vec3(0, 0, 0)
end_vec = glm.vec3(10, 20, 30)
interpolated_vec = glm.mix(start_vec, end_vec, 0.5) # vec3(5, 10, 15)
# Step function for thresholding
values = glm.vec4(0.2, 0.5, 0.8, 1.2)
threshold = 0.6
stepped = glm.step(threshold, values) # vec4(0, 0, 1, 1)
# Smooth step for easing
smooth_values = glm.smoothstep(0.0, 1.0, glm.vec4(0, 0.25, 0.5, 0.75))
# Results in smooth curve instead of linear
# === Practical Examples ===
# Normalize angle to [0, 2π] range
def normalize_angle(angle):
two_pi = 2.0 * glm.pi()
return angle - glm.floor(angle / two_pi) * two_pi
# Smooth falloff function for lighting
def smooth_falloff(distance, max_distance):
t = glm.clamp(distance / max_distance, 0.0, 1.0)
return 1.0 - glm.smoothstep(0.0, 1.0, t)
# Convert temperature (Celsius to Fahrenheit)
celsius_temps = glm.vec3(0, 25, 100) # Freezing, room temp, boiling
fahrenheit_temps = celsius_temps * (9.0/5.0) + 32.0 # vec3(32, 77, 212)
# Oscillating movement using sine wave
time = 2.5
amplitude = 5.0
frequency = 1.0
position = amplitude * glm.sin(frequency * time)
# Exponential decay (e.g., for fade effects)
initial_value = 100.0
decay_rate = 0.1
time_elapsed = 3.0
current_value = initial_value * glm.exp(-decay_rate * time_elapsed)
# Distance-based color mixing
distance = 15.0
near_color = glm.vec3(1.0, 0.0, 0.0) # Red
far_color = glm.vec3(0.0, 0.0, 1.0) # Blue
max_distance = 20.0
t = glm.clamp(distance / max_distance, 0.0, 1.0)
final_color = glm.mix(near_color, far_color, t)PyGLM provides convenient access to mathematical constants:
from pyglm import glm
# Common constants
pi_val = glm.pi() # π
half_pi = glm.half_pi() # π/2
two_pi = glm.two_pi() # 2π
e_val = glm.e() # Euler's number
golden_ratio = glm.golden_ratio() # φ
root_two = glm.root_two() # √2All functions support both scalar and vector inputs, making them highly versatile for graphics programming where component-wise operations on vectors are common.
Install with Tessl CLI
npx tessl i tessl/pypi-pyglm