Skip to content

Commit 2448ee3

Browse files
feat: add-essential files
1 parent f29381d commit 2448ee3

File tree

27 files changed

+2151
-0
lines changed

27 files changed

+2151
-0
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Gamma Lanczos Sum
22+
23+
> Calculate the Lanczos sum for the approximation of the [gamma function][gamma-function].
24+
25+
<section class="intro">
26+
27+
The [Lanczos approximation][lanczos-approximation] for the [gamma function][gamma-function] can be written in partial fraction form as follows:
28+
29+
<!-- <equation class="equation" label="eq:lanczos_approximation" align="center" raw="\Gamma ( n ) = \frac{(n+g-0.5)^{n-0.5}}{e^{n+g-0.5}} L_g(n)" alt="Lanczos approximation for gamma function."> -->
30+
31+
```math
32+
\Gamma ( n ) = \frac{(n+g-0.5)^{n-0.5}}{e^{n+g-0.5}} L_g(n)
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\Gamma ( n ) = \frac{(n+g-0.5)^{n-0.5}}{e^{n+g-0.5}} L_g(n)" data-equation="eq:lanczos_approximation">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/gamma-lanczos-sum/docs/img/equation_lanczos_approximation.svg" alt="Lanczos approximation for gamma function.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
where `g` is an [arbitrary constant][@stdlib/constants/float32/gamma-lanczos-g] and `L_g(n)` is the Lanczos sum.
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<section class="usage">
49+
50+
## Usage
51+
52+
```javascript
53+
var gammaLanczosSumf = require( '@stdlib/math/base/special/gamma-lanczos-sumf' );
54+
```
55+
56+
#### gammaLanczosSumf( x )
57+
58+
Calculates the Lanczos sum for the approximation of the [gamma function][gamma-function] for single-precision floating-point number.
59+
60+
```javascript
61+
var v = gammaLanczosSumf( 4.0 );
62+
// returns ~950.366
63+
64+
v = gammaLanczosSumf( -1.5 );
65+
// returns ~1373366.245
66+
67+
v = gammaLanczosSumf( -0.5 );
68+
// returns ~-699841.735
69+
70+
v = gammaLanczosSumf( 0.5 );
71+
// returns ~96074.186
72+
73+
v = gammaLanczosSumf( 0.0 );
74+
// returns Infinity
75+
76+
v = gammaLanczosSumf( NaN );
77+
// returns NaN
78+
```
79+
80+
</section>
81+
82+
<!-- /.usage -->
83+
84+
<section class="examples">
85+
86+
## Examples
87+
88+
<!-- eslint no-undef: "error" -->
89+
90+
```javascript
91+
var linspace = require( '@stdlib/array/base/linspace' );
92+
var gammaLanczosSumf = require( '@stdlib/math/base/special/gamma-lanczos-sumf' );
93+
94+
var x = linspace( -10.0, 10.0, 100 );
95+
96+
var i;
97+
for ( i = 0; i < x.length; i++ ) {
98+
console.log( 'x: %d, f(x): %d', x[ i ], gammaLanczosSumf( x[ i ] ) );
99+
}
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- C interface documentation. -->
107+
108+
* * *
109+
110+
<section class="c">
111+
112+
## C APIs
113+
114+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
115+
116+
<section class="intro">
117+
118+
</section>
119+
120+
<!-- /.intro -->
121+
122+
<!-- C usage documentation. -->
123+
124+
<section class="usage">
125+
126+
### Usage
127+
128+
```c
129+
#include "stdlib/math/base/special/gamma_lanczos_sumf.h"
130+
```
131+
132+
#### stdlib_base_gamma_lanczos_sumf( x )
133+
134+
Calculates the Lanczos sum for the approximation of the [gamma function][gamma-function] for single-precision floating-point number.
135+
136+
```c
137+
float out = stdlib_base_gamma_lanczos_sumf( 4.0f );
138+
// returns ~950.366f
139+
140+
out = stdlib_base_gamma_lanczos_sumf( -1.5f );
141+
// returns ~1373366.245f
142+
```
143+
144+
The function accepts the following arguments:
145+
146+
- **x**: `[in] float` input value.
147+
148+
```c
149+
float stdlib_base_gamma_lanczos_sumf( const float x );
150+
```
151+
152+
</section>
153+
154+
<!-- /.usage -->
155+
156+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
157+
158+
<section class="notes">
159+
160+
</section>
161+
162+
<!-- /.notes -->
163+
164+
<!-- C API usage examples. -->
165+
166+
<section class="examples">
167+
168+
### Examples
169+
170+
```c
171+
#include "stdlib/math/base/special/gamma_lanczos_sumf.h"
172+
#include <stdlib.h>
173+
#include <stdio.h>
174+
175+
int main( void ) {
176+
const float x[] = { 4.0f, -1.5f, -0.5f, 0.5f };
177+
178+
float y;
179+
int i;
180+
for ( i = 0; i < 4; i++ ) {
181+
y = stdlib_base_gamma_lanczos_sumf( x[ i ] );
182+
printf( "gamma_lanczos_sumf(%f) = %f\n", x[ i ], y );
183+
}
184+
}
185+
```
186+
187+
</section>
188+
189+
<!-- /.examples -->
190+
191+
</section>
192+
193+
<!-- /.c -->
194+
195+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
196+
197+
<section class="related">
198+
199+
* * *
200+
201+
## See Also
202+
203+
- <span class="package-name">[`@stdlib/math/base/special/gamma-lanczos-sum`][@stdlib/math/base/special/gamma-lanczos-sum]</span><span class="delimiter">: </span><span class="description">gamma function.</span>
204+
- <span class="package-name">[`@stdlib/math/base/special/gamma`][@stdlib/math/base/special/gamma]</span><span class="delimiter">: </span><span class="description">gamma function.</span>
205+
- <span class="package-name">[`@stdlib/math/base/special/gamma-lanczos-sum-expg-scaled`][@stdlib/math/base/special/gamma-lanczos-sum-expg-scaled]</span><span class="delimiter">: </span><span class="description">calculate a scaled Lanczos sum for the approximation of the gamma function.</span>
206+
207+
</section>
208+
209+
<!-- /.related -->
210+
211+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
212+
213+
<section class="links">
214+
215+
[@stdlib/constants/float64/gamma-lanczos-g]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/constants/float64/gamma-lanczos-g
216+
217+
[gamma-function]: https://en.wikipedia.org/wiki/Gamma_function
218+
219+
[lanczos-approximation]: https://en.wikipedia.org/wiki/Lanczos_approximation
220+
221+
<!-- <related-links> -->
222+
223+
[@stdlib/math/base/special/gamma-lanczos-sum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gamma-lanczos-sum
224+
225+
[@stdlib/math/base/special/gamma]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gamma
226+
227+
[@stdlib/math/base/special/gamma-lanczos-sum-expg-scaled]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gamma-lanczos-sum-expg-scaled
228+
229+
<!-- </related-links> -->
230+
231+
</section>
232+
233+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 bench = require( '@stdlib/bench' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var gammaLanczosSumf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
discreteUniform( 100, -50.0, 50.0 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = gammaLanczosSumf( x[ i % x.length ] );
42+
if ( isnanf( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnanf( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var gammaLanczosSumf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( gammaLanczosSumf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
discreteUniform( 100, -50.0, 50.0 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = gammaLanczosSumf( x[ i % x.length ] );
51+
if ( isnanf( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnanf( y ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

0 commit comments

Comments
 (0)