Skip to content

Commit 4f9ea87

Browse files
committed
feat: add ndarray/base/while-each
1 parent 642af21 commit 4f9ea87

97 files changed

Lines changed: 20039 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# whileEach
22+
23+
> While a test condition is true, invoke a callback function for each element in an ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var whileEach = require( '@stdlib/ndarray/base/while-each' );
37+
```
38+
39+
#### whileEach( arrays, predicate, fcn\[, thisArg] )
40+
41+
While a test condition is true, invokes a callback function for each element in an ndarray.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var naryFunction = require( '@stdlib/utils/nary-function' );
46+
var log = require( '@stdlib/console/log' );
47+
48+
function predicate( value ) {
49+
return value === value;
50+
}
51+
52+
// Create data buffers:
53+
var xbuf = new Float64Array( 12 );
54+
55+
// Define the shape of the array:
56+
var shape = [ 3, 1, 2 ];
57+
58+
// Define the array strides:
59+
var sx = [ 4, 4, 1 ];
60+
61+
// Define the index offset:
62+
var ox = 1;
63+
64+
// Create an ndarray-like object:
65+
var x = {
66+
'dtype': 'float64',
67+
'data': xbuf,
68+
'shape': shape,
69+
'strides': sx,
70+
'offset': ox,
71+
'order': 'row-major'
72+
};
73+
74+
// Apply the callback function:
75+
whileEach( [ x ], predicate, naryFunction( log, 1 ) );
76+
```
77+
78+
The function accepts the following arguments:
79+
80+
- **arrays**: array-like object containing an input ndarray.
81+
- **predicate**: predicate function which determines whether to continue iterating.
82+
- **fcn**: callback to apply.
83+
- **thisArg**: callback execution context.
84+
85+
Both the predicate function and the callback function are provided the following arguments:
86+
87+
- **value**: current array element.
88+
- **indices**: current array element indices.
89+
- **arr**: the input ndarray.
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<section class="notes">
96+
97+
## Notes
98+
99+
- The provided ndarray should be an object with the following properties:
100+
101+
- **dtype**: data type.
102+
- **data**: data buffer.
103+
- **shape**: dimensions.
104+
- **strides**: stride lengths.
105+
- **offset**: index offset.
106+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
107+
108+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a callback function in order to achieve better performance.
109+
110+
</section>
111+
112+
<!-- /.notes -->
113+
114+
<section class="examples">
115+
116+
## Examples
117+
118+
<!-- eslint no-undef: "error" -->
119+
120+
```javascript
121+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
122+
var zeroTo = require( '@stdlib/array/base/zero-to' );
123+
var naryFunction = require( '@stdlib/utils/nary-function' );
124+
var log = require( '@stdlib/console/log' );
125+
var whileEach = require( '@stdlib/ndarray/base/while-each' );
126+
127+
function predicate( value ) {
128+
return value < 6;
129+
}
130+
131+
var x = {
132+
'dtype': 'generic',
133+
'data': zeroTo( 10 ),
134+
'shape': [ 5, 2 ],
135+
'strides': [ -2, 1 ],
136+
'offset': 8,
137+
'order': 'row-major'
138+
};
139+
140+
log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
141+
whileEach( [ x ], predicate, naryFunction( log, 1 ) );
142+
143+
x = {
144+
'dtype': 'generic',
145+
'data': zeroTo( 10 ),
146+
'shape': [ 5, 2 ],
147+
'strides': [ 1, -5 ],
148+
'offset': 5,
149+
'order': 'column-major'
150+
};
151+
152+
log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
153+
whileEach( [ x ], predicate, naryFunction( log, 1 ) );
154+
155+
x = {
156+
'dtype': 'generic',
157+
'data': zeroTo( 18 ),
158+
'shape': [ 2, 3, 3 ],
159+
'strides': [ 9, 3, 1 ],
160+
'offset': 0,
161+
'order': 'row-major'
162+
};
163+
164+
log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
165+
whileEach( [ x ], predicate, naryFunction( log, 1 ) );
166+
167+
x = {
168+
'dtype': 'generic',
169+
'data': zeroTo( 18 ),
170+
'shape': [ 2, 3, 3 ],
171+
'strides': [ -1, -2, -6 ],
172+
'offset': 17,
173+
'order': 'column-major'
174+
};
175+
176+
log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
177+
whileEach( [ x ], predicate, naryFunction( log, 1 ) );
178+
```
179+
180+
</section>
181+
182+
<!-- /.examples -->
183+
184+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
185+
186+
<section class="related">
187+
188+
</section>
189+
190+
<!-- /.related -->
191+
192+
<section class="links">
193+
194+
<!-- <related-links> -->
195+
196+
<!-- </related-links> -->
197+
198+
</section>
199+
200+
<!-- /.links -->
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var floor = require( '@stdlib/math/base/special/floor' );
27+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
28+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var whileEach = require( './../lib/10d_blocked.js' );
32+
33+
34+
// VARIABLES //
35+
36+
var types = [ 'float64' ];
37+
var order = 'column-major';
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Predicate function invoked for each element in an ndarray.
44+
*
45+
* @private
46+
* @param {number} value - array element
47+
* @returns {boolean} result
48+
*/
49+
function predicate( value ) {
50+
return value === value;
51+
}
52+
53+
/**
54+
* Callback invoked for each element in an ndarray.
55+
*
56+
* @private
57+
* @param {number} value - array element
58+
* @throws {Error} unexpected error
59+
*/
60+
function fcn( value ) {
61+
if ( isnan( value ) ) {
62+
throw new Error( 'unexpected error' );
63+
}
64+
}
65+
66+
/**
67+
* Creates a benchmark function.
68+
*
69+
* @private
70+
* @param {PositiveInteger} len - ndarray length
71+
* @param {NonNegativeIntegerArray} shape - ndarray shape
72+
* @param {string} xtype - input ndarray data type
73+
* @returns {Function} benchmark function
74+
*/
75+
function createBenchmark( len, shape, xtype ) {
76+
var x;
77+
78+
x = discreteUniform( len, -100, 100 );
79+
x = {
80+
'dtype': xtype,
81+
'data': x,
82+
'shape': shape,
83+
'strides': shape2strides( shape, order ),
84+
'offset': 0,
85+
'order': order
86+
};
87+
return benchmark;
88+
89+
/**
90+
* Benchmark function.
91+
*
92+
* @private
93+
* @param {Benchmark} b - benchmark instance
94+
*/
95+
function benchmark( b ) {
96+
var i;
97+
98+
b.tic();
99+
for ( i = 0; i < b.iterations; i++ ) {
100+
whileEach( x, predicate, fcn );
101+
if ( isnan( x.data[ i%len ] ) ) {
102+
b.fail( 'should not return NaN' );
103+
}
104+
}
105+
b.toc();
106+
if ( isnan( x.data[ i%len ] ) ) {
107+
b.fail( 'should not return NaN' );
108+
}
109+
b.pass( 'benchmark finished' );
110+
b.end();
111+
}
112+
}
113+
114+
115+
// MAIN //
116+
117+
/**
118+
* Main execution sequence.
119+
*
120+
* @private
121+
*/
122+
function main() {
123+
var len;
124+
var min;
125+
var max;
126+
var sh;
127+
var t1;
128+
var f;
129+
var i;
130+
var j;
131+
132+
min = 1; // 10^min
133+
max = 6; // 10^max
134+
135+
for ( j = 0; j < types.length; j++ ) {
136+
t1 = types[ j ];
137+
for ( i = min; i <= max; i++ ) {
138+
len = pow( 10, i );
139+
140+
sh = [ len/2, 2, 1, 1, 1, 1, 1, 1, 1, 1 ];
141+
f = createBenchmark( len, sh, t1 );
142+
bench( format( '%s::blocked:ndims=%d,len=%d,shape=[%s],xorder=%s,xtype=%s', pkg, sh.length, len, sh.join(','), order, t1 ), f );
143+
144+
sh = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, len/2 ];
145+
f = createBenchmark( len, sh, t1 );
146+
bench( format( '%s::blocked:ndims=%d,len=%d,shape=[%s],xorder=%s,xtype=%s', pkg, sh.length, len, sh.join(','), order, t1 ), f );
147+
148+
len = floor( pow( len, 1.0/10.0 ) );
149+
sh = [ len, len, len, len, len, len, len, len, len, len ];
150+
len *= pow( len, 9 );
151+
f = createBenchmark( len, sh, t1 );
152+
bench( format( '%s::blocked:ndims=%d,len=%d,shape=[%s],xorder=%s,xtype=%s', pkg, sh.length, len, sh.join(','), order, t1 ), f );
153+
}
154+
}
155+
}
156+
157+
main();

0 commit comments

Comments
 (0)