Skip to content

Commit d8b3767

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/gcircshift
PR-URL: #11007 Closes: stdlib-js/metr-issue-tracker#199 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent f9d1409 commit d8b3767

File tree

15 files changed

+1979
-0
lines changed

15 files changed

+1979
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
# gcircshift
22+
23+
> Circularly shift the elements of a strided array by a specified number of positions.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var gcircshift = require( '@stdlib/blas/ext/base/gcircshift' );
31+
```
32+
33+
#### gcircshift( N, k, x, strideX )
34+
35+
Circularly shifts the elements of a strided array by a specified number of positions.
36+
37+
```javascript
38+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
39+
40+
gcircshift( x.length, 2, x, 1 );
41+
// x => [ 4.0, 5.0, 1.0, 2.0, 3.0 ]
42+
```
43+
44+
The function has the following parameters:
45+
46+
- **N**: number of indexed elements.
47+
- **k**: number of positions to shift.
48+
- **x**: input array.
49+
- **strideX**: stride length.
50+
51+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to circularly shift every other element:
52+
53+
```javascript
54+
var x = [ 1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0, 0.0 ];
55+
56+
gcircshift( 4, 1, x, 2 );
57+
// x => [ 4.0, 0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 0.0 ]
58+
```
59+
60+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
61+
62+
```javascript
63+
var Float64Array = require( '@stdlib/array/float64' );
64+
65+
// Initial array...
66+
var x0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] );
67+
68+
// Create an offset view...
69+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
70+
71+
// Circularly shift elements in the view:
72+
gcircshift( 5, 2, x1, 1 );
73+
// x0 => <Float64Array>[ 0.0, 4.0, 5.0, 1.0, 2.0, 3.0 ]
74+
```
75+
76+
#### gcircshift.ndarray( N, k, x, strideX, offsetX )
77+
78+
Circularly shifts the elements of a strided array by a specified number of positions using alternative indexing semantics.
79+
80+
```javascript
81+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
82+
83+
gcircshift.ndarray( x.length, 2, x, 1, 0 );
84+
// x => [ 4.0, 5.0, 1.0, 2.0, 3.0 ]
85+
```
86+
87+
The function has the following additional parameters:
88+
89+
- **offsetX**: starting index.
90+
91+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of the strided array:
92+
93+
```javascript
94+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
95+
96+
gcircshift.ndarray( 3, 1, x, 1, x.length-3 );
97+
// x => [ 1.0, 2.0, 3.0, 6.0, 4.0, 5.0 ]
98+
```
99+
100+
</section>
101+
102+
<!-- /.usage -->
103+
104+
<section class="notes">
105+
106+
## Notes
107+
108+
- If `N <= 0`, both functions return the strided array unchanged.
109+
- If `k` is a multiple of `N`, both functions return the strided array unchanged.
110+
- If `k > 0`, elements are shifted to the right.
111+
- If `k < 0`, elements are shifted to the left.
112+
- Depending on the environment, the typed versions ([`dcircshift`][@stdlib/blas/ext/base/dcircshift], [`scircshift`][@stdlib/blas/ext/base/scircshift], etc.) are likely to be significantly more performant.
113+
114+
</section>
115+
116+
<!-- /.notes -->
117+
118+
<section class="examples">
119+
120+
## Examples
121+
122+
<!-- eslint no-undef: "error" -->
123+
124+
```javascript
125+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
126+
var gcircshift = require( '@stdlib/blas/ext/base/gcircshift' );
127+
128+
var x = discreteUniform( 10, -100, 100, {
129+
'dtype': 'generic'
130+
});
131+
console.log( x );
132+
133+
gcircshift( x.length, 3, x, 1 );
134+
console.log( x );
135+
```
136+
137+
</section>
138+
139+
<!-- /.examples -->
140+
141+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
142+
143+
<section class="related">
144+
145+
</section>
146+
147+
<!-- /.related -->
148+
149+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="links">
152+
153+
[@stdlib/blas/ext/base/dcircshift]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dcircshift
154+
155+
[@stdlib/blas/ext/base/scircshift]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/scircshift
156+
157+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
158+
159+
<!-- <related-links> -->
160+
161+
<!-- </related-links> -->
162+
163+
</section>
164+
165+
<!-- /.links -->
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 floor = require( '@stdlib/math/base/special/floor' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gcircshift = require( './../lib/main.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Create a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var x = uniform( len, -100.0, 100.0, {
44+
'dtype': 'generic'
45+
});
46+
var k = floor( len / 2 );
47+
return benchmark;
48+
49+
/**
50+
* Benchmark function.
51+
*
52+
* @private
53+
* @param {Benchmark} b - benchmark instance
54+
*/
55+
function benchmark( b ) {
56+
var y;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
y = gcircshift( x.length, k, x, 1 );
62+
if ( isnan( y[ i%x.length ] ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( y[ i%x.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
}
73+
}
74+
75+
76+
// MAIN //
77+
78+
/**
79+
* Main execution sequence.
80+
*
81+
* @private
82+
*/
83+
function main() {
84+
var len;
85+
var min;
86+
var max;
87+
var f;
88+
var i;
89+
90+
min = 1; // 10^min
91+
max = 6; // 10^max
92+
93+
for ( i = min; i <= max; i++ ) {
94+
len = pow( 10, i );
95+
f = createBenchmark( len );
96+
bench( format( '%s:len=%d', pkg, len ), f );
97+
}
98+
}
99+
100+
main();
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 floor = require( '@stdlib/math/base/special/floor' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gcircshift = require( './../lib/ndarray.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Create a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var x = uniform( len, -100.0, 100.0, {
44+
'dtype': 'generic'
45+
});
46+
var k = floor( len / 2 );
47+
return benchmark;
48+
49+
/**
50+
* Benchmark function.
51+
*
52+
* @private
53+
* @param {Benchmark} b - benchmark instance
54+
*/
55+
function benchmark( b ) {
56+
var y;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
y = gcircshift( x.length, k, x, 1, 0 );
62+
if ( isnan( y[ i%x.length ] ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( y[ i%x.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
}
73+
}
74+
75+
76+
// MAIN //
77+
78+
/**
79+
* Main execution sequence.
80+
*
81+
* @private
82+
*/
83+
function main() {
84+
var len;
85+
var min;
86+
var max;
87+
var f;
88+
var i;
89+
90+
min = 1; // 10^min
91+
max = 6; // 10^max
92+
93+
for ( i = min; i <= max; i++ ) {
94+
len = pow( 10, i );
95+
f = createBenchmark( len );
96+
bench( format( '%s:ndarray:len=%d', pkg, len ), f );
97+
}
98+
}
99+
100+
main();

0 commit comments

Comments
 (0)