1
1
local MATH = {} for k ,v in next ,math do MATH [k ]= v end
2
2
3
- local floor ,ceil = math.floor ,math.ceil
4
- local rnd = math.random
5
- local exp = math.exp
6
-
7
3
MATH .tau = 2 * math.pi
8
4
MATH .phi = (1 + math.sqrt (5 ))/ 2
9
5
MATH .inf = 1 / 0
10
6
MATH .nan = 0 / 0
11
7
8
+ local floor ,ceil = math.floor ,math.ceil
9
+ local sin ,cos = math.sin ,math.cos
10
+ local rnd = math.random
11
+ local exp ,log = math.exp ,math.log
12
+ local tau = MATH .tau
13
+
12
14
--- Check if a number is NaN
13
15
--- @param n number
14
16
--- @return boolean
@@ -59,8 +61,8 @@ function MATH.rand(a,b)
59
61
end
60
62
61
63
--- Get a random integer with specified frequency list
62
- --- @param fList number[] positive numbers
63
- --- @return integer
64
+ --- @param fList number[] positive numbers
65
+ --- @return integer
64
66
function MATH .randFreq (fList )
65
67
local sum = TABLE .sum (fList )
66
68
for i = 1 ,# fList do sum = sum + fList [i ] end
@@ -73,6 +75,22 @@ function MATH.randFreq(fList)
73
75
error (" Frequency list should be a simple positive number list" )
74
76
end
75
77
78
+ --- Get a random numbers in gaussian distribution (Box-Muller algorithm + stream buffer)
79
+ --- @return number
80
+ local randNormBF
81
+ function MATH .randNorm ()
82
+ if randNormBF then
83
+ local res = randNormBF
84
+ randNormBF = nil
85
+ return res
86
+ else
87
+ local r = rnd ()* tau
88
+ local d = (- 2 * log (1 - rnd ()* tau ))^ .5
89
+ randNormBF = sin (r )* d
90
+ return cos (r )* d
91
+ end
92
+ end
93
+
76
94
--- Restrict a number in a range
77
95
--- @param v number
78
96
--- @param low number
@@ -142,12 +160,12 @@ end
142
160
143
161
--- Check if a point is in a polygon
144
162
---
145
- --- By Pedro Gimeno,donated to the public domain
146
- --- @param x number
147
- --- @param y number
148
- --- @param poly number[] {x1,y1,x2,y2,... }
149
- --- @param evenOddRule boolean
150
- --- @return boolean
163
+ --- By Pedro Gimeno, donated to the public domain
164
+ --- @param x number
165
+ --- @param y number
166
+ --- @param poly number[] {x1,y1,x2,y2,... }
167
+ --- @param evenOddRule boolean
168
+ --- @return boolean
151
169
function MATH .pointInPolygon (x ,y ,poly ,evenOddRule )
152
170
local x1 ,y1 ,x2 ,y2
153
171
local len =# poly
@@ -174,9 +192,9 @@ function MATH.pointInPolygon(x,y,poly,evenOddRule)
174
192
end
175
193
176
194
--- Get the greatest common divisor of two positive integers
177
- --- @param a number
178
- --- @param b number
179
- --- @return number
195
+ --- @param a number
196
+ --- @param b number
197
+ --- @return number
180
198
function MATH .gcd (a ,b )
181
199
repeat
182
200
a = a % b
0 commit comments