Skip to content

Commit d36550d

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/scircshift
PR-URL: #11006 Closes: stdlib-js/metr-issue-tracker#198 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 231767b commit d36550d

33 files changed

+3918
-0
lines changed
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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+
# scircshift
22+
23+
> Circularly shift the elements of a single-precision floating-point strided array by a specified number of positions.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var scircshift = require( '@stdlib/blas/ext/base/scircshift' );
31+
```
32+
33+
#### scircshift( N, k, x, strideX )
34+
35+
Circularly shifts the elements of a single-precision floating-point strided array by a specified number of positions.
36+
37+
```javascript
38+
var Float32Array = require( '@stdlib/array/float32' );
39+
40+
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
41+
42+
scircshift( x.length, 2, x, 1 );
43+
// x => <Float32Array>[ 4.0, 5.0, 1.0, 2.0, 3.0 ]
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **k**: number of positions to shift.
50+
- **x**: input [`Float32Array`][@stdlib/array/float32].
51+
- **strideX**: stride length.
52+
53+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to circularly shift every other element:
54+
55+
```javascript
56+
var Float32Array = require( '@stdlib/array/float32' );
57+
58+
var x = new Float32Array( [ 1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0, 0.0 ] );
59+
60+
scircshift( 4, 1, x, 2 );
61+
// x => <Float32Array>[ 4.0, 0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 0.0 ]
62+
```
63+
64+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
65+
66+
```javascript
67+
var Float32Array = require( '@stdlib/array/float32' );
68+
69+
// Initial array...
70+
var x0 = new Float32Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] );
71+
72+
// Create an offset view...
73+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
74+
75+
// Circularly shift elements in the view:
76+
scircshift( 5, 2, x1, 1 );
77+
// x0 => <Float32Array>[ 0.0, 4.0, 5.0, 1.0, 2.0, 3.0 ]
78+
```
79+
80+
#### scircshift.ndarray( N, k, x, strideX, offsetX )
81+
82+
Circularly shifts the elements of a single-precision floating-point strided array by a specified number of positions using alternative indexing semantics.
83+
84+
```javascript
85+
var Float32Array = require( '@stdlib/array/float32' );
86+
87+
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
88+
89+
scircshift.ndarray( x.length, 2, x, 1, 0 );
90+
// x => <Float32Array>[ 4.0, 5.0, 1.0, 2.0, 3.0 ]
91+
```
92+
93+
The function has the following additional parameters:
94+
95+
- **offsetX**: starting index.
96+
97+
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:
98+
99+
```javascript
100+
var Float32Array = require( '@stdlib/array/float32' );
101+
102+
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
103+
104+
scircshift.ndarray( 3, 1, x, 1, x.length-3 );
105+
// x => <Float32Array>[ 1.0, 2.0, 3.0, 6.0, 4.0, 5.0 ]
106+
```
107+
108+
</section>
109+
110+
<!-- /.usage -->
111+
112+
<section class="notes">
113+
114+
## Notes
115+
116+
- If `N <= 0`, both functions return the strided array unchanged.
117+
- If `k` is a multiple of `N`, both functions return the strided array unchanged.
118+
- If `k > 0`, elements are shifted to the right.
119+
- If `k < 0`, elements are shifted to the left.
120+
121+
</section>
122+
123+
<!-- /.notes -->
124+
125+
<section class="examples">
126+
127+
## Examples
128+
129+
<!-- eslint no-undef: "error" -->
130+
131+
```javascript
132+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
133+
var scircshift = require( '@stdlib/blas/ext/base/scircshift' );
134+
135+
var x = discreteUniform( 10, -100, 100, {
136+
'dtype': 'float32'
137+
});
138+
console.log( x );
139+
140+
scircshift( x.length, 3, x, 1 );
141+
console.log( x );
142+
```
143+
144+
</section>
145+
146+
<!-- /.examples -->
147+
148+
<!-- C interface documentation. -->
149+
150+
* * *
151+
152+
<section class="c">
153+
154+
## C APIs
155+
156+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
157+
158+
<section class="intro">
159+
160+
</section>
161+
162+
<!-- /.intro -->
163+
164+
<!-- C usage documentation. -->
165+
166+
<section class="usage">
167+
168+
### Usage
169+
170+
```c
171+
#include "stdlib/blas/ext/base/scircshift.h"
172+
```
173+
174+
#### stdlib_strided_scircshift( N, k, \*X, strideX )
175+
176+
Circularly shifts the elements of a single-precision floating-point strided array by a specified number of positions.
177+
178+
```c
179+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
180+
181+
stdlib_strided_scircshift( 5, 2, x, 1 );
182+
```
183+
184+
The function accepts the following arguments:
185+
186+
- **N**: `[in] CBLAS_INT` number of indexed elements.
187+
- **k**: `[in] CBLAS_INT` number of positions to shift.
188+
- **X**: `[inout] float*` input array.
189+
- **strideX**: `[in] CBLAS_INT` stride length.
190+
191+
```c
192+
void stdlib_strided_scircshift( const CBLAS_INT N, const CBLAS_INT k, float *X, const CBLAS_INT strideX );
193+
```
194+
195+
#### stdlib_strided_scircshift_ndarray( N, k, \*X, strideX, offsetX )
196+
197+
Circularly shifts the elements of a single-precision floating-point strided array by a specified number of positions using alternative indexing semantics.
198+
199+
```c
200+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
201+
202+
stdlib_strided_scircshift_ndarray( 5, 2, x, 1, 0 );
203+
```
204+
205+
The function accepts the following arguments:
206+
207+
- **N**: `[in] CBLAS_INT` number of indexed elements.
208+
- **k**: `[in] CBLAS_INT` number of positions to shift.
209+
- **X**: `[inout] float*` input array.
210+
- **strideX**: `[in] CBLAS_INT` stride length.
211+
- **offsetX**: `[in] CBLAS_INT` starting index.
212+
213+
```c
214+
void stdlib_strided_scircshift_ndarray( const CBLAS_INT N, const CBLAS_INT k, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
215+
```
216+
217+
</section>
218+
219+
<!-- /.usage -->
220+
221+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="notes">
224+
225+
</section>
226+
227+
<!-- /.notes -->
228+
229+
<!-- C API usage examples. -->
230+
231+
<section class="examples">
232+
233+
### Examples
234+
235+
```c
236+
#include "stdlib/blas/ext/base/scircshift.h"
237+
#include <stdio.h>
238+
239+
int main( void ) {
240+
// Create a strided array:
241+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
242+
243+
// Specify the number of elements:
244+
const int N = 8;
245+
246+
// Specify the shift amount:
247+
const int k = 3;
248+
249+
// Specify a stride:
250+
const int strideX = 1;
251+
252+
// Perform the circular shift:
253+
stdlib_strided_scircshift( N, k, x, strideX );
254+
255+
// Print the result:
256+
for ( int i = 0; i < 8; i++ ) {
257+
printf( "x[ %i ] = %f\n", i, x[ i ] );
258+
}
259+
}
260+
```
261+
262+
</section>
263+
264+
<!-- /.examples -->
265+
266+
</section>
267+
268+
<!-- /.c -->
269+
270+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
271+
272+
<section class="related">
273+
274+
</section>
275+
276+
<!-- /.related -->
277+
278+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
279+
280+
<section class="links">
281+
282+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
283+
284+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
285+
286+
<!-- <related-links> -->
287+
288+
<!-- </related-links> -->
289+
290+
</section>
291+
292+
<!-- /.links -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
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 scircshift = require( './../lib/scircshift.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float32'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Create 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, -10.0, 10.0, options );
51+
var k = floor( len / 2 );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var y;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
y = scircshift( x.length, k, x, 1 );
67+
if ( isnanf( y[ i%x.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnanf( y[ i%x.length ] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = pow( 10, i );
100+
f = createBenchmark( len );
101+
bench( format( '%s:len=%d', pkg, len ), f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)