Skip to content

Commit 802c112

Browse files
committed
feat: add the JS native files
1 parent 24b9448 commit 802c112

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-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 mgf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( mgf instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var len;
44+
var t;
45+
var k;
46+
var y;
47+
var i;
48+
49+
len = 100;
50+
t = new Float64Array( len );
51+
k = new Float64Array( len );
52+
for ( i = 0; i < len; i++ ) {
53+
t[ i ] = ( randu() * 0.8 ) - 0.4;
54+
k[ i ] = ( randu() * 10.0 ) + 0.1;
55+
}
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
y = mgf( t[ i % len ], k[ 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 moment-generating function (MGF) for a chi-squared distribution with degrees of freedom `k` at a value `t`.
30+
*
31+
* @private
32+
* @param {number} t - input value
33+
* @param {PositiveNumber} k - degrees of freedom
34+
* @returns {number} evaluated MGF
35+
*
36+
* @example
37+
* var v = mgf( 0.4, 2.0 );
38+
* // returns ~5.0
39+
*
40+
* @example
41+
* var v = mgf( -1.0, 5.0 );
42+
* // returns ~0.0642
43+
*
44+
* @example
45+
* var v = mgf( 0.0, 10.0 );
46+
* // returns 1.0
47+
*
48+
* @example
49+
* var v = mgf( NaN, 5.0 );
50+
* // returns NaN
51+
*/
52+
function mgf( t, k ) {
53+
return addon( t, k );
54+
}
55+
56+
57+
// EXPORTS //
58+
59+
module.exports = mgf;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 mgf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( mgf instanceof Error )
37+
};
38+
39+
40+
// FIXTURES //
41+
42+
var decimalDecimal = require( './fixtures/julia/decimal_decimal.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 mgf, 'function', 'main export is a function' );
50+
t.end();
51+
});
52+
53+
tape( 'if provided `NaN` for any parameter, the function returns `NaN`', opts, function test( t ) {
54+
var y = mgf( NaN, 1.0 );
55+
t.equal( isnan( y ), true, 'returns NaN' );
56+
y = mgf( 0.0, NaN );
57+
t.equal( isnan( y ), true, 'returns NaN' );
58+
t.end();
59+
});
60+
61+
tape( 'if provided a negative `k`, the function always returns `NaN`', opts, function test( t ) {
62+
var y;
63+
64+
y = mgf( 0.4, -1.0 );
65+
t.equal( isnan( y ), true, 'returns NaN' );
66+
67+
y = mgf( 0.0, -1.0 );
68+
t.equal( isnan( y ), true, 'returns NaN' );
69+
70+
y = mgf( 0.4, NINF );
71+
t.equal( isnan( y ), true, 'returns NaN' );
72+
73+
t.end();
74+
});
75+
76+
tape( 'if provided `t >= 0.5`, the function always returns `NaN`', opts, function test( t ) {
77+
var y;
78+
79+
y = mgf( 0.5, 1.0 );
80+
t.equal( isnan( y ), true, 'returns NaN' );
81+
82+
y = mgf( 1.0, 1.0 );
83+
t.equal( isnan( y ), true, 'returns NaN' );
84+
85+
y = mgf( 10.0, 1.0 );
86+
t.equal( isnan( y ), true, 'returns NaN' );
87+
88+
t.end();
89+
});
90+
91+
tape( 'the function evaluates the MGF for `x` given degrees of freedom `k`', opts, function test( t ) {
92+
var expected;
93+
var delta;
94+
var tol;
95+
var x;
96+
var k;
97+
var y;
98+
var i;
99+
100+
expected = decimalDecimal.expected;
101+
x = decimalDecimal.x;
102+
k = decimalDecimal.k;
103+
for ( i = 0; i < x.length; i++ ) {
104+
y = mgf( x[i], k[i] );
105+
if ( y === expected[i] ) {
106+
t.equal( y, expected[i], 'x: '+x[i]+', k: '+k[i]+', y: '+y+', expected: '+expected[i] );
107+
} else {
108+
delta = abs( y - expected[ i ] );
109+
tol = EPS * abs( expected[ i ] );
110+
t.ok( delta <= tol, 'within tolerance. x: '+x[ i ]+'. k: '+k[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
111+
}
112+
}
113+
t.end();
114+
});

0 commit comments

Comments
 (0)