FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/function/probability/gamma.js at develop · intervalia/mathjs · GitHub
intervalia
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
src
/
function
/
probability
/
gamma.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
194 lines (160 loc) · 4.77 KB
Breadcrumbs
mathjs
/
src
/
function
/
probability
/
gamma.js
Copy path
File metadata and controls
194 lines (160 loc) · 4.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
'use strict'
const
deepMap
=
require
(
'../../utils/collection/deepMap'
)
const
isInteger
=
require
(
'../../utils/number'
)
.
isInteger
function
factory
(
type
,
config
,
load
,
typed
)
{
const
multiply
=
load
(
require
(
'../arithmetic/multiply'
)
)
const
pow
=
load
(
require
(
'../arithmetic/pow'
)
)
const
product
=
require
(
'./product'
)
/**
* Compute the gamma function of a value using Lanczos approximation for
* small values, and an extended Stirling approximation for large values.
*
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.gamma(n)
*
* Examples:
*
* math.gamma(5) // returns 24
* math.gamma(-0.5) // returns -3.5449077018110335
* math.gamma(math.i) // returns -0.15494982830180973 - 0.49801566811835596i
*
* See also:
*
* combinations, factorial, permutations
*
*
@param
{
number | Array | Matrix
} n A real or complex number
*
@return
{
number | Array | Matrix
} The gamma of `n`
*/
const
gamma
=
typed
(
'gamma'
,
{
'number'
:
function
(
n
)
{
let
t
,
x
if
(
isInteger
(
n
)
)
{
if
(
n
<=
0
)
{
return
isFinite
(
n
)
?
Infinity
:
NaN
}
if
(
n
>
171
)
{
return
Infinity
// Will overflow
}
return
product
(
1
,
n
-
1
)
}
if
(
n
<
0.5
)
{
return
Math
.
PI
/
(
Math
.
sin
(
Math
.
PI
*
n
)
*
gamma
(
1
-
n
)
)
}
if
(
n
>=
171.35
)
{
return
Infinity
// will overflow
}
if
(
n
>
85.0
)
{
// Extended Stirling Approx
const
twoN
=
n
*
n
const
threeN
=
twoN
*
n
const
fourN
=
threeN
*
n
const
fiveN
=
fourN
*
n
return
Math
.
sqrt
(
2
*
Math
.
PI
/
n
)
*
Math
.
pow
(
(
n
/
Math
.
E
)
,
n
)
*
(
1
+
1
/
(
12
*
n
)
+
1
/
(
288
*
twoN
)
-
139
/
(
51840
*
threeN
)
-
571
/
(
2488320
*
fourN
)
+
163879
/
(
209018880
*
fiveN
)
+
5246819
/
(
75246796800
*
fiveN
*
n
)
)
}
--
n
x
=
p
[
0
]
for
(
let
i
=
1
;
i
<
p
.
length
;
++
i
)
{
x
+=
p
[
i
]
/
(
n
+
i
)
}
t
=
n
+
g
+
0.5
return
Math
.
sqrt
(
2
*
Math
.
PI
)
*
Math
.
pow
(
t
,
n
+
0.5
)
*
Math
.
exp
(
-
t
)
*
x
}
,
'Complex'
:
function
(
n
)
{
let
t
,
x
if
(
n
.
im
===
0
)
{
return
gamma
(
n
.
re
)
}
n
=
new
type
.
Complex
(
n
.
re
-
1
,
n
.
im
)
x
=
new
type
.
Complex
(
p
[
0
]
,
0
)
for
(
let
i
=
1
;
i
<
p
.
length
;
++
i
)
{
const
real
=
n
.
re
+
i
// x += p[i]/(n+i)
const
den
=
real
*
real
+
n
.
im
*
n
.
im
if
(
den
!==
0
)
{
x
.
re
+=
p
[
i
]
*
real
/
den
x
.
im
+=
-
(
p
[
i
]
*
n
.
im
)
/
den
}
else
{
x
.
re
=
p
[
i
]
<
0
?
-
Infinity
:
Infinity
}
}
t
=
new
type
.
Complex
(
n
.
re
+
g
+
0.5
,
n
.
im
)
const
twoPiSqrt
=
Math
.
sqrt
(
2
*
Math
.
PI
)
n
.
re
+=
0.5
const
result
=
pow
(
t
,
n
)
if
(
result
.
im
===
0
)
{
// sqrt(2*PI)*result
result
.
re
*=
twoPiSqrt
}
else
if
(
result
.
re
===
0
)
{
result
.
im
*=
twoPiSqrt
}
else
{
result
.
re
*=
twoPiSqrt
result
.
im
*=
twoPiSqrt
}
const
r
=
Math
.
exp
(
-
t
.
re
)
// exp(-t)
t
.
re
=
r
*
Math
.
cos
(
-
t
.
im
)
t
.
im
=
r
*
Math
.
sin
(
-
t
.
im
)
return
multiply
(
multiply
(
result
,
t
)
,
x
)
}
,
'BigNumber'
:
function
(
n
)
{
if
(
n
.
isInteger
(
)
)
{
return
(
n
.
isNegative
(
)
||
n
.
isZero
(
)
)
?
new
type
.
BigNumber
(
Infinity
)
:
bigFactorial
(
n
.
minus
(
1
)
)
}
if
(
!
n
.
isFinite
(
)
)
{
return
new
type
.
BigNumber
(
n
.
isNegative
(
)
?
NaN
:
Infinity
)
}
throw
new
Error
(
'Integer BigNumber expected'
)
}
,
'Array | Matrix'
:
function
(
n
)
{
return
deepMap
(
n
,
gamma
)
}
}
)
/**
* Calculate factorial for a BigNumber
*
@param
{
BigNumber
} n
*
@returns
{
BigNumber
} Returns the factorial of n
*/
function
bigFactorial
(
n
)
{
if
(
n
.
isZero
(
)
)
{
return
new
type
.
BigNumber
(
1
)
// 0! is per definition 1
}
const
precision
=
config
.
precision
+
(
Math
.
log
(
n
.
toNumber
(
)
)
|
0
)
const
Big
=
type
.
BigNumber
.
clone
(
{
precision
:
precision
}
)
let
res
=
new
Big
(
n
)
let
value
=
n
.
toNumber
(
)
-
1
// number
while
(
value
>
1
)
{
res
=
res
.
times
(
value
)
value
--
}
return
new
type
.
BigNumber
(
res
.
toPrecision
(
type
.
BigNumber
.
precision
)
)
}
gamma
.
toTex
=
{
1
:
`\\Gamma\\left(\${args[0]}\\right)`
}
return
gamma
}
// TODO: comment on the variables g and p
const
g
=
4.7421875
const
p
=
[
0.99999999999999709182
,
57.156235665862923517
,
-
59.597960355475491248
,
14.136097974741747174
,
-
0.49191381609762019978
,
0.33994649984811888699e-4
,
0.46523628927048575665e-4
,
-
0.98374475304879564677e-4
,
0.15808870322491248884e-3
,
-
0.21026444172410488319e-3
,
0.21743961811521264320e-3
,
-
0.16431810653676389022e-3
,
0.84418223983852743293e-4
,
-
0.26190838401581408670e-4
,
0.36899182659531622704e-5
]
exports
.
name
=
'gamma'
exports
.
factory
=
factory
Back
|
FazBrowse Home
|
New Git URL