|
| 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 | +# rangeBy |
| 22 | + |
| 23 | +> Calculate the [range][range] of a strided array via a callback function. |
| 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 rangeBy = require( '@stdlib/stats/strided/range-by' ); |
| 39 | +``` |
| 40 | + |
| 41 | +#### rangeBy( N, x, strideX, clbk\[, thisArg] ) |
| 42 | + |
| 43 | +Computes the [range][range] of a strided array via a callback function. |
| 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, 0.0, -1.0, -3.0 ]; |
| 51 | + |
| 52 | +var v = rangeBy( 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, 0.0, -1.0, -3.0 ]; |
| 80 | + |
| 81 | +var context = { |
| 82 | + 'count': 0 |
| 83 | +}; |
| 84 | + |
| 85 | +var v = rangeBy( x.length, x, 1, accessor, context ); |
| 86 | +// returns 18.0 |
| 87 | + |
| 88 | +var cnt = context.count; |
| 89 | +// returns 8 |
| 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 ]; |
| 100 | + |
| 101 | +var v = rangeBy( 4, 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 = rangeBy( 3, x1, 2, accessor ); |
| 122 | +// returns 8.0 |
| 123 | +``` |
| 124 | + |
| 125 | +#### rangeBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] ) |
| 126 | + |
| 127 | +Computes the [range][range] of a strided array via a callback function 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, 0.0, -1.0, -3.0 ]; |
| 135 | + |
| 136 | +var v = rangeBy.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 = rangeBy.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 does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**. |
| 168 | +- 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]). |
| 169 | +- When possible, prefer using [`drange`][@stdlib/stats/strided/drange], [`srange`][@stdlib/stats/strided/srange], and/or [`range`][@stdlib/stats/base/range], as, depending on the environment, these interfaces are likely to be significantly more performant. |
| 170 | + |
| 171 | +</section> |
| 172 | + |
| 173 | +<!-- /.notes --> |
| 174 | + |
| 175 | +<section class="examples"> |
| 176 | + |
| 177 | +## Examples |
| 178 | + |
| 179 | +<!-- eslint no-undef: "error" --> |
| 180 | + |
| 181 | +```javascript |
| 182 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 183 | +var rangeBy = require( '@stdlib/stats/strided/range-by' ); |
| 184 | + |
| 185 | +function accessor( v ) { |
| 186 | + return v * 2.0; |
| 187 | +} |
| 188 | + |
| 189 | +var x = discreteUniform( 10, -50, 50, { |
| 190 | + 'dtype': 'float64' |
| 191 | +}); |
| 192 | +console.log( x ); |
| 193 | + |
| 194 | +var v = rangeBy( x.length, x, 1, accessor ); |
| 195 | +console.log( v ); |
| 196 | +``` |
| 197 | + |
| 198 | +</section> |
| 199 | + |
| 200 | +<!-- /.examples --> |
| 201 | + |
| 202 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 203 | + |
| 204 | +<section class="related"> |
| 205 | + |
| 206 | +* * * |
| 207 | + |
| 208 | +## See Also |
| 209 | + |
| 210 | +- <span class="package-name">[`@stdlib/stats/strided/drange`][@stdlib/stats/strided/drange]</span><span class="delimiter">: </span><span class="description">calculate the range of a double-precision floating-point strided array.</span> |
| 211 | +- <span class="package-name">[`@stdlib/stats/strided/max-by`][@stdlib/stats/strided/max-by]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array via a callback function.</span> |
| 212 | +- <span class="package-name">[`@stdlib/stats/strided/min-by`][@stdlib/stats/strided/min-by]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a strided array via a callback function.</span> |
| 213 | +- <span class="package-name">[`@stdlib/stats/strided/nanrange-by`][@stdlib/stats/strided/nanrange-by]</span><span class="delimiter">: </span><span class="description">calculate the range of a strided array via a callback function, ignoring NaN values.</span> |
| 214 | +- <span class="package-name">[`@stdlib/stats/base/range`][@stdlib/stats/base/range]</span><span class="delimiter">: </span><span class="description">calculate the range of a strided array.</span> |
| 215 | +- <span class="package-name">[`@stdlib/stats/strided/srange`][@stdlib/stats/strided/srange]</span><span class="delimiter">: </span><span class="description">calculate the range of a single-precision floating-point strided array.</span> |
| 216 | + |
| 217 | +</section> |
| 218 | + |
| 219 | +<!-- /.related --> |
| 220 | + |
| 221 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 222 | + |
| 223 | +<section class="links"> |
| 224 | + |
| 225 | +[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29 |
| 226 | + |
| 227 | +[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array |
| 228 | + |
| 229 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 230 | + |
| 231 | +[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor |
| 232 | + |
| 233 | +<!-- <related-links> --> |
| 234 | + |
| 235 | +[@stdlib/stats/strided/drange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/drange |
| 236 | + |
| 237 | +[@stdlib/stats/strided/max-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/max-by |
| 238 | + |
| 239 | +[@stdlib/stats/strided/min-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/min-by |
| 240 | + |
| 241 | +[@stdlib/stats/strided/nanrange-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanrange-by |
| 242 | + |
| 243 | +[@stdlib/stats/base/range]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/range |
| 244 | + |
| 245 | +[@stdlib/stats/strided/srange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/srange |
| 246 | + |
| 247 | +<!-- </related-links> --> |
| 248 | + |
| 249 | +</section> |
| 250 | + |
| 251 | +<!-- /.links --> |
0 commit comments