Skip to content

Commit e7b4e8b

Browse files
committed
feat: add C implementation for math/base/special/binomcoeff
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: passed - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 4e50bdd commit e7b4e8b

File tree

18 files changed

+153
-363
lines changed

18 files changed

+153
-363
lines changed

lib/node_modules/@stdlib/math/base/special/binomcoeff/README.md

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# Binomial Coefficient
2222

23-
> Compute the [binomial coefficient][binomial-coefficient].
23+
> Compute the [binomial coefficient][binomial-coefficient] as a single-precision floating-point number.
2424
2525
<section class="intro">
2626

@@ -32,11 +32,6 @@ The [binomial coefficient][binomial-coefficient] of two nonnegative integers `n`
3232
\binom {n}{k} = \frac{n!}{k!\,(n-k)!} \quad \text{for }\ 0\leq k\leq n
3333
```
3434

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-
4035
<!-- </equation> -->
4136

4237
The [binomial coefficient][binomial-coefficient] can be generalized to negative integers `n` as follows:
@@ -47,11 +42,6 @@ The [binomial coefficient][binomial-coefficient] can be generalized to negative
4742
\binom {-n}{k} = (-1)^{k} \binom{n + k - 1}{k} = (-1)^{k} \left(\!\!{\binom {n}{k}}\!\!\right)
4843
```
4944

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-
5545
<!-- </equation> -->
5646

5747
</section>
@@ -63,53 +53,53 @@ The [binomial coefficient][binomial-coefficient] can be generalized to negative
6353
## Usage
6454

6555
```javascript
66-
var binomcoef = require( '@stdlib/math/base/special/binomcoef' );
56+
var binomcoeff = require( '@stdlib/math/base/special/binomcoeff' );
6757
```
6858

69-
#### binomcoef( n, k )
59+
#### binomcoeff( n, k )
7060

71-
Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k`.
61+
Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k` as a single-precision floating-point number.
7262

7363
```javascript
74-
var v = binomcoef( 8, 2 );
64+
var v = binomcoeff( 8, 2 );
7565
// returns 28
7666

77-
v = binomcoef( 0, 0 );
67+
v = binomcoeff( 0, 0 );
7868
// returns 1
7969

80-
v = binomcoef( -4, 2 );
70+
v = binomcoeff( -4, 2 );
8171
// returns 10
8272

83-
v = binomcoef( 5, 3 );
73+
v = binomcoeff( 5, 3 );
8474
// returns 10
8575

86-
v = binomcoef( NaN, 3 );
76+
v = binomcoeff( NaN, 3 );
8777
// returns NaN
8878

89-
v = binomcoef( 5, NaN );
79+
v = binomcoeff( 5, NaN );
9080
// returns NaN
9181

92-
v = binomcoef( NaN, NaN );
82+
v = binomcoeff( NaN, NaN );
9383
// returns NaN
9484
```
9585

9686
For negative `k`, the function returns `0`.
9787

9888
```javascript
99-
var v = binomcoef( 2, -1 );
89+
var v = binomcoeff( 2, -1 );
10090
// returns 0
10191

102-
v = binomcoef( -3, -1 );
92+
v = binomcoeff( -3, -1 );
10393
// returns 0
10494
```
10595

10696
The function returns `NaN` for non-integer `n` or `k`.
10797

10898
```javascript
109-
var v = binomcoef( 2, 1.5 );
99+
var v = binomcoeff( 2, 1.5 );
110100
// returns NaN
111101

112-
v = binomcoef( 5.5, 2 );
102+
v = binomcoeff( 5.5, 2 );
113103
// returns NaN
114104
```
115105

@@ -129,12 +119,12 @@ var logEachMap = require( '@stdlib/console/log-each-map' );
129119
var binomcoef = require( '@stdlib/math/base/special/binomcoef' );
130120

131121
var opts = {
132-
'dtype': 'float64'
122+
'dtype': 'int32'
133123
};
134124
var n = discreteUniform( 100, -10, 30, opts );
135125
var k = discreteUniform( 100, 0, 20, opts );
136126

137-
logEachMap( '%d choose %d = %d', n, k, binomcoef );
127+
logEachMap( 'binomcoeff(%d,%d) = %0.4f', n, k, binomcoeff );
138128
```
139129

140130
</section>
@@ -164,25 +154,25 @@ logEachMap( '%d choose %d = %d', n, k, binomcoef );
164154
### Usage
165155

166156
```c
167-
#include "stdlib/math/base/special/binomcoef.h"
157+
#include "stdlib/math/base/special/binomcoeff.h"
168158
```
169159

170-
#### stdlib_base_binomcoef( n, k )
160+
#### stdlib_base_binomcoeff( n, k )
171161

172-
Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k`.
162+
Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k` as a single-precision floating-point number.
173163

174164
```c
175-
double v = stdlib_base_binomcoef( 8, 2 );
176-
// returns 28.0
165+
float v = stdlib_base_binomcoeff( 8, 2 );
166+
// returns 28.0f
177167
```
178168

179169
The function accepts the following arguments:
180170

181-
- **n**: `[in] int64_t` input value.
182-
- **k**: `[in] int64_t` input value.
171+
- **n**: `[in] int32_t` input value.
172+
- **k**: `[in] int32_t` input value.
183173

184174
```c
185-
double stdlib_base_binomcoef( const int64_t n, const int64_t k );
175+
float stdlib_base_binomcoeff( const int32_t n, const int32_t k );
186176
```
187177
188178
</section>
@@ -204,20 +194,19 @@ double stdlib_base_binomcoef( const int64_t n, const int64_t k );
204194
### Examples
205195
206196
```c
207-
#include "stdlib/math/base/special/binomcoef.h"
197+
#include "stdlib/math/base/special/binomcoeff.h"
208198
#include <stdio.h>
209199
#include <stdint.h>
210-
#include <inttypes.h>
211200
212201
int main( void ) {
213-
const int64_t a[] = { 24, 32, 48, 116, 33 };
214-
const int64_t b[] = { 12, 6, 15, 52, 22 };
202+
const int32_t a[] = { 24, 32, 48, 116, 33 };
203+
const int32_t b[] = { 12, 6, 15, 52, 22 };
215204
216-
double out;
205+
float out;
217206
int i;
218207
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 );
208+
out = stdlib_base_binomcoeff( a[ i ], b[ i ] );
209+
printf( "binomcoeff(%d, %d) = %f\n", a[ i ], b[ i ], out );
221210
}
222211
}
223212
```

lib/node_modules/@stdlib/math/base/special/binomcoeff/benchmark/benchmark.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,35 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pkg = require( './../package.json' ).name;
27-
var binomcoef = require( './../lib' );
27+
var binomcoeff = require( './../lib' );
2828

2929

3030
// MAIN //
3131

3232
bench( pkg, function benchmark( b ) {
33+
var opts;
3334
var x;
3435
var y;
3536
var z;
3637
var i;
3738

38-
x = discreteUniform( 100, 20, 70 );
39-
y = discreteUniform( 100, 0, 20 );
39+
opts = {
40+
'dtype': 'int32'
41+
};
42+
x = discreteUniform( 100, 20, 70, opts );
43+
y = discreteUniform( 100, 0, 20, opts );
4044

4145
b.tic();
4246
for ( i = 0; i < b.iterations; i++ ) {
43-
z = binomcoef( x[ i%x.length ], y[ i%y.length ] );
44-
if ( isnan( z ) ) {
47+
z = binomcoeff( x[ i%x.length ], y[ i%y.length ] );
48+
if ( isnanf( z ) ) {
4549
b.fail( 'should not return NaN' );
4650
}
4751
}
4852
b.toc();
49-
if ( isnan( z ) ) {
53+
if ( isnanf( z ) ) {
5054
b.fail( 'should not return NaN' );
5155
}
5256
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/math/base/special/binomcoeff/benchmark/benchmark.native.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,43 @@
2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
2929

3030

3131
// VARIABLES //
3232

33-
var binomcoef = tryRequire( resolve( __dirname, './../lib/native.js' ) );
33+
var binomcoeff = tryRequire( resolve( __dirname, './../lib/native.js' ) );
3434
var opts = {
35-
'skip': ( binomcoef instanceof Error )
35+
'skip': ( binomcoeff instanceof Error )
3636
};
3737

3838

3939
// MAIN //
4040

4141
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var options;
4243
var x;
4344
var y;
4445
var z;
4546
var i;
4647

47-
x = discreteUniform( 100, 20, 70 );
48-
y = discreteUniform( 100, 0, 20 );
48+
options = {
49+
'dtype': 'int32'
50+
};
51+
x = discreteUniform( 100, 20, 70, options );
52+
y = discreteUniform( 100, 0, 20, options );
4953

5054
b.tic();
5155
for ( i = 0; i < b.iterations; i++ ) {
52-
z = binomcoef( x[ i%x.length ], y[ i%y.length ] );
53-
if ( isnan( z ) ) {
56+
z = binomcoeff( x[ i%x.length ], y[ i%y.length ] );
57+
if ( isnanf( z ) ) {
5458
b.fail( 'should not return NaN' );
5559
}
5660
}
5761
b.toc();
58-
if ( isnan( z ) ) {
62+
if ( isnanf( z ) ) {
5963
b.fail( 'should not return NaN' );
6064
}
6165
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/math/base/special/binomcoeff/benchmark/c/native/benchmark.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/special/binomcoef.h"
19+
#include "stdlib/math/base/special/binomcoeff.h"
2020
#include <stdlib.h>
2121
#include <stdio.h>
2222
#include <math.h>
2323
#include <time.h>
2424
#include <sys/time.h>
2525

26-
#define NAME "binomcoef"
26+
#define NAME "binomcoeff"
2727
#define ITERATIONS 1000000
2828
#define REPEATS 3
2929

@@ -79,9 +79,9 @@ static double tic( void ) {
7979
*
8080
* @return random number
8181
*/
82-
static double rand_double( void ) {
82+
static float rand_float( void ) {
8383
int r = rand();
84-
return (double)r / ( (double)RAND_MAX + 1.0 );
84+
return (float)r / ( (float)RAND_MAX + 1.0f );
8585
}
8686

8787
/**
@@ -90,21 +90,21 @@ static double rand_double( void ) {
9090
* @return elapsed time in seconds
9191
*/
9292
static double benchmark( void ) {
93-
int64_t n[ 100 ];
94-
int64_t k[ 100 ];
93+
int32_t n[ 100 ];
94+
int32_t k[ 100 ];
9595
double elapsed;
96-
double y;
9796
double t;
97+
float y;
9898
int i;
9999

100100
for ( i = 0; i < 100; i++ ) {
101-
n[ i ] = (int64_t)round( 500.0 * rand_double() );
102-
k[ i ] = (int64_t)round( 500.0 * rand_double() );
101+
n[ i ] = (int32_t)round( 100.0 * rand_float() );
102+
k[ i ] = (int32_t)round( 100.0 * rand_float() );
103103
}
104104

105105
t = tic();
106106
for ( i = 0; i < ITERATIONS; i++ ) {
107-
y = stdlib_base_binomcoef( n[ i%100 ], k[ i%100 ] );
107+
y = stdlib_base_binomcoeff( n[ i%100 ], k[ i%100 ] );
108108
if ( y != y ) {
109109
printf( "should not return NaN\n" );
110110
break;

lib/node_modules/@stdlib/math/base/special/binomcoeff/benchmark/cpp/boost/benchmark.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
using boost::random::uniform_real_distribution;
2929
using boost::random::mt19937;
3030

31-
#define NAME "binomcoef"
31+
#define NAME "binomcoeff"
3232
#define ITERATIONS 1000000
3333
#define REPEATS 3
3434

@@ -87,23 +87,23 @@ double tic() {
8787
double benchmark() {
8888
double elapsed;
8989
double t;
90-
double x;
91-
double y;
92-
double z;
90+
float x;
91+
float y;
92+
float z;
9393
int i;
9494

9595
// Define a new pseudorandom number generator:
9696
mt19937 rng;
9797

98-
// Define a uniform distribution for generating pseudorandom numbers as "doubles" between a minimum value (inclusive) and a maximum value (exclusive):
99-
uniform_real_distribution<> randu1( 20.0, 70.0 );
100-
uniform_real_distribution<> randu2( 0.0, 20.0 );
98+
// Define a uniform distribution for generating pseudorandom numbers as "floats" between a minimum value (inclusive) and a maximum value (exclusive):
99+
uniform_real_distribution<> randu1( 20.0f, 70.0f );
100+
uniform_real_distribution<> randu2( 0.0f, 20.0f );
101101

102102
t = tic();
103103
for ( i = 0; i < ITERATIONS; i++ ) {
104-
x = randu1( rng );
105-
y = randu2( rng );
106-
z = boost::math::binomial_coefficient<double>( x, y );
104+
x = (int)randu1( rng );
105+
y = (int)randu2( rng );
106+
z = boost::math::binomial_coefficient<float>( x, y );
107107
if ( z != z ) {
108108
printf( "should not return NaN\n" );
109109
break;

lib/node_modules/@stdlib/math/base/special/binomcoeff/benchmark/python/scipy/benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from __future__ import print_function
2222
import timeit
2323

24-
NAME = "binomcoef"
24+
NAME = "binomcoeff"
2525
REPEATS = 3
2626
ITERATIONS = 1000000
2727

0 commit comments

Comments
 (0)