Skip to content

Commit f6362fc

Browse files
committed
feat: add ndarray/base/any-by
--- 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 ---
1 parent 44999d8 commit f6362fc

39 files changed

+8492
-0
lines changed
Lines changed: 208 additions & 0 deletions
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+
# anyBy
22+
23+
> Test whether at least one element 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 anyBy = require( '@stdlib/ndarray/base/any-by' );
37+
```
38+
39+
#### anyBy( arrays, predicate\[, thisArg] )
40+
41+
Tests whether at least one element 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 = anyBy( [ 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 = anyBy( [ x ], clbk, ctx );
140+
// returns false
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 `false`.
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 anyBy = require( '@stdlib/ndarray/base/any-by' );
171+
172+
function clbk( value ) {
173+
return value > 0;
174+
}
175+
176+
var x = {
177+
'dtype': 'generic',
178+
'data': discreteUniform( 10, -5, 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 = anyBy( [ 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 -->
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
27+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
28+
var pkg = require( './../package.json' ).name;
29+
var anyBy = 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 < 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+
62+
x = discreteUniform( len, 1, 100 );
63+
x = {
64+
'dtype': xtype,
65+
'data': x,
66+
'shape': shape,
67+
'strides': shape2strides( shape, order ),
68+
'offset': 0,
69+
'order': order
70+
};
71+
return benchmark;
72+
73+
/**
74+
* Benchmark function.
75+
*
76+
* @private
77+
* @param {Benchmark} b - benchmark instance
78+
*/
79+
function benchmark( b ) {
80+
var out;
81+
var i;
82+
83+
b.tic();
84+
for ( i = 0; i < b.iterations; i++ ) {
85+
out = anyBy( [ x ], clbk );
86+
if ( typeof out !== 'boolean' ) {
87+
b.fail( 'should return a boolean' );
88+
}
89+
}
90+
b.toc();
91+
if ( !isBoolean( out ) ) {
92+
b.fail( 'should return a boolean' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
}
97+
}
98+
99+
100+
// MAIN //
101+
102+
/**
103+
* Main execution sequence.
104+
*
105+
* @private
106+
*/
107+
function main() {
108+
var len;
109+
var min;
110+
var max;
111+
var sh;
112+
var t1;
113+
var f;
114+
var i;
115+
var j;
116+
117+
min = 1; // 10^min
118+
max = 6; // 10^max
119+
120+
for ( j = 0; j < types.length; j++ ) {
121+
t1 = types[ j ];
122+
for ( i = min; i <= max; i++ ) {
123+
len = pow( 10, i );
124+
125+
sh = [ len ];
126+
f = createBenchmark( len, sh, t1 );
127+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
128+
}
129+
}
130+
}
131+
132+
main();

0 commit comments

Comments
 (0)