Codes Otaku Math Module (60+ functions, 700+ lines)

Few fixes (Thanks to @jonbyte) and some more documentation
Here is the changes: Few fixes and documentation · CodesOtakuYT/CodesOtakuModules@5d6e823 (github.com)

This is really cool @IlyasTawawe! Thanks for sharing!

1 Like

Added tons of new functions, and some fixes :smiley:
Also documented everything decently for now

{
    --> Practical functions
	lerp = lerp, -- takes a range [a -> b] and a t value [0->1]. it returns a value travalling from a to b linearly, where t is the percentage it advanced (Ex: 50% = 0.5)
	inverseLerp = inverseLerp, -- takes a range [c -> d] and a value x, it returns the t value of the value in range, basically where the value is relative to the range.
	map = map, -- lerp(o1, o2, inverseLerp(i1, i2, x)), remaps a value x from the range [i1->i2] to [o1->o2], can be useful to create sliders.

    --> Calculus
    compositeDerivative = compositeDerivative, -- (f(g))' = f'(g)*g'

    --> Table Math
	mapT = mapT, -- Gets a table [x1, x2, x3...xn] and a function, returns [f(x1), f(x2), f(x3)...f(xn)]
	filterT = filterT, -- Gets a table and a function, returns a new table with the elements that evaluated the function f(value, key) to true
	countT = countT, -- Accumulates the result of a function f(v, k) traversing a table, returns the accumulated value
	countMulT = countMulT, -- The same as countT but with multiplication
	sampleT = sampleT, -- Sample function output n times from x1 to x2 linearly

	----> Vector math
	distanceV = distanceV, -- Returns the distance between the position vec1 and vec2
	midPosV = midPosV, -- Returns the middle position between the position vec1 and vec2, equivalent to lerp(vec1, vec2, 0.5)
	applyV = applyV, -- Returns a new Vector3 by applying a function on the X, Y and Z components of a Vector3
	absV = absV, -- Returns a new Vector3 where all the components of the vector are absolute (positive or nil)

       -- Helper functions
	copyT = copyT, -- Returns a copy of a table
	invertT = invertT, -- the keys becomes the values and vice versa, takes a table [y1 = x1, y2 = x2...yn = xn], returns [x1 = y1, x2 = y2...xn = yn]
	zipT = zipT, -- takes 2 tables and zip them together {{t1[1], t2[1]}, {t1[2], t2[2]}...{t1[n], t2[n]}}
	unzipT = unzipT, -- the inverse of zip, takes 1 table of pairs and unzip it into 2 tables
	valuesT = valuesT, -- return a table of all the values inside the table, there is also recursive mode for nested tables
	keysT = keysT, -- the same as valuesT, but for table keys
}

Tons of new functions and some fixes! · CodesOtakuYT/CodesOtakuModules@c3bb372 (github.com)

You should add more trigonometric functions like these ones

versin = function(x) return 1 - math.cos(x) end -- versin(θ) = 1 - cos(θ) = 2sin²(θ/2)
coversin = function(x) return 1 - math.sin(x) end -- coversine(θ) = 1 - sin(θ)
exsec = function(x) return (1 / math.cos(x)) - 1 end -- exsecant(θ) = sec(θ) - 1 = 1 / cos(θ) - 1
haversin = function(x) return 1 / 2 * (1 - math.cos(x)) end -- haversine(θ) = sin²(0⁄2) = 1 - cos(θ) / 2
1 Like

You can commit it to github and I’ll accept it. otherwise I can add them myself. Thanks for your contribution :smiley:

1 Like

You could also add exponential lerp functions (non-linear)

--Credits to https://github.com/FreyaHolmer/Mathfs/blob/e13700a9636190a118730c09a389aa316d8ec240/Mathfs.cs

eerp = function(a, b, t) --exponential lerp
    return a ^ (1 - t) * b ^ t
end

ieerp = function(a, b, t) --inverse exponential lerp
    return math.log(a / t) / math.log(a / b)
end
1 Like

Ohh, that’s interesting, I’ll add them in the next update for sure, thanks a lot!

1 Like

Update: Update Math.lua · CodesOtakuYT/CodesOtakuModules@2981170 (github.com)
New functions:

    ----> Practical functions
	lerpExp = lerpExp, -- exponential lerp
	invLerpExp = invLerpExp, inverseLerpExp = invLerpExp, -- inverse exponential lerp
       
    ----> Pure mathematical functions
	versin = versin, -- versin(θ) = 1 - cos(θ) = 2sin²(θ/2)
	coversin = coversin, -- coversine(θ) = 1 - sin(θ)
	exsec = exsec, -- exsecant(θ) = sec(θ) - 1 = 1 / cos(θ) - 1
	haversin = haversin, -- haversine(θ) = sin²(0⁄2) = 1 - cos(θ) / 2

	----> Random
	randomItemT = randomItemT, -- Returns a random key in table
	random = random, -- returns a random number in range [a,b]
	randomN = randomN, -- returns a random integer in range [a,b]

Thanks to @thrixtle for his contribution:

versin, coversin, exsec, haversin, lerpExp, invLerpExp

Looking forward to hear your feedback everyone!

1 Like

If you’re gonna go with trig functions:

(source: Wikipedia Commons)

2 Likes

Am gonna revamp the documentation a bit in the future update, and add with it useful resources like such image links and desmos graphs (the way I personally learn and enjoy math) if needed

I found this cool function while watching this youtube video, and I thought it could be useful.

local function factorial(n)
    if (n == 0) then
        return 1
    else
        return n * factorial(n - 1)
    end
end

local function fastFactorial(n)
    return math.sqrt((2*n + 1/3) * 3.1415926535897932) * (n / 2.718281828459045) ^ n
end

print(fastFactorial(50)) --> 3.0414009581301e+64
print(factorial(50)) --> 3.0414093201713e+64

print(factorial(50) / fastFactorial(50)) --> 1.0000027494044215
1 Like

Update:
Update Math.lua · CodesOtakuYT/CodesOtakuModules@58de414 (github.com)

deepCopyT = deepCopyT, -- Returns a copy of the table and all of the tables inside it, if isKeyCopy, then it will also deep copy the keys of the table if they're also tables, it also supports cyclic tables copying.
toRawT = toRawT, -- returns a one dimensional table copy of a complex recursive table
randomV = randomV, -- returns a unit vector3 as a random direction
angleV = angleV, -- Returns the angle between the given vectors or vector
fastFactorial = factorialFast, factorialFast = factorialFast, -- x! but sacrifice accuracy for speed

This update’s contributions:

Bug thrixtle: fastFactorial
1 Like