Skip to content

Commit a2fd4d6

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/some
PR-URL: #8514 Closes: stdlib-js/metr-issue-tracker#83 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 974a2c4 commit a2fd4d6

File tree

16 files changed

+3979
-0
lines changed

16 files changed

+3979
-0
lines changed
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
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+
# some
22+
23+
> Test whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions are truthy.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var some = require( '@stdlib/ndarray/some' );
37+
```
38+
39+
#### some( x, n\[, options] )
40+
41+
Tests whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions are truthy.
42+
43+
```javascript
44+
var array = require( '@stdlib/ndarray/array' );
45+
46+
// Create an input ndarray:
47+
var x = array( [ [ [ 1.0, 0.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
48+
// returns <ndarray>
49+
50+
// Perform reduction:
51+
var out = some( x, 3 );
52+
// returns <ndarray>
53+
54+
var v= out.get();
55+
// returns true
56+
```
57+
58+
The function accepts the following arguments:
59+
60+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
61+
- **n**: number of elements which must be truthy. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of input [`ndarray`][@stdlib/ndarray/ctor]. Must have an integer [data type][@stdlib/ndarray/dtypes].
62+
- **options**: function options (_optional_).
63+
64+
The function accepts the following options:
65+
66+
- **dims**: list of dimensions over which to perform a reduction.
67+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [`ndarray`][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
68+
69+
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.
70+
71+
```javascript
72+
var array = require( '@stdlib/ndarray/array' );
73+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
74+
75+
// Create an input ndarray:
76+
var x = array( [ [ [ 1.0, 0.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
77+
// returns <ndarray>
78+
79+
var opts = {
80+
'dims': [ 0, 1 ]
81+
};
82+
83+
// Perform reduction:
84+
var out = some( x, 2, opts );
85+
// returns <ndarray>
86+
87+
var v = ndarray2array( out );
88+
// returns [ true, true ]
89+
```
90+
91+
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`.
92+
93+
```javascript
94+
var array = require( '@stdlib/ndarray/array' );
95+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
96+
97+
// Create an input ndarray:
98+
var x = array( [ [ [ 1.0, 0.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
99+
// returns <ndarray>
100+
101+
var opts = {
102+
'dims': [ 0, 1 ],
103+
'keepdims': true
104+
};
105+
106+
// Perform reduction:
107+
var out = some( x, 2, opts );
108+
// returns <ndarray>
109+
110+
var v = ndarray2array( out );
111+
// returns [ [ [ true, true ] ] ]
112+
```
113+
114+
#### some.assign( x, n, out\[, options] )
115+
116+
Tests whether at least `n` elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions are truthy and assigns the results to an output [`ndarray`][@stdlib/ndarray/ctor].
117+
118+
```javascript
119+
var array = require( '@stdlib/ndarray/array' );
120+
var empty = require( '@stdlib/ndarray/empty' );
121+
122+
// Create an input ndarray:
123+
var x = array( [ [ [ 1.0, 0.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
124+
// returns <ndarray>
125+
126+
// Create an output ndarray:
127+
var y = empty( [], {
128+
'dtype': 'bool'
129+
});
130+
131+
// Perform reduction:
132+
var out = some.assign( x, 3, y );
133+
// returns <ndarray>
134+
135+
var bool = ( out === y );
136+
// returns true
137+
138+
var v = y.get();
139+
// returns true
140+
```
141+
142+
The function accepts the following arguments:
143+
144+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
145+
- **n**: number of elements which must be truthy. May be either a scalar or an [`ndarray`][@stdlib/ndarray/ctor]. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the non-reduced dimensions of input [`ndarray`][@stdlib/ndarray/ctor]. Must have an integer [data type][@stdlib/ndarray/dtypes].
146+
- **out**: 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].
147+
- **options**: function options (_optional_).
148+
149+
The function accepts the following `options`:
150+
151+
- **dims**: list of dimensions over which to perform a reduction.
152+
153+
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.
154+
155+
```javascript
156+
var array = require( '@stdlib/ndarray/array' );
157+
var empty = require( '@stdlib/ndarray/empty' );
158+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
159+
160+
// Create an input ndarray:
161+
var x = array( [ [ [ 1.0, 0.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
162+
// returns <ndarray>
163+
164+
// Create an output ndarray:
165+
var y = empty( [ 2 ], {
166+
'dtype': 'bool'
167+
});
168+
169+
var opts = {
170+
'dims': [ 0, 1 ]
171+
};
172+
173+
// Perform reduction:
174+
var out = some.assign( x, 2, y, opts );
175+
// returns <ndarray>
176+
177+
var bool = ( out === y );
178+
// returns true
179+
180+
var v = ndarray2array( out );
181+
// returns [ true, true ]
182+
```
183+
184+
</section>
185+
186+
<!-- /.usage -->
187+
188+
<section class="notes">
189+
190+
</section>
191+
192+
<!-- /.notes -->
193+
194+
<section class="examples">
195+
196+
## Examples
197+
198+
<!-- eslint no-undef: "error" -->
199+
200+
```javascript
201+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
202+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
203+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
204+
var fillBy = require( '@stdlib/ndarray/fill-by' );
205+
var zeros = require( '@stdlib/ndarray/zeros' );
206+
var some = require( '@stdlib/ndarray/some' );
207+
208+
var x = zeros( [ 2, 4, 5 ], {
209+
'dtype': 'float64'
210+
});
211+
x = fillBy( x, discreteUniform( 0, 10 ) );
212+
console.log( ndarray2array( x ) );
213+
214+
var n = scalar2ndarray( 4, {
215+
'dtype': 'int8'
216+
});
217+
var y = some( x, n );
218+
console.log( y.get() );
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 for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
234+
235+
<section class="links">
236+
237+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
238+
239+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
240+
241+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
242+
243+
</section>
244+
245+
<!-- /.links -->
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
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 ndarray = require( '@stdlib/ndarray/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var some = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var types = [ 'float64' ];
36+
var orders = [ 'row-major', 'column-major' ];
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - ndarray length
46+
* @param {NonNegativeIntegerArray} shape - ndarray shape
47+
* @param {string} xtype - ndarray data type
48+
* @param {string} order - memory layout
49+
* @param {NonNegativeIntegerArray} dims - list of dimensions to reduce
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( len, shape, xtype, order, dims ) {
53+
var x;
54+
55+
x = discreteUniform( len, 0, 10 );
56+
x = new ndarray( xtype, x, shape, shape2strides( shape, order ), 0, order );
57+
58+
return benchmark;
59+
60+
/**
61+
* Benchmark function.
62+
*
63+
* @private
64+
* @param {Benchmark} b - benchmark instance
65+
*/
66+
function benchmark( b ) {
67+
var opts;
68+
var out;
69+
var i;
70+
71+
opts = {
72+
'dims': dims
73+
};
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
out = some( x, len, opts );
78+
if ( typeof out !== 'object' ) {
79+
b.fail( 'should return an ndarray' );
80+
}
81+
}
82+
b.toc();
83+
if ( !isndarrayLike( out ) ) {
84+
b.fail( 'should return an ndarray' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
}
89+
}
90+
91+
92+
// MAIN //
93+
94+
/**
95+
* Main execution sequence.
96+
*
97+
* @private
98+
*/
99+
function main() {
100+
var dims;
101+
var len;
102+
var min;
103+
var max;
104+
var ord;
105+
var sh;
106+
var t1;
107+
var f;
108+
var i;
109+
var j;
110+
var k;
111+
var n;
112+
var d;
113+
114+
min = 1; // 10^min
115+
max = 6; // 10^max
116+
117+
d = [
118+
[ 0 ],
119+
[]
120+
];
121+
122+
for ( n = 0; n < d.length; n++ ) {
123+
dims = d[ n ];
124+
for ( k = 0; k < orders.length; k++ ) {
125+
ord = orders[ k ];
126+
for ( j = 0; j < types.length; j++ ) {
127+
t1 = types[ j ];
128+
for ( i = min; i <= max; i++ ) {
129+
len = pow( 10, i );
130+
131+
sh = [ len ];
132+
f = createBenchmark( len, sh, t1, ord, dims );
133+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1+',dims=['+dims.join(',')+']', f );
134+
}
135+
}
136+
}
137+
}
138+
}
139+
140+
main();

0 commit comments

Comments
 (0)