Skip to content

Commit d92656d

Browse files
committed
Auto-generated commit
1 parent 78efc94 commit d92656d

File tree

11 files changed

+1221
-0
lines changed

11 files changed

+1221
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`534fbd0`](https://github.com/stdlib-js/stdlib/commit/534fbd0fab785e6e2b439cdf5966f0cbef47f570) - add `ndarray/base/shift` [(#8118)](https://github.com/stdlib-js/stdlib/pull/8118)
1314
- [`ba14b89`](https://github.com/stdlib-js/stdlib/commit/ba14b8911df283cc272c48d8d382df60f1a3e750) - add `ndarray/any-by` [(#8110)](https://github.com/stdlib-js/stdlib/pull/8110)
1415
- [`b5a916e`](https://github.com/stdlib-js/stdlib/commit/b5a916e413d8658977ab2383ec6fe411f90c557d) - update `ndarray/base` TypeScript declarations [(#8127)](https://github.com/stdlib-js/stdlib/pull/8127)
1516
- [`70e9b6c`](https://github.com/stdlib-js/stdlib/commit/70e9b6c1b5b3521412ed4a8fed9ab3de337ab661) - add `ndarray/base/pop` [(#8108)](https://github.com/stdlib-js/stdlib/pull/8108)
@@ -595,6 +596,7 @@ A total of 25 issues were closed in this release:
595596

596597
<details>
597598

599+
- [`534fbd0`](https://github.com/stdlib-js/stdlib/commit/534fbd0fab785e6e2b439cdf5966f0cbef47f570) - **feat:** add `ndarray/base/shift` [(#8118)](https://github.com/stdlib-js/stdlib/pull/8118) _(by Muhammad Haris, Athan Reines)_
598600
- [`95e69dc`](https://github.com/stdlib-js/stdlib/commit/95e69dce83d0cfb84e270e9db302ae35178728bd) - **test:** add tests to `ndarray/some-by` for complete test coverage [(#8125)](https://github.com/stdlib-js/stdlib/pull/8125) _(by Muhammad Haris, Athan Reines)_
599601
- [`b7368a4`](https://github.com/stdlib-js/stdlib/commit/b7368a45d5b0cc0bbdf695291837eee3d726da0a) - **fix:** handle edge case when provided an empty ndarray _(by Athan Reines)_
600602
- [`2798013`](https://github.com/stdlib-js/stdlib/commit/2798013a8edf4eb58a762928e8625089eef0f4f5) - **docs:** update example _(by Athan Reines)_

base/shift/README.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# shift
22+
23+
> Return an array containing a truncated view of an input ndarray and a view of the first element(s) along a specified dimension.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var shift = require( '@stdlib/ndarray/base/shift' );
41+
```
42+
43+
#### shift( x, dim, writable )
44+
45+
Returns an array containing a truncated view of an input ndarray and a view of the first element(s) along a specified dimension.
46+
47+
```javascript
48+
var ndarray = require( '@stdlib/ndarray/ctor' );
49+
var getShape = require( '@stdlib/ndarray/shape' );
50+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
51+
52+
var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
53+
var shape = [ 3, 2 ];
54+
var strides = [ 2, 1 ];
55+
var offset = 0;
56+
57+
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
58+
// returns <ndarray>
59+
60+
var sh = getShape( x );
61+
// returns [ 3, 2 ]
62+
63+
var arr = ndarray2array( x );
64+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
65+
66+
var y = shift( x, 0, false );
67+
// returns [ <ndarray>, <ndarray> ]
68+
69+
arr = ndarray2array( y[ 0 ] );
70+
// returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
71+
72+
arr = ndarray2array( y[ 1 ] );
73+
// returns [ [ 1.0, 2.0 ] ]
74+
```
75+
76+
The function accepts the following arguments:
77+
78+
- **x**: input ndarray.
79+
- **dim**: dimension along which to perform the operation. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`.
80+
- **writable**: boolean indicating whether a returned ndarray should be writable.
81+
82+
</section>
83+
84+
<!-- /.usage -->
85+
86+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="notes">
89+
90+
## Notes
91+
92+
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
93+
94+
</section>
95+
96+
<!-- /.notes -->
97+
98+
<!-- Package usage examples. -->
99+
100+
<section class="examples">
101+
102+
## Examples
103+
104+
<!-- eslint no-undef: "error" -->
105+
106+
```javascript
107+
var array = require( '@stdlib/ndarray/array' );
108+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
109+
var zeroTo = require( '@stdlib/array/base/zero-to' );
110+
var shift = require( '@stdlib/ndarray/base/shift' );
111+
112+
// Create a linear ndarray buffer:
113+
var buf = zeroTo( 27 );
114+
115+
// Create an ndarray which is a stack of three 3x3 matrices:
116+
var x = array( buf, {
117+
'shape': [ 3, 3, 3 ]
118+
});
119+
120+
// Remove the first row from each matrix:
121+
var y = shift( x, 1, false );
122+
// returns [ <ndarray>, <ndarray> ]
123+
124+
console.log( ndarray2array( y[ 0 ] ) );
125+
console.log( ndarray2array( y[ 1 ] ) );
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
133+
134+
<section class="references">
135+
136+
</section>
137+
138+
<!-- /.references -->
139+
140+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
141+
142+
<section class="related">
143+
144+
</section>
145+
146+
<!-- /.related -->
147+
148+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
149+
150+
<section class="links">
151+
152+
</section>
153+
154+
<!-- /.links -->

base/shift/benchmark/benchmark.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var empty = require( './../../../empty' );
26+
var pkg = require( './../package.json' ).name;
27+
var shift = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::1d', function benchmark( b ) {
33+
var values;
34+
var v;
35+
var i;
36+
37+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
38+
39+
values = [
40+
empty( [ 2 ], { 'dtype': 'float64' } ),
41+
empty( [ 2 ], { 'dtype': 'float32' } ),
42+
empty( [ 2 ], { 'dtype': 'int32' } ),
43+
empty( [ 2 ], { 'dtype': 'complex128' } ),
44+
empty( [ 2 ], { 'dtype': 'generic' } )
45+
];
46+
47+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
v = shift( values[ i%values.length ], 0, false );
52+
if ( typeof v !== 'object' ) {
53+
b.fail( 'should return an array of ndarrays' );
54+
}
55+
}
56+
b.toc();
57+
if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) {
58+
b.fail( 'should return an array of ndarrays' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
63+
64+
bench( pkg+'::2d', function benchmark( b ) {
65+
var values;
66+
var v;
67+
var i;
68+
69+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
70+
71+
values = [
72+
empty( [ 2, 2 ], { 'dtype': 'float64' } ),
73+
empty( [ 2, 2 ], { 'dtype': 'float32' } ),
74+
empty( [ 2, 2 ], { 'dtype': 'int32' } ),
75+
empty( [ 2, 2 ], { 'dtype': 'complex128' } ),
76+
empty( [ 2, 2 ], { 'dtype': 'generic' } )
77+
];
78+
79+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
v = shift( values[ i%values.length ], 0, false );
84+
if ( typeof v !== 'object' ) {
85+
b.fail( 'should return an array of ndarrays' );
86+
}
87+
}
88+
b.toc();
89+
if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) {
90+
b.fail( 'should return an array of ndarrays' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
});
95+
96+
bench( pkg+'::3d', function benchmark( b ) {
97+
var values;
98+
var v;
99+
var i;
100+
101+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
102+
103+
values = [
104+
empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ),
105+
empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ),
106+
empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ),
107+
empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ),
108+
empty( [ 2, 2, 2 ], { 'dtype': 'generic' } )
109+
];
110+
111+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
112+
113+
b.tic();
114+
for ( i = 0; i < b.iterations; i++ ) {
115+
v = shift( values[ i%values.length ], 0, false );
116+
if ( typeof v !== 'object' ) {
117+
b.fail( 'should return an array of ndarrays' );
118+
}
119+
}
120+
b.toc();
121+
if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) {
122+
b.fail( 'should return an array of ndarrays' );
123+
}
124+
b.pass( 'benchmark finished' );
125+
b.end();
126+
});
127+
128+
bench( pkg+'::4d', function benchmark( b ) {
129+
var values;
130+
var v;
131+
var i;
132+
133+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
134+
135+
values = [
136+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'float64' } ),
137+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'float32' } ),
138+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'int32' } ),
139+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'complex128' } ),
140+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'generic' } )
141+
];
142+
143+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
144+
145+
b.tic();
146+
for ( i = 0; i < b.iterations; i++ ) {
147+
v = shift( values[ i%values.length ], 0, false );
148+
if ( typeof v !== 'object' ) {
149+
b.fail( 'should return an array of ndarrays' );
150+
}
151+
}
152+
b.toc();
153+
if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) {
154+
b.fail( 'should return an array of ndarrays' );
155+
}
156+
b.pass( 'benchmark finished' );
157+
b.end();
158+
});
159+
160+
bench( pkg+'::5d', function benchmark( b ) {
161+
var values;
162+
var v;
163+
var i;
164+
165+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
166+
167+
values = [
168+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float64' } ),
169+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float32' } ),
170+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'int32' } ),
171+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'complex128' } ),
172+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'generic' } )
173+
];
174+
175+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
176+
177+
b.tic();
178+
for ( i = 0; i < b.iterations; i++ ) {
179+
v = shift( values[ i%values.length ], 0, false );
180+
if ( typeof v !== 'object' ) {
181+
b.fail( 'should return an array of ndarrays' );
182+
}
183+
}
184+
b.toc();
185+
if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) {
186+
b.fail( 'should return an array of ndarrays' );
187+
}
188+
b.pass( 'benchmark finished' );
189+
b.end();
190+
});

0 commit comments

Comments
 (0)