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