Skip to content

Commit cebb74b

Browse files
feat: add math/base/special/hyp2f1
PR-URL: #5140 Closes: #122 Closes: #216 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 11f1341 commit cebb74b

24 files changed

+2267
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
# Gaussian hypergeometric function
22+
23+
> Evaluates the [Gaussian hypergeometric function][hypergeometric-function].
24+
25+
<section class="intro">
26+
27+
The [Gaussian hypergeometric function][hypergeometric-function] is defined for `|x| < 1` by the power series:
28+
29+
<!-- <equation class="equation" label="eq:hypergeometric_function" align="center" raw="{}_2F_1(a, b; c; x) = \sum_{n=0}^{\infty} \frac{(a)_n (b)_n}{(c)_n} \frac{x^n}{n!} = 1 + \frac{a b}{c} x + \frac{a(a+1) b(b+1)}{c(c+1)} \frac{x^2}{2!} + \frac{a(a+1)(a+2) b(b+1)(b+2)}{c(c+1)(c+2)} \frac{x^3}{3!} + \cdots" alt="Gaussian hypergeometric function."> -->
30+
31+
```math
32+
{}_2F_1(a, b; c; x) = \sum_{n=0}^{\infty} \frac{(a)_n (b)_n}{(c)_n} \frac{x^n}{n!} = 1 + \frac{a b}{c} x + \frac{a(a+1) b(b+1)}{c(c+1)} \frac{x^2}{2!} + \frac{a(a+1)(a+2) b(b+1)(b+2)}{c(c+1)(c+2)} \frac{x^3}{3!} + \cdots
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="{}_2F_1(a, b; c; x) = \sum_{n=0}^{\infty} \frac{(a)_n (b)_n}{(c)_n} \frac{x^n}{n!} = 1 + \frac{a b}{c} x + \frac{a(a+1) b(b+1)}{c(c+1)} \frac{x^2}{2!} + \frac{a(a+1)(a+2) b(b+1)(b+2)}{c(c+1)(c+2)} \frac{x^3}{3!} + \cdots" data-equation="eq:hypergeometric_function">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/hyp2f1/docs/img/equation_hypergeometric_function.svg" alt="Gaussian hypergeometric function.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
and is undefined (or infinite) if `c` equals a non-positive integer.
43+
44+
Here `(q)ₙ` is the (rising) [Pochhammer symbol][pochhammer-symbol], which is defined by:
45+
46+
<!-- <equation class="equation" label="eq:pochhammer_symbol" align="center" raw="(q)_n = \begin{cases} 1 & n = 0 \\ q(q+1) \cdots (q+n-1) & n > 0 \end{cases}" alt="Pochhammer symbol."> -->
47+
48+
```math
49+
(q)_n = \begin{cases} 1 & n = 0 \\ q(q+1) \cdots (q+n-1) & n > 0 \end{cases}
50+
```
51+
52+
<!-- <div class="equation" align="center" data-raw-text="(q)_n = \begin{cases} 1 & n = 0 \\ q(q+1) \cdots (q+n-1) & n > 0 \end{cases}" data-equation="eq:pochhammer_symbol">
53+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/hyp2f1/docs/img/equation_pochhammer_symbol.svg" alt="Pochhammer symbol.">
54+
<br>
55+
</div> -->
56+
57+
<!-- </equation> -->
58+
59+
For `|x| >= 1`, the function can be [analytically continued][analytic-continuation] using functional identities and transformation formulas.
60+
61+
</section>
62+
63+
<!-- /.intro -->
64+
65+
<section class="usage">
66+
67+
## Usage
68+
69+
```javascript
70+
var hyp2f1 = require( '@stdlib/math/base/special/hyp2f1' );
71+
```
72+
73+
#### hyp2f1( a, b, c, x )
74+
75+
Evaluates the [Gaussian hypergeometric function][hypergeometric-function].
76+
77+
```javascript
78+
var v = hyp2f1( 1.0, 1.0, 1.0, 0.0 );
79+
// returns 1.0
80+
81+
v = hyp2f1( 10.0, 7.4, -1.8, -0.99 );
82+
// returns ~0.423
83+
84+
v = hyp2f1( 3.0, 4.0, 7.0, 1.0 );
85+
// returns +Infinity
86+
87+
v = hyp2f1( NaN, 3.0, 2.0, 0.5 );
88+
// returns NaN
89+
```
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<section class="examples">
96+
97+
## Examples
98+
99+
<!-- eslint no-undef: "error" -->
100+
101+
```javascript
102+
var linspace = require( '@stdlib/array/base/linspace' );
103+
var hyp2f1 = require( '@stdlib/math/base/special/hyp2f1' );
104+
105+
var a = linspace( -50.0, 50.0, 100 );
106+
var b = linspace( -50.0, 50.0, 100 );
107+
var c = linspace( -50.0, 50.0, 100 );
108+
var x = linspace( -50.0, 50.0, 100 );
109+
110+
var i;
111+
for ( i = 0; i < x.length; i++ ) {
112+
console.log( 'a: %d, b: %d, c: %d, x: %d, 2F1(a,b;c;x): %d', a[ i ], b[ i ], c[ i ], x[ i ], hyp2f1( a[ i ], b[ i ], c[ i ], x[ i ] ) );
113+
}
114+
```
115+
116+
</section>
117+
118+
<!-- /.examples -->
119+
120+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
121+
122+
<section class="related">
123+
124+
</section>
125+
126+
<!-- /.related -->
127+
128+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="links">
131+
132+
[hypergeometric-function]: https://en.wikipedia.org/wiki/Hypergeometric_function
133+
134+
[pochhammer-symbol]: https://en.wikipedia.org/wiki/Falling_and_rising_factorials
135+
136+
[analytic-continuation]: https://en.wikipedia.org/wiki/Analytic_continuation
137+
138+
</section>
139+
140+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var hyp2f1 = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( bm ) {
33+
var x;
34+
var a;
35+
var b;
36+
var c;
37+
var z;
38+
var i;
39+
40+
a = uniform( 100, -50.0, 50.0 );
41+
b = uniform( 100, -50.0, 50.0 );
42+
c = uniform( 100, -50.0, 50.0 );
43+
x = uniform( 100, -50.0, 50.0 );
44+
45+
bm.tic();
46+
for ( i = 0; i < bm.iterations; i++ ) {
47+
// eslint-disable-next-line max-len
48+
z = hyp2f1( a[ i % x.length ], b[ i % x.length ], c[ i % x.length ], x[ i % x.length ] );
49+
if ( isnan( z ) ) {
50+
bm.fail( 'should not return NaN' );
51+
}
52+
}
53+
bm.toc();
54+
if ( isnan( z ) ) {
55+
bm.fail( 'should not return NaN' );
56+
}
57+
bm.pass( 'benchmark finished' );
58+
bm.end();
59+
});

0 commit comments

Comments
 (0)