|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# dzasum |
| 22 | + |
| 23 | +> Compute the sum of the [absolute values][@stdlib/blas/base/dcab1] of each element in a double-precision [complex][@stdlib/complex/float64/ctor] floating-point vector. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +dzasum is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:l1norm" align="center" raw="\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \left( |a_i| + |b_i| \right)" alt="L1 norm definition."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \left( |a_i| + |b_i| \right) |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \left( |a_i| + |b_i| \right)" data-equation="eq:l1norm"> |
| 36 | + <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."> |
| 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 dzasum = require( '@stdlib/blas/base/dzasum' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### dzasum( N, x, stride ) |
| 55 | + |
| 56 | +Compute the sum of the [absolute values][@stdlib/blas/base/dcab1] of each element in a double-precision [complex][@stdlib/complex/float64/ctor] floating-point vector. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 60 | + |
| 61 | +var x = new Complex128Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 62 | + |
| 63 | +var sum = dzasum( x.length, x, 1 ); |
| 64 | +// returns 19.0 |
| 65 | +``` |
| 66 | + |
| 67 | +The function has the following parameters: |
| 68 | + |
| 69 | +- **N**: number of indexed elements. |
| 70 | +- **x**: input [`Complex128Array`][complex128array]. |
| 71 | +- **stride**: index increment. |
| 72 | + |
| 73 | +The `N` and `stride` parameters determine which elements in `x` are used to compute the sum. For example, to sum every other value, |
| 74 | + |
| 75 | +```javascript |
| 76 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 77 | + |
| 78 | +var x = new Complex128Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 79 | + |
| 80 | +var sum = dzasum( 2, x, 2 ); |
| 81 | +// returns 7.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 Complex128Array = require( '@stdlib/array/complex128' ); |
| 88 | + |
| 89 | +// Initial array... |
| 90 | +var x0 = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 ] ); |
| 91 | + |
| 92 | +// Create an offset view... |
| 93 | +var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 94 | + |
| 95 | +// Sum every other value... |
| 96 | +var sum = dzasum( 2, x1, 2 ); |
| 97 | +// returns 22.0 |
| 98 | +``` |
| 99 | + |
| 100 | +If `N` is less than or equal to `0`, the function returns `0`. |
| 101 | + |
| 102 | +#### dzasum.ndarray( N, x, stride, offset ) |
| 103 | + |
| 104 | +Compute the sum of the [absolute values][@stdlib/blas/base/dcab1] of each element in a double-precision [complex][@stdlib/complex/float64/ctor] floating-point vector using alternative indexing semantics. |
| 105 | + |
| 106 | +```javascript |
| 107 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 108 | + |
| 109 | +var x = new Complex128Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); |
| 110 | + |
| 111 | +var sum = dzasum.ndarray( x.length, x, 1, 0 ); |
| 112 | +// returns 19.0 |
| 113 | +``` |
| 114 | + |
| 115 | +The function has the following additional parameters: |
| 116 | + |
| 117 | +- **offset**: starting index. |
| 118 | + |
| 119 | +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, |
| 120 | + |
| 121 | +```javascript |
| 122 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 123 | + |
| 124 | +var x = new Complex128Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 ] ); |
| 125 | + |
| 126 | +var sum = dzasum.ndarray( 3, x, 1, x.length-3 ); |
| 127 | +// returns 33.0 |
| 128 | + |
| 129 | +// Using a negative stride to sum from the last element: |
| 130 | +sum = dzasum.ndarray( 3, x, -1, x.length-1 ); |
| 131 | +// returns 33.0 |
| 132 | +``` |
| 133 | + |
| 134 | +</section> |
| 135 | + |
| 136 | +<!-- /.usage --> |
| 137 | + |
| 138 | +<section class="notes"> |
| 139 | + |
| 140 | +## Notes |
| 141 | + |
| 142 | +- If `N <= 0`, the sum is `0`. |
| 143 | +- `dzasum()` corresponds to the [BLAS][blas] level 1 function dzasum. |
| 144 | + |
| 145 | +</section> |
| 146 | + |
| 147 | +<!-- /.notes --> |
| 148 | + |
| 149 | +<section class="examples"> |
| 150 | + |
| 151 | +## Examples |
| 152 | + |
| 153 | +<!-- eslint no-undef: "error" --> |
| 154 | + |
| 155 | +```javascript |
| 156 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 157 | +var Complex128 = require( '@stdlib/complex/float64/ctor' ); |
| 158 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 159 | +var dzasum = require( '@stdlib/blas/base/dzasum' ); |
| 160 | + |
| 161 | +function rand() { |
| 162 | + return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) ); |
| 163 | +} |
| 164 | + |
| 165 | +// Generate random input arrays: |
| 166 | +var x = filledarrayBy( 10, 'complex128', rand ); |
| 167 | +console.log( x.toString() ); |
| 168 | + |
| 169 | +var out = dzasum( x.length, x, 1 ); |
| 170 | +console.log( out ); |
| 171 | +``` |
| 172 | + |
| 173 | +</section> |
| 174 | + |
| 175 | +<!-- /.examples --> |
| 176 | + |
| 177 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 178 | + |
| 179 | +<section class="related"> |
| 180 | + |
| 181 | +</section> |
| 182 | + |
| 183 | +<!-- /.related --> |
| 184 | + |
| 185 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 186 | + |
| 187 | +<section class="links"> |
| 188 | + |
| 189 | +[blas]: http://www.netlib.org/blas |
| 190 | + |
| 191 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 192 | + |
| 193 | +[@stdlib/blas/base/dcab1]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dcab1 |
| 194 | + |
| 195 | +[complex128array]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128 |
| 196 | + |
| 197 | +[@stdlib/complex/float64/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/ctor |
| 198 | + |
| 199 | +<!-- <related-links> --> |
| 200 | + |
| 201 | +<!-- </related-links> --> |
| 202 | + |
| 203 | +</section> |
| 204 | + |
| 205 | +<!-- /.links --> |
0 commit comments