Skip to content

Commit 3cd1e90

Browse files
committed
feat: add the JS native files
1 parent 98d2714 commit 3cd1e90

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 pmf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( pmf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var len;
44+
var x;
45+
var mu;
46+
var y;
47+
var i;
48+
49+
len = 100;
50+
x = new Float64Array( len );
51+
mu = new Float64Array( len );
52+
for ( i = 0; i < len; i++ ) {
53+
x[ i ] = ( randu() * 20.0 ) - 10.0;
54+
mu[ i ] = ( randu() * 40.0 ) - 20.0;
55+
}
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
y = pmf( x[ i % len ], mu[ i % len ] );
60+
if ( isnan( y ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
}
64+
b.toc();
65+
if ( isnan( y ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
* Evaluates the probability mass function (PMF) for a degenerate distribution centered at `mu`.
30+
*
31+
* @private
32+
* @param {number} x - input value
33+
* @param {number} mu - constant value of the distribution
34+
* @returns {number} evaluated probability mass function
35+
*
36+
* @example
37+
* var y = pmf( 3.0, 3.0 );
38+
* // returns 1.0
39+
*
40+
* @example
41+
* var y = pmf( 2.0, 3.0 );
42+
* // returns 0.0
43+
*
44+
* @example
45+
* var y = pmf( NaN, 2.0 );
46+
* // returns NaN
47+
*
48+
* @example
49+
* var y = pmf( 3.0, NaN );
50+
* // returns NaN
51+
*/
52+
function pmf( x, mu ) {
53+
return addon( x, mu );
54+
}
55+
56+
57+
// EXPORTS //
58+
59+
module.exports = pmf;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
28+
29+
// VARIABLES //
30+
31+
var pmf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
32+
var opts = {
33+
'skip': ( pmf instanceof Error )
34+
};
35+
36+
37+
// TESTS //
38+
39+
tape( 'main export is a function', opts, function test( t ) {
40+
t.ok( true, __filename );
41+
t.strictEqual( typeof pmf, 'function', 'main export is a function' );
42+
t.end();
43+
});
44+
45+
tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) {
46+
var y;
47+
48+
y = pmf( NaN, 0.0 );
49+
t.equal( isnan( y ), true, 'returns NaN' );
50+
51+
y = pmf( 0.0, NaN );
52+
t.equal( isnan( y ), true, 'returns NaN' );
53+
54+
y = pmf( NaN, NaN );
55+
t.equal( isnan( y ), true, 'returns NaN' );
56+
57+
t.end();
58+
});
59+
60+
tape( 'the function returns `1.0` if provided `x` equal to `mu`', opts, function test( t ) {
61+
var y;
62+
63+
y = pmf( 2.0, 2.0 );
64+
t.equal( y, 1.0, 'returns 1.0' );
65+
66+
y = pmf( 0.0, 0.0 );
67+
t.equal( y, 1.0, 'returns 1.0' );
68+
69+
y = pmf( -3.0, -3.0 );
70+
t.equal( y, 1.0, 'returns 1.0' );
71+
72+
t.end();
73+
});
74+
75+
tape( 'the function returns `0.0` if provided `x` not equal to `mu`', opts, function test( t ) {
76+
var y;
77+
78+
y = pmf( 2.0, 3.0 );
79+
t.equal( y, 0.0, 'returns 0.0' );
80+
81+
y = pmf( 4.0, 3.0 );
82+
t.equal( y, 0.0, 'returns 0.0' );
83+
84+
t.end();
85+
});

0 commit comments

Comments
 (0)