|
| 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 | +# min |
| 22 | + |
| 23 | +> Compute the minimum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var min = require( '@stdlib/stats/min' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### min( x\[, options] ) |
| 34 | + |
| 35 | +Computes the minimum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var array = require( '@stdlib/ndarray/array' ); |
| 39 | + |
| 40 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 41 | + |
| 42 | +var y = min( x ); |
| 43 | +// returns <ndarray> |
| 44 | + |
| 45 | +var v = y.get(); |
| 46 | +// returns -3.0 |
| 47 | +``` |
| 48 | + |
| 49 | +The function has the following parameters: |
| 50 | + |
| 51 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 52 | +- **options**: function options (_optional_). |
| 53 | + |
| 54 | +The function accepts the following options: |
| 55 | + |
| 56 | +- **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]. |
| 57 | +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 58 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 59 | + |
| 60 | +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. |
| 61 | + |
| 62 | +```javascript |
| 63 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 64 | +var array = require( '@stdlib/ndarray/array' ); |
| 65 | + |
| 66 | +var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { |
| 67 | + 'shape': [ 2, 2 ], |
| 68 | + 'order': 'row-major' |
| 69 | +}); |
| 70 | +var v = ndarray2array( x ); |
| 71 | +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] |
| 72 | + |
| 73 | +var y = min( x, { |
| 74 | + 'dims': [ 0 ] |
| 75 | +}); |
| 76 | +// returns <ndarray> |
| 77 | + |
| 78 | +v = ndarray2array( y ); |
| 79 | +// returns [ -3.0, 2.0 ] |
| 80 | + |
| 81 | +y = min( x, { |
| 82 | + 'dims': [ 1 ] |
| 83 | +}); |
| 84 | +// returns <ndarray> |
| 85 | + |
| 86 | +v = ndarray2array( y ); |
| 87 | +// returns [ -1.0, -3.0 ] |
| 88 | + |
| 89 | +y = min( x, { |
| 90 | + 'dims': [ 0, 1 ] |
| 91 | +}); |
| 92 | +// returns <ndarray> |
| 93 | + |
| 94 | +v = y.get(); |
| 95 | +// returns -3.0 |
| 96 | +``` |
| 97 | + |
| 98 | +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`. |
| 99 | + |
| 100 | +```javascript |
| 101 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 102 | +var array = require( '@stdlib/ndarray/array' ); |
| 103 | + |
| 104 | +var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { |
| 105 | + 'shape': [ 2, 2 ], |
| 106 | + 'order': 'row-major' |
| 107 | +}); |
| 108 | + |
| 109 | +var v = ndarray2array( x ); |
| 110 | +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] |
| 111 | + |
| 112 | +var y = min( x, { |
| 113 | + 'dims': [ 0 ], |
| 114 | + 'keepdims': true |
| 115 | +}); |
| 116 | +// returns <ndarray> |
| 117 | + |
| 118 | +v = ndarray2array( y ); |
| 119 | +// returns [ [ -3.0, 2.0 ] ] |
| 120 | + |
| 121 | +y = min( x, { |
| 122 | + 'dims': [ 1 ], |
| 123 | + 'keepdims': true |
| 124 | +}); |
| 125 | +// returns <ndarray> |
| 126 | + |
| 127 | +v = ndarray2array( y ); |
| 128 | +// returns [ [ -1.0 ], [ -3.0 ] ] |
| 129 | + |
| 130 | +y = min( x, { |
| 131 | + 'dims': [ 0, 1 ], |
| 132 | + 'keepdims': true |
| 133 | +}); |
| 134 | +// returns <ndarray> |
| 135 | + |
| 136 | +v = ndarray2array( y ); |
| 137 | +// returns [ [ -3.0 ] ] |
| 138 | +``` |
| 139 | + |
| 140 | +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. |
| 141 | + |
| 142 | +```javascript |
| 143 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 144 | +var array = require( '@stdlib/ndarray/array' ); |
| 145 | + |
| 146 | +var x = array( [ -1.0, 2.0, -3.0 ], { |
| 147 | + 'dtype': 'generic' |
| 148 | +}); |
| 149 | + |
| 150 | +var y = min( x, { |
| 151 | + 'dtype': 'float64' |
| 152 | +}); |
| 153 | +// returns <ndarray> |
| 154 | + |
| 155 | +var dt = getDType( y ); |
| 156 | +// returns 'float64' |
| 157 | +``` |
| 158 | + |
| 159 | +#### min.assign( x, out\[, options] ) |
| 160 | + |
| 161 | +Computes the minimum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. |
| 162 | + |
| 163 | +```javascript |
| 164 | +var array = require( '@stdlib/ndarray/array' ); |
| 165 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 166 | + |
| 167 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 168 | +var y = zeros( [] ); |
| 169 | + |
| 170 | +var out = min.assign( x, y ); |
| 171 | +// returns <ndarray> |
| 172 | + |
| 173 | +var v = out.get(); |
| 174 | +// returns -3.0 |
| 175 | + |
| 176 | +var bool = ( out === y ); |
| 177 | +// returns true |
| 178 | +``` |
| 179 | + |
| 180 | +The method has the following parameters: |
| 181 | + |
| 182 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes]. |
| 183 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. |
| 184 | +- **options**: function options (_optional_). |
| 185 | + |
| 186 | +The method accepts the following options: |
| 187 | + |
| 188 | +- **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]. |
| 189 | + |
| 190 | +</section> |
| 191 | + |
| 192 | +<!-- /.usage --> |
| 193 | + |
| 194 | +<section class="notes"> |
| 195 | + |
| 196 | +## Notes |
| 197 | + |
| 198 | +- 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]. |
| 199 | +- 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 the same [data type][@stdlib/ndarray/dtypes] as the input [ndarray][@stdlib/ndarray/ctor]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. |
| 200 | + |
| 201 | +</section> |
| 202 | + |
| 203 | +<!-- /.notes --> |
| 204 | + |
| 205 | +<section class="examples"> |
| 206 | + |
| 207 | +## Examples |
| 208 | + |
| 209 | +<!-- eslint no-undef: "error" --> |
| 210 | + |
| 211 | +```javascript |
| 212 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 213 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 214 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 215 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 216 | +var min = require( '@stdlib/stats/min' ); |
| 217 | + |
| 218 | +// Generate an array of random numbers: |
| 219 | +var xbuf = discreteUniform( 25, 0, 20, { |
| 220 | + 'dtype': 'generic' |
| 221 | +}); |
| 222 | + |
| 223 | +// Wrap in an ndarray: |
| 224 | +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); |
| 225 | +console.log( ndarray2array( x ) ); |
| 226 | + |
| 227 | +// Perform a reduction: |
| 228 | +var y = min( x, { |
| 229 | + 'dims': [ 0 ] |
| 230 | +}); |
| 231 | + |
| 232 | +// Resolve the output array data type: |
| 233 | +var dt = getDType( y ); |
| 234 | +console.log( dt ); |
| 235 | + |
| 236 | +// Print the results: |
| 237 | +console.log( ndarray2array( y ) ); |
| 238 | +``` |
| 239 | + |
| 240 | +</section> |
| 241 | + |
| 242 | +<!-- /.examples --> |
| 243 | + |
| 244 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 245 | + |
| 246 | +<section class="related"> |
| 247 | + |
| 248 | +</section> |
| 249 | + |
| 250 | +<!-- /.related --> |
| 251 | + |
| 252 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 253 | + |
| 254 | +<section class="links"> |
| 255 | + |
| 256 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 257 | + |
| 258 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 259 | + |
| 260 | +[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies |
| 261 | + |
| 262 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 263 | + |
| 264 | +</section> |
| 265 | + |
| 266 | +<!-- /.links --> |
0 commit comments