Skip to content

Commit c3b490e

Browse files
feat(add c implementation): add stats/base/dists/bernoulli/entropy
1 parent 76198ea commit c3b490e

File tree

11 files changed

+96
-34
lines changed

11 files changed

+96
-34
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ double out = stdlib_base_dists_bernoulli_entropy( 0.1 );
167167

168168
The function accepts the following arguments:
169169

170-
- **p**: `[in] double` maximum support
170+
- **p**: `[in] double` success probability
171171

172172
```c
173173
double stdlib_base_dists_bernoulli_entropy( const double p );
@@ -201,8 +201,8 @@ int main( void ) {
201201
int i;
202202
203203
for ( i = 0; i < 25; i++ ) {
204-
p = ( (double)rand() / (double)RAND_MAX );
205-
y = stdlib_base_dists_arcsine_entropy( p );
204+
p = (double)rand() / ( (double)RAND_MAX + 1.0 );
205+
y = stdlib_base_dists_bernoulli_entropy( p );
206206
printf( "x: %lf, H(X;p): %lf\n", p, y );
207207
}
208208
}

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

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

2323
var bench = require( '@stdlib/bench' );
24+
var Float64Array = require( '@stdlib/array/float64' );
2425
var randu = require( '@stdlib/random/base/randu' );
2526
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2627
var pkg = require( './../package.json' ).name;
@@ -30,14 +31,20 @@ var entropy = require( './../lib' );
3031
// MAIN //
3132

3233
bench( pkg, function benchmark( b ) {
34+
var len;
3335
var p;
3436
var y;
3537
var i;
3638

39+
len = 100;
40+
p = new Float64Array( len );
41+
for ( i = 0; i < len; i++ ) {
42+
p[ i ] = randu();
43+
}
44+
3745
b.tic();
3846
for ( i = 0; i < b.iterations; i++ ) {
39-
p = randu();
40-
y = entropy( p );
47+
y = entropy( p[ i % len ] );
4148
if ( isnan( y ) ) {
4249
b.fail( 'should not return NaN' );
4350
}
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) 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 Float64Array = require( '@stdlib/array/float64' );
26+
var randu = require( '@stdlib/random/base/randu' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var entropy = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( entropy instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg, opts, function benchmark( b ) {
43+
var len;
44+
var p;
45+
var y;
46+
var i;
47+
48+
len = 100;
49+
p = new Float64Array( len );
50+
for ( i = 0; i < len; i++ ) {
51+
p[ i ] = randu();
52+
}
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = entropy( p[ i % len ] );
57+
if ( isnan( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( y ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

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

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

19-
#include <sys/time.h>
2019
#include "stdlib/stats/base/dists/bernoulli/entropy.h"
2120
#include <math.h>
2221
#include <stdio.h>
2322
#include <stdlib.h>
2423
#include <time.h>
24+
#include <sys/time.h>
2525

2626
#define NAME "bernoulli-entropy"
2727
#define ITERATIONS 1000000
@@ -91,15 +91,18 @@ static double rand_double( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
double p;
94+
double p[ 100 ];
9595
double y;
9696
double t;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
p[ i ] = rand_double();
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
p = ( (double)rand() / (double)RAND_MAX );
102-
y = stdlib_base_dists_bernoulli_entropy( p );
105+
y = stdlib_base_dists_bernoulli_entropy( p[ i%100 ] );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

lib/node_modules/@stdlib/stats/base/dists/bernoulli/entropy/examples/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ run: $(c_targets)
143143
clean:
144144
$(QUIET) -rm -f *.o *.out
145145

146-
.PHONY: clean
146+
.PHONY: clean

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main( void ) {
2626
int i;
2727

2828
for ( i = 0; i < 25; i++ ) {
29-
p = ( (double)rand() / (double)RAND_MAX );
29+
p = (double)rand() / ( (double)RAND_MAX + 1.0 );
3030
y = stdlib_base_dists_bernoulli_entropy( p );
3131
printf( "x: %lf , H(X;p): %lf\n", p, y );
3232
}

lib/node_modules/@stdlib/stats/base/dists/bernoulli/entropy/include.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
'<!@(node -e "var arr = require(\'@stdlib/utils/library-manifest\')(\'./manifest.json\',{},{\'basedir\':process.cwd(),\'paths\':\'posix\'}).libpath; for ( var i = 0; i < arr.length; i++ ) { console.log( arr[ i ] ); }")',
5151
],
5252
}, # end variables
53-
}
53+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ function entropy( p ) {
5858

5959
// EXPORTS //
6060

61-
module.exports = entropy;
61+
module.exports = entropy;

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,9 @@
3838
"libraries": [],
3939
"libpath": [],
4040
"dependencies": [
41-
"@stdlib/math/base/napi/ternary",
41+
"@stdlib/math/base/napi/unary",
4242
"@stdlib/math/base/assert/is-nan",
43-
"@stdlib/math/base/special/ln",
44-
"@stdlib/constants/float64/ln-pi",
45-
"@stdlib/constants/float64/ln-two",
46-
"@stdlib/constants/float64/ninf",
47-
"@stdlib/math/base/special/asin",
48-
"@stdlib/math/base/special/sqrt"
43+
"@stdlib/math/base/special/ln"
4944
]
5045
},
5146
{
@@ -61,12 +56,7 @@
6156
"libpath": [],
6257
"dependencies": [
6358
"@stdlib/math/base/assert/is-nan",
64-
"@stdlib/math/base/special/ln",
65-
"@stdlib/constants/float64/ln-pi",
66-
"@stdlib/constants/float64/ln-two",
67-
"@stdlib/constants/float64/ninf",
68-
"@stdlib/math/base/special/asin",
69-
"@stdlib/math/base/special/sqrt"
59+
"@stdlib/math/base/special/ln"
7060
]
7161
},
7262
{
@@ -82,12 +72,7 @@
8272
"libpath": [],
8373
"dependencies": [
8474
"@stdlib/math/base/assert/is-nan",
85-
"@stdlib/math/base/special/ln",
86-
"@stdlib/constants/float64/ln-pi",
87-
"@stdlib/constants/float64/ln-two",
88-
"@stdlib/constants/float64/ninf",
89-
"@stdlib/math/base/special/asin",
90-
"@stdlib/math/base/special/sqrt"
75+
"@stdlib/math/base/special/ln"
9176
]
9277
}
9378
]

lib/node_modules/@stdlib/stats/base/dists/bernoulli/entropy/src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ clean-addon:
6767
#/
6868
clean: clean-addon
6969

70-
.PHONY: clean
70+
.PHONY: clean

0 commit comments

Comments
 (0)