Skip to content

Commit 4e50bdd

Browse files
committed
feat: temp commit
1 parent 61706f6 commit 4e50bdd

38 files changed

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

0 commit comments

Comments
 (0)