Skip to content

Commit 1affa4e

Browse files
authored
feat: add blas/base/ndarray/cswap
PR-URL: #11894 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent ec1d66c commit 1affa4e

10 files changed

Lines changed: 1012 additions & 0 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# cswap
22+
23+
> Interchange two one-dimensional complex single-precision floating-point ndarrays.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var cswap = require( '@stdlib/blas/base/ndarray/cswap' );
37+
```
38+
39+
#### cswap( arrays )
40+
41+
Interchanges two one-dimensional complex single-precision floating-point ndarrays.
42+
43+
```javascript
44+
var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
45+
46+
var x = new Complex64Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
47+
var y = new Complex64Vector( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
48+
49+
var z = cswap( [ x, y ] );
50+
// x => <ndarray>[ <Complex64>[ 7.0, 8.0 ], <Complex64>[ 9.0, 10.0 ], <Complex64>[ 11.0, 12.0 ] ]
51+
// y => <ndarray>[ <Complex64>[ 1.0, 2.0 ], <Complex64>[ 3.0, 4.0 ], <Complex64>[ 5.0, 6.0 ] ]
52+
53+
var bool = ( z === y );
54+
// returns true
55+
```
56+
57+
The function has the following parameters:
58+
59+
- **arrays**: array-like object containing the following ndarrays:
60+
61+
- first one-dimensional input ndarray.
62+
- second one-dimensional input ndarray.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="notes">
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
```javascript
81+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
82+
var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
83+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
84+
var cswap = require( '@stdlib/blas/base/ndarray/cswap' );
85+
86+
var opts = {
87+
'dtype': 'float32'
88+
};
89+
90+
var x = new Complex64Vector( discreteUniform( 10, 0, 100, opts ) );
91+
console.log( ndarray2array( x ) );
92+
93+
var y = new Complex64Vector( discreteUniform( 10, 0, 100, opts ) );
94+
console.log( ndarray2array( y ) );
95+
96+
var out = cswap( [ x, y ] );
97+
console.log( ndarray2array( x ) );
98+
console.log( ndarray2array( out ) );
99+
```
100+
101+
</section>
102+
103+
<!-- /.examples -->
104+
105+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
106+
107+
<section class="related">
108+
109+
</section>
110+
111+
<!-- /.related -->
112+
113+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="links">
116+
117+
</section>
118+
119+
<!-- /.links -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var realf = require( '@stdlib/complex/float32/real' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var cswap = require( './../lib' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Creates a benchmark function.
38+
*
39+
* @private
40+
* @param {PositiveInteger} len - array length
41+
* @returns {Function} benchmark function
42+
*/
43+
function createBenchmark( len ) {
44+
var xbuf;
45+
var ybuf;
46+
var x;
47+
var y;
48+
49+
xbuf = uniform( len*2, -100.0, 100.0, {
50+
'dtype': 'float32'
51+
});
52+
x = new Complex64Vector( xbuf.buffer );
53+
54+
ybuf = uniform( len*2, -100.0, 100.0, {
55+
'dtype': 'float32'
56+
});
57+
y = new Complex64Vector( ybuf.buffer );
58+
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var z;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
z = cswap( [ x, y ] );
74+
if ( typeof z !== 'object' ) {
75+
b.fail( 'should return an ndarray' );
76+
}
77+
}
78+
b.toc();
79+
if ( isnanf( realf( z.get( i%len ) ) ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( format( '%s:len=%d', pkg, len ), f );
109+
}
110+
}
111+
112+
main();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( arrays )
3+
Interchanges two one-dimensional complex single-precision floating-point
4+
ndarrays.
5+
6+
If provided an empty input ndarray, the function returns the output ndarray
7+
unchanged.
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing the following ndarrays:
13+
14+
- first one-dimensional input ndarray.
15+
- second one-dimensional input ndarray.
16+
17+
Returns
18+
-------
19+
out: ndarray
20+
The second input ndarray.
21+
22+
Examples
23+
--------
24+
> var x = new {{alias:@stdlib/ndarray/vector/complex64}}( [ 4.0, 2.0, -3.0, 5.0 ] );
25+
> var y = new {{alias:@stdlib/ndarray/vector/complex64}}( [ 0.0, 0.0, 0.0, 0.0 ] );
26+
27+
> {{alias}}( [ x, y ] );
28+
> x
29+
<ndarray>[ <Complex64>[ 0.0, 0.0 ], <Complex64>[ 0.0, 0.0 ] ]
30+
> y
31+
<ndarray>[ <Complex64>[ 4.0, 2.0 ], <Complex64>[ -3.0, 5.0 ] ]
32+
33+
See Also
34+
--------
35+
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) 2026 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+
/// <reference types="@stdlib/types"/>
22+
23+
import { complex64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Interchanges two one-dimensional complex single-precision floating-point ndarrays.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - first one-dimensional input ndarray.
33+
* - second one-dimensional input ndarray.
34+
*
35+
* @param arrays - array-like object containing ndarrays
36+
* @returns second input ndarray
37+
*
38+
* @example
39+
* var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
40+
*
41+
* var x = new Complex64Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
42+
* var y = new Complex64Vector( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
43+
*
44+
* var z = cswap( [ x, y ] );
45+
* // x => <ndarray>[ <Complex64>[ 7.0, 8.0 ], <Complex64>[ 9.0, 10.0 ], <Complex64>[ 11.0, 12.0 ] ]
46+
* // y => <ndarray>[ <Complex64>[ 1.0, 2.0 ], <Complex64>[ 3.0, 4.0 ], <Complex64>[ 5.0, 6.0 ] ]
47+
*
48+
* var bool = ( z === y );
49+
* // returns true
50+
*/
51+
declare function cswap( arrays: [ complex64ndarray, complex64ndarray ] ): complex64ndarray;
52+
53+
54+
// EXPORTS //
55+
56+
export = cswap;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import cswap = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'complex64'
31+
});
32+
const y = zeros( [ 10 ], {
33+
'dtype': 'complex64'
34+
});
35+
36+
cswap( [ x, y ] ); // $ExpectType complex64ndarray
37+
}
38+
39+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
40+
{
41+
cswap( '10' ); // $ExpectError
42+
cswap( 10 ); // $ExpectError
43+
cswap( true ); // $ExpectError
44+
cswap( false ); // $ExpectError
45+
cswap( null ); // $ExpectError
46+
cswap( undefined ); // $ExpectError
47+
cswap( [] ); // $ExpectError
48+
cswap( {} ); // $ExpectError
49+
cswap( ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
const x = zeros( [ 10 ], {
55+
'dtype': 'complex64'
56+
});
57+
const y = zeros( [ 10 ], {
58+
'dtype': 'complex64'
59+
});
60+
61+
cswap(); // $ExpectError
62+
cswap( [ x, y ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)