README.md (6014B)
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 # gasumpw 22 23 > Calculate the sum of absolute values ([_L1_ norm][l1norm]) of strided array elements using pairwise summation. 24 25 <section class="intro"> 26 27 The [_L1_ norm][l1norm] is defined as 28 29 <!-- <equation class="equation" label="eq:l1norm" align="center" raw="\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \vert x_i \vert" alt="L1 norm definition."> --> 30 31 <div class="equation" align="center" data-raw-text="\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \vert x_i \vert" data-equation="eq:l1norm"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b989712575768de8eab07dd05f635c636b46f278/lib/node_modules/@stdlib/blas/ext/base/gasumpw/docs/img/equation_l1norm.svg" alt="L1 norm definition."> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 </section> 39 40 <!-- /.intro --> 41 42 <section class="usage"> 43 44 ## Usage 45 46 ```javascript 47 var gasumpw = require( '@stdlib/blas/ext/base/gasumpw' ); 48 ``` 49 50 #### gasumpw( N, x, stride ) 51 52 Computes the sum of absolute values ([_L1_ norm][l1norm]) of strided array elements using pairwise summation. 53 54 ```javascript 55 var x = [ 1.0, -2.0, 2.0 ]; 56 57 var v = gasumpw( x.length, x, 1 ); 58 // returns 5.0 59 ``` 60 61 The function has the following parameters: 62 63 - **N**: number of indexed elements. 64 - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. 65 - **stride**: index increment for `x`. 66 67 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the sum of absolute values of every other element in `x`, 68 69 ```javascript 70 var floor = require( '@stdlib/math/base/special/floor' ); 71 72 var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; 73 var N = floor( x.length / 2 ); 74 75 var v = gasumpw( N, x, 2 ); 76 // returns 9.0 77 ``` 78 79 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 80 81 <!-- eslint-disable stdlib/capitalized-comments --> 82 83 ```javascript 84 var Float64Array = require( '@stdlib/array/float64' ); 85 var floor = require( '@stdlib/math/base/special/floor' ); 86 87 var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 88 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 89 90 var N = floor( x0.length / 2 ); 91 92 var v = gasumpw( N, x1, 2 ); 93 // returns 9.0 94 ``` 95 96 #### gasumpw.ndarray( N, x, stride, offset ) 97 98 Computes the sum of absolute values ([_L1_ norm][l1norm]) of strided array elements using pairwise summation and alternative indexing semantics. 99 100 ```javascript 101 var x = [ 1.0, -2.0, 2.0 ]; 102 103 var v = gasumpw.ndarray( x.length, x, 1, 0 ); 104 // returns 5.0 105 ``` 106 107 The function has the following additional parameters: 108 109 - **offset**: starting index for `x`. 110 111 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 sum of absolute values of every other value in `x` starting from the second value 112 113 ```javascript 114 var floor = require( '@stdlib/math/base/special/floor' ); 115 116 var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; 117 var N = floor( x.length / 2 ); 118 119 var v = gasumpw.ndarray( N, x, 2, 1 ); 120 // returns 9.0 121 ``` 122 123 </section> 124 125 <!-- /.usage --> 126 127 <section class="notes"> 128 129 ## Notes 130 131 - If `N <= 0`, both functions return `0.0`. 132 - In general, pairwise summation is more numerically stable than ordinary recursive summation (i.e., "simple" summation), with slightly worse performance. While not the most numerically stable summation technique (e.g., compensated summation techniques such as the Kahan–Babuška-Neumaier algorithm are generally more numerically stable), pairwise summation strikes a reasonable balance between numerical stability and performance. If either numerical stability or performance is more desirable for your use case, consider alternative summation techniques. 133 - Depending on the environment, the typed versions ([`dasumpw`][@stdlib/blas/ext/base/dasumpw], [`sasumpw`][@stdlib/blas/ext/base/sasumpw], etc.) are likely to be significantly more performant. 134 135 </section> 136 137 <!-- /.notes --> 138 139 <section class="examples"> 140 141 ## Examples 142 143 <!-- eslint no-undef: "error" --> 144 145 ```javascript 146 var randu = require( '@stdlib/random/base/randu' ); 147 var round = require( '@stdlib/math/base/special/round' ); 148 var Float64Array = require( '@stdlib/array/float64' ); 149 var gasumpw = require( '@stdlib/blas/ext/base/gasumpw' ); 150 151 var x; 152 var i; 153 154 x = new Float64Array( 10 ); 155 for ( i = 0; i < x.length; i++ ) { 156 x[ i ] = round( randu()*100.0 ); 157 } 158 console.log( x ); 159 160 var v = gasumpw( x.length, x, 1 ); 161 console.log( v ); 162 ``` 163 164 </section> 165 166 <!-- /.examples --> 167 168 * * * 169 170 <section class="references"> 171 172 ## References 173 174 - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050][@higham:1993a]. 175 176 </section> 177 178 <!-- /.references --> 179 180 <section class="links"> 181 182 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 183 184 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 185 186 [@stdlib/blas/ext/base/dasumpw]: https://www.npmjs.com/package/@stdlib/blas/tree/main/ext/base/dasumpw 187 188 [@stdlib/blas/ext/base/sasumpw]: https://www.npmjs.com/package/@stdlib/blas/tree/main/ext/base/sasumpw 189 190 [l1norm]: http://en.wikipedia.org/wiki/Norm_%28mathematics%29 191 192 [@higham:1993a]: https://doi.org/10.1137/0914050 193 194 </section> 195 196 <!-- /.links -->