Skip to content

Commit 12d83d6

Browse files
committed
feat: blas/base/ndarray/sswap
1 parent 19216fd commit 12d83d6

10 files changed

Lines changed: 839 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)