|
| 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 | +# zdotu |
| 22 | + |
| 23 | +> Calculate the dot product of two double-precision complex floating-point vectors. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [dot product][dot-product] (or scalar product) is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:dot_product" align="center" raw="\mathbf{x}\cdot\mathbf{y} = \sum_{i=0}^{N-1} x_i y_i = x_0 y_0 + x_1 y_1 + \ldots + x_{N-1} y_{N-1}" alt="Dot product definition."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\mathbf{zx}\cdot\mathbf{zy} = \sum_{i=0}^{N-1} zx_i zy_i = zx_0 zy_0 + zx_1 zy_1 + \ldots + zx_{N-1} zy_{N-1} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- <div class="equation" align="center" data-raw-text="\mathbf{x}\cdot\mathbf{y} = \sum_{i=0}^{N-1} x_i y_i = x_0 y_0 + x_1 y_1 + \ldots + x_{N-1} y_{N-1}" data-equation="eq:dot_product"> |
| 36 | + <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@929657146427564b61e3e6bdda76949ebe2ce923/lib/node_modules/@stdlib/blas/base/ddot/docs/img/equation_dot_product.svg" alt="Dot product 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 zdotu = require( '@stdlib/blas/base/zdotu' ); |
| 52 | +``` |
| 53 | + |
| 54 | +#### zdotu( N, zx, strideX, zy, strideY ) |
| 55 | + |
| 56 | +Calculates the dot product of vectors `zx` and `zy`. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 60 | +var real = require( '@stdlib/complex/float64/real' ); |
| 61 | +var imag = require( '@stdlib/complex/float64/imag' ); |
| 62 | + |
| 63 | +var zx = new Complex128Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 7.0 ] ); |
| 64 | +var zy = new Complex128Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 9.0 ] ); |
| 65 | + |
| 66 | +var z = zdotu( 3, zx, 1, zy, 1 ); |
| 67 | +// returns <Complex128> |
| 68 | + |
| 69 | +var re = real( z ); |
| 70 | +// returns -52.0 |
| 71 | + |
| 72 | +var im = imag( z ); |
| 73 | +// returns 82.0 |
| 74 | +``` |
| 75 | + |
| 76 | +The function has the following parameters: |
| 77 | + |
| 78 | +- **N**: number of indexed elements. |
| 79 | +- **zx**: input [`Complex128Array`][@stdlib/array/complex128]. |
| 80 | +- **strideX**: index increment for `zx`. |
| 81 | +- **zy**: input [`Complex128Array`][@stdlib/array/complex128]. |
| 82 | +- **strideY**: index increment for `zy`. |
| 83 | + |
| 84 | +The `N` and strides parameters determine which elements in the strided arrays are accessed at runtime. For example, to calculate the dot product of every other value in `zx` and the first `N` elements of `zy` in reverse order, |
| 85 | + |
| 86 | +```javascript |
| 87 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 88 | +var real = require( '@stdlib/complex/float64/real' ); |
| 89 | +var imag = require( '@stdlib/complex/float64/imag' ); |
| 90 | + |
| 91 | +var zx = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); |
| 92 | +var zy = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); |
| 93 | + |
| 94 | +var z = zdotu( 2, zx, 2, zy, -1 ); |
| 95 | +// returns <Complex128> |
| 96 | + |
| 97 | +var re = real( z ); |
| 98 | +// returns -2.0 |
| 99 | + |
| 100 | +var im = imag( z ); |
| 101 | +// returns 14.0 |
| 102 | +``` |
| 103 | + |
| 104 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 105 | + |
| 106 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 107 | + |
| 108 | +```javascript |
| 109 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 110 | +var real = require( '@stdlib/complex/float64/real' ); |
| 111 | +var imag = require( '@stdlib/complex/float64/imag' ); |
| 112 | + |
| 113 | +// Initial arrays... |
| 114 | +var zx0 = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 115 | +var zy0 = new Complex128Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 116 | + |
| 117 | +// Create offset views... |
| 118 | +var zx1 = new Complex128Array( zx0.buffer, zx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 119 | +var zy1 = new Complex128Array( zy0.buffer, zy0.BYTES_PER_ELEMENT*2 ); // start at 3th element |
| 120 | + |
| 121 | +var z = zdotu( 1, zx1, 1, zy1, 1 ); |
| 122 | +// returns <Complex128> |
| 123 | + |
| 124 | +var re = real( z ); |
| 125 | +// returns -15.0 |
| 126 | + |
| 127 | +var im = imag( z ); |
| 128 | +// returns 80.0 |
| 129 | +``` |
| 130 | + |
| 131 | +#### zdotu.ndarray( N, zx, strideX, offsetX, zy, strideY, offsetY ) |
| 132 | + |
| 133 | +Calculates the dot product of `zx` and `zy` using alternative indexing semantics. |
| 134 | + |
| 135 | +```javascript |
| 136 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 137 | +var real = require( '@stdlib/complex/float64/real' ); |
| 138 | +var imag = require( '@stdlib/complex/float64/imag' ); |
| 139 | + |
| 140 | +var zx = new Complex128Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 7.0 ] ); |
| 141 | +var zy = new Complex128Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 9.0 ] ); |
| 142 | + |
| 143 | +var z = zdotu.ndarray( zx.length, zx, 1, 0, zy, 1, 0 ); |
| 144 | +// returns <Complex128> |
| 145 | + |
| 146 | +var re = real( z ); |
| 147 | +// returns -52.0 |
| 148 | + |
| 149 | +var im = imag( z ); |
| 150 | +// returns 82.0 |
| 151 | +``` |
| 152 | + |
| 153 | +The function has the following additional parameters: |
| 154 | + |
| 155 | +- **offsetX**: starting index for `zx`. |
| 156 | +- **offsetY**: starting index for `zy`. |
| 157 | + |
| 158 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the dot product of every other value in `zx` starting from the second value with the last 2 elements in `zy` in reverse order |
| 159 | + |
| 160 | +```javascript |
| 161 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 162 | +var real = require( '@stdlib/complex/float64/real' ); |
| 163 | +var imag = require( '@stdlib/complex/float64/imag' ); |
| 164 | + |
| 165 | +var zx = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); |
| 166 | +var zy = new Complex128Array( [ 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 ] ); // eslint-disable-line max-len |
| 167 | + |
| 168 | +var z = zdotu.ndarray( 2, zx, 2, 1, zy, -1, zy.length-1 ); |
| 169 | +// returns <Complex128> |
| 170 | + |
| 171 | +var re = real( z ); |
| 172 | +// returns -40.0 |
| 173 | + |
| 174 | +var im = imag( z ); |
| 175 | +// returns 310.0 |
| 176 | +``` |
| 177 | + |
| 178 | +</section> |
| 179 | + |
| 180 | +<!-- /.usage --> |
| 181 | + |
| 182 | +<section class="notes"> |
| 183 | + |
| 184 | +## Notes |
| 185 | + |
| 186 | +- If `N <= 0`, both functions return `0.0 + 0.0i`. |
| 187 | +- `zdotu()` corresponds to the [BLAS][blas] level 1 function [`zdotu`][zdotu]. |
| 188 | + |
| 189 | +</section> |
| 190 | + |
| 191 | +<!-- /.notes --> |
| 192 | + |
| 193 | +<section class="examples"> |
| 194 | + |
| 195 | +## Examples |
| 196 | + |
| 197 | +<!-- eslint no-undef: "error" --> |
| 198 | + |
| 199 | +```javascript |
| 200 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 201 | +var Complex128 = require( '@stdlib/complex/float64/ctor' ); |
| 202 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 203 | +var zdotu = require( '@stdlib/blas/base/zdotu' ); |
| 204 | + |
| 205 | +function rand() { |
| 206 | + return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) ); |
| 207 | +} |
| 208 | + |
| 209 | +var zx = filledarrayBy( 10, 'complex128', rand ); |
| 210 | +console.log( zx.toString() ); |
| 211 | + |
| 212 | +var zy = filledarrayBy( 10, 'complex128', rand ); |
| 213 | +console.log( zy.toString() ); |
| 214 | + |
| 215 | +var out = zdotu.ndarray( zx.length, zx, 1, 0, zy, -1, zy.length-1 ); |
| 216 | +console.log( out.toString() ); |
| 217 | +``` |
| 218 | + |
| 219 | +</section> |
| 220 | + |
| 221 | +<!-- /.examples --> |
| 222 | + |
| 223 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 224 | + |
| 225 | +<section class="related"> |
| 226 | + |
| 227 | +</section> |
| 228 | + |
| 229 | +<!-- /.related --> |
| 230 | + |
| 231 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 232 | + |
| 233 | +<section class="links"> |
| 234 | + |
| 235 | +[dot-product]: https://en.wikipedia.org/wiki/Dot_product |
| 236 | + |
| 237 | +[blas]: http://www.netlib.org/blas |
| 238 | + |
| 239 | +[zdotu]: https://www.netlib.org/lapack/explore-html/d1/dcc/group__dot_ga6b0b69474b384d45fc4c7b1f7ec5959f.html#ga6b0b69474b384d45fc4c7b1f7ec5959f |
| 240 | + |
| 241 | +[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128 |
| 242 | + |
| 243 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 244 | + |
| 245 | +<!-- <related-links> --> |
| 246 | + |
| 247 | +<!-- </related-links> --> |
| 248 | + |
| 249 | +</section> |
| 250 | + |
| 251 | +<!-- /.links --> |
0 commit comments