Skip to content

Commit 35f5f7a

Browse files
feat: add math/base/special/binomcoeff
PR-URL: #7000 Ref: #649 Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Gunj Joshi <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 2501de9 commit 35f5f7a

File tree

30 files changed

+2520
-0
lines changed

30 files changed

+2520
-0
lines changed
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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+
# Binomial Coefficient
22+
23+
> Compute the [binomial coefficient][binomial-coefficient] as a single-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The [binomial coefficient][binomial-coefficient] of two nonnegative integers `n` and `k` is defined as
28+
29+
<!-- <equation class="equation" label="eq:binomial_coefficient" align="center" raw="\binom {n}{k} = \frac{n!}{k!\,(n-k)!} \quad \text{for }\ 0\leq k\leq n" alt="Factorial formula for the Binomial coefficient."> -->
30+
31+
```math
32+
\binom {n}{k} = \frac{n!}{k!\,(n-k)!} \quad \text{for }\ 0\leq k\leq n
33+
```
34+
35+
<!-- </equation> -->
36+
37+
The [binomial coefficient][binomial-coefficient] can be generalized to negative integers `n` as follows:
38+
39+
<!-- <equation class="equation" label="eq:binomial_coefficient_negative_integers" align="center" raw="\binom {-n}{k} = (-1)^{k} \binom{n + k - 1}{k} = (-1)^{k} \left(\!\!{\binom {n}{k}}\!\!\right)" alt="Generalization of the binomial coefficient to negative n."> -->
40+
41+
```math
42+
\binom {-n}{k} = (-1)^{k} \binom{n + k - 1}{k} = (-1)^{k} \left(\!\!{\binom {n}{k}}\!\!\right)
43+
```
44+
45+
<!-- </equation> -->
46+
47+
</section>
48+
49+
<!-- /.intro -->
50+
51+
<section class="usage">
52+
53+
## Usage
54+
55+
```javascript
56+
var binomcoeff = require( '@stdlib/math/base/special/binomcoeff' );
57+
```
58+
59+
#### binomcoeff( n, k )
60+
61+
Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k` as a single-precision floating-point number.
62+
63+
```javascript
64+
var v = binomcoeff( 8, 2 );
65+
// returns 28
66+
67+
v = binomcoeff( 0, 0 );
68+
// returns 1
69+
70+
v = binomcoeff( -4, 2 );
71+
// returns 10
72+
73+
v = binomcoeff( 5, 3 );
74+
// returns 10
75+
76+
v = binomcoeff( NaN, 3 );
77+
// returns NaN
78+
79+
v = binomcoeff( 5, NaN );
80+
// returns NaN
81+
82+
v = binomcoeff( NaN, NaN );
83+
// returns NaN
84+
```
85+
86+
For negative `k`, the function returns `0`.
87+
88+
```javascript
89+
var v = binomcoeff( 2, -1 );
90+
// returns 0
91+
92+
v = binomcoeff( -3, -1 );
93+
// returns 0
94+
```
95+
96+
The function returns `NaN` for non-integer `n` or `k`.
97+
98+
```javascript
99+
var v = binomcoeff( 2, 1.5 );
100+
// returns NaN
101+
102+
v = binomcoeff( 5.5, 2 );
103+
// returns NaN
104+
```
105+
106+
</section>
107+
108+
<!-- /.usage -->
109+
110+
<section class="examples">
111+
112+
## Examples
113+
114+
<!-- eslint no-undef: "error" -->
115+
116+
```javascript
117+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
118+
var logEachMap = require( '@stdlib/console/log-each-map' );
119+
var binomcoeff = require( '@stdlib/math/base/special/binomcoeff' );
120+
121+
var opts = {
122+
'dtype': 'int32'
123+
};
124+
var n = discreteUniform( 100, -10, 30, opts );
125+
var k = discreteUniform( 100, 0, 20, opts );
126+
127+
logEachMap( 'binomcoeff(%d,%d) = %0.4f', n, k, binomcoeff );
128+
```
129+
130+
</section>
131+
132+
<!-- /.examples -->
133+
134+
<!-- C interface documentation. -->
135+
136+
* * *
137+
138+
<section class="c">
139+
140+
## C APIs
141+
142+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
143+
144+
<section class="intro">
145+
146+
</section>
147+
148+
<!-- /.intro -->
149+
150+
<!-- C usage documentation. -->
151+
152+
<section class="usage">
153+
154+
### Usage
155+
156+
```c
157+
#include "stdlib/math/base/special/binomcoeff.h"
158+
```
159+
160+
#### stdlib_base_binomcoeff( n, k )
161+
162+
Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k` as a single-precision floating-point number.
163+
164+
```c
165+
float v = stdlib_base_binomcoeff( 8, 2 );
166+
// returns 28.0f
167+
```
168+
169+
The function accepts the following arguments:
170+
171+
- **n**: `[in] int32_t` input value.
172+
- **k**: `[in] int32_t` input value.
173+
174+
```c
175+
float stdlib_base_binomcoeff( const int32_t n, const int32_t k );
176+
```
177+
178+
</section>
179+
180+
<!-- /.usage -->
181+
182+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
183+
184+
<section class="notes">
185+
186+
</section>
187+
188+
<!-- /.notes -->
189+
190+
<!-- C API usage examples. -->
191+
192+
<section class="examples">
193+
194+
### Examples
195+
196+
```c
197+
#include "stdlib/math/base/special/binomcoeff.h"
198+
#include <stdio.h>
199+
#include <stdint.h>
200+
201+
int main( void ) {
202+
const int32_t a[] = { 24, 32, 48, 116, 33 };
203+
const int32_t b[] = { 12, 6, 15, 52, 22 };
204+
205+
float out;
206+
int i;
207+
for ( i = 0; i < 5; i++ ) {
208+
out = stdlib_base_binomcoeff( a[ i ], b[ i ] );
209+
printf( "binomcoeff(%d, %d) = %f\n", a[ i ], b[ i ], out );
210+
}
211+
}
212+
```
213+
214+
</section>
215+
216+
<!-- /.examples -->
217+
218+
</section>
219+
220+
<!-- /.c -->
221+
222+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
223+
224+
<section class="related">
225+
226+
</section>
227+
228+
<!-- /.related -->
229+
230+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
231+
232+
<section class="links">
233+
234+
[binomial-coefficient]: https://en.wikipedia.org/wiki/Binomial_coefficient
235+
236+
</section>
237+
238+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 binomcoeff = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var opts;
34+
var x;
35+
var y;
36+
var z;
37+
var i;
38+
39+
opts = {
40+
'dtype': 'int32'
41+
};
42+
x = discreteUniform( 100, 20, 70, opts );
43+
y = discreteUniform( 100, 0, 20, opts );
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
z = binomcoeff( x[ i%x.length ], y[ i%y.length ] );
48+
if ( isnanf( z ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnanf( z ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 binomcoeff = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( binomcoeff instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var options;
43+
var x;
44+
var y;
45+
var z;
46+
var i;
47+
48+
options = {
49+
'dtype': 'int32'
50+
};
51+
x = discreteUniform( 100, 20, 70, options );
52+
y = discreteUniform( 100, 0, 20, options );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
z = binomcoeff( x[ i%x.length ], y[ i%y.length ] );
57+
if ( isnanf( z ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnanf( z ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

0 commit comments

Comments
 (0)