Skip to content

Commit eeb2bdd

Browse files
committed
feat: add ndarray/stride
1 parent 654e776 commit eeb2bdd

File tree

10 files changed

+955
-0
lines changed

10 files changed

+955
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# stride
22+
23+
> Return the stride along a specified dimension for a provided [ndarray][@stdlib/ndarray/ctor].
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 stride = require( '@stdlib/ndarray/stride' );
41+
```
42+
43+
#### stride( x, dim )
44+
45+
Returns the stride along a specified dimension for a provided [ndarray][@stdlib/ndarray/ctor].
46+
47+
```javascript
48+
var zeros = require( '@stdlib/ndarray/zeros' );
49+
50+
var x = zeros( [ 3, 2, 3 ] );
51+
// returns <ndarray>
52+
53+
var st = stride( x, 0 );
54+
// returns 6
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input ndarray.
60+
- **dim**: dimension index. 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`.
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- A "stride" is the linear distance (i.e., number of elements) between adjacent elements along a specified dimension.
73+
- This function is intended as a slight performance optimization over [`@stdlib/ndarray/strides`][@stdlib/ndarray/strides] when **only** a single stride is needed. For retrieving multiple strides, use [`@stdlib/ndarray/strides`][@stdlib/ndarray/strides] directly.
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<!-- Package usage examples. -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
<!-- eslint-disable new-cap -->
88+
89+
```javascript
90+
var zeros = require( '@stdlib/ndarray/zeros' );
91+
var slice = require( '@stdlib/ndarray/slice' );
92+
var E = require( '@stdlib/slice/multi' );
93+
var S = require( '@stdlib/slice/ctor' );
94+
var stride = require( '@stdlib/ndarray/stride' );
95+
96+
// Create an array:
97+
var x = zeros( [ 10, 10, 10, 10 ] );
98+
// returns <ndarray>
99+
100+
// Define some slices:
101+
var slices = [
102+
// :,:,:,:
103+
E( null, null, null, null ),
104+
105+
// 5:10,4,2:4,::-1
106+
E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ),
107+
108+
// :,:,2,:
109+
E( null, null, 2, null ),
110+
111+
// 1,2,3,:
112+
E( 1, 2, 3, null ),
113+
114+
// 1,3,::2,4::2
115+
E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) )
116+
];
117+
118+
// Resolve the stride of the first dimension for each slice...
119+
var s;
120+
var i;
121+
for ( i = 0; i < slices.length; i++ ) {
122+
s = slice( x, slices[ i ] );
123+
console.log( '(%s) => %d', s.shape.join( ',' ), stride( s, 0 ) );
124+
}
125+
```
126+
127+
</section>
128+
129+
<!-- /.examples -->
130+
131+
<!-- 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. -->
132+
133+
<section class="references">
134+
135+
</section>
136+
137+
<!-- /.references -->
138+
139+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
140+
141+
<section class="related">
142+
143+
</section>
144+
145+
<!-- /.related -->
146+
147+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="links">
150+
151+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
152+
153+
[@stdlib/ndarray/strides]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/strides
154+
155+
</section>
156+
157+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 zeros = require( '@stdlib/ndarray/zeros' );
25+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var stride = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values;
34+
var out;
35+
var i;
36+
37+
values = [
38+
zeros( [ 10, 10, 10, 1 ] ),
39+
zeros( [ 5, 5, 5, 1, 1 ] ),
40+
zeros( [ 3, 4, 5 ] )
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = stride( values[ i%values.length ], i%2 );
46+
if ( out !== out ) {
47+
b.fail( 'should return an integer' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isInteger( out ) ) {
52+
b.fail( 'should return an integer' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, dim )
3+
Returns the stride along a specified dimension for a provided ndarray.
4+
5+
A "stride" is the linear distance (i.e., number of elements) between
6+
adjacent elements along a specified dimension.
7+
8+
Parameters
9+
----------
10+
x: ndarray
11+
Input ndarray.
12+
13+
dim: integer
14+
Dimension index. If less than zero, the index is resolved relative to
15+
the last dimension, with the last dimension corresponding to the value
16+
`-1`.
17+
18+
Returns
19+
-------
20+
out: integer
21+
Stride.
22+
23+
Examples
24+
--------
25+
> var out = {{alias}}( {{alias:@stdlib/ndarray/zeros}}( [ 3, 3, 3 ] ), 0 )
26+
9
27+
28+
See Also
29+
--------
30+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns the stride along a specified dimension for a provided ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - A "stride" is the linear distance (i.e., number of elements) between adjacent elements along a specified dimension.
31+
*
32+
* @param x - input ndarray
33+
* @param dim - dimension index
34+
* @returns stride
35+
*
36+
* @example
37+
* var zeros = require( `@stdlib/ndarray/zeros` );
38+
*
39+
* var st = stride( zeros( [ 3, 3, 3 ] ), 0 );
40+
* // returns 9
41+
*/
42+
declare function stride( x: ndarray, dim: number ): number;
43+
44+
45+
// EXPORTS //
46+
47+
export = stride;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
import zeros = require( '@stdlib/ndarray/zeros' );
20+
import stride = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a number...
26+
{
27+
stride( zeros( [ 3, 2, 1 ] ), 0 ); // $ExpectType number
28+
}
29+
30+
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
31+
{
32+
stride( '5', 0 ); // $ExpectError
33+
stride( 5, 0 ); // $ExpectError
34+
stride( true, 0 ); // $ExpectError
35+
stride( false, 0 ); // $ExpectError
36+
stride( null, 0 ); // $ExpectError
37+
stride( undefined, 0 ); // $ExpectError
38+
stride( [ '1', '2' ], 0 ); // $ExpectError
39+
stride( {}, 0 ); // $ExpectError
40+
stride( ( x: number ): number => x, 0 ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided a second argument which is not a number...
44+
{
45+
stride( zeros( [ 3, 2, 1 ] ), '5' ); // $ExpectError
46+
stride( zeros( [ 3, 2, 1 ] ), true ); // $ExpectError
47+
stride( zeros( [ 3, 2, 1 ] ), false ); // $ExpectError
48+
stride( zeros( [ 3, 2, 1 ] ), null ); // $ExpectError
49+
stride( zeros( [ 3, 2, 1 ] ), undefined ); // $ExpectError
50+
stride( zeros( [ 3, 2, 1 ] ), [ '1', '2' ] ); // $ExpectError
51+
stride( zeros( [ 3, 2, 1 ] ), {} ); // $ExpectError
52+
stride( zeros( [ 3, 2, 1 ] ), ( x: number ): number => x ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided an unsupported number of arguments...
56+
{
57+
stride(); // $ExpectError
58+
stride( zeros( [ 2, 2 ] ) ); // $ExpectError
59+
stride( zeros( [ 2, 2 ] ), 0, {} ); // $ExpectError
60+
}

0 commit comments

Comments
 (0)