Skip to content

Commit 05fe3c0

Browse files
committed
feat: add the JS native files
1 parent e1b12bb commit 05fe3c0

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed
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 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+'::native', opts, function benchmark( b ) {
43+
var len;
44+
var k;
45+
var y;
46+
var i;
47+
48+
len = 100;
49+
k = new Float64Array( len );
50+
for ( i = 0; i < len; i++ ) {
51+
k[ i ] = ( randu() * 10.0 ) + 0.1; // Ensure k > 0
52+
}
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = entropy( k[ 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+
});
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 addon = require( './../src/addon.node' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Returns the differential entropy of a chi distribution.
30+
*
31+
* @private
32+
* @param {PositiveNumber} k - degrees of freedom
33+
* @returns {number} entropy
34+
*
35+
* @example
36+
* var v = entropy( 9.0 );
37+
* // returns ~1.052
38+
*
39+
* @example
40+
* var v = entropy( 1.0 );
41+
* // returns ~0.726
42+
*
43+
* @example
44+
* var v = entropy( -0.2 );
45+
* // returns NaN
46+
*
47+
* @example
48+
* var v = entropy( NaN );
49+
* // returns NaN
50+
*/
51+
function entropy( k ) {
52+
return addon( k );
53+
}
54+
55+
56+
// EXPORTS //
57+
58+
module.exports = entropy;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 resolve = require( 'path' ).resolve;
24+
var tape = require( 'tape' );
25+
var tryRequire = require( '@stdlib/utils/try-require' );
26+
var abs = require( '@stdlib/math/base/special/abs' );
27+
var NINF = require( '@stdlib/constants/float64/ninf' );
28+
var EPS = require( '@stdlib/constants/float64/eps' );
29+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
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+
// FIXTURES //
41+
42+
var data = require( './fixtures/julia/data.json' );
43+
44+
45+
// TESTS //
46+
47+
tape( 'main export is a function', opts, function test( t ) {
48+
t.ok( true, __filename );
49+
t.strictEqual( typeof entropy, 'function', 'main export is a function' );
50+
t.end();
51+
});
52+
53+
tape( 'if provided `NaN` for `k`, the function returns `NaN`', opts, function test( t ) {
54+
var v = entropy( NaN );
55+
t.equal( isnan( v ), true, 'returns NaN' );
56+
t.end();
57+
});
58+
59+
tape( 'if provided a degrees of freedom parameter `k` that is not a positive number, the function returns `NaN`', opts, function test( t ) {
60+
var v;
61+
62+
v = entropy( 0.0 );
63+
t.equal( isnan( v ), true, 'returns NaN' );
64+
65+
v = entropy( -1.0 );
66+
t.equal( isnan( v ), true, 'returns NaN' );
67+
68+
v = entropy( NINF );
69+
t.equal( isnan( v ), true, 'returns NaN' );
70+
71+
t.end();
72+
});
73+
74+
tape( 'the function returns the entropy of a chi distribution', opts, function test( t ) {
75+
var expected;
76+
var delta;
77+
var tol;
78+
var k;
79+
var i;
80+
var y;
81+
82+
expected = data.expected;
83+
k = data.k;
84+
for ( i = 0; i < expected.length; i++ ) {
85+
y = entropy( k[i] );
86+
if ( y === expected[i] ) {
87+
t.equal( y, expected[i], 'k:'+k[i]+', y: '+y+', expected: '+expected[i] );
88+
} else {
89+
delta = abs( y - expected[ i ] );
90+
tol = 40.0 * EPS * abs( expected[ i ] );
91+
t.ok( delta <= tol, 'within tolerance. k: '+k[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
92+
}
93+
}
94+
t.end();
95+
});

0 commit comments

Comments
 (0)