Skip to content

Commit 224c7be

Browse files
committed
feat: add the JS native files
1 parent 550f864 commit 224c7be

File tree

3 files changed

+217
-0
lines changed

3 files changed

+217
-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 variance = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( variance 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 = variance( 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 variance of a chi distribution.
30+
*
31+
* @private
32+
* @param {NonNegativeNumber} k - degrees of freedom
33+
* @returns {number} variance
34+
*
35+
* @example
36+
* var v = variance( 9.0 );
37+
* // returns ~0.485
38+
*
39+
* @example
40+
* var v = variance( 1.0 );
41+
* // returns ~0.363
42+
*
43+
* @example
44+
* var v = variance( -0.2 );
45+
* // returns NaN
46+
*
47+
* @example
48+
* var v = variance( NaN );
49+
* // returns NaN
50+
*/
51+
function variance( k ) {
52+
return addon( k );
53+
}
54+
55+
56+
// EXPORTS //
57+
58+
module.exports = variance;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 variance = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( variance 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 variance, '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 = variance( 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 nonnegative number, the function returns `NaN`', opts, function test( t ) {
60+
var v;
61+
62+
v = variance( -1.0 );
63+
t.equal( isnan( v ), true, 'returns NaN' );
64+
65+
v = variance( NINF );
66+
t.equal( isnan( v ), true, 'returns NaN' );
67+
68+
t.end();
69+
});
70+
71+
tape( 'the function returns the variance of a chi distribution', opts, function test( t ) {
72+
var expected;
73+
var delta;
74+
var tol;
75+
var k;
76+
var i;
77+
var y;
78+
79+
expected = data.expected;
80+
k = data.k;
81+
for ( i = 0; i < expected.length; i++ ) {
82+
y = variance( k[i] );
83+
if ( y === expected[i] ) {
84+
t.equal( y, expected[i], 'k:'+k[i]+', y: '+y+', expected: '+expected[i] );
85+
} else {
86+
delta = abs( y - expected[ i ] );
87+
tol = 150.0 * EPS * abs( expected[ i ] );
88+
t.ok( delta <= tol, 'within tolerance. k: '+k[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
89+
}
90+
}
91+
t.end();
92+
});

0 commit comments

Comments
 (0)