|
| 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 | +# nanrangeBy |
| 22 | + |
| 23 | +> Calculate the [range][range] of a strided array via a callback function, ignoring `NaN` values. |
| 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 nanrangeBy = require( '@stdlib/stats/strided/nanrange-by' ); |
| 39 | +``` |
| 40 | + |
| 41 | +#### nanrangeBy( N, x, strideX, clbk\[, thisArg] ) |
| 42 | + |
| 43 | +Computes the [range][range] of a strided array via a callback function, ignoring `NaN` values. |
| 44 | + |
| 45 | +```javascript |
| 46 | +function accessor( v ) { |
| 47 | + return v * 2.0; |
| 48 | +} |
| 49 | + |
| 50 | +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ]; |
| 51 | + |
| 52 | +var v = nanrangeBy( x.length, x, 1, accessor ); |
| 53 | +// returns 18.0 |
| 54 | +``` |
| 55 | + |
| 56 | +The function has the following parameters: |
| 57 | + |
| 58 | +- **N**: number of indexed elements. |
| 59 | +- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions). |
| 60 | +- **strideX**: stride length. |
| 61 | +- **clbk**: callback function. |
| 62 | +- **thisArg**: execution context (_optional_). |
| 63 | + |
| 64 | +The invoked callback is provided four arguments: |
| 65 | + |
| 66 | +- **value**: array element. |
| 67 | +- **aidx**: array index. |
| 68 | +- **sidx**: strided index (`offset + aidx*stride`). |
| 69 | +- **array**: input array/collection. |
| 70 | + |
| 71 | +To set the callback execution context, provide a `thisArg`. |
| 72 | + |
| 73 | +```javascript |
| 74 | +function accessor( v ) { |
| 75 | + this.count += 1; |
| 76 | + return v * 2.0; |
| 77 | +} |
| 78 | + |
| 79 | +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ]; |
| 80 | + |
| 81 | +var context = { |
| 82 | + 'count': 0 |
| 83 | +}; |
| 84 | + |
| 85 | +var v = nanrangeBy( x.length, x, 1, accessor, context ); |
| 86 | +// returns 18.0 |
| 87 | + |
| 88 | +var cnt = context.count; |
| 89 | +// returns 10 |
| 90 | +``` |
| 91 | + |
| 92 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element |
| 93 | + |
| 94 | +```javascript |
| 95 | +function accessor( v ) { |
| 96 | + return v * 2.0; |
| 97 | +} |
| 98 | + |
| 99 | +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0, NaN, NaN ]; |
| 100 | + |
| 101 | +var v = nanrangeBy( 5, x, 2, accessor ); |
| 102 | +// returns 12.0 |
| 103 | +``` |
| 104 | + |
| 105 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 106 | + |
| 107 | +```javascript |
| 108 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 109 | + |
| 110 | +function accessor( v ) { |
| 111 | + return v * 2.0; |
| 112 | +} |
| 113 | + |
| 114 | +// Initial array... |
| 115 | +var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); |
| 116 | + |
| 117 | +// Create an offset view... |
| 118 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 119 | + |
| 120 | +// Access every other element... |
| 121 | +var v = nanrangeBy( 3, x1, 2, accessor ); |
| 122 | +// returns 8.0 |
| 123 | +``` |
| 124 | + |
| 125 | +#### nanrangeBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] ) |
| 126 | + |
| 127 | +Computes the [range][range] of a strided array via a callback function, ignoring `NaN` values and using alternative indexing semantics. |
| 128 | + |
| 129 | +```javascript |
| 130 | +function accessor( v ) { |
| 131 | + return v * 2.0; |
| 132 | +} |
| 133 | + |
| 134 | +var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ]; |
| 135 | + |
| 136 | +var v = nanrangeBy.ndarray( x.length, x, 1, 0, accessor ); |
| 137 | +// returns 18.0 |
| 138 | +``` |
| 139 | + |
| 140 | +The function has the following additional parameters: |
| 141 | + |
| 142 | +- **offsetX**: starting index. |
| 143 | + |
| 144 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x` |
| 145 | + |
| 146 | +```javascript |
| 147 | +function accessor( v ) { |
| 148 | + return v * 2.0; |
| 149 | +} |
| 150 | + |
| 151 | +var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; |
| 152 | + |
| 153 | +var v = nanrangeBy.ndarray( 3, x, 1, x.length-3, accessor ); |
| 154 | +// returns 22.0 |
| 155 | +``` |
| 156 | + |
| 157 | +</section> |
| 158 | + |
| 159 | +<!-- /.usage --> |
| 160 | + |
| 161 | +<section class="notes"> |
| 162 | + |
| 163 | +## Notes |
| 164 | + |
| 165 | +- If `N <= 0`, both functions return `NaN`. |
| 166 | +- A provided callback function should return a numeric value. |
| 167 | +- If a provided callback function returns `NaN`, the value is ignored. |
| 168 | +- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is ignored. |
| 169 | +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). |
| 170 | +- When possible, prefer using [`dnanrange`][@stdlib/stats/strided/dnanrange], [`snanrange`][@stdlib/stats/strided/snanrange], and/or [`nanrange`][@stdlib/stats/base/nanrange], as, depending on the environment, these interfaces are likely to be significantly more performant. |
| 171 | + |
| 172 | +</section> |
| 173 | + |
| 174 | +<!-- /.notes --> |
| 175 | + |
| 176 | +<section class="examples"> |
| 177 | + |
| 178 | +## Examples |
| 179 | + |
| 180 | +<!-- eslint no-undef: "error" --> |
| 181 | + |
| 182 | +```javascript |
| 183 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 184 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 185 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 186 | +var nanrangeBy = require( '@stdlib/stats/strided/nanrange-by' ); |
| 187 | + |
| 188 | +function rand() { |
| 189 | + if ( bernoulli( 0.8 ) < 0.2 ) { |
| 190 | + return NaN; |
| 191 | + } |
| 192 | + return uniform( -50.0, 50.0 ); |
| 193 | +} |
| 194 | + |
| 195 | +function accessor( v ) { |
| 196 | + return v * 2.0; |
| 197 | +} |
| 198 | + |
| 199 | +var x = filledarrayBy( 10, 'float64', rand ); |
| 200 | +console.log( x ); |
| 201 | + |
| 202 | +var v = nanrangeBy( x.length, x, 1, accessor ); |
| 203 | +console.log( v ); |
| 204 | +``` |
| 205 | + |
| 206 | +</section> |
| 207 | + |
| 208 | +<!-- /.examples --> |
| 209 | + |
| 210 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 211 | + |
| 212 | +<section class="related"> |
| 213 | + |
| 214 | +* * * |
| 215 | + |
| 216 | +## See Also |
| 217 | + |
| 218 | +- <span class="package-name">[`@stdlib/stats/strided/dnanrange`][@stdlib/stats/strided/dnanrange]</span><span class="delimiter">: </span><span class="description">calculate the range of a double-precision floating-point strided array, ignoring NaN values.</span> |
| 219 | +- <span class="package-name">[`@stdlib/stats/strided/nanmax-by`][@stdlib/stats/strided/nanmax-by]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array via a callback function, ignoring NaN values.</span> |
| 220 | +- <span class="package-name">[`@stdlib/stats/strided/nanmin-by`][@stdlib/stats/strided/nanmin-by]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a strided array via a callback function, ignoring NaN values.</span> |
| 221 | +- <span class="package-name">[`@stdlib/stats/base/nanrange`][@stdlib/stats/base/nanrange]</span><span class="delimiter">: </span><span class="description">calculate the range of a strided array, ignoring NaN values.</span> |
| 222 | +- <span class="package-name">[`@stdlib/stats/base/range-by`][@stdlib/stats/base/range-by]</span><span class="delimiter">: </span><span class="description">calculate the range of a strided array via a callback function.</span> |
| 223 | +- <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> |
| 224 | + |
| 225 | +</section> |
| 226 | + |
| 227 | +<!-- /.related --> |
| 228 | + |
| 229 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 230 | + |
| 231 | +<section class="links"> |
| 232 | + |
| 233 | +[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29 |
| 234 | + |
| 235 | +[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array |
| 236 | + |
| 237 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 238 | + |
| 239 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 240 | + |
| 241 | +<!-- <related-links> --> |
| 242 | + |
| 243 | +[@stdlib/stats/strided/dnanrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanrange |
| 244 | + |
| 245 | +[@stdlib/stats/strided/nanmax-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanmax-by |
| 246 | + |
| 247 | +[@stdlib/stats/strided/nanmin-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanmin-by |
| 248 | + |
| 249 | +[@stdlib/stats/base/nanrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanrange |
| 250 | + |
| 251 | +[@stdlib/stats/base/range-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/range-by |
| 252 | + |
| 253 | +[@stdlib/stats/strided/snanrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanrange |
| 254 | + |
| 255 | +<!-- </related-links> --> |
| 256 | + |
| 257 | +</section> |
| 258 | + |
| 259 | +<!-- /.links --> |
0 commit comments