ForeverDev

Untitled

Nov 5th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.88 KB | None | 0 0
  1. local SIGMA_LIMIT = 16
  2.  
  3. function sigma(flags, callback, x)
  4.     local sum = 0
  5.     for n = flags.start, SIGMA_LIMIT do
  6.         sum = sum + callback(n, x)
  7.     end
  8.     return sum
  9. end
  10.  
  11. function newSigmaFunction(flags, callback)
  12.     return setmetatable({flags = flags, callback = callback}, {
  13.         __call = function(this, x)
  14.             return sigma(this.flags, this.callback, x)
  15.         end
  16.     })
  17. end
  18.  
  19. local MATH = {}
  20.  
  21. MATH.factorial = function(number)
  22.     local product = 1
  23.     for i = 1, number do
  24.         product = product * i
  25.     end
  26.     return product
  27. end
  28.  
  29. MATH.power = function(x, p)
  30.     return x ^ p
  31. end
  32.  
  33. MATH.cosine = newSigmaFunction({start = 0}, function(n, x)
  34.     return (MATH.power(-1, n) * MATH.power(x, 2 * n)) / MATH.factorial(2 * n)
  35. end)
  36.  
  37. MATH.sine = newSigmaFunction({start = 1}, function(n, x)
  38.     return (MATH.power(-1, n - 1) / MATH.factorial((2 * n) - 1)) * MATH.power(x, (2 * n) - 1)
  39. end)
  40.  
  41. print(MATH.sine(5))
Advertisement
Add Comment
Please, Sign In to add comment