Skip to content

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.

local result = math.vector_add({1, 2, 3}, {4, 5, 6})
-- result is {5, 7, 9}

math.vector_subtract(v1, v2)

Subtracts the second vector from the first.

local result = math.vector_subtract({4, 5, 6}, {1, 2, 3})
-- result is {3, 3, 3}

math.vector_dot_product(v1, v2)

Calculates the dot product of two vectors.

local result = math.vector_dot_product({1, 2, 3}, {4, 5, 6})
-- result is 32

math.vector_cross_product(v1, v2)

Calculates the cross product of two 3D vectors.

local result = math.vector_cross_product({1, 0, 0}, {0, 1, 0})
-- result is {0, 0, 1}

math.vector_normalize(v)

Normalizes a vector (makes its length 1).

local result = math.vector_normalize({3, 4, 0})
-- result is approximately {0.6, 0.8, 0}

math.vector_magnitude(v)

Calculates the magnitude (length) of a vector.

local result = math.vector_magnitude({3, 4, 0})
-- result is 5

Matrix Operations

math.matrix_create(rows, cols, initial_value)

Creates a new matrix with the specified dimensions.

local matrix = math.matrix_create(3, 3, 0)
-- Creates a 3x3 matrix filled with zeros

math.matrix_add(m1, m2)

Adds two matrices.

local result = math.matrix_add({{1, 2}, {3, 4}}, {{5, 6}, {7, 8}})
-- result is {{6, 8}, {10, 12}}

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.

local result = math.matrix_transpose({{1, 2}, {3, 4}})
-- result is {{1, 3}, {2, 4}}

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.

local result = math.matrix_determinant({{1, 2}, {3, 4}})
-- result is -2

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.

local result = math.transform_translate({1, 2, 3}, {4, 5, 6})
-- result is {5, 7, 9}

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.

local result = math.transform_scale({1, 2, 3}, 2)
-- result is {2, 4, 6}

Number Theory

math.gcd(a, b)

Calculates the Greatest Common Divisor of two integers.

local result = math.gcd(48, 18)
-- result is 6

math.lcm(a, b)

Calculates the Least Common Multiple of two integers.

local result = math.lcm(4, 6)
-- result is 12

math.is_prime(n)

Checks if a number is prime.

local result = math.is_prime(17)
-- result is true

math.next_prime(n)

Finds the next prime number after the given number.

local result = math.next_prime(18)
-- result is 19

math.modular_exponentiation(base, exponent, modulus)

Performs modular exponentiation.

local result = math.modular_exponentiation(2, 3, 5)
-- result is 3

math.modular_inverse(a, m)

Calculates the modular multiplicative inverse.

local result = math.modular_inverse(3, 11)
-- result is 4

Set Operations

math.set_union(set1, set2)

Performs the union of two sets.

local result = math.set_union({1, 2, 3}, {3, 4, 5})
-- result is {1, 2, 3, 4, 5}

math.set_intersection(set1, set2)

Performs the intersection of two sets.

local result = math.set_intersection({1, 2, 3}, {2, 3, 4})
-- result is {2, 3}

math.set_difference(set1, set2)

Performs the difference of two sets.

local result = math.set_difference({1, 2, 3, 4}, {3, 4, 5})
-- result is {1, 2}

math.set_symmetric_difference(set1, set2)

Performs the symmetric difference of two sets.

local result = math.set_symmetric_difference({1, 2, 3}, {2, 3, 4})
-- result is {1, 4}

math.set_cardinality(set)

Returns the number of elements in a set.

local result = math.set_cardinality({1, 2, 3, 4})
-- result is 4

math.set_contains(set, element)

Checks if an element is in a set.

local result = math.set_contains({1, 2, 3}, 2)
-- result is true

Other Math Functions

math.erf(x)

Calculates the error function of x.

local result = math.erf(1.5)
-- result is approximately 0.9661051464753107

math.erfc(x)

Calculates the complementary error function of x.

local result = math.erfc(1.5)
-- result is approximately 0.03389485352468927

math.gamma(x)

Calculates the gamma function of x.

local result = math.gamma(5.5)
-- result is approximately 52.34277778455352

math.lgamma(x)

Calculates the natural logarithm of the absolute value of the gamma function of x.

local result = math.lgamma(5.5)
-- result is approximately 3.9578139676187165

math.exp2(x)

Calculates 2 raised to the power of x.

local result = math.exp2(3)
-- result is 8

math.expm1(x)

Calculates e^x - 1, which is more accurate for small values of x than math.exp(x) - 1.

local result = math.expm1(0.01)
-- result is approximately 0.010050167084168056

math.log1p(x)

Calculates log(1 + x), which is more accurate for small values of x than math.log(1 + x).

local result = math.log1p(1e-10)
-- result is approximately 9.999999999500000e-11

math.dist(p, q)

Calculates the Euclidean distance between two points p and q, represented as tables.

local result = math.dist({0, 0, 0}, {3, 4, 0})
-- result is 5

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.

local result = math.nextafter(1.0, 2.0)
-- result is the smallest number greater than 1.0

math.prod(t, start)

Calculates the product of all numbers in table t, optionally starting with the value start.

local result = math.prod({1, 2, 3, 4}, 2)
-- result is 48

math.remainder(x, y)

Calculates the IEEE 754 floating-point remainder of x divided by y.

local result = math.remainder(10, 3)
-- result is 1

math.sumprod(p, q)

Calculates the sum of products of corresponding elements in tables p and q.

local result = math.sumprod({1, 2, 3}, {4, 5, 6})
-- result is 32

math.isfinite(x)

Checks if a number x is finite (not infinity or NaN).

local result = math.isfinite(1/0)
-- result is false

math.comb(n, k)

Calculates the number of ways to choose k items from n items without repetition and without order.

local result = math.comb(5, 2)
-- result is 10

math.acosh(x)

Calculates the inverse hyperbolic cosine of x.

local result = math.acosh(2)
-- result is approximately 1.3169578969248166

math.asinh(x)

Calculates the inverse hyperbolic sine of x.

local result = math.asinh(1)
-- result is approximately 0.8813735870195430

math.atanh(x)

Calculates the inverse hyperbolic tangent of x.

local result = math.atanh(0.5)
-- result is approximately 0.5493061443340548

math.log2(x)

Calculates the base-2 logarithm of x.

local result = math.log2(8)
-- result is 3

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.

local result = math.isclose(1.0, 1.000001, 1e-5, 0)
-- result is true