README.md (4448B)
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 # range 22 23 > Calculate the [range][range] of a strided array. 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 range = require( '@stdlib/stats/base/range' ); 39 ``` 40 41 #### range( N, x, stride ) 42 43 Computes the [range][range] of a strided array `x`. 44 45 ```javascript 46 var x = [ 1.0, -2.0, 2.0 ]; 47 var N = x.length; 48 49 var v = range( N, x, 1 ); 50 // returns 4.0 51 ``` 52 53 The function has the following parameters: 54 55 - **N**: number of indexed elements. 56 - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. 57 - **stride**: index increment for `x`. 58 59 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [range][range] of every other element in `x`, 60 61 ```javascript 62 var floor = require( '@stdlib/math/base/special/floor' ); 63 64 var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; 65 var N = floor( x.length / 2 ); 66 67 var v = range( N, x, 2 ); 68 // returns 6.0 69 ``` 70 71 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 72 73 <!-- eslint-disable stdlib/capitalized-comments --> 74 75 ```javascript 76 var Float64Array = require( '@stdlib/array/float64' ); 77 var floor = require( '@stdlib/math/base/special/floor' ); 78 79 var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 80 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 81 82 var N = floor( x0.length / 2 ); 83 84 var v = range( N, x1, 2 ); 85 // returns 6.0 86 ``` 87 88 #### range.ndarray( N, x, stride, offset ) 89 90 Computes the [range][range] of a strided array using alternative indexing semantics. 91 92 ```javascript 93 var x = [ 1.0, -2.0, 2.0 ]; 94 var N = x.length; 95 96 var v = range.ndarray( N, x, 1, 0 ); 97 // returns 4.0 98 ``` 99 100 The function has the following additional parameters: 101 102 - **offset**: starting index for `x`. 103 104 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 calculate the [range][range] for every other value in `x` starting from the second value 105 106 ```javascript 107 var floor = require( '@stdlib/math/base/special/floor' ); 108 109 var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; 110 var N = floor( x.length / 2 ); 111 112 var v = range.ndarray( N, x, 2, 1 ); 113 // returns 6.0 114 ``` 115 116 </section> 117 118 <!-- /.usage --> 119 120 <section class="notes"> 121 122 ## Notes 123 124 - If `N <= 0`, both functions return `NaN`. 125 - Depending on the environment, the typed versions ([`drange`][@stdlib/stats/base/drange], [`srange`][@stdlib/stats/base/srange], etc.) are likely to be significantly more performant. 126 127 </section> 128 129 <!-- /.notes --> 130 131 <section class="examples"> 132 133 ## Examples 134 135 <!-- eslint no-undef: "error" --> 136 137 ```javascript 138 var randu = require( '@stdlib/random/base/randu' ); 139 var round = require( '@stdlib/math/base/special/round' ); 140 var Float64Array = require( '@stdlib/array/float64' ); 141 var range = require( '@stdlib/stats/base/range' ); 142 143 var x; 144 var i; 145 146 x = new Float64Array( 10 ); 147 for ( i = 0; i < x.length; i++ ) { 148 x[ i ] = round( (randu()*100.0) - 50.0 ); 149 } 150 console.log( x ); 151 152 var v = range( x.length, x, 1 ); 153 console.log( v ); 154 ``` 155 156 </section> 157 158 <!-- /.examples --> 159 160 <section class="links"> 161 162 [range]: https://en.wikipedia.org/wiki/Range_%28statistics%29 163 164 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 165 166 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 167 168 [@stdlib/stats/base/drange]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/drange 169 170 [@stdlib/stats/base/srange]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/srange 171 172 </section> 173 174 <!-- /.links -->