Skip to content

Commit 962d072

Browse files
aayush0325kgryte
andauthored
feat: add lapack/base/dlapy2
PR-URL: #7120 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent ec8118b commit 962d072

File tree

13 files changed

+772
-0
lines changed

13 files changed

+772
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<!--
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+
-->
20+
21+
# dlapy2
22+
23+
> LAPACK routine to calculate `sqrt(x^2 + y^2)` in a manner which doesn't cause unnecessary overflow.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dlapy2 = require( '@stdlib/lapack/base/dlapy2' );
31+
```
32+
33+
#### dlapy2( x, y )
34+
35+
Computes `sqrt(x^2 + y^2)` in a manner which doesn't cause unnecessary overflow.
36+
37+
```javascript
38+
var out = dlapy2( 3.0, 4.0 );
39+
// returns 5.0
40+
```
41+
42+
The function has the following parameters:
43+
44+
- **x**: first input number.
45+
- **y**: second input number.
46+
47+
</section>
48+
49+
<!-- /.usage -->
50+
51+
<section class="notes">
52+
53+
## Notes
54+
55+
- `dlapy2()` corresponds to the [LAPACK][LAPACK] function [`dlapy2`][lapack-dlapy2].
56+
57+
</section>
58+
59+
<!-- /.notes -->
60+
61+
<section class="examples">
62+
63+
## Examples
64+
65+
<!-- eslint no-undef: "error" -->
66+
67+
```javascript
68+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
69+
var logEachMap = require( '@stdlib/console/log-each-map' );
70+
var dlapy2 = require( '@stdlib/lapack/base/dlapy2' );
71+
72+
var opts = {
73+
'dtype': 'float64'
74+
};
75+
var x = discreteUniform( 100, -50, 50, opts );
76+
var y = discreteUniform( 100, -50, 50, opts );
77+
78+
logEachMap( 'dlapy2( %d, %d ) = %0.4f', x, y, dlapy2 );
79+
```
80+
81+
</section>
82+
83+
<!-- /.examples -->
84+
85+
<!-- C interface documentation. -->
86+
87+
* * *
88+
89+
<section class="c">
90+
91+
## C APIs
92+
93+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
94+
95+
<section class="intro">
96+
97+
</section>
98+
99+
<!-- /.intro -->
100+
101+
<!-- C usage documentation. -->
102+
103+
<section class="usage">
104+
105+
### Usage
106+
107+
```c
108+
TODO
109+
```
110+
111+
#### TODO
112+
113+
TODO.
114+
115+
```c
116+
TODO
117+
```
118+
119+
TODO
120+
121+
```c
122+
TODO
123+
```
124+
125+
</section>
126+
127+
<!-- /.usage -->
128+
129+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
130+
131+
<section class="notes">
132+
133+
</section>
134+
135+
<!-- /.notes -->
136+
137+
<!-- C API usage examples. -->
138+
139+
<section class="examples">
140+
141+
### Examples
142+
143+
```c
144+
TODO
145+
```
146+
147+
</section>
148+
149+
<!-- /.examples -->
150+
151+
</section>
152+
153+
<!-- /.c -->
154+
155+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
156+
157+
<section class="related">
158+
159+
</section>
160+
161+
<!-- /.related -->
162+
163+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
164+
165+
<section class="links">
166+
167+
[lapack]: https://www.netlib.org/lapack/explore-html/
168+
169+
[lapack-dlapy2]: https://www.netlib.org/lapack/explore-html-3.6.1/d7/d43/group__aux_o_t_h_e_rauxiliary_gacf4c47c2f593fb3a4e842bca6df1240b.html
170+
171+
</section>
172+
173+
<!-- /.links -->
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( x, y )
3+
Computes `sqrt(x^2 + y^2)` in a manner which doesn't cause unnecessary
4+
overflow.
5+
6+
Parameters
7+
----------
8+
x: number
9+
First number.
10+
11+
y: number
12+
Second number.
13+
14+
Returns
15+
-------
16+
out: number
17+
Square root of sum of squares.
18+
19+
Examples
20+
--------
21+
> var h = {{alias}}( -5.0, 12.0 )
22+
13.0
23+
> h = {{alias}}( -0.0, -0.0 )
24+
0.0
25+
26+
See Also
27+
--------
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* LAPACK routine to calculate `sqrt(x^2 + y^2)` in a manner which doesn't cause unnecessary overflow.
23+
*
24+
* @param x - first input number
25+
* @param y - second input number
26+
* @returns `sqrt(x^2 + y^2)`
27+
*
28+
* @example
29+
* var h = dlapy2( -5.0, 12.0 );
30+
* // returns 13.0
31+
*
32+
* @example
33+
* var h = dlapy2( -0.0, -0.0 );
34+
* // returns 0.0
35+
*/
36+
declare function dlapy2( x: number, y: number ): number;
37+
38+
39+
// EXPORTS //
40+
41+
export = dlapy2;
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+
import dlapy2 = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
dlapy2( 8, 2 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided values other than two numbers...
30+
{
31+
dlapy2( true, 3 ); // $ExpectError
32+
dlapy2( false, 2 ); // $ExpectError
33+
dlapy2( '5', 1 ); // $ExpectError
34+
dlapy2( [], 1 ); // $ExpectError
35+
dlapy2( {}, 2 ); // $ExpectError
36+
dlapy2( ( x: number ): number => x, 2 ); // $ExpectError
37+
38+
dlapy2( 9, true ); // $ExpectError
39+
dlapy2( 9, false ); // $ExpectError
40+
dlapy2( 5, '5' ); // $ExpectError
41+
dlapy2( 8, [] ); // $ExpectError
42+
dlapy2( 9, {} ); // $ExpectError
43+
dlapy2( 8, ( x: number ): number => x ); // $ExpectError
44+
45+
dlapy2( [], true ); // $ExpectError
46+
dlapy2( {}, false ); // $ExpectError
47+
dlapy2( false, '5' ); // $ExpectError
48+
dlapy2( {}, [] ); // $ExpectError
49+
dlapy2( '5', ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided insufficient arguments...
53+
{
54+
dlapy2(); // $ExpectError
55+
dlapy2( 3 ); // $ExpectError
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 );

0 commit comments

Comments
 (0)