|
| 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 | +# cdotu |
| 22 | + |
| 23 | +> Calculate the [dot product][dot-product] of two single-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{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}" 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 | +<!-- </equation> --> |
| 36 | + |
| 37 | +</section> |
| 38 | + |
| 39 | +<!-- /.intro --> |
| 40 | + |
| 41 | +<section class="usage"> |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +```javascript |
| 46 | +var cdotu = require( '@stdlib/blas/base/cdotu' ); |
| 47 | +``` |
| 48 | + |
| 49 | +#### cdotu( N, zx, strideX, zy, strideY ) |
| 50 | + |
| 51 | +Calculates the dot product of vectors `zx` and `zy`. |
| 52 | + |
| 53 | +```javascript |
| 54 | +var Complex64Array = require( '@stdlib/array/complex64' ); |
| 55 | +var real = require( '@stdlib/complex/float32/real' ); |
| 56 | +var imag = require( '@stdlib/complex/float32/imag' ); |
| 57 | + |
| 58 | +var zx = new Complex64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 7.0 ] ); |
| 59 | +var zy = new Complex64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 9.0 ] ); |
| 60 | + |
| 61 | +var z = cdotu( 3, zx, 1, zy, 1 ); |
| 62 | +// returns <Complex64> |
| 63 | + |
| 64 | +var re = real( z ); |
| 65 | +// returns -52.0 |
| 66 | + |
| 67 | +var im = imag( z ); |
| 68 | +// returns 82.0 |
| 69 | +``` |
| 70 | + |
| 71 | +The function has the following parameters: |
| 72 | + |
| 73 | +- **N**: number of indexed elements. |
| 74 | +- **zx**: input [`Complex64Array`][@stdlib/array/complex64]. |
| 75 | +- **strideX**: index increment for `zx`. |
| 76 | +- **zy**: input [`Complex64Array`][@stdlib/array/complex64]. |
| 77 | +- **strideY**: index increment for `zy`. |
| 78 | + |
| 79 | +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, |
| 80 | + |
| 81 | +```javascript |
| 82 | +var Complex64Array = require( '@stdlib/array/complex64' ); |
| 83 | +var real = require( '@stdlib/complex/float32/real' ); |
| 84 | +var imag = require( '@stdlib/complex/float32/imag' ); |
| 85 | + |
| 86 | +var zx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); |
| 87 | +var zy = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); |
| 88 | + |
| 89 | +var z = cdotu( 2, zx, 2, zy, -1 ); |
| 90 | +// returns <Complex64> |
| 91 | + |
| 92 | +var re = real( z ); |
| 93 | +// returns -2.0 |
| 94 | + |
| 95 | +var im = imag( z ); |
| 96 | +// returns 14.0 |
| 97 | +``` |
| 98 | + |
| 99 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 100 | + |
| 101 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 102 | + |
| 103 | +```javascript |
| 104 | +var Complex64Array = require( '@stdlib/array/complex64' ); |
| 105 | +var real = require( '@stdlib/complex/float32/real' ); |
| 106 | +var imag = require( '@stdlib/complex/float32/imag' ); |
| 107 | + |
| 108 | +// Initial arrays... |
| 109 | +var zx0 = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 110 | +var zy0 = new Complex64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 111 | + |
| 112 | +// Create offset views... |
| 113 | +var zx1 = new Complex64Array( zx0.buffer, zx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 114 | +var zy1 = new Complex64Array( zy0.buffer, zy0.BYTES_PER_ELEMENT*2 ); // start at 3th element |
| 115 | + |
| 116 | +var z = cdotu( 1, zx1, 1, zy1, 1 ); |
| 117 | +// returns <Complex64> |
| 118 | + |
| 119 | +var re = real( z ); |
| 120 | +// returns -15.0 |
| 121 | + |
| 122 | +var im = imag( z ); |
| 123 | +// returns 80.0 |
| 124 | +``` |
| 125 | + |
| 126 | +#### cdotu.ndarray( N, zx, strideX, offsetX, zy, strideY, offsetY ) |
| 127 | + |
| 128 | +Calculates the dot product of `zx` and `zy` using alternative indexing semantics. |
| 129 | + |
| 130 | +```javascript |
| 131 | +var Complex64Array = require( '@stdlib/array/complex64' ); |
| 132 | +var real = require( '@stdlib/complex/float32/real' ); |
| 133 | +var imag = require( '@stdlib/complex/float32/imag' ); |
| 134 | + |
| 135 | +var zx = new Complex64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 7.0 ] ); |
| 136 | +var zy = new Complex64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 9.0 ] ); |
| 137 | + |
| 138 | +var z = cdotu.ndarray( zx.length, zx, 1, 0, zy, 1, 0 ); |
| 139 | +// returns <Complex64> |
| 140 | + |
| 141 | +var re = real( z ); |
| 142 | +// returns -52.0 |
| 143 | + |
| 144 | +var im = imag( z ); |
| 145 | +// returns 82.0 |
| 146 | +``` |
| 147 | + |
| 148 | +The function has the following additional parameters: |
| 149 | + |
| 150 | +- **offsetX**: starting index for `zx`. |
| 151 | +- **offsetY**: starting index for `zy`. |
| 152 | + |
| 153 | +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 |
| 154 | + |
| 155 | +```javascript |
| 156 | +var Complex64Array = require( '@stdlib/array/complex64' ); |
| 157 | +var real = require( '@stdlib/complex/float32/real' ); |
| 158 | +var imag = require( '@stdlib/complex/float32/imag' ); |
| 159 | + |
| 160 | +var zx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); |
| 161 | +var zy = new Complex64Array( [ 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 ] ); // eslint-disable-line max-len |
| 162 | + |
| 163 | +var z = cdotu.ndarray( 2, zx, 2, 1, zy, -1, zy.length-1 ); |
| 164 | +// returns <Complex64> |
| 165 | + |
| 166 | +var re = real( z ); |
| 167 | +// returns -40.0 |
| 168 | + |
| 169 | +var im = imag( z ); |
| 170 | +// returns 310.0 |
| 171 | +``` |
| 172 | + |
| 173 | +</section> |
| 174 | + |
| 175 | +<!-- /.usage --> |
| 176 | + |
| 177 | +<section class="notes"> |
| 178 | + |
| 179 | +## Notes |
| 180 | + |
| 181 | +- If `N <= 0`, both functions return `0.0 + 0.0i`. |
| 182 | +- `cdotu()` corresponds to the [BLAS][blas] level 1 function [`cdotu`][cdotu]. |
| 183 | + |
| 184 | +</section> |
| 185 | + |
| 186 | +<!-- /.notes --> |
| 187 | + |
| 188 | +<section class="examples"> |
| 189 | + |
| 190 | +## Examples |
| 191 | + |
| 192 | +<!-- eslint no-undef: "error" --> |
| 193 | + |
| 194 | +```javascript |
| 195 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 196 | +var Complex64 = require( '@stdlib/complex/float32/ctor' ); |
| 197 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 198 | +var cdotu = require( '@stdlib/blas/base/cdotu' ); |
| 199 | + |
| 200 | +function rand() { |
| 201 | + return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) ); |
| 202 | +} |
| 203 | + |
| 204 | +var zx = filledarrayBy( 10, 'complex64', rand ); |
| 205 | +console.log( zx.toString() ); |
| 206 | + |
| 207 | +var zy = filledarrayBy( 10, 'complex64', rand ); |
| 208 | +console.log( zy.toString() ); |
| 209 | + |
| 210 | +var out = cdotu.ndarray( zx.length, zx, 1, 0, zy, -1, zy.length-1 ); |
| 211 | +console.log( out.toString() ); |
| 212 | +``` |
| 213 | + |
| 214 | +</section> |
| 215 | + |
| 216 | +<!-- /.examples --> |
| 217 | + |
| 218 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 219 | + |
| 220 | +<section class="related"> |
| 221 | + |
| 222 | +</section> |
| 223 | + |
| 224 | +<!-- /.related --> |
| 225 | + |
| 226 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 227 | + |
| 228 | +<section class="links"> |
| 229 | + |
| 230 | +[dot-product]: https://en.wikipedia.org/wiki/Dot_product |
| 231 | + |
| 232 | +[blas]: http://www.netlib.org/blas |
| 233 | + |
| 234 | +[cdotu]: https://www.netlib.org/lapack/explore-html/d7/d7b/cdotu_8f_source.html |
| 235 | + |
| 236 | +[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64 |
| 237 | + |
| 238 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 239 | + |
| 240 | +<!-- <related-links> --> |
| 241 | + |
| 242 | +<!-- </related-links> --> |
| 243 | + |
| 244 | +</section> |
| 245 | + |
| 246 | +<!-- /.links --> |
0 commit comments