README.md (5675B)
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 # minBy 22 23 > Calculate the minimum value of a strided array via a callback function. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var minBy = require( '@stdlib/stats/base/min-by' ); 31 ``` 32 33 #### minBy( N, x, stride, clbk\[, thisArg] ) 34 35 Calculates the minimum value of strided array `x` via a callback function. 36 37 ```javascript 38 function accessor( v ) { 39 return v * 2.0; 40 } 41 42 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 43 44 var v = minBy( x.length, x, 1, accessor ); 45 // returns -10.0 46 ``` 47 48 The function has the following parameters: 49 50 - **N**: number of indexed elements. 51 - **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions). 52 - **stride**: index increment. 53 - **clbk**: callback function. 54 - **thisArg**: execution context (_optional_). 55 56 The invoked callback is provided four arguments: 57 58 - **value**: array element. 59 - **aidx**: array index. 60 - **sidx**: strided index (`offset + aidx*stride`). 61 - **array**: input array/collection. 62 63 To set the callback execution context, provide a `thisArg`. 64 65 ```javascript 66 function accessor( v ) { 67 this.count += 1; 68 return v * 2.0; 69 } 70 71 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 72 73 var context = { 74 'count': 0 75 }; 76 77 var v = minBy( x.length, x, 1, accessor, context ); 78 // returns -10.0 79 80 var cnt = context.count; 81 // returns 8 82 ``` 83 84 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element 85 86 ```javascript 87 var floor = require( '@stdlib/math/base/special/floor' ); 88 89 function accessor( v ) { 90 return v * 2.0; 91 } 92 93 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 94 var N = floor( x.length / 2 ); 95 96 var v = minBy( N, x, 2, accessor ); 97 // returns -4.0 98 ``` 99 100 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 101 102 ```javascript 103 var Float64Array = require( '@stdlib/array/float64' ); 104 var floor = require( '@stdlib/math/base/special/floor' ); 105 106 function accessor( v ) { 107 return v * 2.0; 108 } 109 110 // Initial array... 111 var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); 112 113 // Create an offset view... 114 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 115 var N = floor( x0.length/2 ); 116 117 // Access every other element... 118 var v = minBy( N, x1, 2, accessor ); 119 // returns -12.0 120 ``` 121 122 #### minBy.ndarray( N, x, stride, offset, clbk\[, thisArg] ) 123 124 Calculates the minimum value of strided array `x` via a callback function and using alternative indexing semantics. 125 126 ```javascript 127 function accessor( v ) { 128 return v * 2.0; 129 } 130 131 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 132 133 var v = minBy.ndarray( x.length, x, 1, 0, accessor ); 134 // returns -10.0 135 ``` 136 137 The function has the following additional parameters: 138 139 - **offset**: starting index. 140 141 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` 142 143 ```javascript 144 function accessor( v ) { 145 return v * 2.0; 146 } 147 148 var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; 149 150 var v = minBy.ndarray( 3, x, 1, x.length-3, accessor ); 151 // returns -12.0 152 ``` 153 154 </section> 155 156 <!-- /.usage --> 157 158 <section class="notes"> 159 160 ## Notes 161 162 - If `N <= 0`, both functions return `NaN`. 163 - A provided callback function should return a numeric value. 164 - If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**. 165 - When possible, prefer using [`dmin`][@stdlib/stats/base/dmin], [`smin`][@stdlib/stats/base/smin], and/or [`min`][@stdlib/stats/base/min], as, depending on the environment, these interfaces are likely to be significantly more performant. 166 167 </section> 168 169 <!-- /.notes --> 170 171 <section class="examples"> 172 173 ## Examples 174 175 <!-- eslint no-undef: "error" --> 176 177 ```javascript 178 var round = require( '@stdlib/math/base/special/round' ); 179 var randu = require( '@stdlib/random/base/randu' ); 180 var Float64Array = require( '@stdlib/array/float64' ); 181 var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' ); 182 var minBy = require( '@stdlib/stats/base/min-by' ); 183 184 function fill() { 185 return round( ( randu()*100.0 ) - 50.0 ); 186 } 187 188 function accessor( v ) { 189 return v * 2.0; 190 } 191 192 var x = new Float64Array( 10 ); 193 194 gfillBy( x.length, x, 1, fill ); 195 console.log( x ); 196 197 var v = minBy( x.length, x, 1, accessor ); 198 console.log( v ); 199 ``` 200 201 </section> 202 203 <!-- /.examples --> 204 205 <section class="links"> 206 207 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 208 209 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 210 211 [@stdlib/stats/base/dmin]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/dmin 212 213 [@stdlib/stats/base/min]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/min 214 215 [@stdlib/stats/base/smin]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/smin 216 217 </section> 218 219 <!-- /.links -->