Skip to content

Commit b8de2da

Browse files
committed
feat: add ndarray/base/next-cartesian-index
1 parent 4968a1a commit b8de2da

File tree

11 files changed

+1637
-0
lines changed

11 files changed

+1637
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
# nextCartesianIndex
22+
23+
> Return the next Cartesian index (i.e., set of subscripts/dimension indices).
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 nextCartesianIndex = require( '@stdlib/ndarray/base/next-cartesian-index' );
41+
```
42+
43+
#### nextCartesianIndex( shape, order, idx, dim )
44+
45+
Returns the next Cartesian index (i.e., set of subscripts/dimension indices).
46+
47+
```javascript
48+
var idx = nextCartesianIndex( [ 2, 2, 2 ], 'row-major', [ 0, 0, 1 ], -1 );
49+
// returns [ 0, 1, 0 ]
50+
```
51+
52+
The function accepts the following arguments:
53+
54+
- **shape**: array shape.
55+
- **order**: index iteration order. Must be either `row-major` (C-style) or `column-major` (Fortran-style).
56+
- **idx**: current dimension indices.
57+
- **dim**: index of the dimension from which to start incrementing (inclusive).
58+
59+
The `order` parameter specifies the index iteration order. When `order` is `row-major`, the last indices change fastest, and, when the `order` is `column-major`, the first indices change fastest.
60+
61+
```javascript
62+
var idx = nextCartesianIndex( [ 2, 2, 2 ], 'column-major', [ 0, 1, 0 ], 0 );
63+
// returns [ 1, 1, 0 ]
64+
```
65+
66+
The `dim` parameter controls which dimensions are incremented. When `order` is `row-major`, if `dim` equals `shape.length-1` (or equivalently `-1`), the function increments over all dimensions from right-to-left (last-to-first). Similarly, when `order` is `column-major`, if `dim` equals `0`, the function increments over all dimensions from left-to-right (first-to-last). To restrict which dimensions can be incremented, set `dim` to a value other than the respective end. For example,
67+
68+
```javascript
69+
// Increment starting from the second-to-last dimension:
70+
var idx = nextCartesianIndex( [ 2, 2, 2 ], 'row-major', [ 0, 0, 0 ], -2 );
71+
// returns [ 0, 1, 0 ]
72+
73+
idx = nextCartesianIndex( [ 2, 2, 2 ], 'row-major', idx, -2 );
74+
// returns [ 1, 0, 0 ]
75+
76+
idx = nextCartesianIndex( [ 2, 2, 2 ], 'row-major', idx, -2 );
77+
// returns [ 1, 1, 0 ]
78+
```
79+
80+
#### nextCartesianIndex.assign( shape, order, idx, dim, out )
81+
82+
Returns the next Cartesian index (i.e., set of subscripts/dimension indices) and assigns results to a provided output array.
83+
84+
```javascript
85+
var out = [ 0, 0, 0 ];
86+
var idx = nextCartesianIndex.assign( [ 2, 2, 2 ], 'row-major', [ 0, 0, 1 ], -1, out );
87+
// returns [ 0, 1, 0 ]
88+
89+
var bool = ( out === idx );
90+
// returns true
91+
```
92+
93+
The function accepts the following arguments:
94+
95+
- **shape**: array shape.
96+
- **order**: index iteration order. Must be either `row-major` (C-style) or `column-major` (Fortran-style).
97+
- **idx**: current dimension indices.
98+
- **dim**: index of the dimension from which to start incrementing (inclusive).
99+
- **out**: output array.
100+
101+
</section>
102+
103+
<!-- /.usage -->
104+
105+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
106+
107+
<section class="notes">
108+
109+
## Notes
110+
111+
- The function does not check whether the current index is the "last" index. Instead, if the function is provided dimension indices corresponding to the last element, the function will cycle back to the "first" index.
112+
- If provided an empty shape (i.e., a shape corresponding to a zero-dimensional ndarray) or a dimension index `dim` which is out-of-bounds, the function returns `null`.
113+
114+
</section>
115+
116+
<!-- /.notes -->
117+
118+
<!-- Package usage examples. -->
119+
120+
<section class="examples">
121+
122+
## Examples
123+
124+
<!-- eslint no-undef: "error" -->
125+
126+
```javascript
127+
var array = require( '@stdlib/ndarray/array' );
128+
var zeroTo = require( '@stdlib/array/base/zero-to' );
129+
var nextCartesianIndex = require( '@stdlib/ndarray/base/next-cartesian-index' );
130+
131+
// Create an ndarray:
132+
var x = array( zeroTo( 27 ), {
133+
'shape': [ 3, 3, 3 ]
134+
});
135+
136+
// Initialize a set of indices:
137+
var idx = [ 0, 0, 0 ];
138+
139+
// Iterate over each element in the array...
140+
var i;
141+
for ( i = 0; i < x.length; i++ ) {
142+
console.log( 'x[%s] = %d', idx.join( ',' ), x.get.apply( x, idx ) );
143+
idx = nextCartesianIndex.assign( x.shape, x.order, idx, -1, idx );
144+
}
145+
```
146+
147+
</section>
148+
149+
<!-- /.examples -->
150+
151+
<!-- 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. -->
152+
153+
<section class="references">
154+
155+
</section>
156+
157+
<!-- /.references -->
158+
159+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
160+
161+
<section class="related">
162+
163+
</section>
164+
165+
<!-- /.related -->
166+
167+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
168+
169+
<section class="links">
170+
171+
</section>
172+
173+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 isArray = require( '@stdlib/assert/is-array' );
25+
var zeros = require( '@stdlib/array/base/zeros' );
26+
var pkg = require( './../package.json' ).name;
27+
var nextCartesianIndex = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':order=row-major,ndims=3', function benchmark( b ) {
33+
var shape;
34+
var out;
35+
var i;
36+
37+
shape = [ 10, 10, 10 ];
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
out = nextCartesianIndex( shape, 'row-major', [ (i+2)%10, (i+1)%10, i%10 ], -1 );
42+
if ( out.length !== shape.length ) {
43+
b.fail( 'should have expected length' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isArray( out ) ) {
48+
b.fail( 'should return an array' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
53+
54+
bench( pkg+':order=column-major,ndims=3', function benchmark( b ) {
55+
var shape;
56+
var out;
57+
var i;
58+
59+
shape = [ 10, 10, 10 ];
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
out = nextCartesianIndex( shape, 'column-major', [ (i+2)%10, (i+1)%10, i%10 ], 0 );
64+
if ( out.length !== shape.length ) {
65+
b.fail( 'should have expected length' );
66+
}
67+
}
68+
b.toc();
69+
if ( !isArray( out ) ) {
70+
b.fail( 'should return an array' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});
75+
76+
bench( pkg+':assign:order=row-major,ndims=3', function benchmark( b ) {
77+
var shape;
78+
var out;
79+
var i;
80+
81+
shape = [ 10, 10, 10 ];
82+
out = zeros( shape.length );
83+
84+
b.tic();
85+
for ( i = 0; i < b.iterations; i++ ) {
86+
out = nextCartesianIndex.assign( shape, 'row-major', [ (i+2)%10, (i+1)%10, i%10 ], -1, out );
87+
if ( out.length !== shape.length ) {
88+
b.fail( 'should have expected length' );
89+
}
90+
}
91+
b.toc();
92+
if ( !isArray( out ) ) {
93+
b.fail( 'should return an array' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
});
98+
99+
bench( pkg+':assign:order=column-major,ndims=3', function benchmark( b ) {
100+
var shape;
101+
var out;
102+
var i;
103+
104+
shape = [ 10, 10, 10 ];
105+
out = zeros( shape.length );
106+
107+
b.tic();
108+
for ( i = 0; i < b.iterations; i++ ) {
109+
out = nextCartesianIndex.assign( shape, 'column-major', [ (i+2)%10, (i+1)%10, i%10 ], 0, out );
110+
if ( out.length !== shape.length ) {
111+
b.fail( 'should have expected length' );
112+
}
113+
}
114+
b.toc();
115+
if ( !isArray( out ) ) {
116+
b.fail( 'should return an array' );
117+
}
118+
b.pass( 'benchmark finished' );
119+
b.end();
120+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
{{alias}}( shape, order, idx, dim )
3+
Returns the next Cartesian index (i.e., set of subscripts/dimension
4+
indices).
5+
6+
The function does not check whether the current index is the "last" index.
7+
Instead, if the function is provided dimension indices corresponding to the
8+
last element, the function will cycle back to the "first" index.
9+
10+
If provided an empty shape (i.e., a shape corresponding to a zero-
11+
dimensional ndarray) or a dimension index which is out-of-bounds, the
12+
function returns `null`.
13+
14+
Parameters
15+
----------
16+
shape: ArrayLike<integer>
17+
Array shape.
18+
19+
order: string
20+
Index iteration order. Must be either row-major (C-style) or column-
21+
major (Fortran-style).
22+
23+
idx: ArrayLike<integer>
24+
Current dimension indices.
25+
26+
dim: integer
27+
Index of the dimension from which to start incrementing (inclusive).
28+
29+
Returns
30+
-------
31+
out: Array<integer>|null
32+
Updated dimension indices (or null).
33+
34+
Examples
35+
--------
36+
> var sh = [ 2, 2, 2 ];
37+
> var ord = 'row-major';
38+
> var idx = {{alias}}( sh, ord, [ 0, 0, 1 ], -1 )
39+
[ 0, 1, 0 ]
40+
> idx = {{alias}}( sh, ord, idx, -1 )
41+
[ 0, 1, 1 ]
42+
43+
44+
{{alias}}.assign( shape, order, idx, dim, out )
45+
Returns the next Cartesian index (i.e., set of subscripts/dimension
46+
indices) and assigns results to a provided output array.
47+
48+
The function does not check whether the current index is the "last" index.
49+
Instead, if the function is provided dimension indices corresponding to the
50+
last element, the function will cycle back to the "first" index.
51+
52+
If provided an empty shape (i.e., a shape corresponding to a zero-
53+
dimensional ndarray) or a dimension index which is out-of-bounds, the
54+
function returns `null`.
55+
56+
Parameters
57+
----------
58+
shape: ArrayLike<integer>
59+
Array shape.
60+
61+
order: string
62+
Index iteration order. Must be either row-major (C-style) or column-
63+
major (Fortran-style).
64+
65+
idx: ArrayLike<integer>
66+
Current dimension indices.
67+
68+
dim: integer
69+
Index of the dimension from which to start incrementing (inclusive).
70+
71+
out: Array|TypedArray|Object
72+
Output array.
73+
74+
Returns
75+
-------
76+
out: Array|TypedArray|Object
77+
Output array.
78+
79+
Examples
80+
--------
81+
> var sh = [ 2, 2, 2 ];
82+
> var ord = 'row-major';
83+
> var out = [ 0, 0, 0 ];
84+
> var idx = {{alias}}.assign( sh, ord, [ 0, 0, 1 ], -1, out )
85+
[ 0, 1, 0 ]
86+
> var bool = ( out === idx )
87+
true
88+
89+
See Also
90+
--------
91+

0 commit comments

Comments
 (0)