Skip to content

Commit 00450cc

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/every-by
PR-URL: #6667 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 11547d9 commit 00450cc

File tree

93 files changed

+15192
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+15192
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# everyBy
22+
23+
> Test whether all elements in an ndarray pass a test implemented by a predicate function.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var everyBy = require( '@stdlib/ndarray/base/every-by' );
37+
```
38+
39+
#### everyBy( arrays, predicate\[, thisArg] )
40+
41+
Tests whether all elements in an ndarray pass a test implemented by a predicate function.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
48+
function clbk( value ) {
49+
return value > 0.0;
50+
}
51+
52+
// Create a data buffer:
53+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
54+
55+
// Define the shape of the input 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 = 0;
63+
64+
// Create the input 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+
// Test elements:
75+
var out = everyBy( [ x ], clbk );
76+
// returns true
77+
```
78+
79+
The function accepts the following arguments:
80+
81+
- **arrays**: array-like object containing an input ndarray.
82+
- **predicate**: predicate function.
83+
- **thisArg**: predicate function execution context (_optional_).
84+
85+
The provided ndarray should be an `object` with the following properties:
86+
87+
- **dtype**: data type.
88+
- **data**: data buffer.
89+
- **shape**: dimensions.
90+
- **strides**: stride lengths.
91+
- **offset**: index offset.
92+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
93+
94+
The predicate function is provided the following arguments:
95+
96+
- **value**: current array element.
97+
- **indices**: current array element indices.
98+
- **arr**: the input ndarray.
99+
100+
To set the predicate function execution context, provide a `thisArg`.
101+
102+
<!-- eslint-disable no-invalid-this, max-len -->
103+
104+
```javascript
105+
var Float64Array = require( '@stdlib/array/float64' );
106+
107+
function clbk( value ) {
108+
this.count += 1;
109+
return value > 0.0;
110+
}
111+
112+
// Create a data buffer:
113+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
114+
115+
// Define the shape of the input array:
116+
var shape = [ 3, 1, 2 ];
117+
118+
// Define the array strides:
119+
var sx = [ 4, 4, 1 ];
120+
121+
// Define the index offset:
122+
var ox = 0;
123+
124+
// Create the input ndarray-like object:
125+
var x = {
126+
'dtype': 'float64',
127+
'data': xbuf,
128+
'shape': shape,
129+
'strides': sx,
130+
'offset': ox,
131+
'order': 'row-major'
132+
};
133+
134+
var ctx = {
135+
'count': 0
136+
};
137+
138+
// Test elements:
139+
var out = everyBy( [ x ], clbk, ctx );
140+
// returns true
141+
142+
var count = ctx.count;
143+
// returns 6
144+
```
145+
146+
</section>
147+
148+
<!-- /.usage -->
149+
150+
<section class="notes">
151+
152+
## Notes
153+
154+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing the operation in order to achieve better performance.
155+
- If provided an empty ndarray, the function returns `true`.
156+
157+
</section>
158+
159+
<!-- /.notes -->
160+
161+
<section class="examples">
162+
163+
## Examples
164+
165+
<!-- eslint no-undef: "error" -->
166+
167+
```javascript
168+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
169+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
170+
var everyBy = require( '@stdlib/ndarray/base/every-by' );
171+
172+
function clbk( value ) {
173+
return value > 0;
174+
}
175+
176+
var x = {
177+
'dtype': 'generic',
178+
'data': discreteUniform( 10, -2, 10, {
179+
'dtype': 'generic'
180+
}),
181+
'shape': [ 5, 2 ],
182+
'strides': [ 2, 1 ],
183+
'offset': 0,
184+
'order': 'row-major'
185+
};
186+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
187+
188+
var out = everyBy( [ x ], clbk );
189+
console.log( out );
190+
```
191+
192+
</section>
193+
194+
<!-- /.examples -->
195+
196+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
197+
198+
<section class="related">
199+
200+
</section>
201+
202+
<!-- /.related -->
203+
204+
<section class="links">
205+
206+
</section>
207+
208+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
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 pkg = require( './../package.json' ).name;
30+
var everyBy = require( './../lib/10d_blocked.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var types = [ 'float64' ];
36+
var order = 'column-major';
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Callback function.
43+
*
44+
* @param {*} value - ndarray element
45+
* @returns {boolean} result
46+
*/
47+
function clbk( value ) {
48+
return value > 0.0;
49+
}
50+
51+
/**
52+
* Creates a benchmark function.
53+
*
54+
* @private
55+
* @param {PositiveInteger} len - ndarray length
56+
* @param {NonNegativeIntegerArray} shape - ndarray shape
57+
* @param {string} xtype - ndarray data type
58+
* @returns {Function} benchmark function
59+
*/
60+
function createBenchmark( len, shape, xtype ) {
61+
var x;
62+
63+
x = discreteUniform( len, 1, 100 );
64+
x = {
65+
'dtype': xtype,
66+
'data': x,
67+
'shape': shape,
68+
'strides': shape2strides( shape, order ),
69+
'offset': 0,
70+
'order': order
71+
};
72+
return benchmark;
73+
74+
/**
75+
* Benchmark function.
76+
*
77+
* @private
78+
* @param {Benchmark} b - benchmark instance
79+
*/
80+
function benchmark( b ) {
81+
var out;
82+
var i;
83+
84+
b.tic();
85+
for ( i = 0; i < b.iterations; i++ ) {
86+
out = everyBy( x, clbk );
87+
if ( typeof out !== 'boolean' ) {
88+
b.fail( 'should return a boolean' );
89+
}
90+
}
91+
b.toc();
92+
if ( !isBoolean( out ) ) {
93+
b.fail( 'should return a boolean' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
}
98+
}
99+
100+
101+
// MAIN //
102+
103+
/**
104+
* Main execution sequence.
105+
*
106+
* @private
107+
*/
108+
function main() {
109+
var len;
110+
var min;
111+
var max;
112+
var sh;
113+
var t1;
114+
var f;
115+
var i;
116+
var j;
117+
118+
min = 1; // 10^min
119+
max = 6; // 10^max
120+
121+
for ( j = 0; j < types.length; j++ ) {
122+
t1 = types[ j ];
123+
for ( i = min; i <= max; i++ ) {
124+
len = pow( 10, i );
125+
126+
sh = [ len/2, 2, 1, 1, 1, 1, 1, 1, 1, 1 ];
127+
f = createBenchmark( len, sh, t1 );
128+
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
129+
130+
sh = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, len/2 ];
131+
f = createBenchmark( len, sh, t1 );
132+
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
133+
134+
len = floor( pow( len, 1.0/10.0 ) );
135+
sh = [ len, len, len, len, len, len, len, len, len, len ];
136+
len *= pow( len, 9 );
137+
f = createBenchmark( len, sh, t1 );
138+
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
139+
}
140+
}
141+
}
142+
143+
main();

0 commit comments

Comments
 (0)