Skip to content

Commit 0cf9821

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/circshift
PR-URL: #11189 Closes: stdlib-js/metr-issue-tracker#257 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 68e429f commit 0cf9821

File tree

11 files changed

+2055
-0
lines changed

11 files changed

+2055
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
# circshift
22+
23+
> Circularly shift the elements of an input [ndarray][@stdlib/ndarray/ctor] by a specified number of positions along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var circshift = require( '@stdlib/blas/ext/circshift' );
31+
```
32+
33+
#### circshift( x, k\[, options] )
34+
35+
Circularly shifts the elements of an input [ndarray][@stdlib/ndarray/ctor] by a specified number of positions along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
36+
37+
```javascript
38+
var array = require( '@stdlib/ndarray/array' );
39+
40+
var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
41+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
42+
43+
var y = circshift( x, 2 );
44+
// returns <ndarray>[ 4.0, 5.0, 1.0, 2.0, 3.0 ]
45+
46+
var bool = ( x === y );
47+
// returns true
48+
```
49+
50+
The function has the following parameters:
51+
52+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
53+
- **k**: number of positions to shift. May be either an integer scalar value or an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] for `k` must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] for `k` must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
54+
- **options**: function options (_optional_).
55+
56+
The function accepts the following options:
57+
58+
- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
59+
60+
By default, the function circularly shifts all elements. To perform the operation over specific dimensions, provide a `dims` option.
61+
62+
```javascript
63+
var array = require( '@stdlib/ndarray/array' );
64+
65+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ], {
66+
'shape': [ 2, 2 ],
67+
'order': 'row-major'
68+
});
69+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
70+
71+
var y = circshift( x, 1, {
72+
'dims': [ 0 ]
73+
});
74+
// returns <ndarray>[ [ 3.0, 4.0 ], [ 1.0, 2.0 ] ]
75+
```
76+
77+
</section>
78+
79+
<!-- /.usage -->
80+
81+
<section class="notes">
82+
83+
## Notes
84+
85+
- The input [ndarray][@stdlib/ndarray/ctor] is shifted **in-place** (i.e., the input [ndarray][@stdlib/ndarray/ctor] is **mutated**).
86+
- When shifting elements along a single dimension, a positive `k` shifts elements to the right (toward higher indices), and a negative `k` shifts elements to the left (toward lower indices). If `k` is zero, the input [ndarray][@stdlib/ndarray/ctor] is left unchanged.
87+
- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before performing the circular shift.
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
101+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
102+
var circshift = require( '@stdlib/blas/ext/circshift' );
103+
104+
// Generate an ndarray of random numbers:
105+
var x = discreteUniform( [ 5, 5 ], -20, 20, {
106+
'dtype': 'generic'
107+
});
108+
console.log( ndarray2array( x ) );
109+
110+
// Perform operation:
111+
circshift( x, 2, {
112+
'dims': [ 0 ]
113+
});
114+
115+
// Print the results:
116+
console.log( ndarray2array( x ) );
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
124+
125+
<section class="related">
126+
127+
</section>
128+
129+
<!-- /.related -->
130+
131+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="links">
134+
135+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
136+
137+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
138+
139+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
140+
141+
</section>
142+
143+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var circshift = 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 x = uniform( len, -50.0, 50.0, options );
51+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
52+
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var o;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
o = circshift( x, ( i%len ) + 1 );
68+
if ( typeof o !== 'object' ) {
69+
b.fail( 'should return an ndarray' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( o.get( i%len ) ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f );
103+
}
104+
}
105+
106+
main();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
{{alias}}( x, k[, options] )
3+
Circularly shifts the elements of an input ndarray by a specified number
4+
of positions along one or more ndarray dimensions.
5+
6+
When shifting elements along a single dimension, a positive `k` shifts
7+
elements to the right (toward higher indices), and a negative `k` shifts
8+
elements to the left (toward lower indices). If `k` is zero, the input
9+
ndarray is left unchanged.
10+
11+
The function circularly shifts an input ndarray in-place and thus mutates
12+
an input ndarray.
13+
14+
Parameters
15+
----------
16+
x: ndarray
17+
Input array.
18+
19+
k: ndarray|number
20+
Number of positions to shift. May be either a scalar value or an ndarray
21+
having a real-valued or "generic" data type. If provided an ndarray, the
22+
value must have a shape which is broadcast compatible with the
23+
complement of the shape defined by `options.dims`. For example, given
24+
the input shape `[2, 3, 4]` and `options.dims=[0]`, an ndarray for `k`
25+
must have a shape which is broadcast compatible with the shape `[3, 4]`.
26+
Similarly, when performing the operation over all elements in a provided
27+
input ndarray, an ndarray for `k` must be a zero-dimensional ndarray.
28+
29+
options: Object (optional)
30+
Function options.
31+
32+
options.dims: Array<integer> (optional)
33+
List of dimensions over which to perform operation. If not provided, the
34+
function performs the operation over all elements in a provided input
35+
ndarray.
36+
37+
Returns
38+
-------
39+
out: ndarray
40+
Input array.
41+
42+
Examples
43+
--------
44+
> var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
45+
> var y = {{alias}}( x, 2 );
46+
> {{alias:@stdlib/ndarray/to-array}}( y )
47+
[ 4.0, 5.0, 1.0, 2.0, 3.0 ]
48+
49+
See Also
50+
--------
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 { ArrayLike } from '@stdlib/types/array';
24+
import { typedndarray } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Interface defining options.
28+
*/
29+
interface Options {
30+
/**
31+
* List of dimensions over which to perform operation.
32+
*/
33+
dims?: ArrayLike<number>;
34+
}
35+
36+
/**
37+
* Circularly shifts the elements of an input ndarray by a specified number of positions along one or more ndarray dimensions.
38+
*
39+
* ## Notes
40+
*
41+
* - The input ndarray is shifted **in-place** (i.e., the input ndarray is **mutated**).
42+
* - When shifting elements along a single dimension, a positive `k` shifts elements to the right (toward higher indices), and a negative `k` shifts elements to the left (toward lower indices). If `k` is zero, the input ndarray is left unchanged.
43+
*
44+
* @param x - input ndarray
45+
* @param k - number of positions to shift
46+
* @param options - function options
47+
* @returns input ndarray
48+
*
49+
* @example
50+
* var array = require( '@stdlib/ndarray/array' );
51+
*
52+
* var x = array( [ 1.0, 2.0, 3.0, 4.0 ], {
53+
* 'shape': [ 2, 2 ],
54+
* 'order': 'row-major'
55+
* });
56+
* // returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
57+
*
58+
* var y = circshift( x, 1, {
59+
* 'dims': [ 0 ]
60+
* });
61+
* // returns <ndarray>[ [ 3.0, 4.0 ], [ 1.0, 2.0 ] ]
62+
*/
63+
declare function circshift<T extends typedndarray<unknown>>( x: T, k: typedndarray<number> | number, options?: Options ): T;
64+
65+
66+
// EXPORTS //
67+
68+
export = circshift;

0 commit comments

Comments
 (0)