Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0d6484

Browse files
feat: add assert/is-almost-equal-complex128array
PR-URL: #7687 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 2037f1e commit f0d6484

File tree

10 files changed

+724
-0
lines changed

10 files changed

+724
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
# isAlmostEqualComplex128Array
22+
23+
> Test if two arguments are both [Complex128Arrays][@stdlib/array/complex128] and contain respective elements which are [approximately equal][@stdlib/assert/is-almost-equal] within a specified number of ULPs (units in the last place).
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
<!-- eslint-disable id-length -->
30+
31+
```javascript
32+
var isAlmostEqualComplex128Array = require( '@stdlib/assert/is-almost-equal-complex128array' );
33+
```
34+
35+
#### isAlmostEqualComplex128Array( v1, v2, maxULP )
36+
37+
Tests if two arguments are both [Complex128Arrays][@stdlib/array/complex128] and contain respective elements which are [approximately equal][@stdlib/assert/is-almost-equal] within a specified number of ULPs (units in the last place).
38+
39+
<!-- eslint-disable id-length -->
40+
41+
```javascript
42+
var EPS = require( '@stdlib/constants/float64/eps' );
43+
var Complex128Array = require( '@stdlib/array/complex128' );
44+
45+
var x = new Complex128Array( [ 1.0, 2.0 ] );
46+
var y = new Complex128Array( [ 1.0+EPS, 2.0 ] );
47+
48+
var bool = isAlmostEqualComplex128Array( x, y, 0 );
49+
// returns false
50+
51+
bool = isAlmostEqualComplex128Array( x, y, 1 );
52+
// returns true
53+
54+
bool = isAlmostEqualComplex128Array( x, [ 1.0, 2.0 ], 1 );
55+
// returns false
56+
```
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<section class="notes">
63+
64+
## Notes
65+
66+
- The function returns `false` if either input value is a `Complex128Array` containing `NaN`.
67+
- The function does not distinguish between `-0` and `+0`, treating them as equal.
68+
69+
</section>
70+
71+
<!-- /.notes -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
<!-- eslint-disable id-length -->
80+
81+
```javascript
82+
var Complex128Array = require( '@stdlib/array/complex128' );
83+
var isAlmostEqualComplex128Array = require( '@stdlib/assert/is-almost-equal-complex128array' );
84+
85+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] );
86+
var y = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] );
87+
var out = isAlmostEqualComplex128Array( x, y, 0 );
88+
console.log( out );
89+
// => true
90+
91+
x = new Complex128Array( [ -0.0, 0.0, -0.0, 0.0 ] );
92+
y = new Complex128Array( [ 0.0, -0.0, 0.0, -0.0 ] );
93+
out = isAlmostEqualComplex128Array( x, y, 1 );
94+
console.log( out );
95+
// => true
96+
97+
x = new Complex128Array( [ NaN, NaN, NaN, NaN ] );
98+
y = new Complex128Array( [ NaN, NaN, NaN, NaN ] );
99+
out = isAlmostEqualComplex128Array( x, y, 0 );
100+
console.log( out );
101+
// => false
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
109+
110+
<section class="related">
111+
112+
</section>
113+
114+
<!-- /.related -->
115+
116+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
117+
118+
<section class="links">
119+
120+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
121+
122+
[@stdlib/assert/is-almost-equal]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-almost-equal
123+
124+
</section>
125+
126+
<!-- /.links -->
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var Complex128Array = require( '@stdlib/array/complex128' );
28+
var pkg = require( './../package.json' ).name;
29+
var isAlmostEqualComplex128Array = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x = new Complex128Array( zeroTo( len*2 ) );
43+
var y = new Complex128Array( zeroTo( len*2 ) );
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var bool;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
bool = isAlmostEqualComplex128Array( x, y, 1 );
59+
if ( typeof bool !== 'boolean' ) {
60+
b.fail( 'should return a boolean' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isBoolean( bool ) ) {
65+
b.fail( 'should return a boolean' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( pkg+':len='+len, f );
94+
}
95+
}
96+
97+
main();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( v1, v2, maxULP )
3+
Tests if two arguments are both Complex128Arrays and contain respective
4+
elements which are approximately equal within a specified number of ULPs
5+
(units in the last place).
6+
7+
The function returns `false` if either input value is a `Complex128Array`
8+
containing `NaN`.
9+
10+
The function does not distinguish between `-0` and `+0`, treating them as
11+
equal.
12+
13+
Parameters
14+
----------
15+
v1: any
16+
First input value.
17+
18+
v2: any
19+
Second input value.
20+
21+
maxULP: integer
22+
Maximum allowed ULP difference.
23+
24+
Returns
25+
-------
26+
bool: boolean
27+
Boolean indicating whether two arguments are approximately equal.
28+
29+
Examples
30+
--------
31+
> var x = new {{alias:@stdlib/array/complex128}}( [ 1.0, 2.0, 3.0, 4.0 ] );
32+
> var y = new {{alias:@stdlib/array/complex128}}( [ 1.0, 2.0, 3.0, 4.0 ] );
33+
> var bool = {{alias}}( x, y, 0 )
34+
true
35+
36+
> x = new {{alias:@stdlib/array/complex128}}( [ NaN, NaN, NaN, NaN ] );
37+
> y = new {{alias:@stdlib/array/complex128}}( [ NaN, NaN, NaN, NaN ] );
38+
> bool = {{alias}}( x, y, 1 )
39+
false
40+
41+
See Also
42+
--------
43+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
* Tests if two arguments are both Complex128Arrays and contain respective elements which are approximately equal within a specified number of ULPs (units in the last place).
23+
*
24+
* ## Notes
25+
*
26+
* - The function returns `false` if either input value is a `Complex128Array` containing `NaN`.
27+
* - The function does not distinguish between `-0` and `+0`, treating them as equal.
28+
*
29+
* @param v1 - first input value
30+
* @param v2 - second input value
31+
* @param maxULP - maximum allowed ULP difference
32+
* @returns boolean indicating whether two arguments are approximately equal
33+
*
34+
* @example
35+
* var Complex128Array = require( '@stdlib/array/complex128' );
36+
*
37+
* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] );
38+
* var y = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] );
39+
*
40+
* var out = isAlmostEqualComplex128Array( x, y, 0 );
41+
* // returns true
42+
*
43+
* @example
44+
* var Complex128Array = require( '@stdlib/array/complex128' );
45+
*
46+
* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] );
47+
* var y = new Complex128Array( [ 1.0, 2.0, 4.0, 4.0 ] );
48+
*
49+
* var out = isAlmostEqualComplex128Array( x, y, 1 );
50+
* // returns false
51+
*/
52+
declare function isAlmostEqualComplex128Array( v1: any, v2: any, maxULP: number ): boolean;
53+
54+
55+
// EXPORTS //
56+
57+
export = isAlmostEqualComplex128Array;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 isAlmostEqualComplex128Array = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isAlmostEqualComplex128Array( 3.14, 3.14, 1 ); // $ExpectType boolean
27+
isAlmostEqualComplex128Array( null, null, 1 ); // $ExpectType boolean
28+
isAlmostEqualComplex128Array( 'beep', 'boop', 1 ); // $ExpectType boolean
29+
}
30+
31+
// The compiler throws an error if the function is provided a third argument which is not a number...
32+
{
33+
isAlmostEqualComplex128Array( 3.14, 3.14, '1' ); // $ExpectError
34+
isAlmostEqualComplex128Array( 3.14, 3.14, true ); // $ExpectError
35+
isAlmostEqualComplex128Array( 3.14, 3.14, false ); // $ExpectError
36+
isAlmostEqualComplex128Array( 3.14, 3.14, null ); // $ExpectError
37+
isAlmostEqualComplex128Array( 3.14, 3.14, undefined ); // $ExpectError
38+
isAlmostEqualComplex128Array( 3.14, 3.14, [] ); // $ExpectError
39+
isAlmostEqualComplex128Array( 3.14, 3.14, {} ); // $ExpectError
40+
isAlmostEqualComplex128Array( 3.14, 3.14, ( x: number ): number => x ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided an unsupported number of arguments...
44+
{
45+
isAlmostEqualComplex128Array(); // $ExpectError
46+
isAlmostEqualComplex128Array( 3.14 ); // $ExpectError
47+
isAlmostEqualComplex128Array( 3.14, 3.14 ); // $ExpectError
48+
isAlmostEqualComplex128Array( 'beep', 'beep', 2, {} ); // $ExpectError
49+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /