|
| 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 | +# find |
| 22 | + |
| 23 | +> Return a new [ndarray][@stdlib/ndarray/ctor] containing the first elements which pass a test implemented by a predicate function along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +<!-- eslint-disable no-redeclare --> |
| 36 | + |
| 37 | +```javascript |
| 38 | +var find = require( '@stdlib/ndarray/find' ); |
| 39 | +``` |
| 40 | + |
| 41 | +<!-- eslint-enable no-redeclare --> |
| 42 | + |
| 43 | +#### find( x\[, options], predicate\[, thisArg] ) |
| 44 | + |
| 45 | +Returns a new [ndarray][@stdlib/ndarray/ctor] containing the first elements which pass a test implemented by a predicate function along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 46 | + |
| 47 | +<!-- eslint-disable no-invalid-this, max-len --> |
| 48 | + |
| 49 | +```javascript |
| 50 | +var array = require( '@stdlib/ndarray/array' ); |
| 51 | + |
| 52 | +function isEven( value ) { |
| 53 | + return value % 2.0 === 0.0; |
| 54 | +} |
| 55 | + |
| 56 | +// Create an input ndarray: |
| 57 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 58 | +// returns <ndarray> |
| 59 | + |
| 60 | +// Perform reduction: |
| 61 | +var out = find( x, isEven ); |
| 62 | +// returns <ndarray> |
| 63 | + |
| 64 | +var v = out.get(); |
| 65 | +// returns 2.0 |
| 66 | +``` |
| 67 | + |
| 68 | +The function accepts the following arguments: |
| 69 | + |
| 70 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 71 | +- **options**: function options _(optional)_. |
| 72 | +- **predicate**: predicate function. |
| 73 | +- **thisArg**: predicate function execution context _(optional)_. |
| 74 | + |
| 75 | +The function accepts the following options: |
| 76 | + |
| 77 | +- **dims**: list of dimensions over which to perform a reduction. |
| 78 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 79 | +- **sentinelValue**: value to return when no element passes the test. May be either a scalar value or a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 80 | + |
| 81 | +By default, the function performs reduction over all all elements in a provided [ndarray][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option. |
| 82 | + |
| 83 | +```javascript |
| 84 | +var array = require( '@stdlib/ndarray/array' ); |
| 85 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 86 | + |
| 87 | +function isEven( value ) { |
| 88 | + return value % 2.0 === 0.0; |
| 89 | +} |
| 90 | + |
| 91 | +// Create an input ndarray: |
| 92 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 93 | +// returns <ndarray> |
| 94 | + |
| 95 | +var opts = { |
| 96 | + 'dims': [ 0 ] |
| 97 | +}; |
| 98 | + |
| 99 | +// Perform reduction: |
| 100 | +var out = find( x, opts, isEven ); |
| 101 | +// returns <ndarray> |
| 102 | + |
| 103 | +var v = ndarray2array( out ); |
| 104 | +// returns [ [ NaN, 2.0 ], [ NaN, 4.0 ] ] |
| 105 | +``` |
| 106 | + |
| 107 | +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`. |
| 108 | + |
| 109 | +```javascript |
| 110 | +var array = require( '@stdlib/ndarray/array' ); |
| 111 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 112 | + |
| 113 | +function isEven( value ) { |
| 114 | + return value % 2.0 === 0.0; |
| 115 | +} |
| 116 | + |
| 117 | +// Create an input ndarray: |
| 118 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 119 | +// returns <ndarray> |
| 120 | + |
| 121 | +var opts = { |
| 122 | + 'dims': [ 0 ], |
| 123 | + 'keepdims': true |
| 124 | +}; |
| 125 | + |
| 126 | +// Perform reduction: |
| 127 | +var out = find( x, opts, isEven ); |
| 128 | +// returns <ndarray> |
| 129 | + |
| 130 | +var v = ndarray2array( out ); |
| 131 | +// returns [ [ [ NaN, 2 ], [ NaN, 4 ] ] ] |
| 132 | +``` |
| 133 | + |
| 134 | +To specify a custom sentinel value to return when no element passes the test, set the `sentinelValue` option. |
| 135 | + |
| 136 | +```javascript |
| 137 | +var array = require( '@stdlib/ndarray/array' ); |
| 138 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 139 | + |
| 140 | +function isEven( value ) { |
| 141 | + return value % 2.0 === 0.0; |
| 142 | +} |
| 143 | + |
| 144 | +// Create an input ndarray: |
| 145 | +var x = array( [ [ [ 1.0, 3.0 ], [ 5.0, 7.0 ] ], [ [ 9.0, 11.0 ], [ 13.0, 15.0 ] ] ] ); |
| 146 | +// returns <ndarray> |
| 147 | + |
| 148 | +var opts = { |
| 149 | + 'sentinelValue': -999 |
| 150 | +}; |
| 151 | + |
| 152 | +// Perform reduction: |
| 153 | +var out = find( x, opts, isEven ); |
| 154 | +// returns <ndarray> |
| 155 | + |
| 156 | +var v = out.get(); |
| 157 | +// returns -999 |
| 158 | +``` |
| 159 | + |
| 160 | +To set the `predicate` function execution context, provide a `thisArg`. |
| 161 | + |
| 162 | +<!-- eslint-disable no-invalid-this --> |
| 163 | + |
| 164 | +```javascript |
| 165 | +var array = require( '@stdlib/ndarray/array' ); |
| 166 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 167 | + |
| 168 | +function isEven( value ) { |
| 169 | + this.count += 1; |
| 170 | + return value % 2.0 === 0.0; |
| 171 | +} |
| 172 | + |
| 173 | +// Create an input ndarray: |
| 174 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 175 | +// returns <ndarray> |
| 176 | + |
| 177 | +var ctx = { |
| 178 | + 'count': 0 |
| 179 | +}; |
| 180 | + |
| 181 | +// Perform reduction: |
| 182 | +var out = find( x, isEven, ctx ); |
| 183 | +// returns <ndarray> |
| 184 | + |
| 185 | +var v = out.get(); |
| 186 | +// returns 2.0 |
| 187 | + |
| 188 | +var count = ctx.count; |
| 189 | +// returns 2 |
| 190 | +``` |
| 191 | + |
| 192 | +#### find.assign( x, out\[, options], predicate\[, thisArg] ) |
| 193 | + |
| 194 | +Finds the first elements which pass a test implemented by a predicate function along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. |
| 195 | + |
| 196 | +```javascript |
| 197 | +var array = require( '@stdlib/ndarray/array' ); |
| 198 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 199 | + |
| 200 | +function isEven( value ) { |
| 201 | + return value % 2.0 === 0.0; |
| 202 | +} |
| 203 | + |
| 204 | +// Create an input ndarray: |
| 205 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 206 | +// returns <ndarray> |
| 207 | + |
| 208 | +// Create an output ndarray: |
| 209 | +var y = empty( [], { |
| 210 | + 'dtype': x.dtype |
| 211 | +}); |
| 212 | + |
| 213 | +// Perform reduction: |
| 214 | +var out = find.assign( x, y, isEven ); |
| 215 | +// returns <ndarray> |
| 216 | + |
| 217 | +var bool = ( out === y ); |
| 218 | +// returns true |
| 219 | + |
| 220 | +var v = y.get(); |
| 221 | +// returns 2.0 |
| 222 | +``` |
| 223 | + |
| 224 | +The function accepts the following arguments: |
| 225 | + |
| 226 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 227 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. |
| 228 | +- **options**: function options _(optional)_. |
| 229 | +- **predicate**: predicate function. |
| 230 | +- **thisArg**: predicate function execution context _(optional)_. |
| 231 | + |
| 232 | +The function accepts the following options: |
| 233 | + |
| 234 | +- **dims**: list of dimensions over which to perform a reduction. |
| 235 | +- **sentinelValue**: value to return when no element passes the test. May be either a scalar value or a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 236 | + |
| 237 | +```javascript |
| 238 | +var array = require( '@stdlib/ndarray/array' ); |
| 239 | +var empty = require( '@stdlib/ndarray/empty' ); |
| 240 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 241 | + |
| 242 | +function isEven( value ) { |
| 243 | + return value % 2.0 === 0.0; |
| 244 | +} |
| 245 | + |
| 246 | +// Create an input ndarray: |
| 247 | +var x = array( [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] ] ); |
| 248 | +// returns <ndarray> |
| 249 | + |
| 250 | +// Create an output ndarray: |
| 251 | +var y = empty( [ 2, 2 ], { |
| 252 | + 'dtype': x.dtype |
| 253 | +}); |
| 254 | + |
| 255 | +var opts = { |
| 256 | + 'dims': [ 0 ] |
| 257 | +}; |
| 258 | + |
| 259 | +// Perform reduction: |
| 260 | +var out = find.assign( x, y, opts, isEven ); |
| 261 | + |
| 262 | +var bool = ( out === y ); |
| 263 | +// returns true |
| 264 | + |
| 265 | +var v = ndarray2array( y ); |
| 266 | +// returns [ [ NaN, 2.0 ], [ NaN, 4.0 ] ] |
| 267 | +``` |
| 268 | + |
| 269 | +</section> |
| 270 | + |
| 271 | +<!-- /.usage --> |
| 272 | + |
| 273 | +<section class="notes"> |
| 274 | + |
| 275 | +## Notes |
| 276 | + |
| 277 | +- By default, when no `sentinelValue` is provided, the function returns a default sentinel value based on the input [ndarray][@stdlib/ndarray/ctor] [data-type][@stdlib/ndarray/dtypes]: |
| 278 | + |
| 279 | + - real-valued floating-point data types: `NaN`. |
| 280 | + - complex-valued floating-point data types: `NaN + NaNj`. |
| 281 | + - integer data types: maximum value. |
| 282 | + - boolean data types: `false`. |
| 283 | + |
| 284 | +- The `predicate` function is provided the following arguments: |
| 285 | + |
| 286 | + - **value**: current array element. |
| 287 | + - **indices**: current array element indices. |
| 288 | + - **arr**: the input [ndarray][@stdlib/ndarray/ctor]. |
| 289 | + |
| 290 | +</section> |
| 291 | + |
| 292 | +<!-- /.notes --> |
| 293 | + |
| 294 | +<section class="examples"> |
| 295 | + |
| 296 | +## Examples |
| 297 | + |
| 298 | +<!-- eslint no-undef: "error" --> |
| 299 | + |
| 300 | +```javascript |
| 301 | +var uniform = require( '@stdlib/random/uniform' ); |
| 302 | +var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive; |
| 303 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 304 | +var find = require( '@stdlib/ndarray/find' ); |
| 305 | + |
| 306 | +var x = uniform( [ 2, 4, 5 ], -10.0, 10.0, { |
| 307 | + 'dtype': 'float64' |
| 308 | +}); |
| 309 | +console.log( ndarray2array( x ) ); |
| 310 | + |
| 311 | +var y = find( x, isPositive ); |
| 312 | +console.log( y.get() ); |
| 313 | +``` |
| 314 | + |
| 315 | +</section> |
| 316 | + |
| 317 | +<!-- /.examples --> |
| 318 | + |
| 319 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 320 | + |
| 321 | +<section class="related"> |
| 322 | + |
| 323 | +</section> |
| 324 | + |
| 325 | +<!-- /.related --> |
| 326 | + |
| 327 | +<section class="links"> |
| 328 | + |
| 329 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 330 | + |
| 331 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 332 | + |
| 333 | +<!-- <related-links> --> |
| 334 | + |
| 335 | +<!-- </related-links> --> |
| 336 | + |
| 337 | +</section> |
| 338 | + |
| 339 | +<!-- /.links --> |
0 commit comments