Skip to content

Commit eba1910

Browse files
committed
Auto-generated commit
1 parent dcba884 commit eba1910

File tree

12 files changed

+2105
-1
lines changed

12 files changed

+2105
-1
lines changed

CHANGELOG.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
<section class="release" id="unreleased">
66

7-
## Unreleased (2024-08-10)
7+
## Unreleased (2024-08-11)
88

99
<section class="packages">
1010

@@ -552,6 +552,50 @@ This release closes the following issue:
552552

553553
<!-- /.package -->
554554

555+
<section class="package" id="ndarray-iter-unreleased">
556+
557+
#### [@stdlib/ndarray/iter](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/iter)
558+
559+
<details>
560+
561+
<section class="features">
562+
563+
##### Features
564+
565+
- [`46aec25`](https://github.com/stdlib-js/stdlib/commit/46aec25aac5d4a0c8a3fd7b719dd6a080e59beb8) - add `nditerSelectDimension` to namespace
566+
567+
</section>
568+
569+
<!-- /.features -->
570+
571+
</details>
572+
573+
</section>
574+
575+
<!-- /.package -->
576+
577+
<section class="package" id="ndarray-iter-select-dimension-unreleased">
578+
579+
#### [@stdlib/ndarray/iter/select-dimension](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/iter/select-dimension)
580+
581+
<details>
582+
583+
<section class="features">
584+
585+
##### Features
586+
587+
- [`6dce19b`](https://github.com/stdlib-js/stdlib/commit/6dce19b2a2dfae6159257dab0c52a8421e0861d2) - add `ndarray/iter/select-dimension`
588+
589+
</section>
590+
591+
<!-- /.features -->
592+
593+
</details>
594+
595+
</section>
596+
597+
<!-- /.package -->
598+
555599
<section class="package" id="ndarray-min-dtype-unreleased">
556600

557601
#### [@stdlib/ndarray/min-dtype](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/min-dtype)
@@ -816,6 +860,8 @@ A total of 4 people contributed to this release. Thank you to the following cont
816860

817861
<details>
818862

863+
- [`46aec25`](https://github.com/stdlib-js/stdlib/commit/46aec25aac5d4a0c8a3fd7b719dd6a080e59beb8) - **feat:** add `nditerSelectDimension` to namespace _(by Athan Reines)_
864+
- [`6dce19b`](https://github.com/stdlib-js/stdlib/commit/6dce19b2a2dfae6159257dab0c52a8421e0861d2) - **feat:** add `ndarray/iter/select-dimension` _(by Athan Reines)_
819865
- [`4ec7a03`](https://github.com/stdlib-js/stdlib/commit/4ec7a031214836b442e5bce57b57b20e166ef8a4) - **refactor:** improve type specificity _(by Athan Reines)_
820866
- [`0fef28c`](https://github.com/stdlib-js/stdlib/commit/0fef28ccec002134fb0ebd2db1a713404ab1db17) - **refactor:** improve type specificity _(by Athan Reines)_
821867
- [`9a4b430`](https://github.com/stdlib-js/stdlib/commit/9a4b430c6013d48480cb73a266fabd9683f49b31) - **refactor:** improve type specificity _(by Athan Reines)_

iter/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ setReadOnly( ns, 'nditerRowEntries', require( './../../iter/row-entries' ) );
108108
*/
109109
setReadOnly( ns, 'nditerRows', require( './../../iter/rows' ) );
110110

111+
/**
112+
* @name nditerSelectDimension
113+
* @memberof ns
114+
* @readonly
115+
* @type {Function}
116+
* @see {@link module:@stdlib/ndarray/iter/select-dimension}
117+
*/
118+
setReadOnly( ns, 'nditerSelectDimension', require( './../../iter/select-dimension' ) );
119+
111120
/**
112121
* @name nditer2arrayEach
113122
* @memberof ns

iter/select-dimension/README.md

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# nditerSelectDimension
22+
23+
> Create an iterator which iterates over each view 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 nditerSelectDimension = require( '@stdlib/ndarray/iter/select-dimension' );
41+
```
42+
43+
#### nditerSelectDimension( x, dim\[, options] )
44+
45+
Returns an iterator which iterates over each view along a specified dimension.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
50+
51+
var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] );
52+
// returns <ndarray>
53+
54+
var iter = nditerSelectDimension( x, 0 );
55+
56+
var v = iter.next().value;
57+
// returns <ndarray>
58+
59+
var arr = ndarray2array( v );
60+
// returns [ [ 1, 2 ], [ 3, 4 ] ]
61+
62+
v = iter.next().value;
63+
// returns <ndarray>
64+
65+
arr = ndarray2array( v );
66+
// returns [ [ 5, 6 ], [ 7, 8 ] ]
67+
68+
// ...
69+
```
70+
71+
The function accepts the following `options`:
72+
73+
- **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`.
74+
- **keepdim**: boolean indicating whether returned [`ndarray`][@stdlib/ndarray/ctor] views should include the selected dimension as a singleton dimension. If `true`, each returned view has the same number of dimensions as the input [`ndarray`][@stdlib/ndarray/ctor]. Otherwise, each returned view has one fewer dimensions than the input [`ndarray`][@stdlib/ndarray/ctor]. Default: `false`.
75+
76+
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`.
77+
78+
```javascript
79+
var array = require( '@stdlib/ndarray/array' );
80+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
81+
82+
var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] );
83+
// returns <ndarray>
84+
85+
var iter = nditerSelectDimension( x, 0, {
86+
'readonly': false
87+
});
88+
89+
var v = iter.next().value;
90+
// returns <ndarray>
91+
92+
var arr = ndarray2array( v );
93+
// returns [ [ 1, 2 ], [ 3, 4 ] ]
94+
95+
v.set( 0, 0, 10 );
96+
97+
arr = ndarray2array( v );
98+
// returns [ [ 10, 2 ], [ 3, 4 ] ]
99+
```
100+
101+
By default, the iterator returns [`ndarray`][@stdlib/ndarray/ctor] views which omit the selected dimension, resulting in [`ndarray`][@stdlib/ndarray/ctor] views having one fewer dimensions than the provided input [`ndarray`][@stdlib/ndarray/ctor]. To include the selected dimension as a singleton dimension in the returned [`ndarray`][@stdlib/ndarray/ctor] views, set the `keepdim` option to `true`.
102+
103+
```javascript
104+
var array = require( '@stdlib/ndarray/array' );
105+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
106+
107+
var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] );
108+
// returns <ndarray>
109+
110+
var iter = nditerSelectDimension( x, 0, {
111+
'keepdim': true
112+
});
113+
114+
var v = iter.next().value;
115+
// returns <ndarray>
116+
117+
var arr = ndarray2array( v );
118+
// returns [ [ [ 1, 2 ], [ 3, 4 ] ] ]
119+
120+
v = iter.next().value;
121+
// returns <ndarray>
122+
123+
arr = ndarray2array( v );
124+
// returns [ [ [ 5, 6 ], [ 7, 8 ] ] ]
125+
126+
// ...
127+
```
128+
129+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
130+
131+
- **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.
132+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
133+
134+
</section>
135+
136+
<!-- /.usage -->
137+
138+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
139+
140+
<section class="notes">
141+
142+
## Notes
143+
144+
- If an environment supports `Symbol.iterator`, the returned iterator is iterable.
145+
- 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.
146+
- In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke an ndarray's `@@iterator` method, regardless of whether this method is defined.
147+
- Including the selected dimension as a singleton dimension can be useful when wanting to ensure that returned views remain [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the input [`ndarray`][@stdlib/ndarray/ctor].
148+
149+
</section>
150+
151+
<!-- /.notes -->
152+
153+
<!-- Package usage examples. -->
154+
155+
<section class="examples">
156+
157+
## Examples
158+
159+
<!-- eslint no-undef: "error" -->
160+
161+
```javascript
162+
var array = require( '@stdlib/ndarray/array' );
163+
var zeroTo = require( '@stdlib/array/base/zero-to' );
164+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
165+
var nditerSelectDimension = require( '@stdlib/ndarray/iter/select-dimension' );
166+
167+
// Define an input array:
168+
var x = array( zeroTo( 27 ), {
169+
'shape': [ 3, 3, 3 ]
170+
});
171+
172+
// Create an iterator for iterating over matrices:
173+
var it = nditerSelectDimension( x, 0 );
174+
175+
// Perform manual iteration...
176+
var v;
177+
while ( true ) {
178+
v = it.next();
179+
if ( v.done ) {
180+
break;
181+
}
182+
console.log( ndarray2array( v.value ) );
183+
}
184+
```
185+
186+
</section>
187+
188+
<!-- /.examples -->
189+
190+
<!-- 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. -->
191+
192+
<section class="references">
193+
194+
</section>
195+
196+
<!-- /.references -->
197+
198+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
199+
200+
<section class="related">
201+
202+
</section>
203+
204+
<!-- /.related -->
205+
206+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
207+
208+
<section class="links">
209+
210+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
211+
212+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor
213+
214+
[@stdlib/ndarray/slice]: https://github.com/stdlib-js/ndarray/tree/main/slice
215+
216+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/ndarray/tree/main/base/broadcast-shapes
217+
218+
<!-- <related-links> -->
219+
220+
<!-- </related-links> -->
221+
222+
</section>
223+
224+
<!-- /.links -->
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var array = require( './../../../array' );
27+
var pkg = require( './../package.json' ).name;
28+
var nditerSelectDimension = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var iter;
35+
var x;
36+
var i;
37+
38+
x = array( [ [ [ 1, 2, 3, 4 ] ] ] );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
iter = nditerSelectDimension( x, 0 );
43+
if ( typeof iter !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isIteratorLike( iter ) ) {
49+
b.fail( 'should return an iterator protocol-compliant object' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+'::iteration', function benchmark( b ) {
56+
var xbuf;
57+
var iter;
58+
var x;
59+
var z;
60+
var i;
61+
62+
xbuf = [];
63+
xbuf.length = b.iterations + 1;
64+
x = array( xbuf, {
65+
'shape': [ xbuf.length, 1, 1 ],
66+
'dtype': 'generic',
67+
'copy': false
68+
});
69+
70+
iter = nditerSelectDimension( x, 0 );
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
z = iter.next().value;
75+
if ( typeof z !== 'object' ) {
76+
b.fail( 'should return an ndarray' );
77+
}
78+
}
79+
b.toc();
80+
if ( !isndarrayLike( z ) ) {
81+
b.fail( 'should return an ndarray' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
});

0 commit comments

Comments
 (0)