Skip to content

Commit 2f15c20

Browse files
headlessNodekgryte
authored andcommitted
feat: add ndarray/base/some-by
PR-URL: #7087 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent fe0071e commit 2f15c20

File tree

92 files changed

+15390
-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.

92 files changed

+15390
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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+
# someBy
22+
23+
> Test whether at least `n` 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 someBy = require( '@stdlib/ndarray/base/some-by' );
37+
```
38+
39+
#### someBy( arrays, predicate\[, thisArg ] )
40+
41+
Tests whether at least `n` 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+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
48+
49+
function predicate( value ) {
50+
return value > 0.0;
51+
}
52+
53+
// Create a data buffer:
54+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
55+
56+
// Define the shape of the input array:
57+
var shape = [ 3, 1, 2 ];
58+
59+
// Define the array strides:
60+
var sx = [ 4, 4, 1 ];
61+
62+
// Define the index offset:
63+
var ox = 0;
64+
65+
// Create the input ndarray-like object:
66+
var x = {
67+
'dtype': 'float64',
68+
'data': xbuf,
69+
'shape': shape,
70+
'strides': sx,
71+
'offset': ox,
72+
'order': 'row-major'
73+
};
74+
75+
// Define the success criterion:
76+
var n = scalar2ndarray( 3, {
77+
'dtype': 'generic'
78+
});
79+
80+
// Test elements:
81+
var out = someBy( [ x, n ], predicate );
82+
// returns true
83+
```
84+
85+
The function accepts the following arguments:
86+
87+
- **arrays**: array-like object containing an input ndarray and a zero-dimensional ndarray specifying the minimum number of elements in the input ndarray that must satisfy the predicate function.
88+
- **predicate**: predicate function.
89+
- **thisArg**: predicate function execution context (_optional_).
90+
91+
Each provided ndarray should be an `object` with the following properties:
92+
93+
- **dtype**: data type.
94+
- **data**: data buffer.
95+
- **shape**: dimensions.
96+
- **strides**: stride lengths.
97+
- **offset**: index offset.
98+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
99+
100+
The predicate function is provided the following arguments:
101+
102+
- **value**: current array element.
103+
- **indices**: current array element indices.
104+
- **arr**: the input ndarray.
105+
106+
To set the predicate function execution context, provide a `thisArg`.
107+
108+
<!-- eslint-disable no-invalid-this, max-len -->
109+
110+
```javascript
111+
var Float64Array = require( '@stdlib/array/float64' );
112+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
113+
114+
function predicate( value ) {
115+
this.count += 1;
116+
return value > 0.0;
117+
}
118+
119+
// Create a data buffer:
120+
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 ] );
121+
122+
// Define the shape of the input array:
123+
var shape = [ 3, 1, 2 ];
124+
125+
// Define the array strides:
126+
var sx = [ 4, 4, 1 ];
127+
128+
// Define the index offset:
129+
var ox = 0;
130+
131+
// Create the input ndarray-like object:
132+
var x = {
133+
'dtype': 'float64',
134+
'data': xbuf,
135+
'shape': shape,
136+
'strides': sx,
137+
'offset': ox,
138+
'order': 'row-major'
139+
};
140+
141+
// Define the success criterion:
142+
var n = scalar2ndarray( 6, {
143+
'dtype': 'generic'
144+
});
145+
146+
// Create a context object:
147+
var ctx = {
148+
'count': 0
149+
};
150+
151+
// Test elements:
152+
var out = someBy( [ x, n ], predicate, ctx );
153+
// returns true
154+
155+
var count = ctx.count;
156+
// returns 6
157+
```
158+
159+
</section>
160+
161+
<!-- /.usage -->
162+
163+
<section class="notes">
164+
165+
## Notes
166+
167+
- 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.
168+
169+
</section>
170+
171+
<!-- /.notes -->
172+
173+
<section class="examples">
174+
175+
## Examples
176+
177+
<!-- eslint no-undef: "error" -->
178+
179+
```javascript
180+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
181+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
182+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
183+
var someBy = require( '@stdlib/ndarray/base/some-by' );
184+
185+
function predicate( value ) {
186+
return value > 0;
187+
}
188+
189+
var x = {
190+
'dtype': 'generic',
191+
'data': discreteUniform( 10, -2, 10, {
192+
'dtype': 'generic'
193+
}),
194+
'shape': [ 5, 2 ],
195+
'strides': [ 2, 1 ],
196+
'offset': 0,
197+
'order': 'row-major'
198+
};
199+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
200+
201+
var n = scalar2ndarray( 5, {
202+
'dtype': 'generic'
203+
});
204+
205+
var out = someBy( [ x, n ], predicate );
206+
console.log( out );
207+
```
208+
209+
</section>
210+
211+
<!-- /.examples -->
212+
213+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
214+
215+
<section class="related">
216+
217+
</section>
218+
219+
<!-- /.related -->
220+
221+
<section class="links">
222+
223+
</section>
224+
225+
<!-- /.links -->
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 someBy = 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+
* Predicate 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 = discreteUniform( len, 1, 100 );
62+
x = {
63+
'dtype': xtype,
64+
'data': x,
65+
'shape': shape,
66+
'strides': shape2strides( shape, order ),
67+
'offset': 0,
68+
'order': order
69+
};
70+
return benchmark;
71+
72+
/**
73+
* Benchmark function.
74+
*
75+
* @private
76+
* @param {Benchmark} b - benchmark instance
77+
*/
78+
function benchmark( b ) {
79+
var out;
80+
var i;
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
out = someBy( x, len, clbk );
85+
if ( typeof out !== 'boolean' ) {
86+
b.fail( 'should return a boolean' );
87+
}
88+
}
89+
b.toc();
90+
if ( !isBoolean( out ) ) {
91+
b.fail( 'should return a boolean' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
}
96+
}
97+
98+
99+
// MAIN //
100+
101+
/**
102+
* Main execution sequence.
103+
*
104+
* @private
105+
*/
106+
function main() {
107+
var len;
108+
var min;
109+
var max;
110+
var sh;
111+
var t1;
112+
var f;
113+
var i;
114+
var j;
115+
116+
min = 1; // 10^min
117+
max = 6; // 10^max
118+
119+
for ( j = 0; j < types.length; j++ ) {
120+
t1 = types[ j ];
121+
for ( i = min; i <= max; i++ ) {
122+
len = pow( 10, i );
123+
124+
sh = [ len/2, 2, 1, 1, 1, 1, 1, 1, 1, 1 ];
125+
f = createBenchmark( len, sh, t1 );
126+
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
127+
128+
sh = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, len/2 ];
129+
f = createBenchmark( len, sh, t1 );
130+
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
131+
132+
len = floor( pow( len, 1.0/10.0 ) );
133+
sh = [ len, len, len, len, len, len, len, len, len, len ];
134+
len *= pow( len, 9 );
135+
f = createBenchmark( len, sh, t1 );
136+
bench( pkg+'::blocked:ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
137+
}
138+
}
139+
}
140+
141+
main();

0 commit comments

Comments
 (0)