Module mmath
A grab bag of linear equation solvers.
Useful for finding solution using linear algebra.
An introduction to Linear Algebra can be found on Wikipedia.
If you are interested in using CCSharp, here is the compatible MMath.cs file.
Credit goes to sans.9536 of the Minecraft Computer Mods discord for these functions and this file.
Functions
| F.solveSysEq (f, g, min, max, steps, tol, closeThresh) | Solves f(x) = g(x) over an interval by probing many starting points and using Newton-Raphson. |
| F.weightedTable (values, weights) | Expand a set of values according to integer weights. |
| F.scramble (t) | Shuffle (randomize) an array-like table in-place and return it. |
| F.integrateSimple (f, n, a, b, init) | Numerically integrate f on [a, b] using the trapezoid rule. |
| F.integrateComplex (f, n, a, b, init) | Numerically integrate f on [a, b] using Simpson-like parabolic rule. |
| F.ARC (f, x, h) | Approximate the derivative (central difference) of a single-variable function. |
| F.solveRoot (f, x0, tol) | Find a root using Newton–Raphson starting from x0. |
Functions
- F.solveSysEq (f, g, min, max, steps, tol, closeThresh)
-
Solves f(x) = g(x) over an interval by probing many starting points and using Newton-Raphson.
Usage:
local roots = F.solveSysEq(math.sin, function(x) return 0 end, 0, 2 * math.pi, 100, 1e-8, 1e-6)
- F.weightedTable (values, weights)
-
Expand a set of values according to integer weights.
For each index i in
values,values[i]is inserted into the returned tableweights[i]times. Bothvaluesandweightsare treated as array-like tables using matching indices.Usage:
local expanded = F.weightedTable({"a","b"}, {3,1}) -- {"a","a","a","b"}
- F.scramble (t)
-
Shuffle (randomize) an array-like table in-place and return it.
This function mutates the input table
tby repeatedly removing each element and inserting it at a random position. It is a simple shuffle and not an in-place Fisher–Yates implementation.Usage:
F.scramble(myArray) - F.integrateSimple (f, n, a, b, init)
-
Numerically integrate
fon [a, b] using the trapezoid rule.Usage:
local I = F.integrateSimple(math.sin, 1000, 0, math.pi)
- F.integrateComplex (f, n, a, b, init)
-
Numerically integrate
fon [a, b] using Simpson-like parabolic rule.If
nis odd it is decremented to make it even. This method is generally more accurate than the trapezoid rule for smooth functions.Usage:
local I = F.integrateComplex(math.sin, 1000, 0, math.pi)
- F.ARC (f, x, h)
-
Approximate the derivative (central difference) of a single-variable function.
Computes (f(x + h) - f(x - h)) / (2 * h). Use smaller
hto approach the analytical derivative but beware of numerical round-off for extremely smallh.Usage:
local d = F.ARC(math.sin, 1.0, 1e-6)
- F.solveRoot (f, x0, tol)
-
Find a root using Newton–Raphson starting from
x0.Attempts up to 500 iterations. The derivative is approximated using F.ARC. On success returns the root (a number), otherwise returns
nilto indicate failure.Usage:
local r = F.solveRoot(math.sin, 3.0, 1e-12)