|
| 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 in an [`ndarray`][@stdlib/ndarray/ctor] pass a test implemented by a predicate function along one or more dimensions. |
| 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/someBy' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### someBy( x, n\[, options], predicate\[, thisArg] ) |
| 40 | + |
| 41 | +Tests whether at least `n` elements in an [`ndarray`][@stdlib/ndarray/ctor] pass a test implemented by a predicate function along one or more dimensions. |
| 42 | + |
| 43 | +<!-- eslint-disable max-len --> |
| 44 | + |
| 45 | +```javascript |
| 46 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 47 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 48 | + |
| 49 | +function predicate( value ) { |
| 50 | + return value > 0.0; |
| 51 | +} |
| 52 | + |
| 53 | +// Create a data buffer: |
| 54 | +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 ] ); |
| 55 | + |
| 56 | +// Define the shape of the input array: |
| 57 | +var sh = [ 3, 1, 2 ]; |
| 58 | + |
| 59 | +// Define the array strides: |
| 60 | +var sx = [ 4, 4, 1 ]; |
| 61 | + |
| 62 | +// Define the index offset: |
| 63 | +var ox = 1; |
| 64 | + |
| 65 | +// Create an input ndarray: |
| 66 | +var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); |
| 67 | + |
| 68 | +// Perform reduction: |
| 69 | +var out = someBy( x, 2, predicate ); |
| 70 | +// returns <ndarray> |
| 71 | + |
| 72 | +console.log( out.get() ); |
| 73 | +// => true |
| 74 | +``` |
| 75 | + |
| 76 | +The function accepts the following arguments: |
| 77 | + |
| 78 | +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. |
| 79 | +- **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]. |
| 80 | +- **options**: function options (_optional_). |
| 81 | +- **predicate**: predicate function. |
| 82 | +- **thisArg**: predicate execution context (_optional_). |
| 83 | + |
| 84 | +The function accepts the following `options`: |
| 85 | + |
| 86 | +- **dims**: list of dimensions over which to perform a reduction. |
| 87 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [`ndarray`][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 88 | + |
| 89 | +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. |
| 90 | + |
| 91 | +<!-- eslint-disable max-len --> |
| 92 | + |
| 93 | +```javascript |
| 94 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 95 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 96 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 97 | + |
| 98 | +function predicate( value ) { |
| 99 | + return value > 0.0; |
| 100 | +} |
| 101 | + |
| 102 | +// Create a data buffer: |
| 103 | +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 ] ); |
| 104 | + |
| 105 | +// Define the shape of the input array: |
| 106 | +var sh = [ 3, 1, 2 ]; |
| 107 | + |
| 108 | +// Define the array strides: |
| 109 | +var sx = [ 4, 4, 1 ]; |
| 110 | + |
| 111 | +// Define the index offset: |
| 112 | +var ox = 1; |
| 113 | + |
| 114 | +// Create an input ndarray: |
| 115 | +var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); |
| 116 | + |
| 117 | +var opts = { |
| 118 | + 'dims': [ 0, 1 ] |
| 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 | +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`. |
| 130 | + |
| 131 | +<!-- eslint-disable max-len --> |
| 132 | + |
| 133 | +```javascript |
| 134 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 135 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 136 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 137 | + |
| 138 | +function predicate( value ) { |
| 139 | + return value > 0.0; |
| 140 | +} |
| 141 | + |
| 142 | +// Create a data buffer: |
| 143 | +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 ] ); |
| 144 | + |
| 145 | +// Define the shape of the input array: |
| 146 | +var sh = [ 3, 1, 2 ]; |
| 147 | + |
| 148 | +// Define the array strides: |
| 149 | +var sx = [ 4, 4, 1 ]; |
| 150 | + |
| 151 | +// Define the index offset: |
| 152 | +var ox = 1; |
| 153 | + |
| 154 | +// Create an input ndarray: |
| 155 | +var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); |
| 156 | + |
| 157 | +var opts = { |
| 158 | + 'dims': [ 0, 1 ], |
| 159 | + 'keepdims': true |
| 160 | +}; |
| 161 | + |
| 162 | +// Perform reduction: |
| 163 | +var out = someBy( x, 2, opts, predicate ); |
| 164 | +// returns <ndarray> |
| 165 | + |
| 166 | +var v = ndarray2array( out ); |
| 167 | +// returns [[[ true, true ]]] |
| 168 | +``` |
| 169 | + |
| 170 | +To set the predicate function execution context, provide a `thisArg`. |
| 171 | + |
| 172 | +<!-- eslint-disable no-invalid-this, max-len --> |
| 173 | + |
| 174 | +```javascript |
| 175 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 176 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 177 | + |
| 178 | +function predicate( value ) { |
| 179 | + this.count += 1; |
| 180 | + return value > 0.0; |
| 181 | +} |
| 182 | + |
| 183 | +// Create a data buffer: |
| 184 | +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 ] ); |
| 185 | + |
| 186 | +// Define the shape of the input array: |
| 187 | +var sh = [ 3, 1, 2 ]; |
| 188 | + |
| 189 | +// Define the array strides: |
| 190 | +var sx = [ 4, 4, 1 ]; |
| 191 | + |
| 192 | +// Define the index offset: |
| 193 | +var ox = 1; |
| 194 | + |
| 195 | +// Create an input ndarray: |
| 196 | +var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); |
| 197 | + |
| 198 | +// Create a context object: |
| 199 | +var ctx = { |
| 200 | + 'count': 0 |
| 201 | +}; |
| 202 | + |
| 203 | +// Perform operation: |
| 204 | +var out = someBy( x, 2, predicate, ctx ); |
| 205 | +// returns <ndarray> |
| 206 | + |
| 207 | +var v = out.get(); |
| 208 | +// returns true |
| 209 | + |
| 210 | +var count = ctx.count; |
| 211 | +// returns 6 |
| 212 | +``` |
| 213 | + |
| 214 | +#### someBy.assign( x, n, out\[, options], predicate\[, thisArg] ) |
| 215 | + |
| 216 | +Test whether at least `n` elements in an [`ndarray`][@stdlib/ndarray/ctor] pass a test implemented by a predicate function along one or more dimensions and assigns results to a provided output [`ndarray`][@stdlib/ndarray/ctor]. |
| 217 | + |
| 218 | +<!-- eslint-disable max-len --> |
| 219 | + |
| 220 | +```javascript |
| 221 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 222 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 223 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 224 | + |
| 225 | +function predicate( value ) { |
| 226 | + return value > 0.0; |
| 227 | +} |
| 228 | + |
| 229 | +// Create a data buffer: |
| 230 | +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 ] ); |
| 231 | + |
| 232 | +// Define the shape of the input array: |
| 233 | +var sh = [ 3, 1, 2 ]; |
| 234 | + |
| 235 | +// Define the array strides: |
| 236 | +var sx = [ 4, 4, 1 ]; |
| 237 | + |
| 238 | +// Define the index offset: |
| 239 | +var ox = 1; |
| 240 | + |
| 241 | +// Create an input ndarray: |
| 242 | +var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); |
| 243 | + |
| 244 | +// Create an output ndarray: |
| 245 | +var y = empty( [], { |
| 246 | + 'dtype': 'bool' |
| 247 | +}); |
| 248 | + |
| 249 | +// Perform reduction: |
| 250 | +var out = someBy.assign( x, 2, y, predicate ); |
| 251 | +// returns <ndarray> |
| 252 | + |
| 253 | +var bool = ( out === y ); |
| 254 | +// returns true |
| 255 | + |
| 256 | +var v = y.get(); |
| 257 | +// returns true |
| 258 | +``` |
| 259 | + |
| 260 | +The function accepts the following arguments: |
| 261 | + |
| 262 | +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. |
| 263 | +- **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]. |
| 264 | +- **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]. |
| 265 | +- **options**: function options (_optional_). |
| 266 | +- **predicate**: predicate function. |
| 267 | +- **thisArg**: predicate execution context (_optional_). |
| 268 | + |
| 269 | +The function accepts the following `options`: |
| 270 | + |
| 271 | +- **dims**: list of dimensions over which to perform a reduction. |
| 272 | + |
| 273 | +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. |
| 274 | + |
| 275 | +<!-- eslint-disable max-len --> |
| 276 | + |
| 277 | +```javascript |
| 278 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 279 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 280 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 281 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 282 | + |
| 283 | +function predicate( value ) { |
| 284 | + return value > 0.0; |
| 285 | +} |
| 286 | + |
| 287 | +// Create a data buffer: |
| 288 | +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 ] ); |
| 289 | + |
| 290 | +// Define the shape of the input array: |
| 291 | +var sh = [ 3, 1, 2 ]; |
| 292 | + |
| 293 | +// Define the array strides: |
| 294 | +var sx = [ 4, 4, 1 ]; |
| 295 | + |
| 296 | +// Define the index offset: |
| 297 | +var ox = 1; |
| 298 | + |
| 299 | +// Create an input ndarray: |
| 300 | +var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); |
| 301 | + |
| 302 | +// Create an output ndarray: |
| 303 | +var y = empty( [ 3 ], { |
| 304 | + 'dtype': 'bool' |
| 305 | +}); |
| 306 | + |
| 307 | +var opts = { |
| 308 | + 'dims': [ 0, 1 ] |
| 309 | +}; |
| 310 | + |
| 311 | +// Perform reduction: |
| 312 | +var out = someBy.assign( x, 2, y, opts, predicate ); |
| 313 | + |
| 314 | +var bool = ( out === y ); |
| 315 | +// returns true |
| 316 | + |
| 317 | +var v = ndarray2array( y ); |
| 318 | +// returns [ true, true ] |
| 319 | +``` |
| 320 | + |
| 321 | +</section> |
| 322 | + |
| 323 | +<!-- /.usage --> |
| 324 | + |
| 325 | +<section class="notes"> |
| 326 | + |
| 327 | +## Notes |
| 328 | + |
| 329 | +- The predicate function is provided the following arguments: |
| 330 | + |
| 331 | + - **value**: current array element. |
| 332 | + - **indices**: current array element indices. |
| 333 | + - **arr**: the input ndarray. |
| 334 | + |
| 335 | +</section> |
| 336 | + |
| 337 | +<!-- /.notes --> |
| 338 | + |
| 339 | +<section class="examples"> |
| 340 | + |
| 341 | +## Examples |
| 342 | + |
| 343 | +<!-- eslint no-undef: "error" --> |
| 344 | + |
| 345 | +```javascript |
| 346 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; |
| 347 | +var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; |
| 348 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 349 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 350 | +var fillBy = require( '@stdlib/ndarray/fill-by' ); |
| 351 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 352 | + |
| 353 | +var x = zeros( [ 2, 4, 5 ], { |
| 354 | + 'dtype': 'float64' |
| 355 | +}); |
| 356 | +x = fillBy( x, discreteUniform( 0, 10 ) ); |
| 357 | +console.log( ndarray2array( x ) ); |
| 358 | + |
| 359 | +var n = scalar2ndarray( 4, { |
| 360 | + 'dtype': 'int8' |
| 361 | +}); |
| 362 | +var y = someBy( x, n, isEven ); |
| 363 | +console.log( y.get() ); |
| 364 | +``` |
| 365 | + |
| 366 | +</section> |
| 367 | + |
| 368 | +<!-- /.examples --> |
| 369 | + |
| 370 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 371 | + |
| 372 | +<section class="related"> |
| 373 | + |
| 374 | +</section> |
| 375 | + |
| 376 | +<!-- /.related --> |
| 377 | + |
| 378 | +<section class="links"> |
| 379 | + |
| 380 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 381 | + |
| 382 | +<!-- <related-links> --> |
| 383 | + |
| 384 | +<!-- </related-links> --> |
| 385 | + |
| 386 | +</section> |
| 387 | + |
| 388 | +<!-- /.links --> |
0 commit comments