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