Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 50b84b5

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/find
PR-URL: #7426 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent bbaf951 commit 50b84b5

Some content is hidden

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

68 files changed

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /