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