README.md (5572B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # gasum 22 23 > Compute the sum of [absolute values][@stdlib/math/base/special/abs] ([_L1_ norm][l1norm]). 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@30de397fadee10fa175a8332ae1447737f201818/lib/node_modules/@stdlib/blas/base/gasum/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 gasum = require( '@stdlib/blas/base/gasum' ); 48 ``` 49 50 #### gasum( N, x, stride ) 51 52 Computes the sum of [absolute values][@stdlib/math/base/special/abs]. 53 54 ```javascript 55 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 56 57 var sum = gasum( x.length, x, 1 ); 58 // returns 19.0 59 ``` 60 61 The function has the following parameters: 62 63 - **N**: number of elements to sum. 64 - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. 65 - **stride**: index increment. 66 67 The `N` and `stride` parameters determine which elements in `x` are used to compute the sum. For example, to sum every other value, 68 69 ```javascript 70 var floor = require( '@stdlib/math/base/special/floor' ); 71 72 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 73 74 var N = floor( x.length / 2 ); 75 var stride = 2; 76 77 var sum = gasum( N, x, stride ); 78 // returns 10.0 79 ``` 80 81 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 82 83 ```javascript 84 var Float64Array = require( '@stdlib/array/float64' ); 85 86 var floor = require( '@stdlib/math/base/special/floor' ); 87 88 // Initial array... 89 var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); 90 91 // Create an offset view... 92 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 93 94 var N = floor( x0.length / 2 ); 95 96 // Sum every other value... 97 var sum = gasum( N, x1, 2 ); 98 // returns 12.0 99 ``` 100 101 If either `N` or `stride` is less than or equal to `0`, the function returns `0`. 102 103 #### gasum.ndarray( N, x, stride, offset ) 104 105 Computes the sum of [absolute values][@stdlib/math/base/special/abs] using alternative indexing semantics. 106 107 ```javascript 108 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 109 110 var sum = gasum.ndarray( x.length, x, 1, 0 ); 111 // returns 19.0 112 ``` 113 114 The function has the following additional parameters: 115 116 - **offset**: starting index. 117 118 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 sum the last three elements, 119 120 ```javascript 121 var Float64Array = require( '@stdlib/array/float64' ); 122 123 var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); 124 125 var sum = gasum.ndarray( 3, x, 1, x.length-3 ); 126 // returns 15.0 127 128 // Using a negative stride to sum from the last element: 129 sum = gasum.ndarray( 3, x, -1, x.length-1 ); 130 // returns 15.0 131 ``` 132 133 </section> 134 135 <!-- /.usage --> 136 137 <section class="notes"> 138 139 ## Notes 140 141 - If `N <= 0`, both functions return `0`. 142 - `gasum()` corresponds to the [BLAS][blas] level 1 function [`dasum`][dasum] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dasum`][@stdlib/blas/base/dasum], [`sasum`][@stdlib/blas/base/sasum], etc.) are likely to be significantly more performant. 143 144 </section> 145 146 <!-- /.notes --> 147 148 <section class="examples"> 149 150 ## Examples 151 152 <!-- eslint no-undef: "error" --> 153 154 ```javascript 155 var round = require( '@stdlib/math/base/special/round' ); 156 var randu = require( '@stdlib/random/base/randu' ); 157 var gasum = require( '@stdlib/blas/base/gasum' ); 158 159 var rand; 160 var sign; 161 var x; 162 var i; 163 164 x = []; 165 for ( i = 0; i < 100; i++ ) { 166 rand = round( randu()*100.0 ); 167 sign = randu(); 168 if ( sign < 0.5 ) { 169 sign = -1.0; 170 } else { 171 sign = 1.0; 172 } 173 x.push( sign*rand ); 174 } 175 console.log( gasum( x.length, x, 1 ) ); 176 ``` 177 178 </section> 179 180 <!-- /.examples --> 181 182 <section class="links"> 183 184 [blas]: http://www.netlib.org/blas 185 186 [dasum]: http://www.netlib.org/lapack/explore-html/de/da4/group__double__blas__level1.html 187 188 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 189 190 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 191 192 [l1norm]: http://en.wikipedia.org/wiki/Norm_%28mathematics%29 193 194 [@stdlib/math/base/special/abs]: https://www.npmjs.com/package/@stdlib/math-base-special-abs 195 196 [@stdlib/blas/base/dasum]: https://www.npmjs.com/package/@stdlib/blas/tree/main/base/dasum 197 198 [@stdlib/blas/base/sasum]: https://www.npmjs.com/package/@stdlib/blas/tree/main/base/sasum 199 200 </section> 201 202 <!-- /.links -->