Skip to content

Commit b97de57

Browse files
committed
feat: add ndarray/base/find
1 parent 36e56bb commit b97de57

34 files changed

+4211
-0
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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+
# find
22+
23+
> Return the first element in an ndarray which 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 find = require( '@stdlib/ndarray/base/find' );
37+
```
38+
39+
#### find( arrays, predicate\[, thisArg] )
40+
41+
Returns the first element in an ndarray which 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 % 2.0 === 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 sentinel value ndarray-like object:
65+
var sentinelValue = {
66+
'dtype': 'float64',
67+
'data': new Float64Array( [ -1.0 ] ),
68+
'shape': [],
69+
'strides': [ 0 ],
70+
'offset': 0,
71+
'order': 'row-major'
72+
};
73+
74+
// Create the input ndarray-like object:
75+
var x = {
76+
'dtype': 'float64',
77+
'data': xbuf,
78+
'shape': shape,
79+
'strides': sx,
80+
'offset': ox,
81+
'order': 'row-major'
82+
};
83+
84+
// Perform reduction:
85+
var out = find( [ x, sentinelValue ], clbk );
86+
// returns 2.0
87+
```
88+
89+
The function accepts the following arguments:
90+
91+
- **arrays**: array-like object containing an input ndarray and a zero-dimensional sentinel value ndarray.
92+
- **predicate**: predicate function.
93+
- **thisArg**: predicate function execution context (_optional_).
94+
95+
Each provided ndarray should be an object with the following properties:
96+
97+
- **dtype**: data type.
98+
- **data**: data buffer.
99+
- **shape**: dimensions.
100+
- **strides**: stride lengths.
101+
- **offset**: index offset.
102+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
103+
104+
The predicate function is provided the following arguments:
105+
106+
- **value**: current array element.
107+
- **indices**: current array element indices.
108+
- **arr**: the input ndarray.
109+
110+
To set the predicate function execution context, provide a `thisArg`.
111+
112+
<!-- eslint-disable no-invalid-this, max-len -->
113+
114+
```javascript
115+
var Float64Array = require( '@stdlib/array/float64' );
116+
117+
function clbk( value ) {
118+
this.count += 1;
119+
return value % 2.0 === 0.0;
120+
}
121+
122+
// Create a data buffer:
123+
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 ] );
124+
125+
// Define the shape of the input array:
126+
var shape = [ 3, 1, 2 ];
127+
128+
// Define the array strides:
129+
var sx = [ 4, 4, 1 ];
130+
131+
// Define the index offset:
132+
var ox = 0;
133+
134+
// Create the sentinel value ndarray-like object:
135+
var sentinelValue = {
136+
'dtype': 'float64',
137+
'data': new Float64Array( [ -1.0 ] ),
138+
'shape': [],
139+
'strides': [ 0 ],
140+
'offset': 0,
141+
'order': 'row-major'
142+
};
143+
144+
// Create the input ndarray-like object:
145+
var x = {
146+
'dtype': 'float64',
147+
'data': xbuf,
148+
'shape': shape,
149+
'strides': sx,
150+
'offset': ox,
151+
'order': 'row-major'
152+
};
153+
154+
var ctx = {
155+
'count': 0
156+
};
157+
158+
// Test elements:
159+
var out = find( [ x, sentinelValue ], clbk, ctx );
160+
// returns 2.0
161+
162+
var count = ctx.count;
163+
// returns 2
164+
```
165+
166+
</section>
167+
168+
<!-- /.usage -->
169+
170+
<section class="notes">
171+
172+
## Notes
173+
174+
- 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.
175+
176+
</section>
177+
178+
<!-- /.notes -->
179+
180+
<section class="examples">
181+
182+
## Examples
183+
184+
<!-- eslint no-undef: "error" -->
185+
186+
```javascript
187+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
188+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
189+
var find = require( '@stdlib/ndarray/base/find' );
190+
191+
function clbk( value ) {
192+
return value % 2.0 === 0.0;
193+
}
194+
195+
var x = {
196+
'dtype': 'generic',
197+
'data': discreteUniform( 10, 0, 10, {
198+
'dtype': 'generic'
199+
}),
200+
'shape': [ 5, 2 ],
201+
'strides': [ 2, 1 ],
202+
'offset': 0,
203+
'order': 'row-major'
204+
};
205+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
206+
207+
var sv = {
208+
'dtype': x.dtype,
209+
'data': [ -1 ],
210+
'shape': [],
211+
'strides': [ 0 ],
212+
'offset': 0,
213+
'order': x.order
214+
};
215+
console.log( 'Sentinel Value: %d', sv.data[ 0 ] );
216+
217+
var out = find( [ x, sv ], clbk );
218+
console.log( out );
219+
```
220+
221+
</section>
222+
223+
<!-- /.examples -->
224+
225+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
226+
227+
<section class="related">
228+
229+
</section>
230+
231+
<!-- /.related -->
232+
233+
<section class="links">
234+
235+
<!-- <related-links> -->
236+
237+
<!-- </related-links> -->
238+
239+
</section>
240+
241+
<!-- /.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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
27+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
28+
var pkg = require( './../package.json' ).name;
29+
var find = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var types = [ 'float64' ];
35+
var order = 'column-major';
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Callback function.
42+
*
43+
* @param {*} value - ndarray element
44+
* @returns {boolean} result
45+
*/
46+
function clbk( value ) {
47+
return value % 2.0 === 0.0;
48+
}
49+
50+
/**
51+
* Creates a benchmark function.
52+
*
53+
* @private
54+
* @param {PositiveInteger} len - ndarray length
55+
* @param {NonNegativeIntegerArray} shape - ndarray shape
56+
* @param {string} xtype - ndarray data type
57+
* @returns {Function} benchmark function
58+
*/
59+
function createBenchmark( len, shape, xtype ) {
60+
var x;
61+
var v;
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+
v = {
73+
'dtype': xtype,
74+
'data': [ -1.0 ],
75+
'shape': [],
76+
'strides': [ 0 ],
77+
'offset': 0,
78+
'order': order
79+
};
80+
return benchmark;
81+
82+
/**
83+
* Benchmark function.
84+
*
85+
* @private
86+
* @param {Benchmark} b - benchmark instance
87+
*/
88+
function benchmark( b ) {
89+
var out;
90+
var i;
91+
92+
b.tic();
93+
for ( i = 0; i < b.iterations; i++ ) {
94+
out = find( [ x, v ], clbk );
95+
if ( typeof out !== 'boolean' ) {
96+
b.fail( 'should not return NaN' );
97+
}
98+
}
99+
b.toc();
100+
if ( !isnan( out ) ) {
101+
b.fail( 'should not return NaN' );
102+
}
103+
b.pass( 'benchmark finished' );
104+
b.end();
105+
}
106+
}
107+
108+
109+
// MAIN //
110+
111+
/**
112+
* Main execution sequence.
113+
*
114+
* @private
115+
*/
116+
function main() {
117+
var len;
118+
var min;
119+
var max;
120+
var sh;
121+
var t1;
122+
var f;
123+
var i;
124+
var j;
125+
126+
min = 1; // 10^min
127+
max = 6; // 10^max
128+
129+
for ( j = 0; j < types.length; j++ ) {
130+
t1 = types[ j ];
131+
for ( i = min; i <= max; i++ ) {
132+
len = pow( 10, i );
133+
134+
sh = [ len ];
135+
f = createBenchmark( len, sh, t1 );
136+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
137+
}
138+
}
139+
}
140+
141+
main();

0 commit comments

Comments
 (0)