|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2020 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 | +<!-- lint disable maximum-heading-length --> |
| 22 | + |
| 23 | +# snanmskrange |
| 24 | + |
| 25 | +> Calculate the [range][range] of a single-precision floating-point strided array according to a mask, ignoring `NaN` values. |
| 26 | +
|
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +The [**range**][range] is defined as the difference between the maximum and minimum values. |
| 30 | + |
| 31 | +</section> |
| 32 | + |
| 33 | +<!-- /.intro --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var snanmskrange = require( '@stdlib/stats/strided/snanmskrange' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### snanmskrange( N, x, strideX, mask, strideMask ) |
| 44 | + |
| 45 | +Computes the [range][range] of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 49 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 50 | + |
| 51 | +var x = new Float32Array( [ 1.0, -2.0, 4.0, 2.0, NaN ] ); |
| 52 | +var mask = new Uint8Array( [ 0, 0, 1, 0, 0 ] ); |
| 53 | + |
| 54 | +var v = snanmskrange( x.length, x, 1, mask, 1 ); |
| 55 | +// returns 4.0 |
| 56 | +``` |
| 57 | + |
| 58 | +The function has the following parameters: |
| 59 | + |
| 60 | +- **N**: number of indexed elements. |
| 61 | +- **x**: input [`Float32Array`][@stdlib/array/float32]. |
| 62 | +- **strideX**: stride length for `x`. |
| 63 | +- **mask**: mask [`Uint8Array`][@stdlib/array/uint8]. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in computation. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from computation. |
| 64 | +- **strideMask**: stride length for `mask`. |
| 65 | + |
| 66 | +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the [range][range] of every other element in `x`, |
| 67 | + |
| 68 | +```javascript |
| 69 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 70 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 71 | + |
| 72 | +var x = new Float32Array( [ 1.0, 2.0, -7.0, -2.0, 4.0, 3.0, 5.0, 6.0 ] ); |
| 73 | +var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] ); |
| 74 | + |
| 75 | +var v = snanmskrange( 4, x, 2, mask, 2 ); |
| 76 | +// returns 11.0 |
| 77 | +``` |
| 78 | + |
| 79 | +Note that indexing is relative to the first index. To introduce offsets, use [`typed array`][mdn-typed-array] views. |
| 80 | + |
| 81 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 82 | + |
| 83 | +```javascript |
| 84 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 85 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 86 | + |
| 87 | +var x0 = new Float32Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 88 | +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 89 | + |
| 90 | +var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] ); |
| 91 | +var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 92 | + |
| 93 | +var v = snanmskrange( 4, x1, 2, mask1, 2 ); |
| 94 | +// returns 6.0 |
| 95 | +``` |
| 96 | + |
| 97 | +#### snanmskrange.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask ) |
| 98 | + |
| 99 | +Computes the [range][range] of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values and using alternative indexing semantics. |
| 100 | + |
| 101 | +```javascript |
| 102 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 103 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 104 | + |
| 105 | +var x = new Float32Array( [ 1.0, -2.0, 4.0, 2.0, NaN ] ); |
| 106 | +var mask = new Uint8Array( [ 0, 0, 1, 0, 0 ] ); |
| 107 | + |
| 108 | +var v = snanmskrange.ndarray( x.length, x, 1, 0, mask, 1, 0 ); |
| 109 | +// returns 4.0 |
| 110 | +``` |
| 111 | + |
| 112 | +The function has the following additional parameters: |
| 113 | + |
| 114 | +- **offsetX**: starting index for `x`. |
| 115 | +- **offsetMask**: starting index for `mask`. |
| 116 | + |
| 117 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to calculate the [range][range] for every other element in `x` starting from the second element |
| 118 | + |
| 119 | +```javascript |
| 120 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 121 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 122 | + |
| 123 | +var x = new Float32Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 124 | +var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] ); |
| 125 | + |
| 126 | +var v = snanmskrange.ndarray( 4, x, 2, 1, mask, 2, 1 ); |
| 127 | +// returns 6.0 |
| 128 | +``` |
| 129 | + |
| 130 | +</section> |
| 131 | + |
| 132 | +<!-- /.usage --> |
| 133 | + |
| 134 | +<section class="notes"> |
| 135 | + |
| 136 | +## Notes |
| 137 | + |
| 138 | +- If `N <= 0`, both functions return `NaN`. |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.notes --> |
| 143 | + |
| 144 | +<section class="examples"> |
| 145 | + |
| 146 | +## Examples |
| 147 | + |
| 148 | +<!-- eslint no-undef: "error" --> |
| 149 | + |
| 150 | +```javascript |
| 151 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 152 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 153 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 154 | +var snanmskrange = require( '@stdlib/stats/strided/snanmskrange' ); |
| 155 | + |
| 156 | +function rand() { |
| 157 | + if ( bernoulli( 0.8 ) < 1 ) { |
| 158 | + return NaN; |
| 159 | + } |
| 160 | + return uniform( -50.0, 50.0 ); |
| 161 | +} |
| 162 | + |
| 163 | +var x = filledarrayBy( 10, 'float32', rand ); |
| 164 | +console.log( x ); |
| 165 | + |
| 166 | +var mask = filledarrayBy( x.length, 'uint8', bernoulli.factory( 0.2 ) ); |
| 167 | +console.log( mask ); |
| 168 | + |
| 169 | +var v = snanmskrange( x.length, x, 1, mask, 1 ); |
| 170 | +console.log( v ); |
| 171 | +``` |
| 172 | + |
| 173 | +</section> |
| 174 | + |
| 175 | +<!-- /.examples --> |
| 176 | + |
| 177 | +<!-- C interface documentation. --> |
| 178 | + |
| 179 | +* * * |
| 180 | + |
| 181 | +<section class="c"> |
| 182 | + |
| 183 | +## C APIs |
| 184 | + |
| 185 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 186 | + |
| 187 | +<section class="intro"> |
| 188 | + |
| 189 | +</section> |
| 190 | + |
| 191 | +<!-- /.intro --> |
| 192 | + |
| 193 | +<!-- C usage documentation. --> |
| 194 | + |
| 195 | +<section class="usage"> |
| 196 | + |
| 197 | +### Usage |
| 198 | + |
| 199 | +```c |
| 200 | +#include "stdlib/stats/strided/snanmskrange.h" |
| 201 | +``` |
| 202 | + |
| 203 | +#### stdlib_strided_snanmskrange( N, \*X, strideX, \*Mask, strideMask ) |
| 204 | + |
| 205 | +Computes the [range][range] of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values. |
| 206 | + |
| 207 | +```c |
| 208 | +#include <stdint.h> |
| 209 | + |
| 210 | +const float x[] = { 1.0f, -2.0f, 4.0f, 2.0f, 0.0f/0.0f }; |
| 211 | +const uint8_t mask[] = { 0, 0, 1, 0, 0 }; |
| 212 | + |
| 213 | +float v = stdlib_strided_snanmskrange( 5, x, 1, mask, 1 ); |
| 214 | +// returns 4.0f |
| 215 | +``` |
| 216 | +
|
| 217 | +The function accepts the following arguments: |
| 218 | +
|
| 219 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 220 | +- **X**: `[in] float*` input array. |
| 221 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 222 | +- **Mask**: `[in] uint8_t*` mask array. If a `Mask` array element is `0`, the corresponding element in `X` is considered valid and included in computation. If a `Mask` array element is `1`, the corresponding element in `X` is considered invalid/missing and excluded from computation. |
| 223 | +- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`. |
| 224 | +
|
| 225 | +```c |
| 226 | +float stdlib_strided_snanmskrange( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const uint8_t *Mask, const CBLAS_INT strideMask ); |
| 227 | +``` |
| 228 | + |
| 229 | +#### stdlib_strided_snanmskrange_ndarray( N, \*X, strideX, offsetX, \*Mask, strideMask, offsetMask ) |
| 230 | + |
| 231 | +Computes the [range][range] of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values and using alternative indexing semantics. |
| 232 | + |
| 233 | +```c |
| 234 | +#include <stdint.h> |
| 235 | + |
| 236 | +const float x[] = { 1.0f, -2.0f, 4.0f, 2.0f, 0.0f/0.0f }; |
| 237 | +const uint8_t mask[] = { 0, 0, 1, 0, 0 }; |
| 238 | + |
| 239 | +float v = stdlib_strided_snanmskrange_ndarray( 5, x, 1, 0, mask, 1, 0 ); |
| 240 | +// returns 4.0f |
| 241 | +``` |
| 242 | +
|
| 243 | +The function accepts the following arguments: |
| 244 | +
|
| 245 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 246 | +- **X**: `[in] float*` input array. |
| 247 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 248 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 249 | +- **Mask**: `[in] uint8_t*` mask array. If a `Mask` array element is `0`, the corresponding element in `X` is considered valid and included in computation. If a `Mask` array element is `1`, the corresponding element in `X` is considered invalid/missing and excluded from computation. |
| 250 | +- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`. |
| 251 | +- **offsetMask**: `[in] CBLAS_INT` starting index for `Mask`. |
| 252 | +
|
| 253 | +```c |
| 254 | +float stdlib_strided_snanmskrange_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const uint8_t *Mask, const CBLAS_INT strideMask, const CBLAS_INT offsetMask ); |
| 255 | +``` |
| 256 | + |
| 257 | +</section> |
| 258 | + |
| 259 | +<!-- /.usage --> |
| 260 | + |
| 261 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 262 | + |
| 263 | +<section class="notes"> |
| 264 | + |
| 265 | +</section> |
| 266 | + |
| 267 | +<!-- /.notes --> |
| 268 | + |
| 269 | +<!-- C API usage examples. --> |
| 270 | + |
| 271 | +<section class="examples"> |
| 272 | + |
| 273 | +### Examples |
| 274 | + |
| 275 | +```c |
| 276 | +#include "stdlib/stats/strided/snanmskrange.h" |
| 277 | +#include <stdint.h> |
| 278 | +#include <stdio.h> |
| 279 | + |
| 280 | +int main( void ) { |
| 281 | + // Create a strided array: |
| 282 | + const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 0.0f/0.0f, 0.0f/0.0f }; |
| 283 | + |
| 284 | + // Create a mask array: |
| 285 | + const uint8_t mask[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 }; |
| 286 | + |
| 287 | + // Specify the number of elements: |
| 288 | + const int N = 5; |
| 289 | + |
| 290 | + // Specify the stride lengths: |
| 291 | + const int strideX = 2; |
| 292 | + const int strideMask = 2; |
| 293 | + |
| 294 | + // Compute the range: |
| 295 | + float v = stdlib_strided_snanmskrange( N, x, strideX, mask, strideMask ); |
| 296 | + |
| 297 | + // Print the result: |
| 298 | + printf( "range: %f\n", v ); |
| 299 | +} |
| 300 | +``` |
| 301 | +
|
| 302 | +</section> |
| 303 | +
|
| 304 | +<!-- /.examples --> |
| 305 | +
|
| 306 | +</section> |
| 307 | +
|
| 308 | +<!-- /.c --> |
| 309 | +
|
| 310 | +<section class="references"> |
| 311 | +
|
| 312 | +</section> |
| 313 | +
|
| 314 | +<!-- /.references --> |
| 315 | +
|
| 316 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 317 | +
|
| 318 | +<section class="related"> |
| 319 | +
|
| 320 | +* * * |
| 321 | +
|
| 322 | +## See Also |
| 323 | +
|
| 324 | +- <span class="package-name">[`@stdlib/stats/strided/dnanmskrange`][@stdlib/stats/strided/dnanmskrange]</span><span class="delimiter">: </span><span class="description">calculate the range of a double-precision floating-point strided array according to a mask, ignoring NaN values.</span> |
| 325 | +- <span class="package-name">[`@stdlib/stats/base/nanmskrange`][@stdlib/stats/base/nanmskrange]</span><span class="delimiter">: </span><span class="description">calculate the range of a strided array according to a mask, ignoring NaN values.</span> |
| 326 | +- <span class="package-name">[`@stdlib/stats/strided/smskrange`][@stdlib/stats/strided/smskrange]</span><span class="delimiter">: </span><span class="description">calculate the range of a single-precision floating-point strided array according to a mask.</span> |
| 327 | +- <span class="package-name">[`@stdlib/stats/strided/snanrange`][@stdlib/stats/strided/snanrange]</span><span class="delimiter">: </span><span class="description">calculate the range of a single-precision floating-point strided array, ignoring NaN values.</span> |
| 328 | +- <span class="package-name">[`@stdlib/stats/strided/snanmskmax`][@stdlib/stats/strided/snanmskmax]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a single-precision floating-point strided array according to a mask, ignoring NaN values.</span> |
| 329 | +- <span class="package-name">[`@stdlib/stats/strided/snanmskmin`][@stdlib/stats/strided/snanmskmin]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a single-precision floating-point strided array according to a mask, ignoring NaN values.</span> |
| 330 | +
|
| 331 | +</section> |
| 332 | +
|
| 333 | +<!-- /.related --> |
| 334 | +
|
| 335 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 336 | +
|
| 337 | +<section class="links"> |
| 338 | +
|
| 339 | +[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29 |
| 340 | +
|
| 341 | +[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 |
| 342 | +
|
| 343 | +[@stdlib/array/uint8]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint8 |
| 344 | +
|
| 345 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 346 | +
|
| 347 | +<!-- <related-links> --> |
| 348 | +
|
| 349 | +[@stdlib/stats/strided/dnanmskrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmskrange |
| 350 | +
|
| 351 | +[@stdlib/stats/base/nanmskrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmskrange |
| 352 | +
|
| 353 | +[@stdlib/stats/strided/smskrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smskrange |
| 354 | +
|
| 355 | +[@stdlib/stats/strided/snanrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanrange |
| 356 | +
|
| 357 | +[@stdlib/stats/strided/snanmskmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanmskmax |
| 358 | +
|
| 359 | +[@stdlib/stats/strided/snanmskmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanmskmin |
| 360 | +
|
| 361 | +<!-- </related-links> --> |
| 362 | +
|
| 363 | +</section> |
| 364 | +
|
| 365 | +<!-- /.links --> |
0 commit comments