Skip to content

Advanced Math Module

Roblox Math Module Documentation

This document explains the functionality of the math_module Lua module for Roblox. This module provides various mathematical functions categorized into basic, advanced, trigonometric, hyperbolic, vector, matrix, and statistical functions.

Usage

To use this module in your Roblox script, place the following script in a ModuleScript in ReplicatedStorage.

-- math_module.lua

local math_module = {}

-- Simple Math Functions

function math_module.add(a, b)
    return a + b
end

function math_module.sub(a, b)
    return a - b
end

function math_module.mul(a, b)
    return a * b
end

function math_module.div(a, b)
    if b == 0 then
        error("Cannot divide by zero!")
    end
    return a / b
end

function math_module.mod(a, b)
    return a % b
end

function math_module.pow(a, b)
    return a ^ b
end

-- Advanced Math Functions

function math_module.sqrt(a)
    return math.sqrt(a)
end

function math_module.abs(a)
    return math.abs(a)
end

function math_module.sin(a)
    return math.sin(a)
end

function math_module.cos(a)
    return math.cos(a)
end

function math_module.tan(a)
    return math.tan(a)
end

function math_module.asin(a)
    return math.asin(a)
end

function math_module.acos(a)
    return math.acos(a)
end

function math_module.atan(a)
    return math.atan(a)
end

function math_module.atan2(y, x)
    return math.atan2(y, x)
end

function math_module.log(a, base)
    if base then
        return math.log(a) / math.log(base)
    else
        return math.log(a)
    end
end

function math_module.exp(a)
    return math.exp(a)
end

function math_module.ceil(a)
    return math.ceil(a)
end

function math_module.floor(a)
    return math.floor(a)
end

function math_module.round(a)
    return math.floor(a + 0.5)
end

-- Trigonometric Functions

function math_module.cot(a)
    return 1 / math.tan(a)
end

function math_module.sec(a)
    return 1 / math.cos(a)
end

function math_module.csc(a)
    return 1 / math.sin(a)
end

function math_module.hypot(a, b)
    return math.sqrt(a ^ 2 + b ^ 2)
end

-- Hyperbolic Functions

function math_module.sinh(a)
    return (math.exp(a) - math.exp(-a)) / 2
end

function math_module.cosh(a)
    return (math.exp(a) + math.exp(-a)) / 2
end

function math_module.tanh(a)
    return math.sinh(a) / math.cosh(a)
end

function math_module.asinh(a)
    return math.log(a + math.sqrt(a ^ 2 + 1))
end

function math_module.acosh(a)
    return math.log(a + math.sqrt(a ^ 2 - 1))
end

function math_module.atanh(a)
    return math.log((1 + a) / (1 - a)) / 2
end

-- Vector Math Functions

function math_module.dot(a, b)
    return a.x * b.x + a.y * b.y + a.z * b.z
end

function math_module.cross(a, b)
    return Vector3.new(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x)
end

function math_module.magnitude(a)
    return math.sqrt(a.x ^ 2 + a.y ^ 2 + a.z ^ 2)
end

function math_module.normalize(a)
    local magnitude = math_module.magnitude(a)
    return Vector3.new(a.x / magnitude, a.y / magnitude, a.z / magnitude)
end

function math_module.distance(a, b)
    return math.sqrt((a.x - b.x) ^ 2 + (a.y - b.y) ^ 2 + (a.z - b.z) ^ 2)
end

function math_module.angleBetween(a, b)
    local dotProduct = math_module.dot(a, b)
    local magnitudeA = math_module.magnitude(a)
    local magnitudeB = math_module.magnitude(b)
    return math.acos(dotProduct / (magnitudeA * magnitudeB))
end

-- Matrix Math Functions

function math_module.multiplyMatrix(a, b)
    local result = {}
    for i = 1, 4 do
        result[i] = {}
        for j = 1, 4 do
            result[i][j] = 0
            for k = 1, 4 do
                result[i][j] = result[i][j] + a[i][k] * b[k][j]
            end
        end
    end
    return result
end

function math_module.invertMatrix(a)
    -- implement matrix inversion algorithm here
    -- for now, just return the identity matrix
return {
        {1, 0, 0, 0},
        {0, 1, 0, 0},
        {0, 0, 1, 0},
        {0, 0, 0, 1}
    }
end

-- Statistical Functions

function math_module.mean(numbers)
    local sum = 0
    for i, number in ipairs(numbers) do
        sum = sum + number
    end
    return sum / #numbers
end

function math_module.median(numbers)
    table.sort(numbers)
    local midIndex = math.floor(#numbers / 2)
    if #numbers % 2 == 0 then
        return (numbers[midIndex] + numbers[midIndex + 1]) / 2
    else
        return numbers[midIndex]
    end
end

function math_module.mode(numbers)
    local frequencyTable = {}
    for i, number in ipairs(numbers) do
        if not frequencyTable[number] then
            frequencyTable[number] = 1
        else
            frequencyTable[number] = frequencyTable[number] + 1
        end
    end
    local maxFrequency = 0
    local mode = nil
    for number, frequency in pairs(frequencyTable) do
        if frequency > maxFrequency then
            maxFrequency = frequency
            mode = number
        end
    end
    return mode
end

function math_module.standardDeviation(numbers)
    local mean = math_module.mean(numbers)
    local variance = 0
    for i, number in ipairs(numbers) do
        variance = variance + (number - mean) ^ 2
    end
    variance = variance / #numbers
    return math.sqrt(variance)
end

return math_module

Now, require it in any script.

local math_module = require(game.ReplicatedStorage.PathToModule)

Replace PathToModule with the actual path to the script containing the math_module code. Then, you can call the desired functions from the module, like this:

local result = math_module.add(5, 3) -- result will be 8

Simple Math Functions

  • add(a, b): Adds two numbers a and b and returns the sum.
  • sub(a, b): Subtracts b from a and returns the difference.
  • mul(a, b): Multiplies a by b and returns the product.
  • div(a, b): Divides a by b and returns the quotient. Note: This function throws an error if b is zero.
  • mod(a, b): Returns the remainder of dividing a by b.
  • pow(a, b): Raises a to the power of b and returns the result.

Advanced Math Functions

These functions utilize Lua's built-in math library for advanced operations.

  • sqrt(a): Calculates the square root of a.
  • abs(a): Returns the absolute value of a.
  • sin(a): Calculates the sine of a in radians.
  • cos(a): Calculates the cosine of a in radians.
  • tan(a): Calculates the tangent of a in radians.
  • asin(a): Calculates the arcsine of a in radians (inverse sine).
  • acos(a): Calculates the arccosine of a in radians (inverse cosine).
  • atan(a): Calculates the arctangent of a in radians (inverse tangent).
  • atan2(y, x): Calculates the arctangent of y / x in radians, considering the signs to determine the quadrant.
  • log(a, base): Calculates the logarithm of a with optional base. If no base is provided, it defaults to base 10 (common logarithm).
  • exp(a): Calculates the value of e raised to the power of a.
  • ceil(a): Returns the smallest integer greater than or equal to a.
  • floor(a): Returns the largest integer less than or equal to a.
  • round(a): Rounds a to the nearest integer.

Trigonometric Functions (Derived)

These functions are built upon the basic trigonometric functions.

  • cot(a): Calculates the cotangent of a (1 / tan(a)).
  • sec(a): Calculates the secant of a (1 / cos(a)).
  • csc(a): Calculates the cosecant of a (1 / sin(a)).
  • hypot(a, b): Calculates the hypotenuse of a right triangle with sides a and b using the Pythagorean theorem.

Hyperbolic Functions

These functions deal with hyperbolic trigonometric concepts.

  • sinh(a): Calculates the hyperbolic sine of a.
  • cosh(a): Calculates the hyperbolic cosine of a.
  • tanh(a): Calculates the hyperbolic tangent of a.
  • asinh(a): Calculates the inverse hyperbolic sine of a.
  • acosh(a): Calculates the inverse hyperbolic cosine of a.
  • atanh(a): Calculates the inverse hyperbolic tangent of a.

Vector Math Functions (Requires Vector3 objects)

These functions operate on Roblox's Vector3 objects.

  • dot(a, b): Calculates the dot product of vectors a and b.
  • cross(a, b): Calculates the cross product of vectors a and b, resulting in a new vector.
  • magnitude(a): Calculates the magnitude (length) of vector a.
  • normalize(a): Returns a new vector in the same direction as a
  • distance(a, b): Calculates the distance between two points represented by vectors a and b.
  • angleBetween(a, b): Calculates the angle in radians between two vectors a and b.

Matrix Math Functions

These functions operate on 4x4 matrices (represented as tables). Note: The invertMatrix function is currently not implemented and returns the identity matrix.

  • multiplyMatrix(a, b): Multiplies two 4x4 matrices a and b and returns the resulting product matrix.
  • invertMatrix(a): This function is currently not implemented and returns the identity matrix instead. (Placeholder for future implementation)

Statistical Functions

These functions perform calculations on tables containing numerical data.

  • mean(numbers): Calculates the average of the values in the numbers table.
  • median(numbers): Calculates the median of the values in the numbers table (middle value when sorted).
  • mode(numbers): Calculates the most frequent value (mode) in the numbers table.
  • standardDeviation(numbers): Calculates the standard deviation of the values in the numbers table.

This concludes the documentation for the math_module functions.

Note: This documentation may be out of date or may contain errors.