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