Skip to content

Commit 9ef70b2

Browse files
committed
feat: add the JS native files
1 parent 6b64158 commit 9ef70b2

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-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 pdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( pdf 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 = pdf( 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 density function (PDF) 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 density function
35+
*
36+
* @example
37+
* var y = pdf( 3.0, 3.0 );
38+
* // returns Infinity
39+
*
40+
* @example
41+
* var y = pdf( 2.0, 3.0 );
42+
* // returns 0.0
43+
*
44+
* @example
45+
* var y = pdf( NaN, 2.0 );
46+
* // returns NaN
47+
*
48+
* @example
49+
* var y = pdf( 3.0, NaN );
50+
* // returns NaN
51+
*/
52+
function pdf( x, mu ) {
53+
return addon( x, mu );
54+
}
55+
56+
57+
// EXPORTS //
58+
59+
module.exports = pdf;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
var PINF = require( '@stdlib/constants/float64/pinf' );
28+
29+
30+
// VARIABLES //
31+
32+
var pdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
33+
var opts = {
34+
'skip': ( pdf instanceof Error )
35+
};
36+
37+
38+
// TESTS //
39+
40+
tape( 'main export is a function', opts, function test( t ) {
41+
t.ok( true, __filename );
42+
t.strictEqual( typeof pdf, 'function', 'main export is a function' );
43+
t.end();
44+
});
45+
46+
tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) {
47+
var y;
48+
49+
y = pdf( NaN, 0.0 );
50+
t.equal( isnan( y ), true, 'returns NaN' );
51+
52+
y = pdf( 0.0, NaN );
53+
t.equal( isnan( y ), true, 'returns NaN' );
54+
55+
y = pdf( NaN, NaN );
56+
t.equal( isnan( y ), true, 'returns NaN' );
57+
58+
t.end();
59+
});
60+
61+
tape( 'the function returns `+Infinity` if provided `x` equal to `mu`', opts, function test( t ) {
62+
var y;
63+
64+
y = pdf( 2.0, 2.0 );
65+
t.equal( y, PINF, 'returns +Infinity' );
66+
67+
y = pdf( 0.0, 0.0 );
68+
t.equal( y, PINF, 'returns +Infinity' );
69+
70+
y = pdf( -3.0, -3.0 );
71+
t.equal( y, PINF, 'returns +Infinity' );
72+
73+
t.end();
74+
});
75+
76+
tape( 'the function returns `0.0` if provided `x` not equal to `mu`', opts, function test( t ) {
77+
var y;
78+
79+
y = pdf( 2.0, 3.0 );
80+
t.equal( y, 0.0, 'returns 0.0' );
81+
82+
y = pdf( 4.0, 3.0 );
83+
t.equal( y, 0.0, 'returns 0.0' );
84+
85+
t.end();
86+
});

0 commit comments

Comments
 (0)