README.md (6245B)
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/base/nanrange-by' ); 39 ``` 40 41 #### nanrangeBy( N, x, stride, clbk\[, thisArg] ) 42 43 Calculates the [range][range] of strided array `x` 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 - **stride**: index increment. 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 `x` are accessed at runtime. For example, to access every other element 93 94 ```javascript 95 var floor = require( '@stdlib/math/base/special/floor' ); 96 97 function accessor( v ) { 98 return v * 2.0; 99 } 100 101 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0, NaN, NaN ]; 102 var N = floor( x.length / 2 ); 103 104 var v = nanrangeBy( N, x, 2, accessor ); 105 // returns 12.0 106 ``` 107 108 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 109 110 ```javascript 111 var Float64Array = require( '@stdlib/array/float64' ); 112 var floor = require( '@stdlib/math/base/special/floor' ); 113 114 function accessor( v ) { 115 return v * 2.0; 116 } 117 118 // Initial array... 119 var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); 120 121 // Create an offset view... 122 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 123 var N = floor( x0.length/2 ); 124 125 // Access every other element... 126 var v = nanrangeBy( N, x1, 2, accessor ); 127 // returns 8.0 128 ``` 129 130 #### nanrangeBy.ndarray( N, x, stride, offset, clbk\[, thisArg] ) 131 132 Calculates the [range][range] of strided array `x` via a callback function, ignoring `NaN` values and using alternative indexing semantics. 133 134 ```javascript 135 function accessor( v ) { 136 return v * 2.0; 137 } 138 139 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ]; 140 141 var v = nanrangeBy.ndarray( x.length, x, 1, 0, accessor ); 142 // returns 18.0 143 ``` 144 145 The function has the following additional parameters: 146 147 - **offset**: starting index. 148 149 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` 150 151 ```javascript 152 function accessor( v ) { 153 return v * 2.0; 154 } 155 156 var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; 157 158 var v = nanrangeBy.ndarray( 3, x, 1, x.length-3, accessor ); 159 // returns 22.0 160 ``` 161 162 </section> 163 164 <!-- /.usage --> 165 166 <section class="notes"> 167 168 ## Notes 169 170 - If `N <= 0`, both functions return `NaN`. 171 - A provided callback function should return a numeric value. 172 - If a provided callback function returns `NaN`, the value is ignored. 173 - If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is ignored. 174 - When possible, prefer using [`dnanrange`][@stdlib/stats/base/dnanrange], [`snanrange`][@stdlib/stats/base/snanrange], and/or [`nanrange`][@stdlib/stats/base/nanrange], as, depending on the environment, these interfaces are likely to be significantly more performant. 175 176 </section> 177 178 <!-- /.notes --> 179 180 <section class="examples"> 181 182 ## Examples 183 184 <!-- eslint no-undef: "error" --> 185 186 ```javascript 187 var round = require( '@stdlib/math/base/special/round' ); 188 var randu = require( '@stdlib/random/base/randu' ); 189 var Float64Array = require( '@stdlib/array/float64' ); 190 var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' ); 191 var nanrangeBy = require( '@stdlib/stats/base/nanrange-by' ); 192 193 function fill() { 194 if ( randu() < 0.2 ) { 195 return NaN; 196 } 197 return round( ( randu()*100.0 ) - 50.0 ); 198 } 199 200 function accessor( v ) { 201 return v * 2.0; 202 } 203 204 var x = new Float64Array( 10 ); 205 206 gfillBy( x.length, x, 1, fill ); 207 console.log( x ); 208 209 var v = nanrangeBy( x.length, x, 1, accessor ); 210 console.log( v ); 211 ``` 212 213 </section> 214 215 <!-- /.examples --> 216 217 <section class="links"> 218 219 [range]: https://en.wikipedia.org/wiki/Range_%28statistics%29 220 221 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 222 223 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 224 225 [@stdlib/stats/base/dnanrange]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/dnanrange 226 227 [@stdlib/stats/base/nanrange]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/nanrange 228 229 [@stdlib/stats/base/snanrange]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/snanrange 230 231 </section> 232 233 <!-- /.links -->