Advertisement
Bolodefchoco_LUAXML

[Biblioteca] SciLua Math

Jul 17th, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. --Creator: Bolodefchoco
  2. --Made in: 17/07/2016
  3. --Last update: 17/07/2016
  4. --[[ Notes:
  5.     math.round
  6.         Does:
  7.             Arredonda pro número inteiro mais próximo
  8.         Args:
  9.             x --> Número
  10.     math.step
  11.         Does:
  12.             {1x>=0/0x<0}
  13.         Args:
  14.             x --> Número
  15.     math.sign
  16.         Does:
  17.             {1x>=0/-1x<0}
  18.         Args:
  19.             x --> Número
  20.     math.phi
  21.         Does:
  22.             Φ(x)=(∫x−∞)(1/√2π)exp{−(t^2/2)}dt
  23.         Args:
  24.             x --> Número
  25.     math.iphi
  26.         Does:
  27.             (IΦ)(x)=Φ−1(x)
  28.         Args:
  29.             x --> Número
  30.     math.gamma
  31.         Does:
  32.             Γ(z)=(∫∞0)e^−t t^(z−1)dt
  33.         Args:
  34.             z --> Número
  35.     math.loggamma
  36.         Does:
  37.             Valor absoluto do valor da função math.gamma
  38.         Args:
  39.             z --> Número
  40.     math.logbeta
  41.         Does:
  42.             Logaritmo da função math.beta
  43.         Args:
  44.             a --> Número
  45.             b --> Número
  46.     math.beta
  47.         Does:
  48.         B(a,b)=(Γ(a)Γ(b))/Γ(a+b)
  49.  
  50.         Args:
  51.             a --> Número
  52.             b --> Número
  53. ]]--
  54.  
  55.  
  56. math.round = function(x)
  57.     return x<0 and math.ceil(x-.5) or math.floor(x+.5)
  58. end
  59.  
  60. math.step = function(x)
  61.     return math.max(0,math.min(math.floor(x)-1,1))
  62. end
  63.  
  64. math.sign = function(x)
  65.     return -1 + math.step(x)*2
  66. end
  67.  
  68. math.phi = function(x)
  69.     if x<=-8 then return 0 elseif x>=8 then return 1 else
  70.         local s,b,q = x,x,x^2
  71.         for i = 3,1/0,2 do
  72.             b = b*q/i
  73.             local t = s
  74.             s = t + b
  75.             if s == t then break end
  76.         end
  77.         return .5+s*math.exp(-.5*q-.91893853320467274178)
  78.     end
  79. end
  80.  
  81. local iphifast,iphi
  82. do
  83.     local a = {
  84.         [1] = 0,
  85.         [2] = -3.969683028665376e+01,
  86.         [3] = 2.209460984245205e+02,
  87.         [4] = -2.759285104469687e+02,
  88.         [5] = 1.383577518672690e+02,
  89.         [6] = -3.066479806614716e+01,
  90.         [7] = 2.506628277459239e+00
  91.     }
  92.  local b = {
  93.         [1] = 0,
  94.         [2] = -5.447609879822406e+01,
  95.         [3] = 1.615858368580409e+02,
  96.         [4] = -1.556989798598866e+02,
  97.         [5] = 6.680131188771972e+01,
  98.         [6] = -1.328068155288572e+01
  99.     }
  100.   local c = {
  101.         [1] = 0,
  102.         [2] = -7.784894002430293e-03,
  103.         [3] = -3.223964580411365e-01,
  104.         [4] = -2.400758277161838e+00,
  105.         [5] = -2.549732539343734e+00,
  106.         [6] = 4.374664141464968e+00,
  107.         [7] = 2.938163982698783e+00
  108.     }
  109.   local d = {
  110.         [1] = 0,
  111.         [2] = 7.784695709041462e-03,
  112.         [3] = 3.224671290700398e-01,
  113.         [4] = 2.445134137142996e+00,
  114.         [5] = 3.754408661907416e+00
  115.     }
  116.     math.iphifast = function(p)
  117.         if math.abs(p-.5) < .47575 then
  118.             local q = p-.5
  119.             local r = q^2
  120.             return (((((a[1]*r+a[2])*r+a[3])*r+a[4])*r+a[5])*r+a[6])*q/(((((b[1]*r+b[2])*r+b[3])*r+b[4])*r+b[5])*r+1)
  121.         else
  122.             local iu = math.ceil(p-.97575)
  123.             local z = (1-iu)*p+iu*(1-p)
  124.             local sign = 1-2*iu
  125.             local q = math.sqrt(-2*math.log(z))
  126.             return sign*(((((c[1]*q+c[2])*q+c[3])*q+c[4])*q+c[5])*q+c[6])/((((d[1]*q+d[2])*q+d[3])*q+d[4])*q+1)
  127.         end
  128.     end
  129.     math.iphi = function(p)
  130.         if p<=0 then
  131.             return -1/0
  132.         elseif p>=1 then
  133.             return 1/0
  134.         else
  135.             local x = math.iphifast(p)
  136.             local e = math.phi(x)-p
  137.             local u = e*math.sqrt(2*math.pi)*math.exp(x^2/2)
  138.             return x-u/(1+x*u/2)
  139.         end
  140.     end
  141.     local gamma,loggamma
  142. end
  143.  
  144. do
  145.     local gamma_r10 = 10.900511
  146.     local gamma_dk = {
  147.         [1] = 2.48574089138753565546e-5,
  148.         [2] = 1.05142378581721974210,
  149.         [3] = -3.45687097222016235469,
  150.         [4] = 4.51227709466894823700,
  151.         [5] = -2.98285225323576655721,
  152.         [6] = 1.05639711577126713077,
  153.         [7] = -1.95428773191645869583e-1,
  154.         [8] = 1.70970543404441224307e-2,
  155.         [9] = -5.71926117404305781283e-4,
  156.         [10] = 4.63399473359905636708e-6,
  157.         [11] = -2.71994908488607703910e-9
  158.   }
  159.     local gamma_c = 2*math.sqrt(math.exp(1)/math.pi)
  160.     math.gamma = function(z)  
  161.         if z<0 then
  162.             return math.pi/(math.sin(math.pi*z)*math.gamma(1-z))
  163.         end
  164.         local sum = gamma_dk[0];sum = sum + gamma_dk[1]/(z+0);sum = sum + gamma_dk[2]/(z+1);sum = sum + gamma_dk[3]/(z+2);sum = sum + gamma_dk[4]/(z+3);sum = sum + gamma_dk[5]/(z+4);sum = sum + gamma_dk[6]/(z+5);sum = sum + gamma_dk[7]/(z+6);sum = sum + gamma_dk[8]/(z+7);sum = sum + gamma_dk[9]/(z+8);sum = sum + gamma_dk[10]/(z+9)  
  165.         return gamma_c*((z+gamma_r10-.5)/math.exp(1))^(z-.5)*sum
  166.     end
  167.     math.loggamma = function(z)
  168.         if z<0 then
  169.             return math.log(math.pi)-math.log(math.abs(math.sin(math.pi*z)))-math.loggamma(1-z)
  170.         end  
  171.         local sum = gamma_dk[0];sum = sum + gamma_dk[1]/(z+0);sum = sum + gamma_dk[2]/(z+1);sum = sum + gamma_dk[3]/(z+2);sum = sum + gamma_dk[4]/(z+3);sum = sum + gamma_dk[5]/(z+4);sum = sum + gamma_dk[6]/(z+5);sum = sum + gamma_dk[7]/(z+6);sum = sum + gamma_dk[8]/(z+7);sum = sum + gamma_dk[9]/(z+8);sum = sum + gamma_dk[10]/(z+9)
  172.         return math.log(gamma_c)+(z-.5)*math.log(z+gamma_r10-.5)-(z-.5)+math.log(sum)
  173.     end
  174. end
  175.  
  176. local logbeta = function(a,b)
  177.   if a<=0 or b<=0 then return 0/0 end
  178.   return math.loggamma(a)+math.loggamma(b)-math.loggamma(a+b)
  179. end
  180.  
  181. local beta = function(a,b)
  182.   return math.exp(logbeta(a,b))
  183. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement