@ohos-ports/stdlib-math-base-special-gammaln 0.3.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +192 -0
- package/NOTICE +1 -0
- package/README.md +294 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +60 -0
- package/include/stdlib/math/base/special/gammaln.h +38 -0
- package/lib/index.js +58 -0
- package/lib/main.js +405 -0
- package/lib/native.js +70 -0
- package/lib/polyval_a1.js +47 -0
- package/lib/polyval_a2.js +47 -0
- package/lib/polyval_r.js +47 -0
- package/lib/polyval_s.js +47 -0
- package/lib/polyval_t1.js +47 -0
- package/lib/polyval_t2.js +47 -0
- package/lib/polyval_t3.js +47 -0
- package/lib/polyval_u.js +47 -0
- package/lib/polyval_v.js +47 -0
- package/lib/polyval_w.js +47 -0
- package/manifest.json +93 -0
- package/package.json +87 -0
- package/src/addon.c +22 -0
- package/src/main.c +550 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2019 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Evaluates the natural logarithm of the gamma function.
|
|
23
|
+
*
|
|
24
|
+
* @param x - input value
|
|
25
|
+
* @returns function value
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* var v = gammaln( 1.0 );
|
|
29
|
+
* // returns 0.0
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* var v = gammaln( 2.0 );
|
|
33
|
+
* // returns 0.0
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* var v = gammaln( 4.0 );
|
|
37
|
+
* // returns ~1.792
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* var v = gammaln( -0.5 );
|
|
41
|
+
* // returns ~1.266
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* var v = gammaln( 0.5 );
|
|
45
|
+
* // returns ~0.572
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* var v = gammaln( 0.0 );
|
|
49
|
+
* // returns Infinity
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* var v = gammaln( NaN );
|
|
53
|
+
* // returns NaN
|
|
54
|
+
*/
|
|
55
|
+
declare function gammaln( x: number ): number;
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
// EXPORTS //
|
|
59
|
+
|
|
60
|
+
export = gammaln;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#ifndef STDLIB_MATH_BASE_SPECIAL_GAMMALN_H
|
|
20
|
+
#define STDLIB_MATH_BASE_SPECIAL_GAMMALN_H
|
|
21
|
+
|
|
22
|
+
/*
|
|
23
|
+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
|
|
24
|
+
*/
|
|
25
|
+
#ifdef __cplusplus
|
|
26
|
+
extern "C" {
|
|
27
|
+
#endif
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Evaluates the natural logarithm of the gamma function.
|
|
31
|
+
*/
|
|
32
|
+
double stdlib_base_gammaln( const double x );
|
|
33
|
+
|
|
34
|
+
#ifdef __cplusplus
|
|
35
|
+
}
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
#endif // !STDLIB_MATH_BASE_SPECIAL_GAMMALN_H
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Evaluate the natural logarithm of the gamma function.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/math-base-special-gammaln
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var gammaln = require( '@stdlib/math-base-special-gammaln' );
|
|
28
|
+
*
|
|
29
|
+
* var v = gammaln( 1.0 );
|
|
30
|
+
* // returns 0.0
|
|
31
|
+
*
|
|
32
|
+
* v = gammaln( 2.0 );
|
|
33
|
+
* // returns 0.0
|
|
34
|
+
*
|
|
35
|
+
* v = gammaln( 4.0 );
|
|
36
|
+
* // returns ~1.792
|
|
37
|
+
*
|
|
38
|
+
* v = gammaln( -0.5 );
|
|
39
|
+
* // returns ~1.266
|
|
40
|
+
*
|
|
41
|
+
* v = gammaln( 0.5 );
|
|
42
|
+
* // returns ~0.572
|
|
43
|
+
*
|
|
44
|
+
* v = gammaln( 0.0 );
|
|
45
|
+
* // returns Infinity
|
|
46
|
+
*
|
|
47
|
+
* v = gammaln( NaN );
|
|
48
|
+
* // returns NaN
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
// MODULES //
|
|
52
|
+
|
|
53
|
+
var main = require( './main.js' );
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
// EXPORTS //
|
|
57
|
+
|
|
58
|
+
module.exports = main;
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
* ## Notice
|
|
20
|
+
*
|
|
21
|
+
* The following copyright, license, and long comment were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/12.2.0/lib/msun/src/e_lgamma_r.c}. The implementation follows the original, but has been modified for JavaScript.
|
|
22
|
+
*
|
|
23
|
+
* ```text
|
|
24
|
+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
25
|
+
*
|
|
26
|
+
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
27
|
+
* Permission to use, copy, modify, and distribute this
|
|
28
|
+
* software is freely granted, provided that this notice
|
|
29
|
+
* is preserved.
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
'use strict';
|
|
34
|
+
|
|
35
|
+
// MODULES //
|
|
36
|
+
|
|
37
|
+
var isnan = require( '@stdlib/math-base-assert-is-nan' );
|
|
38
|
+
var isInfinite = require( '@stdlib/math-base-assert-is-infinite' );
|
|
39
|
+
var abs = require( '@stdlib/math-base-special-abs' );
|
|
40
|
+
var ln = require( '@stdlib/math-base-special-ln' );
|
|
41
|
+
var trunc = require( '@stdlib/math-base-special-trunc' );
|
|
42
|
+
var sinpi = require( '@stdlib/math-base-special-sinpi' );
|
|
43
|
+
var PI = require( '@stdlib/constants-float64-pi' );
|
|
44
|
+
var PINF = require( '@stdlib/constants-float64-pinf' );
|
|
45
|
+
var polyvalA1 = require( './polyval_a1.js' );
|
|
46
|
+
var polyvalA2 = require( './polyval_a2.js' );
|
|
47
|
+
var polyvalR = require( './polyval_r.js' );
|
|
48
|
+
var polyvalS = require( './polyval_s.js' );
|
|
49
|
+
var polyvalT1 = require( './polyval_t1.js' );
|
|
50
|
+
var polyvalT2 = require( './polyval_t2.js' );
|
|
51
|
+
var polyvalT3 = require( './polyval_t3.js' );
|
|
52
|
+
var polyvalU = require( './polyval_u.js' );
|
|
53
|
+
var polyvalV = require( './polyval_v.js' );
|
|
54
|
+
var polyvalW = require( './polyval_w.js' );
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
// VARIABLES //
|
|
58
|
+
|
|
59
|
+
var A1C = 7.72156649015328655494e-02; // 0x3FB3C467E37DB0C8
|
|
60
|
+
var A2C = 3.22467033424113591611e-01; // 0x3FD4A34CC4A60FAD
|
|
61
|
+
var RC = 1.0;
|
|
62
|
+
var SC = -7.72156649015328655494e-02; // 0xBFB3C467E37DB0C8
|
|
63
|
+
var T1C = 4.83836122723810047042e-01; // 0x3FDEF72BC8EE38A2
|
|
64
|
+
var T2C = -1.47587722994593911752e-01; // 0xBFC2E4278DC6C509
|
|
65
|
+
var T3C = 6.46249402391333854778e-02; // 0x3FB08B4294D5419B
|
|
66
|
+
var UC = -7.72156649015328655494e-02; // 0xBFB3C467E37DB0C8
|
|
67
|
+
var VC = 1.0;
|
|
68
|
+
var WC = 4.18938533204672725052e-01; // 0x3FDACFE390C97D69
|
|
69
|
+
var YMIN = 1.461632144968362245;
|
|
70
|
+
var TWO52 = 4503599627370496; // 2**52
|
|
71
|
+
var TWO56 = 72057594037927936; // 2**56
|
|
72
|
+
var TINY = 1.3877787807814457e-17;
|
|
73
|
+
var TC = 1.46163214496836224576e+00; // 0x3FF762D86356BE3F
|
|
74
|
+
var TF = -1.21486290535849611461e-01; // 0xBFBF19B9BCC38A42
|
|
75
|
+
var TT = -3.63867699703950536541e-18; // 0xBC50C7CAA48A971F => TT = -(tail of TF)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
// MAIN //
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Evaluates the natural logarithm of the gamma function.
|
|
82
|
+
*
|
|
83
|
+
* ## Method
|
|
84
|
+
*
|
|
85
|
+
* 1. Argument reduction for \\(0 < x \leq 8\\). Since \\(\Gamma(1+s) = s \Gamma(s)\\), for \\(x \in \[0,8]\\), we may reduce \\(x\\) to a number in \\(\[1.5,2.5]\\) by
|
|
86
|
+
*
|
|
87
|
+
* ```tex
|
|
88
|
+
* \operatorname{lgamma}(1+s) = \ln(s) + \operatorname{lgamma}(s)
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* For example,
|
|
92
|
+
*
|
|
93
|
+
* ```tex
|
|
94
|
+
* \begin{align*}
|
|
95
|
+
* \operatorname{lgamma}(7.3) &= \ln(6.3) + \operatorname{lgamma}(6.3) \\
|
|
96
|
+
* &= \ln(6.3 \cdot 5.3) + \operatorname{lgamma}(5.3) \\
|
|
97
|
+
* &= \ln(6.3 \cdot 5.3 \cdot 4.3 \cdot 3.3 \cdot2.3) + \operatorname{lgamma}(2.3)
|
|
98
|
+
* \end{align*}
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* 2. Compute a polynomial approximation of \\(\mathrm{lgamma}\\) around its minimum (\\(\mathrm{ymin} = 1.461632144968362245\\)) to maintain monotonicity. On the interval \\(\[\mathrm{ymin} - 0.23, \mathrm{ymin} + 0.27]\\) (i.e., \\(\[1.23164,1.73163]\\)), we let \\(z = x - \mathrm{ymin}\\) and use
|
|
102
|
+
*
|
|
103
|
+
* ```tex
|
|
104
|
+
* \operatorname{lgamma}(x) = -1.214862905358496078218 + z^2 \cdot \operatorname{poly}(z)
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* where \\(\operatorname{poly}(z)\\) is a \\(14\\) degree polynomial.
|
|
108
|
+
*
|
|
109
|
+
* 3. Compute a rational approximation in the primary interval \\(\[2,3]\\). Let \\( s = x - 2.0 \\). We can thus use the approximation
|
|
110
|
+
*
|
|
111
|
+
* ```tex
|
|
112
|
+
* \operatorname{lgamma}(x) = \frac{s}{2} + s\frac{\operatorname{P}(s)}{\operatorname{Q}(s)}
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* with accuracy
|
|
116
|
+
*
|
|
117
|
+
* ```tex
|
|
118
|
+
* \biggl|\frac{\mathrm{P}}{\mathrm{Q}} - \biggr(\operatorname{lgamma}(x)-\frac{s}{2}\biggl)\biggl| < 2^{-61.71}
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* The algorithms are based on the observation
|
|
122
|
+
*
|
|
123
|
+
* ```tex
|
|
124
|
+
* \operatorname{lgamma}(2+s) = s(1 - \gamma) + \frac{\zeta(2) - 1}{2} s^2 - \frac{\zeta(3) - 1}{3} s^3 + \ldots
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* where \\(\zeta\\) is the zeta function and \\(\gamma = 0.5772156649...\\) is the Euler-Mascheroni constant, which is very close to \\(0.5\\).
|
|
128
|
+
*
|
|
129
|
+
* 4. For \\(x \geq 8\\),
|
|
130
|
+
*
|
|
131
|
+
* ```tex
|
|
132
|
+
* \operatorname{lgamma}(x) \approx \biggl(x-\frac{1}{2}\biggr) \ln(x) - x + \frac{\ln(2\pi)}{2} + \frac{1}{12x} - \frac{1}{360x^3} + \ldots
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* which can be expressed
|
|
136
|
+
*
|
|
137
|
+
* ```tex
|
|
138
|
+
* \operatorname{lgamma}(x) \approx \biggl(x-\frac{1}{2}\biggr)(\ln(x)-1)-\frac{\ln(2\pi)-1}{2} + \ldots
|
|
139
|
+
* ```
|
|
140
|
+
*
|
|
141
|
+
* Let \\(z = \frac{1}{x}\\). We can then use the approximation
|
|
142
|
+
*
|
|
143
|
+
* ```tex
|
|
144
|
+
* f(z) = \operatorname{lgamma}(x) - \biggl(x-\frac{1}{2}\biggr)(\ln(x)-1)
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* by
|
|
148
|
+
*
|
|
149
|
+
* ```tex
|
|
150
|
+
* w = w_0 + w_1 z + w_2 z^3 + w_3 z^5 + \ldots + w_6 z^{11}
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* where
|
|
154
|
+
*
|
|
155
|
+
* ```tex
|
|
156
|
+
* |w - f(z)| < 2^{-58.74}
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* 5. For negative \\(x\\), since
|
|
160
|
+
*
|
|
161
|
+
* ```tex
|
|
162
|
+
* -x \Gamma(-x) \Gamma(x) = \frac{\pi}{\sin(\pi x)}
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* where \\(\Gamma\\) is the gamma function, we have
|
|
166
|
+
*
|
|
167
|
+
* ```tex
|
|
168
|
+
* \Gamma(x) = \frac{\pi}{\sin(\pi x)(-x)\Gamma(-x)}
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* Since \\(\Gamma(-x)\\) is positive,
|
|
172
|
+
*
|
|
173
|
+
* ```tex
|
|
174
|
+
* \operatorname{sign}(\Gamma(x)) = \operatorname{sign}(\sin(\pi x))
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* for \\(x < 0\\). Hence, for \\(x < 0\\),
|
|
178
|
+
*
|
|
179
|
+
* ```tex
|
|
180
|
+
* \mathrm{signgam} = \operatorname{sign}(\sin(\pi x))
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* and
|
|
184
|
+
*
|
|
185
|
+
* ```tex
|
|
186
|
+
* \begin{align*}
|
|
187
|
+
* \operatorname{lgamma}(x) &= \ln(|\Gamma(x)|) \\
|
|
188
|
+
* &= \ln\biggl(\frac{\pi}{|x \sin(\pi x)|}\biggr) - \operatorname{lgamma}(-x)
|
|
189
|
+
* \end{align*}
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* <!-- <note> -->
|
|
193
|
+
*
|
|
194
|
+
* Note that one should avoid computing \\(\pi (-x)\\) directly in the computation of \\(\sin(\pi (-x))\\).
|
|
195
|
+
*
|
|
196
|
+
* <!-- </note> -->
|
|
197
|
+
*
|
|
198
|
+
* ## Special Cases
|
|
199
|
+
*
|
|
200
|
+
* ```tex
|
|
201
|
+
* \begin{align*}
|
|
202
|
+
* \operatorname{lgamma}(2+s) &\approx s (1-\gamma) & \mathrm{for\ tiny\ s} \\
|
|
203
|
+
* \operatorname{lgamma}(x) &\approx -\ln(x) & \mathrm{for\ tiny\ x} \\
|
|
204
|
+
* \operatorname{lgamma}(1) &= 0 & \\
|
|
205
|
+
* \operatorname{lgamma}(2) &= 0 & \\
|
|
206
|
+
* \operatorname{lgamma}(0) &= \infty & \\
|
|
207
|
+
* \operatorname{lgamma}(\infty) &= \infty & \\
|
|
208
|
+
* \operatorname{lgamma}(-\mathrm{integer}) &= \pm \infty
|
|
209
|
+
* \end{align*}
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @param {number} x - input value
|
|
213
|
+
* @returns {number} function value
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* var v = gammaln( 1.0 );
|
|
217
|
+
* // returns 0.0
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* var v = gammaln( 2.0 );
|
|
221
|
+
* // returns 0.0
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* var v = gammaln( 4.0 );
|
|
225
|
+
* // returns ~1.792
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* var v = gammaln( -0.5 );
|
|
229
|
+
* // returns ~1.266
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* var v = gammaln( 0.5 );
|
|
233
|
+
* // returns ~0.572
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* var v = gammaln( 0.0 );
|
|
237
|
+
* // returns Infinity
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* var v = gammaln( NaN );
|
|
241
|
+
* // returns NaN
|
|
242
|
+
*/
|
|
243
|
+
function gammaln( x ) {
|
|
244
|
+
var isNegative;
|
|
245
|
+
var nadj;
|
|
246
|
+
var flg;
|
|
247
|
+
var p3;
|
|
248
|
+
var p2;
|
|
249
|
+
var p1;
|
|
250
|
+
var p;
|
|
251
|
+
var q;
|
|
252
|
+
var t;
|
|
253
|
+
var w;
|
|
254
|
+
var y;
|
|
255
|
+
var z;
|
|
256
|
+
var r;
|
|
257
|
+
|
|
258
|
+
// Special cases: NaN, +-infinity
|
|
259
|
+
if ( isnan( x ) || isInfinite( x ) ) {
|
|
260
|
+
return x;
|
|
261
|
+
}
|
|
262
|
+
// Special case: 0
|
|
263
|
+
if ( x === 0.0 ) {
|
|
264
|
+
return PINF;
|
|
265
|
+
}
|
|
266
|
+
if ( x < 0.0 ) {
|
|
267
|
+
isNegative = true;
|
|
268
|
+
x = -x;
|
|
269
|
+
} else {
|
|
270
|
+
isNegative = false;
|
|
271
|
+
}
|
|
272
|
+
// If |x| < 2**-56, return -ln(|x|)
|
|
273
|
+
if ( x < TINY ) {
|
|
274
|
+
return -ln( x );
|
|
275
|
+
}
|
|
276
|
+
if ( isNegative ) {
|
|
277
|
+
// If |x| >= 2**52, must be -integer
|
|
278
|
+
if ( x >= TWO52 ) {
|
|
279
|
+
return PINF;
|
|
280
|
+
}
|
|
281
|
+
t = sinpi( x );
|
|
282
|
+
if ( t === 0.0 ) {
|
|
283
|
+
return PINF;
|
|
284
|
+
}
|
|
285
|
+
nadj = ln( PI / abs( t*x ) );
|
|
286
|
+
}
|
|
287
|
+
// If x equals 1 or 2, return 0
|
|
288
|
+
if ( x === 1.0 || x === 2.0 ) {
|
|
289
|
+
return 0.0;
|
|
290
|
+
}
|
|
291
|
+
// If x < 2, use lgamma(x) = lgamma(x+1) - log(x)
|
|
292
|
+
if ( x < 2.0 ) {
|
|
293
|
+
if ( x <= 0.9 ) {
|
|
294
|
+
r = -ln( x );
|
|
295
|
+
|
|
296
|
+
// 0.7316 <= x <= 0.9
|
|
297
|
+
if ( x >= ( YMIN - 1.0 + 0.27 ) ) {
|
|
298
|
+
y = 1.0 - x;
|
|
299
|
+
flg = 0;
|
|
300
|
+
}
|
|
301
|
+
// 0.2316 <= x < 0.7316
|
|
302
|
+
else if ( x >= (YMIN - 1.0 - 0.27) ) {
|
|
303
|
+
y = x - (TC - 1.0);
|
|
304
|
+
flg = 1;
|
|
305
|
+
}
|
|
306
|
+
// 0 < x < 0.2316
|
|
307
|
+
else {
|
|
308
|
+
y = x;
|
|
309
|
+
flg = 2;
|
|
310
|
+
}
|
|
311
|
+
} else {
|
|
312
|
+
r = 0.0;
|
|
313
|
+
|
|
314
|
+
// 1.7316 <= x < 2
|
|
315
|
+
if ( x >= (YMIN + 0.27) ) {
|
|
316
|
+
y = 2.0 - x;
|
|
317
|
+
flg = 0;
|
|
318
|
+
}
|
|
319
|
+
// 1.2316 <= x < 1.7316
|
|
320
|
+
else if ( x >= (YMIN - 0.27) ) {
|
|
321
|
+
y = x - TC;
|
|
322
|
+
flg = 1;
|
|
323
|
+
}
|
|
324
|
+
// 0.9 < x < 1.2316
|
|
325
|
+
else {
|
|
326
|
+
y = x - 1.0;
|
|
327
|
+
flg = 2;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
switch ( flg ) { // eslint-disable-line default-case
|
|
331
|
+
case 0:
|
|
332
|
+
z = y * y;
|
|
333
|
+
p1 = A1C + (z*polyvalA1( z ));
|
|
334
|
+
p2 = z * (A2C + (z*polyvalA2( z )));
|
|
335
|
+
p = (y*p1) + p2;
|
|
336
|
+
r += ( p - (0.5*y) );
|
|
337
|
+
break;
|
|
338
|
+
case 1:
|
|
339
|
+
z = y * y;
|
|
340
|
+
w = z * y;
|
|
341
|
+
p1 = T1C + (w*polyvalT1( w ));
|
|
342
|
+
p2 = T2C + (w*polyvalT2( w ));
|
|
343
|
+
p3 = T3C + (w*polyvalT3( w ));
|
|
344
|
+
p = (z*p1) - (TT - (w*(p2+(y*p3))));
|
|
345
|
+
r += ( TF + p );
|
|
346
|
+
break;
|
|
347
|
+
case 2:
|
|
348
|
+
p1 = y * (UC + (y*polyvalU( y )));
|
|
349
|
+
p2 = VC + (y*polyvalV( y ));
|
|
350
|
+
r += (-0.5*y) + (p1/p2);
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// 2 <= x < 8
|
|
355
|
+
else if ( x < 8.0 ) {
|
|
356
|
+
flg = trunc( x );
|
|
357
|
+
y = x - flg;
|
|
358
|
+
p = y * (SC + (y*polyvalS( y )));
|
|
359
|
+
q = RC + (y*polyvalR( y ));
|
|
360
|
+
r = (0.5*y) + (p/q);
|
|
361
|
+
z = 1.0; // gammaln(1+s) = ln(s) + gammaln(s)
|
|
362
|
+
switch ( flg ) { // eslint-disable-line default-case
|
|
363
|
+
case 7:
|
|
364
|
+
z *= y + 6.0;
|
|
365
|
+
|
|
366
|
+
/* Falls through */
|
|
367
|
+
case 6:
|
|
368
|
+
z *= y + 5.0;
|
|
369
|
+
|
|
370
|
+
/* Falls through */
|
|
371
|
+
case 5:
|
|
372
|
+
z *= y + 4.0;
|
|
373
|
+
|
|
374
|
+
/* Falls through */
|
|
375
|
+
case 4:
|
|
376
|
+
z *= y + 3.0;
|
|
377
|
+
|
|
378
|
+
/* Falls through */
|
|
379
|
+
case 3:
|
|
380
|
+
z *= y + 2.0;
|
|
381
|
+
r += ln( z );
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
// 8 <= x < 2**56
|
|
385
|
+
else if ( x < TWO56 ) {
|
|
386
|
+
t = ln( x );
|
|
387
|
+
z = 1.0 / x;
|
|
388
|
+
y = z * z;
|
|
389
|
+
w = WC + (z*polyvalW( y ));
|
|
390
|
+
r = ((x-0.5)*(t-1.0)) + w;
|
|
391
|
+
}
|
|
392
|
+
// 2**56 <= x <= Inf
|
|
393
|
+
else {
|
|
394
|
+
r = x * ( ln(x)-1.0 );
|
|
395
|
+
}
|
|
396
|
+
if ( isNegative ) {
|
|
397
|
+
r = nadj - r;
|
|
398
|
+
}
|
|
399
|
+
return r;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
// EXPORTS //
|
|
404
|
+
|
|
405
|
+
module.exports = gammaln;
|
package/lib/native.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var addon = require( './../src/addon.node' );
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Evaluates the natural logarithm of the gamma function.
|
|
30
|
+
*
|
|
31
|
+
* @private
|
|
32
|
+
* @param {number} x - input value
|
|
33
|
+
* @returns {number} function value
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* var v = gammaln( 1.0 );
|
|
37
|
+
* // returns 0.0
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* var v = gammaln( 2.0 );
|
|
41
|
+
* // returns 0.0
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* var v = gammaln( 4.0 );
|
|
45
|
+
* // returns ~1.792
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* var v = gammaln( -0.5 );
|
|
49
|
+
* // returns ~1.266
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* var v = gammaln( 0.5 );
|
|
53
|
+
* // returns ~0.572
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* var v = gammaln( 0.0 );
|
|
57
|
+
* // returns Infinity
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* var v = gammaln( NaN );
|
|
61
|
+
* // returns NaN
|
|
62
|
+
*/
|
|
63
|
+
function gammaln( x ) {
|
|
64
|
+
return addon( x );
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
// EXPORTS //
|
|
69
|
+
|
|
70
|
+
module.exports = gammaln;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/* This is a generated file. Do not edit directly. */
|
|
20
|
+
'use strict';
|
|
21
|
+
|
|
22
|
+
// MAIN //
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Evaluates a polynomial.
|
|
26
|
+
*
|
|
27
|
+
* ## Notes
|
|
28
|
+
*
|
|
29
|
+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
|
|
30
|
+
*
|
|
31
|
+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
|
|
32
|
+
*
|
|
33
|
+
* @private
|
|
34
|
+
* @param {number} x - value at which to evaluate the polynomial
|
|
35
|
+
* @returns {number} evaluated polynomial
|
|
36
|
+
*/
|
|
37
|
+
function evalpoly( x ) {
|
|
38
|
+
if ( x === 0.0 ) {
|
|
39
|
+
return 0.06735230105312927;
|
|
40
|
+
}
|
|
41
|
+
return 0.06735230105312927 + (x * (0.007385550860814029 + (x * (0.0011927076318336207 + (x * (0.00022086279071390839 + (x * 0.000025214456545125733))))))); // eslint-disable-line max-len
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
// EXPORTS //
|
|
46
|
+
|
|
47
|
+
module.exports = evalpoly;
|