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