Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local SIGMA_LIMIT = 16
- function sigma(flags, callback, x)
- local sum = 0
- for n = flags.start, SIGMA_LIMIT do
- sum = sum + callback(n, x)
- end
- return sum
- end
- function newSigmaFunction(flags, callback)
- return setmetatable({flags = flags, callback = callback}, {
- __call = function(this, x)
- return sigma(this.flags, this.callback, x)
- end
- })
- end
- local MATH = {}
- MATH.factorial = function(number)
- local product = 1
- for i = 1, number do
- product = product * i
- end
- return product
- end
- MATH.power = function(x, p)
- return x ^ p
- end
- MATH.cosine = newSigmaFunction({start = 0}, function(n, x)
- return (MATH.power(-1, n) * MATH.power(x, 2 * n)) / MATH.factorial(2 * n)
- end)
- MATH.sine = newSigmaFunction({start = 1}, function(n, x)
- return (MATH.power(-1, n - 1) / MATH.factorial((2 * n) - 1)) * MATH.power(x, (2 * n) - 1)
- end)
- print(MATH.sine(5))
Advertisement
Add Comment
Please, Sign In to add comment