|
| 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 | +# zswap |
| 22 | + |
| 23 | +> Interchange two complex double-precision floating-point vectors. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +This BLAS level 1 routine interchanges complex double-precision floating-point vectors `x` and `y`. The operation is performed in-place, with `x` being overwritten with the values from `y`, and `y` being overwritten with the values from `x`. |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<section class="usage"> |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +```javascript |
| 38 | +var zswap = require( '@stdlib/blas/base/zswap' ); |
| 39 | +``` |
| 40 | + |
| 41 | +#### zswap( N, x, strideX, y, strideY ) |
| 42 | + |
| 43 | +Interchange two complex double-precision floating-point vectors. |
| 44 | + |
| 45 | +```javascript |
| 46 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 47 | +var real = require( '@stdlib/complex/real' ); |
| 48 | +var imag = require( '@stdlib/complex/imag' ); |
| 49 | + |
| 50 | +var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 51 | +var y = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 52 | + |
| 53 | +zswap( x.length, x, 1, y, 1 ); |
| 54 | + |
| 55 | +var z = y.get( 0 ); |
| 56 | +// returns <Complex128> |
| 57 | + |
| 58 | +var re = real( z ); |
| 59 | +// returns 1.0 |
| 60 | + |
| 61 | +var im = imag( z ); |
| 62 | +// returns 2.0 |
| 63 | + |
| 64 | +z = x.get( 0 ); |
| 65 | +// returns <Complex128> |
| 66 | + |
| 67 | +re = real( z ); |
| 68 | +// returns 0.0 |
| 69 | + |
| 70 | +im = imag( z ); |
| 71 | +// returns 0.0 |
| 72 | +``` |
| 73 | + |
| 74 | +The function has the following parameters: |
| 75 | + |
| 76 | +- **N**: number of indexed elements. |
| 77 | +- **x**: first input [`Complex128Array`][@stdlib/array/complex128]. |
| 78 | +- **strideX**: index increment for `x`. |
| 79 | +- **y**: second input [`Complex128Array`][@stdlib/array/complex128]. |
| 80 | +- **strideY**: index increment for `y`. |
| 81 | + |
| 82 | +The `N` and stride parameters determine how values from `x` are interchanged with values from `y`. For example, to interchange in reverse order every other value in `x` into the first `N` elements of `y`, |
| 83 | + |
| 84 | +```javascript |
| 85 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 86 | +var real = require( '@stdlib/complex/real' ); |
| 87 | +var imag = require( '@stdlib/complex/imag' ); |
| 88 | + |
| 89 | +var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); |
| 90 | +var y = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 91 | + |
| 92 | +zswap( 2, x, -2, y, 1 ); |
| 93 | + |
| 94 | +var z = y.get( 0 ); |
| 95 | +// returns <Complex128> |
| 96 | + |
| 97 | +var re = real( z ); |
| 98 | +// returns 5.0 |
| 99 | + |
| 100 | +var im = imag( z ); |
| 101 | +// returns 6.0 |
| 102 | + |
| 103 | +z = x.get( 0 ); |
| 104 | +// returns <Complex128> |
| 105 | + |
| 106 | +re = real( z ); |
| 107 | +// returns 0.0 |
| 108 | + |
| 109 | +im = imag( z ); |
| 110 | +// returns 0.0 |
| 111 | +``` |
| 112 | + |
| 113 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 114 | + |
| 115 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 116 | + |
| 117 | +```javascript |
| 118 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 119 | +var real = require( '@stdlib/complex/real' ); |
| 120 | +var imag = require( '@stdlib/complex/imag' ); |
| 121 | + |
| 122 | +// Initial arrays... |
| 123 | +var x0 = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); |
| 124 | +var y0 = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 125 | + |
| 126 | +// Create offset views... |
| 127 | +var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 128 | +var y1 = new Complex128Array( y0.buffer, y0.BYTES_PER_ELEMENT*2 ); // start at 3rd element |
| 129 | + |
| 130 | +// Interchange in reverse order every other value from `x1` into `y1`... |
| 131 | +zswap( 2, x1, -2, y1, 1 ); |
| 132 | + |
| 133 | +var z = y0.get( 2 ); |
| 134 | +// returns <Complex128> |
| 135 | + |
| 136 | +var re = real( z ); |
| 137 | +// returns 7.0 |
| 138 | + |
| 139 | +var im = imag( z ); |
| 140 | +// returns 8.0 |
| 141 | + |
| 142 | +z = x0.get( 1 ); |
| 143 | +// returns <Complex128> |
| 144 | + |
| 145 | +re = real( z ); |
| 146 | +// returns 0.0 |
| 147 | + |
| 148 | +im = imag( z ); |
| 149 | +// returns 0.0 |
| 150 | +``` |
| 151 | + |
| 152 | +#### zswap.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) |
| 153 | + |
| 154 | +Interchange two complex double-precision floating-point vectors using alternative indexing semantics. |
| 155 | + |
| 156 | +```javascript |
| 157 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 158 | +var real = require( '@stdlib/complex/real' ); |
| 159 | +var imag = require( '@stdlib/complex/imag' ); |
| 160 | + |
| 161 | +var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 162 | +var y = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 163 | + |
| 164 | +zswap.ndarray( x.length, x, 1, 0, y, 1, 0 ); |
| 165 | + |
| 166 | +var z = y.get( 0 ); |
| 167 | +// returns <Complex128> |
| 168 | + |
| 169 | +var re = real( z ); |
| 170 | +// returns 1.0 |
| 171 | + |
| 172 | +var im = imag( z ); |
| 173 | +// returns 2.0 |
| 174 | + |
| 175 | +z = x.get( 0 ); |
| 176 | +// returns <Complex128> |
| 177 | + |
| 178 | +re = real( z ); |
| 179 | +// returns 0.0 |
| 180 | + |
| 181 | +im = imag( z ); |
| 182 | +// returns 0.0 |
| 183 | +``` |
| 184 | + |
| 185 | +The function has the following additional parameters: |
| 186 | + |
| 187 | +- **offsetX**: starting index for `x`. |
| 188 | +- **offsetY**: starting index for `y`. |
| 189 | + |
| 190 | +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 interchange every other value in `x` starting from the second value into the last `N` elements in `y` where `x[i] = y[n]`, `x[i+2] = y[n-1]`, and so on, |
| 191 | + |
| 192 | +```javascript |
| 193 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 194 | +var real = require( '@stdlib/complex/real' ); |
| 195 | +var imag = require( '@stdlib/complex/imag' ); |
| 196 | + |
| 197 | +var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); |
| 198 | +var y = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 199 | + |
| 200 | +zswap.ndarray( 2, x, 2, 1, y, -1, y.length-1 ); |
| 201 | + |
| 202 | +var z = y.get( y.length-1 ); |
| 203 | +// returns <Complex128> |
| 204 | + |
| 205 | +var re = real( z ); |
| 206 | +// returns 3.0 |
| 207 | + |
| 208 | +var im = imag( z ); |
| 209 | +// returns 4.0 |
| 210 | + |
| 211 | +z = x.get( x.length-1 ); |
| 212 | +// returns <Complex128> |
| 213 | + |
| 214 | +re = real( z ); |
| 215 | +// returns 0.0 |
| 216 | + |
| 217 | +im = imag( z ); |
| 218 | +// returns 0.0 |
| 219 | +``` |
| 220 | + |
| 221 | +</section> |
| 222 | + |
| 223 | +<!-- /.usage --> |
| 224 | + |
| 225 | +<section class="notes"> |
| 226 | + |
| 227 | +## Notes |
| 228 | + |
| 229 | +- If `N <= 0`, both functions leave `x` and `y` unchanged. |
| 230 | +- `zswap()` corresponds to the [BLAS][blas] level 1 function [`zswap`][zswap]. |
| 231 | + |
| 232 | +</section> |
| 233 | + |
| 234 | +<!-- /.notes --> |
| 235 | + |
| 236 | +<section class="examples"> |
| 237 | + |
| 238 | +## Examples |
| 239 | + |
| 240 | +<!-- eslint no-undef: "error" --> |
| 241 | + |
| 242 | +```javascript |
| 243 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 244 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 245 | +var Complex128 = require( '@stdlib/complex/float64' ); |
| 246 | +var zswap = require( '@stdlib/blas/base/zswap' ); |
| 247 | + |
| 248 | +function rand() { |
| 249 | + return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) ); |
| 250 | +} |
| 251 | + |
| 252 | +var x = filledarrayBy( 10, 'complex128', rand ); |
| 253 | +console.log( x.get( 0 ).toString() ); |
| 254 | + |
| 255 | +var y = filledarrayBy( 10, 'complex128', rand ); |
| 256 | +console.log( y.get( 0 ).toString() ); |
| 257 | + |
| 258 | +// Swap elements in `x` into `y` starting from the end of `y`: |
| 259 | +zswap( x.length, x, 1, y, -1 ); |
| 260 | +console.log( x.get( x.length-1 ).toString() ); |
| 261 | +console.log( y.get( y.length-1 ).toString() ); |
| 262 | +``` |
| 263 | + |
| 264 | +</section> |
| 265 | + |
| 266 | +<!-- /.examples --> |
| 267 | + |
| 268 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 269 | + |
| 270 | +<section class="related"> |
| 271 | + |
| 272 | +</section> |
| 273 | + |
| 274 | +<!-- /.related --> |
| 275 | + |
| 276 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 277 | + |
| 278 | +<section class="links"> |
| 279 | + |
| 280 | +[blas]: http://www.netlib.org/blas |
| 281 | + |
| 282 | +[zswap]: https://netlib.org/lapack/explore-html-3.6.1/d2/df9/group__complex16__blas__level1_ga13a187010a0cae1fef2820072404e857.html#ga13a187010a0cae1fef2820072404e857 |
| 283 | + |
| 284 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 285 | + |
| 286 | +[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128 |
| 287 | + |
| 288 | +</section> |
| 289 | + |
| 290 | +<!-- /.links --> |
0 commit comments