Skip to content

Commit 3d81a35

Browse files
committed
chore: minor clean-up
--- 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: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent f309c18 commit 3d81a35

File tree

11 files changed

+78
-69
lines changed

11 files changed

+78
-69
lines changed

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ double out = stdlib_base_dists_binomial_entropy( 20, 0.1 );
178178

179179
The function accepts the following arguments:
180180

181-
- **n**: `[in] double` number of trials.
181+
- **n**: `[in] int32_t` number of trials.
182182
- **p**: `[in] double` success probability.
183183

184184
```c
185-
double stdlib_base_dists_binomial_entropy( const double n, const double p );
185+
double stdlib_base_dists_binomial_entropy( const int32_t n, const double p );
186186
```
187187
188188
</section>
@@ -207,23 +207,28 @@ double stdlib_base_dists_binomial_entropy( const double n, const double p );
207207
#include "stdlib/stats/base/dists/binomial/entropy.h"
208208
#include <stdlib.h>
209209
#include <stdio.h>
210+
#include <stdint.h>
210211
211212
static double random_uniform( const double min, const double max ) {
212213
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
213214
return min + ( v*(max-min) );
214215
}
215216
217+
static int32_t random_int( const int32_t min, const int32_t max ) {
218+
return min + (int32_t)( random_uniform( 0.0, 1.0 ) * ( max - min + 1 ) );
219+
}
220+
216221
int main( void ) {
217-
double v;
218-
double i;
219-
double n;
222+
int32_t n;
220223
double p;
224+
double v;
225+
int i;
221226
222227
for ( i = 0; i < 25; i++ ) {
223-
n = random_uniform( -5.0, 5.0 );
224-
p = random_uniform( 0.0, 20.0 );
228+
n = random_int( 0, 100 );
229+
p = random_uniform( 0.0, 1.0 );
225230
v = stdlib_base_dists_binomial_entropy( n, p );
226-
printf( "n: %lf, p: %lf, H(X;n,p): %lf\n", n, p, v );
231+
printf( "n: %d, p: %lf, H(X;n,p): %lf\n", n, p, v );
227232
}
228233
}
229234
```

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/benchmark/benchmark.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var Float64Array = require( '@stdlib/array/float64' );
25-
var uniform = require( '@stdlib/random/base/uniform' );
26-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2726
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var pkg = require( './../package.json' ).name;
2928
var entropy = require( './../lib' );
@@ -39,12 +38,8 @@ bench( pkg, function benchmark( b ) {
3938
var i;
4039

4140
len = 1000;
42-
n = new Float64Array( len );
43-
p = new Float64Array( len );
44-
for ( i = 0; i < len; i++ ) {
45-
n[ i ] = discreteUniform( 1, 100 );
46-
p[ i ] = uniform( 0.0, 1.0 );
47-
}
41+
n = discreteUniform( len, 1, 100 );
42+
p = uniform( len, 0.0, 1.0 );
4843

4944
b.tic();
5045
for ( i = 0; i < b.iterations; i++ ) {

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/benchmark/benchmark.native.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var Float64Array = require( '@stdlib/array/float64' );
26-
var ceil = require( '@stdlib/math/base/special/ceil' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
28-
var randu = require( '@stdlib/random/base/randu' );
2928
var isnan = require( '@stdlib/math/base/assert/is-nan' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -48,12 +47,8 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4847
var i;
4948

5049
len = 100;
51-
n = new Float64Array( len );
52-
p = new Float64Array( len );
53-
for ( i = 0; i < len; i++ ) {
54-
n[ i ] = ceil( randu() * 100.0 );
55-
p[ i ] = randu();
56-
}
50+
n = discreteUniform( len, 0, 100 );
51+
p = uniform( len, 0.0, 1.0 );
5752

5853
b.tic();
5954
for ( i = 0; i < b.iterations; i++ ) {

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/benchmark/c/benchmark.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dists/binomial/entropy.h"
20-
#include "stdlib/math/base/special/ceil.h"
2120
#include <stdlib.h>
2221
#include <stdio.h>
22+
#include <stdint.h>
2323
#include <math.h>
2424
#include <time.h>
2525
#include <sys/time.h>
@@ -87,6 +87,17 @@ static double random_uniform( const double min, const double max ) {
8787
return min + ( v*(max-min) );
8888
}
8989

90+
/**
91+
* Generates a random integer on the interval [min,max].
92+
*
93+
* @param min minimum value (inclusive)
94+
* @param max maximum value (inclusive)
95+
* @return random integer
96+
*/
97+
static int32_t random_int( const int32_t min, const int32_t max ) {
98+
return min + (int32_t)( random_uniform( 0.0, 1.0 ) * ( max - min + 1 ) );
99+
}
100+
90101
/**
91102
* Runs a benchmark.
92103
*
@@ -95,14 +106,14 @@ static double random_uniform( const double min, const double max ) {
95106
static double benchmark( void ) {
96107
double elapsed;
97108
int len = 100;
98-
double n[ 100 ];
99109
double p[ 100 ];
110+
int32_t n[ 100 ];
100111
double y;
101112
double t;
102113
int i;
103114

104115
for ( i = 0; i < 100; i++ ) {
105-
n[ i ] = stdlib_base_ceil( random_uniform( -50.0, 50.0 ) );
116+
n[ i ] = random_int( 0, 100 );
106117
p[ i ] = random_uniform( 0.0, 1.0);
107118
}
108119

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/examples/c/example.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,27 @@
1919
#include "stdlib/stats/base/dists/binomial/entropy.h"
2020
#include <stdlib.h>
2121
#include <stdio.h>
22+
#include <stdint.h>
2223

2324
static double random_uniform( const double min, const double max ) {
24-
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
25-
return min + ( v*(max-min) );
25+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
26+
return min + ( v*(max-min) );
27+
}
28+
29+
static int32_t random_int( const int32_t min, const int32_t max ) {
30+
return min + (int32_t)( random_uniform( 0.0, 1.0 ) * ( max - min + 1 ) );
2631
}
2732

2833
int main( void ) {
29-
double v;
30-
double i;
31-
double n;
34+
int32_t n;
3235
double p;
36+
double v;
37+
int i;
3338

3439
for ( i = 0; i < 25; i++ ) {
35-
n = random_uniform( -5.0, 5.0 );
36-
p = random_uniform( 0.0, 20.0 );
40+
n = random_int( 0, 100 );
41+
p = random_uniform( 0.0, 1.0 );
3742
v = stdlib_base_dists_binomial_entropy( n, p );
38-
printf( "n: %lf, p: %lf, H(X;n,p): %lf\n", n, p, v );
43+
printf( "n: %d, p: %lf, H(X;n,p): %lf\n", n, p, v );
3944
}
4045
}

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/include/stdlib/stats/base/dists/binomial/entropy.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef STDLIB_STATS_BASE_DISTS_BINOMIAL_ENTROPY_H
2020
#define STDLIB_STATS_BASE_DISTS_BINOMIAL_ENTROPY_H
2121

22+
#include <stdint.h>
23+
2224
/*
2325
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
2426
*/
@@ -27,9 +29,9 @@ extern "C" {
2729
#endif
2830

2931
/**
30-
* Returns the entropy of an binomial distribution.
32+
* Returns the entropy of a binomial distribution.
3133
*/
32-
double stdlib_base_dists_binomial_entropy( const double n, const double p );
34+
double stdlib_base_dists_binomial_entropy( const int32_t n, const double p );
3335

3436
#ifdef __cplusplus
3537
}

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/lib/native.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
// MODULES //
2222

23+
var isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var PINF = require( '@stdlib/constants/float64/pinf' );
2326
var addon = require( './../src/addon.node' );
2427

2528

@@ -48,12 +51,15 @@ var addon = require( './../src/addon.node' );
4851
* @example
4952
* var v = entropy( 20, 1.1 );
5053
* // returns NaN
51-
*
52-
* @example
53-
* var v = entropy( 20, NaN );
54-
* // returns NaN
5554
*/
5655
function entropy( n, p ) {
56+
if (
57+
isnan( n ) ||
58+
!isNonNegativeInteger( n ) ||
59+
n === PINF
60+
) {
61+
return NaN;
62+
}
5763
return addon( n, p );
5864
}
5965

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/manifest.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
"@stdlib/math/base/napi/binary",
4242
"@stdlib/math/base/assert/is-nan",
4343
"@stdlib/math/base/special/ln",
44-
"@stdlib/math/base/special/exp",
45-
"@stdlib/math/base/assert/is-nonnegative-integer",
46-
"@stdlib/math/base/special/ceil"
44+
"@stdlib/math/base/special/exp"
4745
]
4846
},
4947
{
@@ -60,9 +58,7 @@
6058
"dependencies": [
6159
"@stdlib/math/base/assert/is-nan",
6260
"@stdlib/math/base/special/ln",
63-
"@stdlib/math/base/special/exp",
64-
"@stdlib/math/base/assert/is-nonnegative-integer",
65-
"@stdlib/math/base/special/ceil"
61+
"@stdlib/math/base/special/exp"
6662
]
6763
},
6864
{
@@ -79,9 +75,7 @@
7975
"dependencies": [
8076
"@stdlib/math/base/assert/is-nan",
8177
"@stdlib/math/base/special/ln",
82-
"@stdlib/math/base/special/exp",
83-
"@stdlib/math/base/assert/is-nonnegative-integer",
84-
"@stdlib/math/base/special/ceil"
78+
"@stdlib/math/base/special/exp"
8579
]
8680
}
8781
]

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/src/addon.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@
1919
#include "stdlib/stats/base/dists/binomial/entropy.h"
2020
#include "stdlib/math/base/napi/binary.h"
2121

22-
// cppcheck-suppress shadowFunction
23-
STDLIB_MATH_BASE_NAPI_MODULE_DD_D( stdlib_base_dists_binomial_entropy )
22+
STDLIB_MATH_BASE_NAPI_MODULE_ID_D( stdlib_base_dists_binomial_entropy )

lib/node_modules/@stdlib/stats/base/dists/binomial/entropy/src/main.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818

1919
#include "stdlib/stats/base/dists/binomial/entropy.h"
2020
#include "stdlib/math/base/assert/is_nan.h"
21-
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
2221
#include "stdlib/math/base/special/ln.h"
2322
#include "stdlib/math/base/special/exp.h"
24-
#include <math.h>
23+
#include <stdint.h>
2524

2625
/**
2726
* Returns the entropy of a binomial distribution.
2827
*
29-
* @param n - number of trials
30-
* @param p - success probability
31-
* @returns entropy
28+
* @param n number of trials
29+
* @param p success probability
30+
* @return entropy
3231
*
3332
* @example
3433
* double v = stdlib_base_dists_binomial_entropy( 100, 0.1 );
@@ -42,30 +41,28 @@
4241
* double v = stdlib_base_dists_binomial_entropy( 20, NAN );
4342
* // returns NaN
4443
*/
45-
double stdlib_base_dists_binomial_entropy( const double n, const double p ) {
44+
double stdlib_base_dists_binomial_entropy( const int32_t n, const double p ) {
4645
double nlq;
4746
double out;
48-
double i;
4947
double q;
48+
int32_t i;
5049

5150
if (
52-
stdlib_base_is_nan( n ) ||
5351
stdlib_base_is_nan( p ) ||
52+
n < 0 ||
5453
p < 0.0 ||
55-
p > 1.0 ||
56-
!stdlib_base_is_nonnegative_integer( n ) ||
57-
n == INFINITY
58-
) {
54+
p > 1.0
55+
) {
5956
return 0.0 / 0.0; // NaN
6057
}
61-
if ( p == 0 || p == 1 || n == 0 ) {
58+
if ( p == 0.0 || p == 1.0 || n == 0 ) {
6259
return 0.0;
6360
}
6461
q = 1.0 - p;
6562
nlq = n * stdlib_base_ln( q );
6663
out = stdlib_base_exp( nlq ) * nlq;
6764
for ( i = 1; i <= n; i++ ) {
68-
nlq += stdlib_base_ln( ( n - i + 1 ) / i ) + stdlib_base_ln( p / q );
65+
nlq += stdlib_base_ln( (double)( n - i + 1 ) / (double)i ) + stdlib_base_ln( p / q );
6966
out += stdlib_base_exp( nlq ) * nlq;
7067
}
7168
return -out;

0 commit comments

Comments
 (0)