Skip to content

Commit 96241d9

Browse files
committed
feat: add ndarray/find
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 4301694 commit 96241d9

File tree

11 files changed

+1960
-0
lines changed

11 files changed

+1960
-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) 2024 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+
# findElement
22+
23+
> Return the first element in the [ndarray][@stdlib/ndarray/ctor] that 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+
```javascript
36+
var findElement = require( '@stdlib/ndarray/find' );
37+
```
38+
39+
#### findElement( x\[, options], predicate\[, thisArg] )
40+
41+
Return the first element in the [ndarray][@stdlib/ndarray/ctor] that passes a test implemented by a predicate function.
42+
43+
<!-- eslint-disable no-invalid-this, max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var ndarray = require( '@stdlib/ndarray/ctor' );
48+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
49+
50+
function predicate( z ) {
51+
return z > 6.0;
52+
}
53+
54+
var buffer = 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 ] );
55+
var shape = [ 2, 3 ];
56+
var strides = [ 6, 1 ];
57+
var offset = 1;
58+
59+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
60+
// returns <ndarray>
61+
62+
var arr = ndarray2array( x );
63+
// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ]
64+
65+
var y = findElement( x, predicate );
66+
// returns 8.0
67+
```
68+
69+
The function accepts the following arguments:
70+
71+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
72+
- **options**: function options _(optional)_.
73+
- **predicate**: predicate function.
74+
- **thisArg**: predicate function execution context _(optional)_.
75+
76+
The function accepts the following options:
77+
78+
- **order**: index iteration order. By default, the function iterates over elements according to the [layout order][@stdlib/ndarray/orders] of the provided [ndarray][@stdlib/ndarray/ctor]. Accordingly, for row-major input [ndarrays][@stdlib/ndarray/ctor], the last dimension indices increment fastest. For column-major input [ndarrays][@stdlib/ndarray/ctor], the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manner, regardless of the input [ndarray][@stdlib/ndarray/ctor]'s layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input [ndarray][@stdlib/ndarray/ctor] may, in some circumstances, result in performance degradation due to cache misses. Must be either `'row-major'` or `'column-major'`.
79+
80+
By default, the output element's [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor].
81+
82+
<!-- eslint-disable max-len -->
83+
84+
```javascript
85+
var Float64Array = require( '@stdlib/array/float64' );
86+
var ndarray = require( '@stdlib/ndarray/ctor' );
87+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
88+
89+
function predicate( z ) {
90+
return z > 3.0;
91+
}
92+
93+
var buffer = 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 ] );
94+
var shape = [ 2, 3 ];
95+
var strides = [ 6, 1 ];
96+
var offset = 1;
97+
98+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
99+
// returns <ndarray>
100+
101+
var arr = ndarray2array( x );
102+
// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ]
103+
104+
var opts = {
105+
'order': 'column-major'
106+
};
107+
var y = findElement( x, opts, predicate );
108+
// returns 8.0
109+
110+
opts = {
111+
'order': 'row-major'
112+
};
113+
y = findElement( x, opts, predicate );
114+
// returns 4.0
115+
```
116+
117+
To set the `predicate` function execution context, provide a `thisArg`.
118+
119+
<!-- eslint-disable no-invalid-this, max-len -->
120+
121+
```javascript
122+
var Float64Array = require( '@stdlib/array/float64' );
123+
var ndarray = require( '@stdlib/ndarray/ctor' );
124+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
125+
126+
function predicate( z ) {
127+
this.count += 1;
128+
return z > 6.0;
129+
}
130+
131+
var buffer = 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 ] );
132+
var shape = [ 2, 3 ];
133+
var strides = [ 6, 1 ];
134+
var offset = 1;
135+
136+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
137+
// returns <ndarray>
138+
139+
var arr = ndarray2array( x );
140+
// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ]
141+
142+
var ctx = {
143+
'count': 0
144+
};
145+
var y = findElement( x, predicate, ctx );
146+
// returns 8.0
147+
148+
var count = ctx.count;
149+
// returns 4
150+
```
151+
152+
The `predicate` function is provided the following arguments:
153+
154+
- **value**: current array element.
155+
- **indices**: current array element indices.
156+
- **arr**: the input [ndarray][@stdlib/ndarray/ctor].
157+
158+
</section>
159+
160+
<!-- /.usage -->
161+
162+
<section class="notes">
163+
164+
## Notes
165+
166+
- The function returns **NaN** if no element in ndarray passes test implemented by the predicate function.
167+
168+
</section>
169+
170+
<!-- /.notes -->
171+
172+
<section class="examples">
173+
174+
## Examples
175+
176+
<!-- eslint no-undef: "error" -->
177+
178+
```javascript
179+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
180+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
181+
var naryFunction = require( '@stdlib/utils/nary-function' );
182+
var array = require( '@stdlib/ndarray/array' );
183+
var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive;
184+
var findElement = require( '@stdlib/ndarray/find' );
185+
186+
var buffer = discreteUniform( 10, -100, 100, {
187+
'dtype': 'generic'
188+
});
189+
var x = array( buffer, {
190+
'shape': [ 5, 2 ],
191+
'dtype': 'generic'
192+
});
193+
console.log( ndarray2array( x ) );
194+
195+
var y = findElement( x, naryFunction( isPositive, 1 ) );
196+
console.log( y );
197+
```
198+
199+
</section>
200+
201+
<!-- /.examples -->
202+
203+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
204+
205+
<section class="related">
206+
207+
</section>
208+
209+
<!-- /.related -->
210+
211+
<section class="links">
212+
213+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
214+
215+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
216+
217+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
218+
219+
<!-- <related-links> -->
220+
221+
<!-- </related-links> -->
222+
223+
</section>
224+
225+
<!-- /.links -->
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isInteger = require( '@stdlib/assert/is-integer' );
27+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
28+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
29+
var ndarray = require( '@stdlib/ndarray/ctor' );
30+
var pkg = require( './../package.json' ).name;
31+
var findElement = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var xtypes = [ 'generic' ];
37+
var orders = [ 'row-major', 'column-major' ];
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Predicate function.
44+
*
45+
* @private
46+
* @param {number} value - array element
47+
* @param {NonNegativeIntegerArray} indices - element indices
48+
* @param {ndarray} arr - input array
49+
* @returns {boolean} result
50+
*/
51+
function predicate( value ) {
52+
return value > 0.0;
53+
}
54+
55+
/**
56+
* Creates a benchmark function.
57+
*
58+
* @private
59+
* @param {PositiveInteger} len - array length
60+
* @param {NonNegativeIntegerArray} shape - ndarray shape
61+
* @param {string} xtype - input ndarray data type
62+
* @param {string} order - ndarray memory layout
63+
* @returns {Function} benchmark function
64+
*/
65+
function createBenchmark( len, shape, xtype, order ) {
66+
var strides;
67+
var xbuf;
68+
var x;
69+
70+
xbuf = discreteUniform( len, -100, 100, {
71+
'dtype': xtype
72+
});
73+
strides = shape2strides( shape, order );
74+
x = ndarray( xtype, xbuf, shape, strides, 0, 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 y;
86+
var i;
87+
88+
b.tic();
89+
for ( i = 0; i < b.iterations; i++ ) {
90+
y = findElement( x, predicate );
91+
if ( isnan( y ) ) {
92+
b.fail( 'should not return NaN' );
93+
}
94+
}
95+
b.toc();
96+
if ( !isInteger( y ) ) {
97+
b.fail( 'should return an ndarray' );
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 ord;
117+
var sh;
118+
var t1;
119+
var f;
120+
var i;
121+
var j;
122+
var k;
123+
124+
min = 1; // 10^min
125+
max = 6; // 10^max
126+
127+
for ( k = 0; k < orders.length; k++ ) {
128+
ord = orders[ k ];
129+
for ( j = 0; j < xtypes.length; j++ ) {
130+
t1 = xtypes[ j ];
131+
for ( i = min; i <= max; i++ ) {
132+
len = pow( 10, i );
133+
134+
sh = [ len ];
135+
f = createBenchmark( len, sh, t1, ord );
136+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f );
137+
}
138+
}
139+
}
140+
}
141+
142+
main();

0 commit comments

Comments
 (0)