8000 Auto-generated commit · stdlib-js/array-complex128@8957a55 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8957a55

Browse files
committed
Auto-generated commit
1 parent b866471 commit 8957a55

File tree

8 files changed

+663
-8
lines changed

8 files changed

+663
-8
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,88 @@ im = imag( z );
23682368
// returns 1.0
23692369
```
23702370

2371+
<a name="method-to-sorted"></a>
2372+
2373+
#### Complex128Array.prototype.toSorted( compareFcn )
2374+
2375+
Returns a new typed array containing the elements in sorted order.
2376+
2377+
```javascript
2378+
var real = require( '@stdlib/complex-real' );
2379+
var imag = require( '@stdlib/complex-imag' );
2380+
2381+
function compare( a, b ) {
2382+
var re1;
2383+
var re2;
2384+
var im1;
2385+
var im2;
2386+
re1 = real( a );
2387+
re2 = real( b );
2388+
if ( re1 < re2 ) {
2389+
return -1;
2390+
}
2391+
if ( re1 > re2 ) {
2392+
return 1;
2393+
}
2394+
im1 = imag( a );
2395+
im2 = imag( b );
2396+
if ( im1 < im2 ) {
2397+
return -1;
2398+
}
2399+
if ( im1 > im2 ) {
2400+
return 1;
2401+
}
2402+
return 0;
2403+
}
2404+
2405+
var arr = new Complex128Array( 3 );
2406+
2407+
arr.set( [ 3.0, -3.0 ], 0 );
2408+
arr.set( [ 1.0, -1.0 ], 1 );
2409+
arr.set( [ 2.0, -2.0 ], 2 );
2410+
2411+
var out = arr.toSorted( compare );
2412+
// returns <Complex128Array>
2413+
2414+
var z = out.get( 0 );
2415+
// returns <Complex128>
2416+
2417+
var re = real( z );
2418+
// returns 1.0
2419+
2420+
var im = imag( z );
2421+
// returns -1.0
2422+
2423+
z = out.get( 1 );
2424+
// returns <Complex128>
2425+
2426+
re = real( z );
2427+
// returns 2.0
2428+
2429+
im = imag( z );
2430+
// returns -2.0
2431+
2432+
z = out.get( 2 );
2433+
// returns <Complex128>
2434+
2435+
re = real( z );
2436+
// returns 3.0
2437+
2438+
im = imag( z );
2439+
// returns -3.0
2440+
```
2441+
2442+
The `compareFcn` determines the order of the elements. The function is called with the following arguments:
2443+
2444+
- **a**: the first element for comparison.
2445+
- **b**: the second element for comparison.
2446+
2447+
The function should return a number where:
2448+
2449+
- a negative value indicates that `a` should come before `b`.
2450+
- a positive value indicates that `a` should come after `b`.
2451+
- zero or `NaN` indicates that `a` and `b` are considered equal.
2452+
23712453
<a name="method-to-string"></a>
23722454

23732455
#### Complex128Array.prototype.toString()

benchmark/benchmark.to_sorted.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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-harness' );
24+
var isComplex128Array = require( '@stdlib/assert-is-complex128array' );
25+
var real = require( '@stdlib/complex-real' );
26+
var imag = require( '@stdlib/complex-imag' );
27+
var pkg = require( './../package.json' ).name;
28+
var Complex128Array = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Comparison function.
35+
*
36+
* @private
37+
* @param {Complex128} a - first value for comparison
38+
* @param {Complex128} b - second value for comparison
39+
* @returns {number} comparison result
40+
*/
41+
function compareFcn( a, b ) {
42+
var re1;
43+
var re2;
44+
var im1;
45+
var im2;
46+
re1 = real( a );
47+
re2 = real( b );
48+
if ( re1 < re2 ) {
49+
return -1;
50+
}
51+
if ( re1 > re2 ) {
52+
return 1;
53+
}
54+
im1 = imag( a );
55+
im2 = imag( b );
56+
if ( im1 < im2 ) {
57+
return -1;
58+
}
59+
if ( im1 > im2 ) {
60+
return 1;
61+
}
62+
return 0;
63+
}
64+
65+
66+
// MAIN //
67+
68+
bench( pkg+':toSorted', function benchmark( b ) {
69+
var out;
70+
var arr;
71+
var i;
72+
73+
arr = new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] );
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
out = arr.toSorted( compareFcn );
78+
if ( typeof out !== 'object' ) {
79+
b.fail( 'should return an object' );
80+
}
81+
}
82+
b.toc();
83+
if ( !isComplex128Array( out ) ) {
84+
b.fail( 'should return a Complex128Array' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
});
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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-harness' );
24+
var isComplex128Array = require( '@stdlib/assert-is-complex128array' );
25+
var real = require( '@stdlib/complex-real' );
26+
var imag = require( '@stdlib/complex-imag' );
27+
var pow = require( '@stdlib/math-base-special-pow' );
28+
var Complex128 = require( '@stdlib/complex-float64' );
29+
var pkg = require( './../package.json' ).name;
30+
var Complex128Array = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Comparison function.
37+
*
38+
* @private
39+
* @param {Complex128} a - first value for comparison
40+
* @param {Complex128} b - second value for comparison
41+
* @returns {number} comparison result
42+
*/
43+
function compareFcn( a, b ) {
44+
var re1;
45+
var re2;
46+
var im1;
47+
var im2;
48+
re1 = real( a );
49+
re2 = real( b );
50+
if ( re1 < re2 ) {
51+
return -1;
52+
}
53+
if ( re1 > re2 ) {
54+
return 1;
55+
}
56+
im1 = imag( a );
57+
im2 = imag( b );
58+
if ( im1 < im2 ) {
59+
return -1;
60+
}
61+
if ( im1 > im2 ) {
62+
return 1;
63+
}
64+
return 0;
65+
}
66+
67+
/**
68+
* Creates a benchmark function.
69+
*
70+
* @private
71+
* @param {PositiveInteger} len - array length
72+
* @returns {Function} benchmark function
73+
*/
74+
function createBenchmark( len ) {
75+
var arr;
76+
var i;
77+
78+
arr = [];
79+
for ( i = 0; i < len; i++ ) {
80+
arr.push( new Complex128( i, i ) );
81+
}
82+
arr = new Complex128Array( arr );
83+
84+
return benchmark;
85+
86+
/**
87+
* Benchmark function.
88+
*
89+
* @private
90+
* @param {Benchmark} b - benchmark instance
91+
*/
92+
function benchmark( b ) {
93+
var out;
94+
var i;
95+
96+
b.tic();
97+
for ( i = 0; i < b.iterations; i++ ) {
98+
out = arr.toSorted( compareFcn );
99+
if ( typeof out !== 'object' ) {
100+
b.fail( 'should return an object' );
101+
}
102+
}
103+
b.toc();
104+
if ( !isComplex128Array( out ) ) {
105+
b.fail( 'should return a Complex128Array' );
106+
}
107+
b.pass( 'benchmark finished' );
108+
b.end();
109+
}
110+
}
111+
112+
113+
// MAIN //
114+
115+
/**
116+
* Main execution sequence.
117+
*
118+
* @private
119+
*/
120+
function main() {
121+
var len;
122+
var min;
123+
var max;
124+
var f;
125+
var i;
126+
127+
min = 1; // 10^min
128+
max = 6; // 10^max
129+
130+
for ( i = min; i <= max; i++ ) {
131+
len = pow( 10, i );
132+
f = createBenchmark( len );
133+
bench( pkg+':toSorted:len='+len, f );
134+
}
135+
}
136+
137+
main();

dist/index.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/types/index.d.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,78 @@ declare class Complex128Array implements Complex128ArrayInterface {
13751375
*/
13761376
toReversed(): Complex128Array;
13771377

1378+
/**
1379+
* Returns a new typed array containing the elements in sorted order.
1380+
*
1381+
* @param compareFcn - comparison function
1382+
* @returns sorted array
1383+
*
1384+
* @example
1385+
* var real = require( '@stdlib/complex-real' );
1386+
* var imag = require( '@stdlib/complex-imag' );
1387+
*
1388+
* function compare( a, b ) {
1389+
* var re1;
1390+
* var re2;
1391+
* var im1;
1392+
* var im2;
1393+
* re1 = real( a );
1394+
* re2 = real( b );
1395+
* if ( re1 < re2 ) {
1396+
* return -1;
1397+
* }
1398+
* if ( re1 > re2 ) {
1399+
* return 1;
1400+
* }
1401+
* im1 = imag( a );
1402+
* im2 = imag( b );
1403+
* if ( im1 < im2 ) {
1404+
* return -1;
1405+
* }
1406+
* if ( im1 > im2 ) {
1407+
* return 1;
1408+
* }
1409+
* return 0;
1410+
* }
1411+
*
1412+
* var arr = new Complex128Array( 3 );
1413+
*
1414+
* arr.set( [ 3.0, -3.0 ], 0 );
1415+
* arr.set( [ 1.0, -1.0 ], 1 );
1416+
* arr.set( [ 2.0, -2.0 ], 2 );
1417+
*
1418+
* var out = arr.toSorted( compare );
1419+
* // returns <Complex128Array>
1420+
*
1421+
* var z = out.get( 0 );
1422+
* // returns <Complex128>
1423+
*
1424+
* var re = real( z );
1425+
* // returns 1.0
1426+
*
1427+
* var im = imag( z );
1428+
* // returns -1.0
1429+
*
1430+
* z = out.get( 1 );
1431+
* // returns <Complex128>
1432+
*
1433+
* re = real( z );
1434+
* // returns 2.0
1435+
*
1436+
* im = imag( z );
1437+
* // returns -2.0
1438+
*
1439+
* z = out.get( 2 );
1440+
* // returns <Complex128>
1441+
*
1442+
* re = real( z );
1443+
* // returns 3.0
1444+
*
1445+
* im = imag( z );
1446+
* // returns -3.0
1447+
*/
1448+
toSorted( compareFcn: CompareFcn ): Complex128Array;
1449+
13781450
/**
13791451
* Serializes an array as a string.
13801452
*

0 commit comments

Comments
 (0)
0