Skip to content

Commit 5f80a15

Browse files
committed
feat: add ndarray/iter/matrix-entries
1 parent 6753c94 commit 5f80a15

File tree

10 files changed

+1463
-0
lines changed

10 files changed

+1463
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
# nditerMatrixEntries
22+
23+
> Create an iterator which returns `[index, matrix]` pairs for each matrix in a stack of matrices.
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 nditerMatrixEntries = require( '@stdlib/ndarray/iter/matrix-entries' );
41+
```
42+
43+
#### nditerMatrixEntries( x\[, options] )
44+
45+
Returns an iterator which returns `[index, matrix]` pairs for each matrix in a stack of matrices.
46+
47+
```javascript
48+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
49+
var array = require( '@stdlib/ndarray/array' );
50+
51+
var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] );
52+
// returns <ndarray>
53+
54+
var iter = nditerMatrixEntries( x );
55+
56+
var v = iter.next().value;
57+
// returns [...]
58+
59+
var idx = v[ 0 ];
60+
// returns [ 0, null, null ]
61+
62+
var mat = ndarray2array( v[ 1 ] );
63+
// returns [ [ 1, 2 ], [ 3, 4 ] ]
64+
65+
v = iter.next().value;
66+
// returns [...]
67+
68+
idx = v[ 0 ];
69+
// returns [ 1, null, null ]
70+
71+
mat = ndarray2array( v[ 1 ] );
72+
// returns [ [ 5, 6 ], [ 7, 8 ] ]
73+
74+
// ...
75+
```
76+
77+
The function accepts the following `options`:
78+
79+
- **readonly**: boolean indicating whether returned [`ndarray`][@stdlib/ndarray/ctor] views should be read-only. In order to return writable [`ndarray`][@stdlib/ndarray/ctor] views, the input [`ndarray`][@stdlib/ndarray/ctor] must be writable. If the input [`ndarray`][@stdlib/ndarray/ctor] is read-only, setting this option to `false` raises an exception. Default: `true`.
80+
81+
By default, the iterator returns [`ndarray`][@stdlib/ndarray/ctor] views which are **read-only**. To return writable [views][@stdlib/ndarray/slice], set the `readonly` option to `false`.
82+
83+
```javascript
84+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
85+
var array = require( '@stdlib/ndarray/array' );
86+
87+
var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] );
88+
// returns <ndarray>
89+
90+
var iter = nditerMatrixEntries( x, {
91+
'readonly': false
92+
});
93+
94+
var v = iter.next().value;
95+
// returns [...]
96+
97+
var mat = ndarray2array( v[ 1 ] );
98+
// returns [ [ 1, 2 ], [ 3, 4 ] ]
99+
100+
v[ 1 ].set( 0, 0, 10 );
101+
102+
mat = ndarray2array( v[ 1 ] );
103+
// returns [ [ 10, 2 ], [ 3, 4 ] ]
104+
```
105+
106+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
107+
108+
- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished.
109+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
110+
111+
</section>
112+
113+
<!-- /.usage -->
114+
115+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
116+
117+
<section class="notes">
118+
119+
## Notes
120+
121+
- Each returned index is a Cartesian index (i.e., an array of subscripts/dimension indices). A dimension index equal to `null` indicates that all values along the respective dimension are included in the returned [`ndarray`][@stdlib/ndarray/ctor].
122+
- If an environment supports `Symbol.iterator`, the returned iterator is iterable.
123+
- A returned iterator does **not** copy a provided [`ndarray`][@stdlib/ndarray/ctor]. To ensure iterable reproducibility, copy the input [`ndarray`][@stdlib/ndarray/ctor] **before** creating an iterator. Otherwise, any changes to the contents of input [`ndarray`][@stdlib/ndarray/ctor] will be reflected in the returned iterator.
124+
- In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke an ndarray's `@@iterator` method, regardless of whether this method is defined.
125+
126+
</section>
127+
128+
<!-- /.notes -->
129+
130+
<!-- Package usage examples. -->
131+
132+
<section class="examples">
133+
134+
## Examples
135+
136+
<!-- eslint no-undef: "error" -->
137+
138+
```javascript
139+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
140+
var array = require( '@stdlib/ndarray/array' );
141+
var zeroTo = require( '@stdlib/array/base/zero-to' );
142+
var nditerMatrixEntries = require( '@stdlib/ndarray/iter/matrix-entries' );
143+
144+
// Define an input array:
145+
var x = array( zeroTo( 27 ), {
146+
'shape': [ 3, 3, 3 ]
147+
});
148+
149+
// Create an iterator for returning [index, matrix] pairs:
150+
var it = nditerMatrixEntries( x );
151+
152+
// Perform manual iteration...
153+
var v;
154+
while ( true ) {
155+
v = it.next();
156+
if ( v.done ) {
157+
break;
158+
}
159+
console.log( v.value[ 0 ] );
160+
console.log( ndarray2array( v.value[ 1 ] ) );
161+
}
162+
```
163+
164+
</section>
165+
166+
<!-- /.examples -->
167+
168+
<!-- 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. -->
169+
170+
<section class="references">
171+
172+
</section>
173+
174+
<!-- /.references -->
175+
176+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
177+
178+
<section class="related">
179+
180+
</section>
181+
182+
<!-- /.related -->
183+
184+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
185+
186+
<section class="links">
187+
188+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
189+
190+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib
191+
192+
[@stdlib/ndarray/slice]: https://github.com/stdlib-js/stdlib
193+
194+
</section>
195+
196+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var array = require( '@stdlib/ndarray/array' );
27+
var zeros = require( '@stdlib/ndarray/zeros' );
28+
var pkg = require( './../package.json' ).name;
29+
var nditerMatrixEntries = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var iter;
36+
var x;
37+
var i;
38+
39+
x = array( [ [ [ 1, 2, 3, 4 ] ] ] );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
iter = nditerMatrixEntries( x );
44+
if ( typeof iter !== 'object' ) {
45+
b.fail( 'should return an object' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isIteratorLike( iter ) ) {
50+
b.fail( 'should return an iterator protocol-compliant object' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+'::iteration', function benchmark( b ) {
57+
var iter;
58+
var x;
59+
var z;
60+
var i;
61+
62+
x = zeros( [ b.iterations+1, 1, 1 ], {
63+
'dtype': 'generic'
64+
});
65+
66+
iter = nditerMatrixEntries( x );
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
z = iter.next().value;
71+
if ( typeof z !== 'object' ) {
72+
b.fail( 'should return an array' );
73+
}
74+
}
75+
b.toc();
76+
if ( !isArray( z ) ) {
77+
b.fail( 'should return an array' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
{{alias}}( x[, options] )
3+
Returns an iterator which returns [index, matrix] pairs for each matrix in a
4+
stack of matrices.
5+
6+
Each returned index is a Cartesian index (i.e., an array of subscripts/
7+
dimension indices). A dimension index equal to `null` indicates that all
8+
values along the respective dimension are included in the returned ndarray.
9+
10+
If an environment supports Symbol.iterator, the returned iterator is
11+
iterable.
12+
13+
If an environment supports Symbol.iterator, the function explicitly does not
14+
invoke an ndarray's `@@iterator` method, regardless of whether this method
15+
is defined.
16+
17+
Parameters
18+
----------
19+
x: ndarray
20+
Input array.
21+
22+
options: Object (optional)
23+
Options.
24+
25+
options.readonly: boolean (optional)
26+
Boolean indicating whether returned ndarray views should be read-only.
27+
If the input ndarray is read-only, setting this option to `false` raises
28+
an exception. Default: true.
29+
30+
Returns
31+
-------
32+
iterator: Object
33+
Iterator.
34+
35+
iterator.next(): Function
36+
Returns an iterator protocol-compliant object containing the next
37+
iterated value (if one exists) and a boolean flag indicating whether the
38+
iterator is finished.
39+
40+
iterator.return( [value] ): Function
41+
Finishes an iterator and returns a provided value.
42+
43+
Examples
44+
--------
45+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ [ 1, 2 ], [ 3, 4 ] ] ] );
46+
> var it = {{alias}}( x );
47+
> var v = it.next().value;
48+
> v[ 0 ]
49+
[ 0, null, null ]
50+
> {{alias:@stdlib/ndarray/to-array}}( v[ 1 ] )
51+
[ [ 1, 2 ], [ 3, 4 ] ]
52+
53+
See Also
54+
--------
55+

0 commit comments

Comments
 (0)