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