Skip to content

Commit ba14b89

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/any-by
PR-URL: #8110 Ref: #2656 Closes: stdlib-js/metr-issue-tracker#82 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 2ebf45f commit ba14b89

File tree

16 files changed

+3882
-0
lines changed

16 files changed

+3882
-0
lines changed
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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 along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions 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 anyBy = require( '@stdlib/ndarray/any-by' );
37+
```
38+
39+
#### anyBy( x\[, options], predicate\[, thisArg] )
40+
41+
Tests whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions passes a test implemented by a predicate function.
42+
43+
```javascript
44+
var array = require( '@stdlib/ndarray/array' );
45+
46+
function isPositive( value ) {
47+
return value > 0.0;
48+
}
49+
50+
// Create an input ndarray:
51+
var x = array( [ [ 1.0, -2.0 ], [ 3.0, -4.0 ] ] );
52+
53+
// Test whether at least one element is positive:
54+
var out = anyBy( x, isPositive );
55+
// returns <ndarray>
56+
57+
var v = out.get();
58+
// returns true
59+
```
60+
61+
The function accepts the following arguments:
62+
63+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
64+
- **options**: function options _(optional)_.
65+
- **predicate**: predicate function.
66+
- **thisArg**: predicate function execution context _(optional)_.
67+
68+
The function accepts the following options:
69+
70+
- **dims**: list of dimensions over which to perform a reduction.
71+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [`ndarray`][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
72+
73+
By default, the function performs a reduction over all elements in a provided [`ndarray`][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option.
74+
75+
```javascript
76+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
77+
var array = require( '@stdlib/ndarray/array' );
78+
79+
function isPositive( value ) {
80+
return value > 0.0;
81+
}
82+
83+
// Create an input ndarray:
84+
var x = array( [ [ 1.0, 2.0 ], [ -3.0, -4.0 ] ] );
85+
86+
var opts = {
87+
'dims': [ 1 ]
88+
};
89+
90+
// Perform reduction:
91+
var out = anyBy( x, opts, isPositive );
92+
// returns <ndarray>
93+
94+
var v = ndarray2array( out );
95+
// returns [ true, false ]
96+
```
97+
98+
By default, the function returns an [`ndarray`][@stdlib/ndarray/ctor] having a shape matching only the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor] (i.e., the reduced dimensions are dropped). To include the reduced dimensions as singleton dimensions in the output [`ndarray`][@stdlib/ndarray/ctor], set the `keepdims` option to `true`.
99+
100+
```javascript
101+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
102+
var array = require( '@stdlib/ndarray/array' );
103+
104+
function isPositive( value ) {
105+
return value > 0.0;
106+
}
107+
108+
// Create an input ndarray:
109+
var x = array( [ [ 1.0, 2.0 ], [ -3.0, -4.0 ] ] );
110+
111+
var opts = {
112+
'keepdims': true
113+
};
114+
115+
// Perform reduction:
116+
var out = anyBy( x, opts, isPositive );
117+
// returns <ndarray>
118+
119+
var v = ndarray2array( out );
120+
// returns [ [ [ true ] ] ]
121+
```
122+
123+
To set the function execution context, provide a `thisArg`.
124+
125+
```javascript
126+
var array = require( '@stdlib/ndarray/array' );
127+
128+
function isPositive( value ) {
129+
this.count += 1;
130+
return value > 0.0;
131+
}
132+
133+
// Create an input ndarray:
134+
var x = array( [ [ -1.0, -2.0 ], [ -3.0, 4.0 ] ] );
135+
// returns <ndarray>
136+
137+
// Create a context object:
138+
var ctx = {
139+
'count': 0
140+
};
141+
142+
// Perform reduction:
143+
var out = anyBy( x, isPositive, ctx );
144+
// returns <ndarray>
145+
146+
var v = out.get();
147+
// returns true
148+
149+
var count = ctx.count;
150+
// returns 4
151+
```
152+
153+
#### anyBy.assign( x, y\[, options], predicate\[, thisArg] )
154+
155+
Tests whether at least one element along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions passes a test implemented by a predicate function and assigns the result to a provided output [`ndarray`][@stdlib/ndarray/ctor].
156+
157+
```javascript
158+
var array = require( '@stdlib/ndarray/array' );
159+
var empty = require( '@stdlib/ndarray/empty' );
160+
161+
function isPositive( value ) {
162+
return value > 0.0;
163+
}
164+
165+
// Create an input ndarray:
166+
var x = array( [ [ 1.0, -2.0 ], [ 3.0, -4.0 ] ] );
167+
168+
// Create an output ndarray:
169+
var y = empty( [], {
170+
'dtype': 'bool'
171+
});
172+
173+
// Perform reduction:
174+
var out = anyBy.assign( x, y, isPositive );
175+
// returns <ndarray>
176+
177+
var v = out.get();
178+
// returns true
179+
180+
var bool = ( out === y );
181+
// returns true
182+
```
183+
184+
The function accepts the following arguments:
185+
186+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
187+
- **y**: output [`ndarray`][@stdlib/ndarray/ctor]. The output [`ndarray`][@stdlib/ndarray/ctor] must have a shape matching the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor].
188+
- **options**: function options _(optional)_.
189+
- **predicate**: predicate function.
190+
- **thisArg**: predicate function execution context _(optional)_.
191+
192+
The function accepts the following options:
193+
194+
- **dims**: list of dimensions over which to perform a reduction.
195+
196+
By default, the function performs a reduction over all elements in a provided [`ndarray`][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option.
197+
198+
```javascript
199+
var array = require( '@stdlib/ndarray/array' );
200+
var empty = require( '@stdlib/ndarray/empty' );
201+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
202+
203+
function predicate( value ) {
204+
return value > 0.0;
205+
}
206+
207+
// Create an input ndarray:
208+
var x = array( [ [ 1.0, 2.0 ], [ -3.0, -4.0 ] ] );
209+
// returns <ndarray>
210+
211+
// Create an output ndarray:
212+
var y = empty( [ 2 ], {
213+
'dtype': 'bool'
214+
});
215+
216+
var opts = {
217+
'dims': [ 1 ]
218+
};
219+
220+
// Perform reduction:
221+
var out = anyBy.assign( x, y, opts, predicate );
222+
223+
var bool = ( out === y );
224+
// returns true
225+
226+
var v = ndarray2array( y );
227+
// returns [ true, false ]
228+
```
229+
230+
</section>
231+
232+
<!-- /.usage -->
233+
234+
<section class="notes">
235+
236+
## Notes
237+
238+
- The predicate function is provided the following arguments:
239+
240+
- **value**: current array element.
241+
- **indices**: current array element indices.
242+
- **arr**: the input ndarray.
243+
244+
</section>
245+
246+
<!-- /.notes -->
247+
248+
<section class="examples">
249+
250+
## Examples
251+
252+
<!-- eslint no-undef: "error" -->
253+
254+
```javascript
255+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
256+
var isEven = require( '@stdlib/assert/is-even' ).isPrimitive;
257+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
258+
var fillBy = require( '@stdlib/ndarray/fill-by' );
259+
var zeros = require( '@stdlib/ndarray/zeros' );
260+
var anyBy = require( '@stdlib/ndarray/any-by' );
261+
262+
var x = zeros( [ 2, 4, 5 ], {
263+
'dtype': 'float64'
264+
});
265+
x = fillBy( x, discreteUniform( 0, 10 ) );
266+
console.log( ndarray2array( x ) );
267+
268+
var y = anyBy( x, isEven );
269+
console.log( y.get() );
270+
```
271+
272+
</section>
273+
274+
<!-- /.examples -->
275+
276+
<!-- Section for related links. Do not manually edit this section, as it is automatically populated. -->
277+
278+
<section class="related">
279+
280+
</section>
281+
282+
<!-- /.related -->
283+
284+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
285+
286+
<section class="links">
287+
288+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib
289+
290+
</section>
291+
292+
<!-- /.links -->

0 commit comments

Comments
 (0)