Math
This library extends the standard math library with additional mathematical functions and operations.
Constants
Constants
math.huge
: Represents positive infinity.math.pi
: The mathematical constant π (pi), approximately 3.14159265358979323846.math.tau
: The mathematical constant Ï„ (tau), equal to 2Ï€.math.e
: The mathematical constant e (Euler's number), approximately 2.71828182845904523536.
Spatial Mathematics
math.vector_add(v1, v2)
Adds two vectors.
math.vector_subtract(v1, v2)
Subtracts the second vector from the first.
math.vector_dot_product(v1, v2)
Calculates the dot product of two vectors.
math.vector_cross_product(v1, v2)
Calculates the cross product of two 3D vectors.
math.vector_normalize(v)
Normalizes a vector (makes its length 1).
math.vector_magnitude(v)
Calculates the magnitude (length) of a vector.
Matrix Operations
math.matrix_create(rows, cols, initial_value)
Creates a new matrix with the specified dimensions.
math.matrix_add(m1, m2)
Adds two matrices.
math.matrix_subtract(m1, m2)
Subtracts the second matrix from the first.
local result = math.matrix_subtract({{5, 6}, {7, 8}}, {{1, 2}, {3, 4}})
-- result is {{4, 4}, {4, 4}}
math.matrix_multiply(m1, m2)
Multiplies two matrices.
local result = math.matrix_multiply({{1, 2}, {3, 4}}, {{5, 6}, {7, 8}})
-- result is {{19, 22}, {43, 50}}
math.matrix_transpose(m)
Transposes a matrix.
math.matrix_multiply_gemm(m1, m2)
Multiplies two matrices using the GEMM (General Matrix Multiply) algorithm.
local result = math.matrix_multiply_gemm({{1, 2}, {3, 4}}, {{5, 6}, {7, 8}})
-- result is {{19, 22}, {43, 50}}
math.matrix_determinant(m)
Calculates the determinant of a square matrix.
math.matrix_inverse(m)
Calculates the inverse of a square matrix.
local result = math.matrix_inverse({{1, 2}, {3, 4}})
-- result is approximately {{-2, 1}, {1.5, -0.5}}
Transformations
math.transform_translate(v, t)
Translates a 3D vector.
math.transform_rotate(v, angle, axis)
Rotates a 3D vector around a specified axis.
local result = math.transform_rotate({1, 0, 0}, math.pi/2, "z")
-- result is approximately {0, 1, 0}
math.transform_scale(v, scale)
Scales a 3D vector.
Number Theory
math.gcd(a, b)
Calculates the Greatest Common Divisor of two integers.
math.lcm(a, b)
Calculates the Least Common Multiple of two integers.
math.is_prime(n)
Checks if a number is prime.
math.next_prime(n)
Finds the next prime number after the given number.
math.modular_exponentiation(base, exponent, modulus)
Performs modular exponentiation.
math.modular_inverse(a, m)
Calculates the modular multiplicative inverse.
Set Operations
math.set_union(set1, set2)
Performs the union of two sets.
math.set_intersection(set1, set2)
Performs the intersection of two sets.
math.set_difference(set1, set2)
Performs the difference of two sets.
math.set_symmetric_difference(set1, set2)
Performs the symmetric difference of two sets.
math.set_cardinality(set)
Returns the number of elements in a set.
math.set_contains(set, element)
Checks if an element is in a set.
Other Math Functions
math.erf(x)
Calculates the error function of x.
math.erfc(x)
Calculates the complementary error function of x.
math.gamma(x)
Calculates the gamma function of x.
math.lgamma(x)
Calculates the natural logarithm of the absolute value of the gamma function of x.
math.exp2(x)
Calculates 2 raised to the power of x.
math.expm1(x)
Calculates e^x - 1, which is more accurate for small values of x than math.exp(x) - 1.
math.log1p(x)
Calculates log(1 + x), which is more accurate for small values of x than math.log(1 + x).
math.dist(p, q)
Calculates the Euclidean distance between two points p and q, represented as tables.
math.nextafter(x, y, steps)
Returns the next representable floating-point value after x in the direction of y. The optional steps parameter specifies how many steps to take.
math.prod(t, start)
Calculates the product of all numbers in table t, optionally starting with the value start.
math.remainder(x, y)
Calculates the IEEE 754 floating-point remainder of x divided by y.
math.sumprod(p, q)
Calculates the sum of products of corresponding elements in tables p and q.
math.isfinite(x)
Checks if a number x is finite (not infinity or NaN).
math.comb(n, k)
Calculates the number of ways to choose k items from n items without repetition and without order.
math.acosh(x)
Calculates the inverse hyperbolic cosine of x.
math.asinh(x)
Calculates the inverse hyperbolic sine of x.
math.atanh(x)
Calculates the inverse hyperbolic tangent of x.
math.log2(x)
Calculates the base-2 logarithm of x.
math.isclose(a, b, rel_tol, abs_tol)
Checks if two values a and b are close to each other within a relative tolerance rel_tol and an absolute tolerance abs_tol.