|
| 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 | +# minBy |
| 22 | + |
| 23 | +> Compute the minimum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a callback function. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var minBy = require( '@stdlib/stats/min-by' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### minBy( x\[, options], clbk\[, thisArg] ) |
| 34 | + |
| 35 | +Computes the minimum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a callback function. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var array = require( '@stdlib/ndarray/array' ); |
| 39 | + |
| 40 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 41 | + |
| 42 | +function clbk( v ) { |
| 43 | + return v * 2.0; |
| 44 | +} |
| 45 | + |
| 46 | +var y = minBy( x, clbk ); |
| 47 | +// returns <ndarray> |
| 48 | + |
| 49 | +var v = y.get(); |
| 50 | +// returns -6.0 |
| 51 | +``` |
| 52 | + |
| 53 | +The function has the following parameters: |
| 54 | + |
| 55 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 56 | +- **options**: function options (_optional_). |
| 57 | +- **clbk**: callback function. |
| 58 | +- **thisArg**: callback function execution context (_optional_). |
| 59 | + |
| 60 | +The invoked callback is provided three arguments: |
| 61 | + |
| 62 | +- **value**: current array element. |
| 63 | +- **idx**: current array element index. |
| 64 | +- **array**: input ndarray. |
| 65 | + |
| 66 | +To set the callback execution context, provide a `thisArg`. |
| 67 | + |
| 68 | +<!-- eslint-disable no-invalid-this --> |
| 69 | + |
| 70 | +```javascript |
| 71 | +var array = require( '@stdlib/ndarray/array' ); |
| 72 | + |
| 73 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 74 | + |
| 75 | +function clbk( v ) { |
| 76 | + this.count += 1; |
| 77 | + return v * 2.0; |
| 78 | +} |
| 79 | + |
| 80 | +var ctx = { |
| 81 | + 'count': 0 |
| 82 | +}; |
| 83 | +var y = minBy( x, clbk, ctx ); |
| 84 | +// returns <ndarray> |
| 85 | + |
| 86 | +var v = y.get(); |
| 87 | +// returns -6.0 |
| 88 | + |
| 89 | +var count = ctx.count; |
| 90 | +// returns 3 |
| 91 | +``` |
| 92 | + |
| 93 | +The function accepts the following options: |
| 94 | + |
| 95 | +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 96 | +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 97 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 98 | + |
| 99 | +By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option. |
| 100 | + |
| 101 | +```javascript |
| 102 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 103 | +var array = require( '@stdlib/ndarray/array' ); |
| 104 | + |
| 105 | +function clbk( v ) { |
| 106 | + return v * 100.0; |
| 107 | +} |
| 108 | + |
| 109 | +var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { |
| 110 | + 'shape': [ 2, 2 ], |
| 111 | + 'order': 'row-major' |
| 112 | +}); |
| 113 | +var v = ndarray2array( x ); |
| 114 | +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] |
| 115 | + |
| 116 | +var opts = { |
| 117 | + 'dims': [ 0 ] |
| 118 | +}; |
| 119 | +var y = minBy( x, opts, clbk ); |
| 120 | +// returns <ndarray> |
| 121 | + |
| 122 | +v = ndarray2array( y ); |
| 123 | +// returns [ -300.0, 200.0 ] |
| 124 | + |
| 125 | +opts = { |
| 126 | + 'dims': [ 1 ] |
| 127 | +}; |
| 128 | +y = minBy( x, opts, clbk ); |
| 129 | +// returns <ndarray> |
| 130 | + |
| 131 | +v = ndarray2array( y ); |
| 132 | +// returns [ -100.0, -300.0 ] |
| 133 | + |
| 134 | +opts = { |
| 135 | + 'dims': [ 0, 1 ] |
| 136 | +}; |
| 137 | +y = minBy( x, opts, clbk ); |
| 138 | +// returns <ndarray> |
| 139 | + |
| 140 | +v = y.get(); |
| 141 | +// returns -300.0 |
| 142 | +``` |
| 143 | + |
| 144 | +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. |
| 145 | + |
| 146 | +```javascript |
| 147 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 148 | +var array = require( '@stdlib/ndarray/array' ); |
| 149 | + |
| 150 | +function clbk( v ) { |
| 151 | + return v * 100.0; |
| 152 | +} |
| 153 | + |
| 154 | +var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { |
| 155 | + 'shape': [ 2, 2 ], |
| 156 | + 'order': 'row-major' |
| 157 | +}); |
| 158 | + |
| 159 | +var v = ndarray2array( x ); |
| 160 | +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] |
| 161 | + |
| 162 | +var opts = { |
| 163 | + 'dims': [ 0 ], |
| 164 | + 'keepdims': true |
| 165 | +}; |
| 166 | +var y = minBy( x, opts, clbk ); |
| 167 | +// returns <ndarray> |
| 168 | + |
| 169 | +v = ndarray2array( y ); |
| 170 | +// returns [ [ -300.0, 200.0 ] ] |
| 171 | + |
| 172 | +opts = { |
| 173 | + 'dims': [ 1 ], |
| 174 | + 'keepdims': true |
| 175 | +}; |
| 176 | +y = minBy( x, opts, clbk ); |
| 177 | +// returns <ndarray> |
| 178 | + |
| 179 | +v = ndarray2array( y ); |
| 180 | +// returns [ [ -100.0 ], [ -300.0 ] ] |
| 181 | + |
| 182 | +opts = { |
| 183 | + 'dims': [ 0, 1 ], |
| 184 | + 'keepdims': true |
| 185 | +}; |
| 186 | +y = minBy( x, opts, clbk ); |
| 187 | +// returns <ndarray> |
| 188 | + |
| 189 | +v = ndarray2array( y ); |
| 190 | +// returns [ [ -300.0 ] ] |
| 191 | +``` |
| 192 | + |
| 193 | +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. |
| 194 | + |
| 195 | +```javascript |
| 196 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 197 | +var array = require( '@stdlib/ndarray/array' ); |
| 198 | + |
| 199 | +function clbk( v ) { |
| 200 | + return v * 100.0; |
| 201 | +} |
| 202 | + |
| 203 | +var x = array( [ -1.0, 2.0, -3.0 ], { |
| 204 | + 'dtype': 'generic' |
| 205 | +}); |
| 206 | + |
| 207 | +var opts = { |
| 208 | + 'dtype': 'float64' |
| 209 | +}; |
| 210 | +var y = minBy( x, opts, clbk ); |
| 211 | +// returns <ndarray> |
| 212 | + |
| 213 | +var dt = getDType( y ); |
| 214 | +// returns 'float64' |
| 215 | +``` |
| 216 | + |
| 217 | +#### minBy.assign( x, out\[, options], clbk\[, thisArg] ) |
| 218 | + |
| 219 | +Computes the minimum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a callback function and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. |
| 220 | + |
| 221 | +```javascript |
| 222 | +var array = require( '@stdlib/ndarray/array' ); |
| 223 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 224 | + |
| 225 | +function clbk( v ) { |
| 226 | + return v * 100.0; |
| 227 | +} |
| 228 | + |
| 229 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 230 | +var y = zeros( [] ); |
| 231 | + |
| 232 | +var out = minBy.assign( x, y, clbk ); |
| 233 | +// returns <ndarray> |
| 234 | + |
| 235 | +var v = out.get(); |
| 236 | +// returns -300.0 |
| 237 | + |
| 238 | +var bool = ( out === y ); |
| 239 | +// returns true |
| 240 | +``` |
| 241 | + |
| 242 | +The method has the following parameters: |
| 243 | + |
| 244 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 245 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. |
| 246 | +- **options**: function options (_optional_). |
| 247 | +- **clbk**: callback function. |
| 248 | +- **thisArg**: callback execution context (_optional_). |
| 249 | + |
| 250 | +The method accepts the following options: |
| 251 | + |
| 252 | +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 253 | + |
| 254 | +</section> |
| 255 | + |
| 256 | +<!-- /.usage --> |
| 257 | + |
| 258 | +<section class="notes"> |
| 259 | + |
| 260 | +## Notes |
| 261 | + |
| 262 | +- A provided callback function should return a numeric value. |
| 263 | +- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**. |
| 264 | +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. |
| 265 | +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. |
| 266 | + |
| 267 | +</section> |
| 268 | + |
| 269 | +<!-- /.notes --> |
| 270 | + |
| 271 | +<section class="examples"> |
| 272 | + |
| 273 | +## Examples |
| 274 | + |
| 275 | +<!-- eslint no-undef: "error" --> |
| 276 | + |
| 277 | +```javascript |
| 278 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 279 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 280 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 281 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 282 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 283 | +var minBy = require( '@stdlib/stats/min-by' ); |
| 284 | + |
| 285 | +// Define a function for generating an object having a random value: |
| 286 | +function random() { |
| 287 | + return { |
| 288 | + 'value': discreteUniform( 0, 20 ) |
| 289 | + }; |
| 290 | +} |
| 291 | + |
| 292 | +// Generate an array of random objects: |
| 293 | +var xbuf = filledarrayBy( 25, 'generic', random ); |
| 294 | + |
| 295 | +// Wrap in an ndarray: |
| 296 | +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); |
| 297 | +console.log( ndarray2array( x ) ); |
| 298 | + |
| 299 | +// Define an accessor function: |
| 300 | +function accessor( v ) { |
| 301 | + return v.value * 100; |
| 302 | +} |
| 303 | + |
| 304 | +// Perform a reduction: |
| 305 | +var opts = { |
| 306 | + 'dims': [ 0 ] |
| 307 | +}; |
| 308 | +var y = minBy( x, opts, accessor ); |
| 309 | + |
| 310 | +// Resolve the output array data type: |
| 311 | +var dt = getDType( y ); |
| 312 | +console.log( dt ); |
| 313 | + |
| 314 | +// Print the results: |
| 315 | +console.log( ndarray2array( y ) ); |
| 316 | +``` |
| 317 | + |
| 318 | +</section> |
| 319 | + |
| 320 | +<!-- /.examples --> |
| 321 | + |
| 322 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 323 | + |
| 324 | +<section class="related"> |
| 325 | + |
| 326 | +</section> |
| 327 | + |
| 328 | +<!-- /.related --> |
| 329 | + |
| 330 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 331 | + |
| 332 | +<section class="links"> |
| 333 | + |
| 334 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 335 | + |
| 336 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 337 | + |
| 338 | +[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies |
| 339 | + |
| 340 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 341 | + |
| 342 | +</section> |
| 343 | + |
| 344 | +<!-- /.links --> |
0 commit comments