|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +```math |
| 32 | +\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \vert x_i \vert |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \vert x_i \vert" data-equation="eq:l1norm"> |
| 36 | + <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."> |
| 37 | + <br> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +</section> |
| 43 | + |
| 44 | +<!-- /.intro --> |
| 45 | + |
| 46 | +<section class="usage"> |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +```javascript |
| 51 | +var sasum = require( '@stdlib/blas/sasum' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### sasum( x\[, dim] ) |
| 55 | + |
| 56 | +Computes the sum of [absolute values][@stdlib/math/base/special/abs]. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 60 | +var array = require( '@stdlib/ndarray/array' ); |
| 61 | + |
| 62 | +var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); |
| 63 | + |
| 64 | +var z = sasum( x ); |
| 65 | +// returns <ndarray> |
| 66 | + |
| 67 | +var v = z.get(); |
| 68 | +// returns 15.0 |
| 69 | +``` |
| 70 | + |
| 71 | +The function has the following parameters: |
| 72 | + |
| 73 | +- **x**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor] whose underlying data type is `float32`. |
| 74 | +- **dim**: dimension for which to compute the sum. Must be a negative integer. Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. Default: `-1`. |
| 75 | + |
| 76 | +For multi-dimensional input [`ndarrays`][@stdlib/ndarray/ctor], the function performs batched computation, such that the function computes the sum for each pair of vectors in `x` according to the specified dimension index. |
| 77 | + |
| 78 | +```javascript |
| 79 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 80 | +var array = require( '@stdlib/ndarray/array' ); |
| 81 | + |
| 82 | +var opts = { |
| 83 | + 'shape': [ 2, 3 ] |
| 84 | +}; |
| 85 | +var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 3.0 ] ), opts ); |
| 86 | + |
| 87 | +var z = sasum( x ); |
| 88 | +// returns <ndarray> |
| 89 | + |
| 90 | +var v1 = z.get( 0 ); |
| 91 | +// returns 9.0 |
| 92 | + |
| 93 | +var v2 = z.get( 1 ); |
| 94 | +// returns 9.0 |
| 95 | +``` |
| 96 | + |
| 97 | +</section> |
| 98 | + |
| 99 | +<!-- /.usage --> |
| 100 | + |
| 101 | +<section class="notes"> |
| 102 | + |
| 103 | +## Notes |
| 104 | + |
| 105 | +- Negative indices are resolved relative to the last [`ndarray`][@stdlib/ndarray/ctor] dimension, with the last dimension corresponding to `-1`. |
| 106 | +- The output [`ndarray`][@stdlib/ndarray/ctor] has the same data type as the input [`ndarray`][@stdlib/ndarray/ctor] and has a shape which is determined by broadcasting and excludes the contracted dimension. |
| 107 | +- If provided an empty vector, the sum is `0`. |
| 108 | +- `sasum()` provides a higher-level interface to the [BLAS][blas] level 1 function [`sasum`][@stdlib/blas/base/sasum]. |
| 109 | + |
| 110 | +</section> |
| 111 | + |
| 112 | +<!-- /.notes --> |
| 113 | + |
| 114 | +<section class="examples"> |
| 115 | + |
| 116 | +## Examples |
| 117 | + |
| 118 | +<!-- eslint no-undef: "error" --> |
| 119 | + |
| 120 | +```javascript |
| 121 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 122 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 123 | +var array = require( '@stdlib/ndarray/array' ); |
| 124 | +var sasum = require( '@stdlib/blas/sasum' ); |
| 125 | + |
| 126 | +var opts = { |
| 127 | + 'dtype': 'float32' |
| 128 | +}; |
| 129 | + |
| 130 | +var x = array( discreteUniform( 10, 0, 100, opts ), { |
| 131 | + 'shape': [ 5, 2 ] |
| 132 | +}); |
| 133 | +console.log( ndarray2array( x ) ); |
| 134 | + |
| 135 | +var z = sasum( x, -1 ); |
| 136 | +console.log( ndarray2array( z ) ); |
| 137 | +``` |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.examples --> |
| 142 | + |
| 143 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 144 | + |
| 145 | +<section class="related"> |
| 146 | + |
| 147 | +</section> |
| 148 | + |
| 149 | +<!-- /.related --> |
| 150 | + |
| 151 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 152 | + |
| 153 | +<section class="links"> |
| 154 | + |
| 155 | +[l1norm]: https://en.wikipedia.org/wiki/Norm_%28mathematics%29 |
| 156 | + |
| 157 | +[@stdlib/math/base/special/abs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/abs |
| 158 | + |
| 159 | +[blas]: http://www.netlib.org/blas |
| 160 | + |
| 161 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 162 | + |
| 163 | +[@stdlib/blas/base/sasum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/sasum |
| 164 | + |
| 165 | +</section> |
| 166 | + |
| 167 | +<!-- /.links --> |
0 commit comments