Skip to content

Commit 0758389

Browse files
committed
Auto-generated commit
1 parent b47e24b commit 0758389

File tree

6 files changed

+671
-0
lines changed

6 files changed

+671
-0
lines changed

complex64/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,88 @@ var count = context.count;
20982098
// returns 2
20992099
```
21002100

2101+
<a name="method-sort"></a>
2102+
2103+
#### Complex64Array.prototype.sort( compareFcn )
2104+
2105+
Sorts an array in-place.
2106+
2107+
```javascript
2108+
var realf = require( '@stdlib/complex/realf' );
2109+
var imagf = require( '@stdlib/complex/imagf' );
2110+
2111+
function compare( a, b ) {
2112+
var re1;
2113+
var re2;
2114+
var im1;
2115+
var im2;
2116+
re1 = realf( a );
2117+
re2 = realf( b );
2118+
if ( re1 < re2 ) {
2119+
return -1;
2120+
}
2121+
if ( re1 > re2 ) {
2122+
return 1;
2123+
}
2124+
im1 = imagf( a );
2125+
im2 = imagf( b );
2126+
if ( im1 < im2 ) {
2127+
return -1;
2128+
}
2129+
if ( im1 > im2 ) {
2130+
return 1;
2131+
}
2132+
return 0;
2133+
}
2134+
2135+
var arr = new Complex64Array( 3 );
2136+
2137+
arr.set( [ 3.0, -3.0 ], 0 );
2138+
arr.set( [ 1.0, -1.0 ], 1 );
2139+
arr.set( [ 2.0, -2.0 ], 2 );
2140+
2141+
var out = arr.sort( compare );
2142+
// returns <Complex64Array>
2143+
2144+
var z = out.get( 0 );
2145+
// returns <Complex64>
2146+
2147+
var re = realf( z );
2148+
// returns 1.0
2149+
2150+
var im = imagf( z );
2151+
// returns -1.0
2152+
2153+
z = out.get( 1 );
2154+
// returns <Complex64>
2155+
2156+
re = realf( z );
2157+
// returns 2.0
2158+
2159+
im = imagf( z );
2160+
// returns -2.0
2161+
2162+
z = out.get( 2 );
2163+
// returns <Complex64>
2164+
2165+
re = realf( z );
2166+
// returns 3.0
2167+
2168+
im = imagf( z );
2169+
// returns -3.0
2170+
```
2171+
2172+
The `compareFcn` determines the order of the elements. The function is called with the following arguments:
2173+
2174+
- **a**: the first element for comparison.
2175+
- **b**: the second element for comparison.
2176+
2177+
The function should return a number where:
2178+
2179+
- a negative value indicates that `a` should come before `b`.
2180+
- a positive value indicates that `a` should come after `b`.
2181+
- zero or `NaN` indicates that `a` and `b` are considered equal.
2182+
21012183
<a name="method-subarray"></a>
21022184

21032185
#### Complex64Array.prototype.subarray( \[begin\[, end]] )
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' );
24+
var isComplex64Array = require( '@stdlib/assert/is-complex64array' );
25+
var realf = require( '@stdlib/complex/realf' );
26+
var imagf = require( '@stdlib/complex/imagf' );
27+
var pkg = require( './../package.json' ).name;
28+
var Complex64Array = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Comparison function.
35+
*
36+
* @private
37+
* @param {Complex64} a - first value for comparison
38+
* @param {Complex64} 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 = realf( a );
47+
re2 = realf( b );
48+
if ( re1 < re2 ) {
49+
return -1;
50+
}
51+
if ( re1 > re2 ) {
52+
return 1;
53+
}
54+
im1 = imagf( a );
55+
im2 = imagf( 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+':sort', function benchmark( b ) {
69+
var out;
70+
var arr;
71+
var i;
72+
73+
arr = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
out = arr.sort( compareFcn );
78+
if ( typeof out !== 'object' ) {
79+
b.fail( 'should return an object' );
80+
}
81+
}
82+
b.toc();
83+
if ( !isComplex64Array( out ) ) {
84+
b.fail( 'should return a Complex64Array' );
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' );
24+
var isComplex64Array = require( '@stdlib/assert/is-complex64array' );
25+
var realf = require( '@stdlib/complex/realf' );
26+
var imagf = require( '@stdlib/complex/imagf' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var Complex64 = require( '@stdlib/complex/float32' );
29+
var pkg = require( './../package.json' ).name;
30+
var Complex64Array = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Comparison function.
37+
*
38+
* @private
39+
* @param {Complex64} a - first value for comparison
40+
* @param {Complex64} 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 = realf( a );
49+
re2 = realf( b );
50+
if ( re1 < re2 ) {
51+
return -1;
52+
}
53+
if ( re1 > re2 ) {
54+
return 1;
55+
}
56+
im1 = imagf( a );
57+
im2 = imagf( 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 Complex64( i, i ) );
81+
}
82+
arr = new Complex64Array( 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.sort( compareFcn );
99+
if ( typeof out !== 'object' ) {
100+
b.fail( 'should return an object' );
101+
}
102+
}
103+
b.toc();
104+
if ( !isComplex64Array( out ) ) {
105+
b.fail( 'should return a Complex64Array' );
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+':sort:len='+len, f );
134+
}
135+
}
136+
137+
main();

0 commit comments

Comments
 (0)