Skip to content

Commit 899e976

Browse files
authored
feat: add blas/ext/base/ndarray/dcircshift
PR-URL: #11176 Closes: stdlib-js/metr-issue-tracker#254 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent ed59264 commit 899e976

File tree

10 files changed

+865
-0
lines changed

10 files changed

+865
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
# dcircshift
22+
23+
> Circularly shift the elements of a one-dimensional double-precision floating-point ndarray by a specified number of positions.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var dcircshift = require( '@stdlib/blas/ext/base/ndarray/dcircshift' );
37+
```
38+
39+
#### dcircshift( arrays )
40+
41+
Circularly shifts the elements of a one-dimensional double-precision floating-point ndarray by a specified number of positions.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
47+
48+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
49+
var x = new ndarray( 'float64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
50+
51+
var k = scalar2ndarray( 2, {
52+
'dtype': 'generic'
53+
});
54+
55+
var out = dcircshift( [ x, k ] );
56+
// returns <ndarray>[ 4.0, 5.0, 1.0, 2.0, 3.0 ]
57+
```
58+
59+
The function has the following parameters:
60+
61+
- **arrays**: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying the number of positions to shift.
62+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<section class="notes">
68+
69+
## Notes
70+
71+
- The input ndarray is shifted **in-place** (i.e., the input ndarray is **mutated**).
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
85+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
86+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
87+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
88+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
89+
var dcircshift = require( '@stdlib/blas/ext/base/ndarray/dcircshift' );
90+
91+
var xbuf = discreteUniform( 10, -100, 100, {
92+
'dtype': 'float64'
93+
});
94+
var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
95+
console.log( ndarray2array( x ) );
96+
97+
var k = scalar2ndarray( 3, {
98+
'dtype': 'generic'
99+
});
100+
console.log( 'Shift:', ndarraylike2scalar( k ) );
101+
102+
dcircshift( [ x, k ] );
103+
console.log( ndarray2array( x ) );
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
</section>
115+
116+
<!-- /.related -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
</section>
123+
124+
<!-- /.links -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 pow = require( '@stdlib/math/base/special/pow' );
26+
var format = require( '@stdlib/string/format' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
29+
var pkg = require( './../package.json' ).name;
30+
var dcircshift = 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 x;
52+
var k;
53+
54+
xbuf = uniform( len, 0.0, 100.0, options );
55+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
56+
57+
k = scalar2ndarray( 3, options );
58+
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var out;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
out = dcircshift( [ x, k ] );
74+
if ( typeof out !== 'object' ) {
75+
b.fail( 'should return an ndarray' );
76+
}
77+
}
78+
b.toc();
79+
if ( xbuf[ i%len ] !== xbuf[ i%len ] ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( format( '%s:len=%d', pkg, len ), f );
109+
}
110+
}
111+
112+
main();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
{{alias}}( arrays )
3+
Circularly shifts the elements of a one-dimensional double-precision
4+
floating-point ndarray by a specified number of positions.
5+
6+
The input ndarray is shifted *in-place* (i.e., the input ndarray is
7+
*mutated*).
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing the following ndarrays:
13+
14+
- a one-dimensional input ndarray.
15+
- a zero-dimensional ndarray specifying the number of positions to
16+
shift.
17+
18+
Returns
19+
-------
20+
out: ndarray
21+
Input ndarray.
22+
23+
Examples
24+
--------
25+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
26+
> var dt = 'float64';
27+
> var sh = [ xbuf.length ];
28+
> var sx = [ 1 ];
29+
> var ox = 0;
30+
> var ord = 'row-major';
31+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
32+
> var k = {{alias:@stdlib/ndarray/from-scalar}}( 2, { 'dtype': dt } );
33+
> {{alias}}( [ x, k ] )
34+
<ndarray>[ 3.0, 4.0, 1.0, 2.0 ]
35+
36+
See Also
37+
--------
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 { typedndarray, float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Circularly shifts the elements of a one-dimensional double-precision floating-point ndarray by a specified number of positions.
27+
*
28+
* @param arrays - array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying the number of positions to shift
29+
* @returns input ndarray
30+
*
31+
* @example
32+
* var Float64Array = require( '@stdlib/array/float64' );
33+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
34+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
35+
*
36+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
37+
* var x = new ndarray( 'float64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
38+
*
39+
* var k = scalar2ndarray( 2, {
40+
* 'dtype': 'generic'
41+
* });
42+
*
43+
* var out = dcircshift( [ x, k ] );
44+
* // returns <ndarray>[ 4.0, 5.0, 1.0, 2.0, 3.0 ]
45+
*/
46+
declare function dcircshift( arrays: [ float64ndarray, typedndarray<number> ] ): float64ndarray;
47+
48+
49+
// EXPORTS //
50+
51+
export = dcircshift;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
23+
import dcircshift = require( './index' );
24+
25+
26+
// TESTS //
27+
28+
// The function returns an ndarray...
29+
{
30+
const x = zeros( [ 10 ], {
31+
'dtype': 'float64'
32+
});
33+
const k = scalar2ndarray( 2, {
34+
'dtype': 'generic'
35+
});
36+
37+
dcircshift( [ x, k ] ); // $ExpectType float64ndarray
38+
}
39+
40+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
41+
{
42+
dcircshift( '10' ); // $ExpectError
43+
dcircshift( 10 ); // $ExpectError
44+
dcircshift( true ); // $ExpectError
45+
dcircshift( false ); // $ExpectError
46+
dcircshift( null ); // $ExpectError
47+
dcircshift( undefined ); // $ExpectError
48+
dcircshift( [] ); // $ExpectError
49+
dcircshift( {} ); // $ExpectError
50+
dcircshift( ( x: number ): number => x ); // $ExpectError
51+
}
52+
53+
// The compiler throws an error if the function is provided an unsupported number of arguments...
54+
{
55+
const x = zeros( [ 10 ], {
56+
'dtype': 'float64'
57+
});
58+
const k = scalar2ndarray( 2, {
59+
'dtype': 'generic'
60+
});
61+
62+
dcircshift(); // $ExpectError
63+
dcircshift( [ x, k ], {} ); // $ExpectError
64+
}

0 commit comments

Comments
 (0)