|
| 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 --> |
0 commit comments