Skip to content

Commit 6ac3ace

Browse files
committed
feat: add examples tests benchmarks
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 924df55 commit 6ac3ace

File tree

7 files changed

+371
-0
lines changed

7 files changed

+371
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var pkg = require( './../package.json' ).name;
27+
var dlapy2 = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var len;
34+
var x;
35+
var y;
36+
var z;
37+
var i;
38+
39+
len = 100;
40+
x = uniform( len, -50, 50 );
41+
y = uniform( len, -50, 50 );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
z = dlapy2( x[ i % len ], y[ i % len ] );
46+
if ( isnan( z ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
}
50+
b.toc();
51+
if ( isnan( z ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var logEachMap = require( '@stdlib/console/log-each-map' );
23+
var dlapy2 = require( './../lib' );
24+
25+
var opts = {
26+
'dtype': 'float64'
27+
};
28+
var x = discreteUniform( 100, -50, 50, opts );
29+
var y = discreteUniform( 100, -50, 50, opts );
30+
31+
logEachMap( 'dlapy2( %d, %d ) = %0.4f', x, y, dlapy2 );
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"name": "@stdlib/lapack/base/dlapy2",
3+
"version": "0.0.0",
4+
"description": "LAPACK routine to calculate sqrt( x^2 + y^2 ) in a manner which doesn't cause unnecessary overflow.",
5+
"license": "Apache-2.0",
6+
"author": {
7+
"name": "The Stdlib Authors",
8+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9+
},
10+
"contributors": [
11+
{
12+
"name": "The Stdlib Authors",
13+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14+
}
15+
],
16+
"main": "./lib",
17+
"directories": {
18+
"benchmark": "./benchmark",
19+
"doc": "./docs",
20+
"example": "./examples",
21+
"lib": "./lib",
22+
"test": "./test"
23+
},
24+
"types": "./docs/types",
25+
"scripts": {},
26+
"homepage": "https://github.com/stdlib-js/stdlib",
27+
"repository": {
28+
"type": "git",
29+
"url": "git://github.com/stdlib-js/stdlib.git"
30+
},
31+
"bugs": {
32+
"url": "https://github.com/stdlib-js/stdlib/issues"
33+
},
34+
"dependencies": {},
35+
"devDependencies": {},
36+
"engines": {
37+
"node": ">=0.10.0",
38+
"npm": ">2.7.0"
39+
},
40+
"os": [
41+
"aix",
42+
"darwin",
43+
"freebsd",
44+
"linux",
45+
"macos",
46+
"openbsd",
47+
"sunos",
48+
"win32",
49+
"windows"
50+
],
51+
"keywords": [
52+
"stdlib",
53+
"stdmath",
54+
"mathematics",
55+
"math",
56+
"lapack",
57+
"dlapy2",
58+
"copy",
59+
"linear",
60+
"algebra",
61+
"subroutines",
62+
"array",
63+
"ndarray",
64+
"matrix",
65+
"float64",
66+
"double",
67+
"float64array"
68+
]
69+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
julia 1.5
2+
JSON 0.21

lib/node_modules/@stdlib/lapack/base/dlapy2/test/fixtures/julia/data.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env julia
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2025 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
import JSON
20+
21+
"""
22+
gen( x, y, name )
23+
24+
Generate fixture data and write to file.
25+
26+
# Arguments
27+
28+
* `x`: x-values
29+
* `y`: y-values
30+
* `name::AbstractString`: output filename
31+
32+
# Examples
33+
34+
``` julia
35+
julia> x = rand( 2003 ) .* 1000.0;
36+
julia> y = rand( 2003 ) .* 1000.0;
37+
julia> gen( x, y, \"data.json\" );
38+
```
39+
"""
40+
function gen( x, y, name )
41+
h = hypot.( x, y );
42+
43+
# Store data to be written to file as a collection:
44+
data = Dict([
45+
("x", x),
46+
("y", y),
47+
("expected", h)
48+
]);
49+
50+
# Based on the script directory, create an output filepath:
51+
filepath = joinpath( dir, name );
52+
53+
# Write the data to the output filepath as JSON:
54+
outfile = open( filepath, "w" );
55+
write( outfile, JSON.json(data) );
56+
write( outfile, "\n" );
57+
close( outfile );
58+
end
59+
60+
# Get the filename:
61+
file = @__FILE__;
62+
63+
# Extract the directory in which this file resides:
64+
dir = dirname( file );
65+
66+
# Generate fixture data:
67+
x = rand( 2003 ) .* 2000.0 .- 1000.0;
68+
y = rand( 2003 ) .* 2000.0 .- 1000.0;
69+
gen( x, y, "data.json" );
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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 tape = require( 'tape' );
24+
var PINF = require( '@stdlib/constants/float64/pinf' );
25+
var EPS = require( '@stdlib/constants/float64/eps' );
26+
var abs = require( '@stdlib/math/base/special/abs' );
27+
var sqrt = require( '@stdlib/math/base/special/sqrt' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
30+
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
31+
var dlapy2 = require( './../lib' );
32+
33+
34+
// FIXTURES //
35+
36+
var data = require( './fixtures/julia/data.json' );
37+
38+
39+
// TESTS //
40+
41+
tape( 'main export is a function', function test( t ) {
42+
t.ok( true, __filename );
43+
t.strictEqual( typeof dlapy2, 'function', 'main export is a function' );
44+
t.end();
45+
});
46+
47+
tape( 'the function returns `NaN` if either argument is `NaN` but not `+-infinity`', function test( t ) {
48+
var h;
49+
50+
h = dlapy2( NaN, 3.14 );
51+
t.strictEqual( isnan( h ), true, 'returns expected value' );
52+
53+
h = dlapy2( 3.14, NaN );
54+
t.strictEqual( isnan( h ), true, 'returns expected value' );
55+
56+
h = dlapy2( NaN, NaN );
57+
t.strictEqual( isnan( h ), true, 'returns expected value' );
58+
59+
t.end();
60+
});
61+
62+
tape( 'the function returns `+0` if both arguments are `+-0`', function test( t ) {
63+
var h;
64+
65+
h = dlapy2( +0.0, +0.0 );
66+
t.strictEqual( isPositiveZero( h ), true, 'returns expected value' );
67+
68+
h = dlapy2( -0.0, +0.0 );
69+
t.strictEqual( isPositiveZero( h ), true, 'returns expected value' );
70+
71+
h = dlapy2( +0.0, -0.0 );
72+
t.strictEqual( isPositiveZero( h ), true, 'returns expected value' );
73+
74+
h = dlapy2( -0.0, -0.0 );
75+
t.strictEqual( isPositiveZero( h ), true, 'returns expected value' );
76+
77+
t.end();
78+
});
79+
80+
tape( 'the function computes the hypotenuse', function test( t ) {
81+
var expected;
82+
var delta;
83+
var tol;
84+
var h;
85+
var x;
86+
var y;
87+
var i;
88+
89+
x = data.x;
90+
y = data.y;
91+
expected = data.expected;
92+
93+
for ( i = 0; i < x.length; i++ ) {
94+
h = dlapy2( x[ i ], y[ i ] );
95+
if ( h === expected[ i ] ) {
96+
t.ok( true, 'x: '+x[i]+'. y: '+y[i]+'. h: '+h+'. Expected: '+expected[i]+'.' );
97+
} else {
98+
delta = abs( h - expected[ i ] );
99+
tol = 1.5 * EPS * abs( expected[ i ] );
100+
t.strictEqual( delta <= tol, true, 'within tolerance. x: '+x[i]+'. y: '+y[i]+'. h: '+h+'. Expected: '+expected[i]+'. Delta: '+delta+'. Tol: '+tol+'.' );
101+
}
102+
}
103+
t.end();
104+
});
105+
106+
tape( 'the function computes the hypotenuse (canonical inputs)', function test( t ) {
107+
var h;
108+
109+
h = dlapy2( 3.0, 4.0 );
110+
t.strictEqual( h, 5.0, 'returns expected value' );
111+
112+
h = dlapy2( 6.0, 8.0 );
113+
t.strictEqual( h, 10.0, 'returns expected value' );
114+
115+
h = dlapy2( 5.0, 12.0 );
116+
t.strictEqual( h, 13.0, 'returns expected value' );
117+
118+
t.end();
119+
});
120+
121+
tape( 'the function avoids overflow', function test( t ) {
122+
var h;
123+
124+
h = sqrt( pow( 1.0e308, 2 ) + pow( 1.0e308, 2 ) );
125+
t.strictEqual( h, PINF, 'returns expected value' );
126+
127+
h = dlapy2( 1.0e308, 1.0e308 );
128+
t.strictEqual( h, 1.4142135623730951e308, 'avoids overflow' );
129+
130+
t.end();
131+
});
132+
133+
tape( 'the function avoids underflow', function test( t ) {
134+
var h;
135+
136+
h = sqrt( pow( 1.0e-200, 2 ) + pow( 1e-200, 2 ) );
137+
t.strictEqual( h, 0.0, 'returns 0' );
138+
139+
h = dlapy2( 1.0e-200, 1.0e-200 );
140+
t.strictEqual( h, 1.414213562373095e-200, 'avoids underflow' );
141+
142+
t.end();
143+
});

0 commit comments

Comments
 (0)