- Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmath.lua
More file actions
Latest commit
333 lines (299 loc) · 11 KB
/
Copy pathmath.lua
File metadata and controls
333 lines (299 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
--------------------------------------------------------------------------------
-- Special math functions module.
--
-- Copyright (C) 2011-2016 Stefano Peluchetti. All rights reserved.
--------------------------------------------------------------------------------
localffi=require"ffi"
localbit=require"bit"
localxsys=require"xsys"
-- Cache all builtin math functions.
local
abs, acos, asin, atan, atan2, ceil, cos, cosh, deg, exp, floor, fmod, frexp,
huge, ldexp, log, log10, max, min, modf, pi, pow, rad, random, randomseed, sin,
sinh, sqrt, tan, tanh
=xsys.from(math, [[
abs, acos, asin, atan, atan2, ceil, cos, cosh, deg, exp, floor, fmod, frexp,
huge, ldexp, log, log10, max, min, modf, pi, pow, rad, random, randomseed, sin,
sinh, sqrt, tan, tanh
]])
localtype=type
-- Step-wise functions ---------------------------------------------------------
-- Halfway cases are rounded away from zero, regardless of the current rounding
-- direction.
localfunctionround(x)
returnx<0.0andceil(x-0.5) orfloor(x+0.5)
end
localfunctionstep(x) -- 1 if x >= 0, 0 otherwise.
returnmax(0, min(floor(x) +1, 1))
end
localfunctionsign(x) -- 1 if x >= 0, -1 otherwise.
return-1+step(x)*2
end
-- Special math functions ------------------------------------------------------
-- From Marsaglia, "Evaluating the Normal Distribution"
-- http://www.jstatsoft.org/v11/a05/paper
-- around 15 digits of absolute precision.
localfunctionphi(x)
-- Ensure 0 <= phi(x) <= 1 :
ifx<=-8then
return0
elseifx>=8then
return1
else
locals, b, q=x, x, x^2
fori=3,1/0,2do
b=b*q/i
localt=s
s=t+b
ifs==tthenbreakend
end
return0.5+s*exp(-0.5*q-0.91893853320467274178)
end
end
-- Inverse cdf for sampling based on Peter John Acklam research, see:
-- http://home.online.no/~pjacklam/notes/invnorm/ .
-- Maximum relative error of 1.15E-9 and machine accuracy with refinement.
-- In iphifast domain must be (0, 1) extremes excluded.
localiphifast, iphi
do
locala=ffi.new("double[7]", { 0,
-3.969683028665376e+01,
2.209460984245205e+02,
-2.759285104469687e+02,
1.383577518672690e+02,
-3.066479806614716e+01,
2.506628277459239e+00 })
localb=ffi.new("double[6]", { 0,
-5.447609879822406e+01,
1.615858368580409e+02,
-1.556989798598866e+02,
6.680131188771972e+01,
-1.328068155288572e+01 })
localc=ffi.new("double[7]", { 0,
-7.784894002430293e-03,
-3.223964580411365e-01,
-2.400758277161838e+00,
-2.549732539343734e+00,
4.374664141464968e+00,
2.938163982698783e+00 })
locald=ffi.new("double[5]", { 0,
7.784695709041462e-03,
3.224671290700398e-01,
2.445134137142996e+00,
3.754408661907416e+00 })
-- PERF: just two branches, central with high prob.
iphifast=function(p)
-- Rational approximation for central region:
ifabs(p-0.5) <0.47575then-- 95.14% of cases if p ~ U(0, 1).
localq=p-0.5
localr=q^2
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)
-- Rational approximation for the two ends:
else
localiu=ceil(p-0.97575) -- 1 if p > 0.97575 (upper).
localz= (1-iu)*p+iu*(1-p) -- p if lower, (1 - p) if upper.
localsign=1-2*iu-- 1 if lower, -1 if upper.
localq=sqrt(-2*log(z))
returnsign*(((((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)
end
end
iphi=function(p)
ifp<=0then
return-1/0
elseifp>=1then
return1/0
else
localx=iphifast(p)
locale=phi(x) -p
localu=e*sqrt(2*pi)*exp(x^2/2)
returnx-u/(1+x*u/2)
end
end
end
localgamma, loggamma
do
-- r(10).
localgamma_r10=10.900511
-- dk[0], ..., dk[10].
localgamma_dk=ffi.new("double[11]",
2.48574089138753565546e-5,
1.05142378581721974210,
-3.45687097222016235469,
4.51227709466894823700,
-2.98285225323576655721,
1.05639711577126713077,
-1.95428773191645869583e-1,
1.70970543404441224307e-2,
-5.71926117404305781283e-4,
4.63399473359905636708e-6,
-2.71994908488607703910e-9
)
localgamma_c=2*sqrt(exp(1)/pi)
-- Lanczos approximation, see:
-- Pugh[2004]: AN ANALYSIS OF THE LANCZOS GAMMA APPROXIMATION
-- http://bh0.physics.ubc.ca/People/matt/Doc/ThesesOthers/Phd/pugh.pdf
-- pag 116 for optimal formula and coefficients. Theoretical accuracy of
-- 16 digits is likely in practice to be around 14.
-- Domain: R except 0 and negative integers.
gamma=function(z)
-- Reflection formula to handle negative z plane.
-- Better to branch at z < 0 as some use cases focus on z >= 0 only.
ifz<0then
returnpi/(sin(pi*z)*gamma(1-z))
end
localsum=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)
returngamma_c*((z+gamma_r10-0.5)/exp(1))^(z-0.5)*sum
end
-- Returns log(abs(gamma(z))).
-- Domain: R except 0 and negative integers.
loggamma=function(z)
ifz<0then
returnlog(pi) -log(abs(sin(pi*z))) -loggamma(1-z)
end
localsum=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)
-- For z >= 0 gamma function is positive, no abs() required.
returnlog(gamma_c) + (z-0.5)*log(z+gamma_r10-0.5)
- (z-0.5) +log(sum)
end
end
-- Domain: a > 0 and b > 0.
localfunctionlogbeta(a, b)
ifa<=0orb<=0thenreturn0/0end
returnloggamma(a) +loggamma(b) -loggamma(a+b)
end
-- Domain: a > 0 and b > 0.
localfunctionbeta(a, b)
returnexp(logbeta(a, b))
end
-- Support for generic arguments -----------------------------------------------
localfunctionrecmax(x, y, ...)
returnyandrecmax(
type(x) ~="number" andx:max(y) or (type(y) ~="number" andy.max(x, y) or
max(x, y)), ...) orx
end
localfunctionrecmin(x, y, ...)
returnyandrecmin(
type(x) ~="number" andx:min(y) or (type(y) ~="number" andy.min(x, y) or
min(x, y)), ...) orx
end
localdisp2= {
atan2=function(x, y)
returntype(x) ~="number" andx:atan2(y)
or (type(y) ~="number" andy.atan2(x, y) oratan2(x, y))
end,
fmod=function(x, y)
returntype(x) ~="number" andx:fmod(y)
or (type(y) ~="number" andy.fmod(x, y) orfmod(x, y))
end,
ldexp=function(x, y)
returntype(x) ~="number" andx:ldexp(y)
or (type(y) ~="number" andy.ldexp(x, y) orldexp(x, y))
end,
pow=function(x, y)
returntype(x) ~="number" andx:pow(y)
or (type(y) ~="number" andy.pow(x, y) orpow(x, y))
end,
beta=function(x, y)
returntype(x) ~="number" andx:beta(y)
or (type(y) ~="number" andy.beta(x, y) orbeta(x, y))
end,
logbeta=function(x, y)
returntype(x) ~="number" andx:logbeta(y)
or (type(y) ~="number" andy.logbeta(x, y) orlogbeta(x, y))
end,
}
localgeneric= {
-- Constants:
pi=pi,
huge=huge,
-- Random numbers:
random=random,
randomseed=randomseed,
-- Generic dispatch based on one variable:
abs=function(x) returntype(x) =="number" andabs(x) orx:abs() end,
acos=function(x) returntype(x) =="number" andacos(x) orx:acos() end,
asin=function(x) returntype(x) =="number" andasin(x) orx:asin() end,
atan=function(x) returntype(x) =="number" andatan(x) orx:atan() end,
ceil=function(x) returntype(x) =="number" andceil(x) orx:ceil() end,
cos=function(x) returntype(x) =="number" andcos(x) orx:cos() end,
cosh=function(x) returntype(x) =="number" andcosh(x) orx:cosh() end,
deg=function(x) returntype(x) =="number" anddeg(x) orx:deg() end,
exp=function(x) returntype(x) =="number" andexp(x) orx:exp() end,
floor=function(x) returntype(x) =="number" andfloor(x) orx:floor() end,
frexp=function(x) returntype(x) =="number" andfrexp(x) orx:frexp() end,
log=function(x) returntype(x) =="number" andlog(x) orx:log() end,
log10=function(x) returntype(x) =="number" andlog10(x) orx:log10() end,
modf=function(x) returntype(x) =="number" andmodf(x) orx:modf() end,
rad=function(x) returntype(x) =="number" andrad(x) orx:rad() end,
sin=function(x) returntype(x) =="number" andsin(x) orx:sin() end,
sinh=function(x) returntype(x) =="number" andsinh(x) orx:sinh() end,
sqrt=function(x) returntype(x) =="number" andsqrt(x) orx:sqrt() end,
tan=function(x) returntype(x) =="number" andtan(x) orx:tan() end,
tanh=function(x) returntype(x) =="number" andtanh(x) orx:tanh() end,
-- Generic dispatch based on two variables:
atan2=disp2.atan2,
fmod=disp2.fmod,
ldexp=disp2.ldexp,
pow=disp2.pow,
-- Special dispatch for vararg max and min functions:
max=recmax,
min=recmin,
-- General dispatch based on one variable:
gamma=function(x) returntype(x) =="number" andgamma(x) or
x:gamma() end,
iphi=function(x) returntype(x) =="number" andiphi(x) or
x:iphi() end,
loggamma=function(x) returntype(x) =="number" andloggamma(x) or
x:loggamma() end,
phi=function(x) returntype(x) =="number" andphi(x) or
x:phi() end,
round=function(x) returntype(x) =="number" andround(x) or
x:round() end,
sign=function(x) returntype(x) =="number" andsign(x) or
x:sign() end,
step=function(x) returntype(x) =="number" andstep(x) or
x:step() end,
-- Generic dispatch based on two variables:
beta=disp2.beta,
logbeta=disp2.logbeta,
-- Note: no generic dispatch for private functions:
_iphifast=iphifast,
}
--------------------------------------------------------------------------------
localM=xsys.table.union(math, {
generic=generic,
round=round,
step=step,
sign=sign,
phi=phi,
iphi=iphi,
gamma=gamma,
loggamma=loggamma,
logbeta=logbeta,
beta=beta,
_iphifast=iphifast,
})
M.generic.std=M-- Gives access to builtin functions.
returnM