Skip to content

Commit d00667e

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into philipp/drift-stats-base-dists-chi-2026-04-23
2 parents 55dcad1 + c660c8e commit d00667e

15 files changed

Lines changed: 898 additions & 28 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
# dswap
22+
23+
> Interchange two one-dimensional double-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 dswap = require( '@stdlib/blas/base/ndarray/dswap' );
37+
```
38+
39+
#### dswap( arrays )
40+
41+
Interchanges two one-dimensional double-precision floating-point ndarrays.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
48+
var x = new ndarray( 'float64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
49+
50+
var ybuf = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
51+
var y = new ndarray( 'float64', ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
52+
53+
var z = dswap( [ x, y ] );
54+
// x => <ndarray>[ 6.0, 7.0, 8.0, 9.0, 10.0 ]
55+
// y => <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
56+
57+
var bool = ( y === z );
58+
// returns true
59+
```
60+
61+
The function has the following parameters:
62+
63+
- **arrays**: array-like object containing two input ndarrays.
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<section class="notes">
70+
71+
</section>
72+
73+
<!-- /.notes -->
74+
75+
<section class="examples">
76+
77+
## Examples
78+
79+
<!-- eslint no-undef: "error" -->
80+
81+
```javascript
82+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
83+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
84+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
85+
var dswap = require( '@stdlib/blas/base/ndarray/dswap' );
86+
87+
var opts = {
88+
'dtype': 'float64'
89+
};
90+
91+
var xbuf = discreteUniform( 10, 0, 100, opts );
92+
var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
93+
console.log( ndarray2array( x ) );
94+
95+
var ybuf = discreteUniform( 10, 0, 100, opts );
96+
var y = new ndarray( opts.dtype, ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
97+
console.log( ndarray2array( y ) );
98+
99+
var out = dswap( [ x, y ] );
100+
console.log( ndarray2array( x ) );
101+
console.log( ndarray2array( out ) );
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+
</section>
121+
122+
<!-- /.links -->
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var dswap = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var xbuf;
51+
var ybuf;
52+
var x;
53+
var y;
54+
55+
xbuf = uniform( len, -100.0, 100.0, options );
56+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
57+
58+
ybuf = uniform( len, -100.0, 100.0, options );
59+
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' );
60+
61+
return benchmark;
62+
63+
/**
64+
* Benchmark function.
65+
*
66+
* @private
67+
* @param {Benchmark} b - benchmark instance
68+
*/
69+
function benchmark( b ) {
70+
var z;
71+
var i;
72+
73+
b.tic();
74+
for ( i = 0; i < b.iterations; i++ ) {
75+
z = dswap( [ x, y ] );
76+
if ( typeof z !== 'object' ) {
77+
b.fail( 'should return an ndarray' );
78+
}
79+
}
80+
b.toc();
81+
if ( isnan( z.get( i%len ) ) ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
}
87+
}
88+
89+
90+
// MAIN //
91+
92+
/**
93+
* Main execution sequence.
94+
*
95+
* @private
96+
*/
97+
function main() {
98+
var len;
99+
var min;
100+
var max;
101+
var f;
102+
var i;
103+
104+
min = 1; // 10^min
105+
max = 6; // 10^max
106+
107+
for ( i = min; i <= max; i++ ) {
108+
len = pow( 10, i );
109+
f = createBenchmark( len );
110+
bench( format( '%s:len=%d', pkg, len ), f );
111+
}
112+
}
113+
114+
main();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( arrays )
3+
Interchanges two one-dimensional double-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 two input ndarrays.
12+
13+
Returns
14+
-------
15+
out: ndarray
16+
The second input ndarray.
17+
18+
Examples
19+
--------
20+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 4.0, 2.0, -3.0 ] );
21+
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0, 0.0, 0.0 ] );
22+
> var dt = 'float64';
23+
> var sh = [ xbuf.length ];
24+
> var st = [ 1 ];
25+
> var oo = 0;
26+
> var ord = 'row-major';
27+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord );
28+
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord );
29+
30+
> {{alias}}( [ x, y ] );
31+
> x
32+
<ndarray>[ 0.0, 0.0, 0.0 ]
33+
> y
34+
<ndarray>[ 4.0, 2.0, -3.0 ]
35+
36+
See Also
37+
--------
38+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 { float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Interchanges two one-dimensional double-precision floating-point ndarrays.
27+
*
28+
* @param arrays - array-like object containing two input ndarrays
29+
* @returns second input ndarray
30+
*
31+
* @example
32+
* var Float64Array = require( '@stdlib/array/float64' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
36+
* var x = new ndarray( 'float64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var ybuf = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
39+
* var y = new ndarray( 'float64', ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
40+
*
41+
* var z = dswap( [ x, y ] );
42+
* // x => <ndarray>[ 6.0, 7.0, 8.0, 9.0, 10.0 ]
43+
* // y => <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
44+
*
45+
* var bool = ( z === y );
46+
* // returns true
47+
*/
48+
declare function dswap( arrays: [ float64ndarray, float64ndarray ] ): float64ndarray;
49+
50+
51+
// EXPORTS //
52+
53+
export = dswap;
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 dswap = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'float64'
31+
});
32+
const y = zeros( [ 10 ], {
33+
'dtype': 'float64'
34+
});
35+
36+
dswap( [ x, y ] ); // $ExpectType float64ndarray
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+
dswap( '10' ); // $ExpectError
42+
dswap( 10 ); // $ExpectError
43+
dswap( true ); // $ExpectError
44+
dswap( false ); // $ExpectError
45+
dswap( null ); // $ExpectError
46+
dswap( undefined ); // $ExpectError
47+
dswap( [] ); // $ExpectError
48+
dswap( {} ); // $ExpectError
49+
dswap( ( 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': 'float64'
56+
});
57+
const y = zeros( [ 10 ], {
58+
'dtype': 'float64'
59+
});
60+
61+
dswap(); // $ExpectError
62+
dswap( [ x, y ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)