|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2020 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 | +# dswap |
| 22 | + |
| 23 | +> Interchange two vectors. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var dswap = require( '@stdlib/blas/base/dswap' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### dswap( N, x, strideX, y, strideY ) |
| 34 | + |
| 35 | +Interchanges vectors `x` and `y`. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 39 | + |
| 40 | +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 41 | +var y = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); |
| 42 | + |
| 43 | +dswap( x.length, x, 1, y, 1 ); |
| 44 | +// x => <Float64Array>[ 6.0, 7.0, 8.0, 9.0, 10.0 ] |
| 45 | +// y => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ] |
| 46 | +``` |
| 47 | + |
| 48 | +The function accepts the following parameters: |
| 49 | + |
| 50 | +- **N**: number of values to swap. |
| 51 | +- **x**: first input [`Float64Array`][mdn-float64array]. |
| 52 | +- **strideX**: index increment for `x`. |
| 53 | +- **y**: second input [`Float64Array`][mdn-float64array]. |
| 54 | +- **strideY**: index increment for `y`. |
| 55 | + |
| 56 | +The `N` and `stride` parameters determine how values from `x` are accessed at runtime. For example, to swap in reverse order every other value in `x` with the first `N` elements of `y`, |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 60 | +var floor = require( '@stdlib/math/base/special/floor' ); |
| 61 | + |
| 62 | +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 63 | +var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 64 | + |
| 65 | +var N = floor( x.length / 2 ); |
| 66 | + |
| 67 | +dswap( N, x, -2, y, 1 ); |
| 68 | +// x => <Float64Array>[ 1.0, 7.0, 3.0, 8.0, 5.0, 9.0 ] |
| 69 | +// y => <Float64Array>[ 5.0, 3.0, 1.0, 10.0, 11.0, 12.0 ] |
| 70 | +``` |
| 71 | + |
| 72 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 73 | + |
| 74 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 75 | + |
| 76 | +```javascript |
| 77 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 78 | +var floor = require( '@stdlib/math/base/special/floor' ); |
| 79 | + |
| 80 | +// Initial arrays... |
| 81 | +var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 82 | +var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 83 | + |
| 84 | +// Create offset views... |
| 85 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 86 | +var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element |
| 87 | + |
| 88 | +var N = floor( x0.length / 2 ); |
| 89 | + |
| 90 | +// Swap in reverse order every other value from `x1` with `y1`... |
| 91 | +dswap( N, x1, -2, y1, 1 ); |
| 92 | +// x0 => <Float64Array>[ 1.0, 12.0, 3.0, 11.0, 5.0, 10.0 ] |
| 93 | +// y0 => <Float64Array>[ 7.0, 8.0, 9.0, 6.0, 4.0, 2.0 ] |
| 94 | +``` |
| 95 | + |
| 96 | +#### dswap.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) |
| 97 | + |
| 98 | +Interchanges vectors `x` and `y`, with alternative indexing semantics. |
| 99 | + |
| 100 | +```javascript |
| 101 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 102 | + |
| 103 | +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 104 | +var y = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); |
| 105 | + |
| 106 | +dswap.ndarray( x.length, x, 1, 0, y, 1, 0 ); |
| 107 | +// x => <Float64Array>[ 6.0, 7.0, 8.0, 9.0, 10.0 ] |
| 108 | +// y => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ] |
| 109 | +``` |
| 110 | + |
| 111 | +The function accepts the following additional parameters: |
| 112 | + |
| 113 | +- **offsetX**: starting index for `x`. |
| 114 | +- **offsetY**: starting index for `y`. |
| 115 | + |
| 116 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offsetX` and `offsetY` parameters support indexing semantics based on starting indices. For example, to swap every other value in `x` starting from the second value with the last `N` elements in `y` where `x[i] = y[n]`, `x[i+2] = y[n-1]`,..., |
| 117 | + |
| 118 | +```javascript |
| 119 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 120 | +var floor = require( '@stdlib/math/base/special/floor' ); |
| 121 | + |
| 122 | +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 123 | +var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 124 | + |
| 125 | +var N = floor( x.length / 2 ); |
| 126 | + |
| 127 | +dswap.ndarray( N, x, 2, 1, y, -1, y.length-1 ); |
| 128 | +// x => <Float64Array>[ 1.0, 12.0, 11.0, 10.0, 5.0, 6.0 ] |
| 129 | +// y => <Float64Array>[ 7.0, 8.0, 9.0, 6.0, 4.0, 2.0 ] |
| 130 | +``` |
| 131 | + |
| 132 | +</section> |
| 133 | + |
| 134 | +<!-- /.usage --> |
| 135 | + |
| 136 | +<section class="notes"> |
| 137 | + |
| 138 | +## Notes |
| 139 | + |
| 140 | +- If `N <= 0`, both functions leave `x` and `y` unchanged. |
| 141 | +- `dswap()` corresponds to the [BLAS][blas] level 1 function [`dswap`][dswap]. |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.notes --> |
| 146 | + |
| 147 | +<section class="examples"> |
| 148 | + |
| 149 | +## Examples |
| 150 | + |
| 151 | +<!-- eslint no-undef: "error" --> |
| 152 | + |
| 153 | +```javascript |
| 154 | +var randu = require( '@stdlib/random/base/randu' ); |
| 155 | +var round = require( '@stdlib/math/base/special/round' ); |
| 156 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 157 | +var dswap = require( '@stdlib/blas/base/dswap' ); |
| 158 | + |
| 159 | +var x; |
| 160 | +var y; |
| 161 | +var i; |
| 162 | + |
| 163 | +x = new Float64Array( 10 ); |
| 164 | +y = new Float64Array( 10 ); |
| 165 | +for ( i = 0; i < x.length; i++ ) { |
| 166 | + x[ i ] = round( randu()*500.0 ); |
| 167 | + y[ i ] = round( randu()*255.0 ); |
| 168 | +} |
| 169 | +console.log( x ); |
| 170 | +console.log( y ); |
| 171 | + |
| 172 | +// Swap elements in `x` and `y` starting from the end of `y`: |
| 173 | +dswap( x.length, x, 1, y, -1 ); |
| 174 | +console.log( x ); |
| 175 | +console.log( y ); |
| 176 | +``` |
| 177 | + |
| 178 | +</section> |
| 179 | + |
| 180 | +<!-- /.examples --> |
| 181 | + |
| 182 | +<section class="links"> |
| 183 | + |
| 184 | +[blas]: http://www.netlib.org/blas |
| 185 | + |
| 186 | +[dswap]: http://www.netlib.org/lapack/explore-html/de/da4/group__double__blas__level1.html |
| 187 | + |
| 188 | +[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array |
| 189 | + |
| 190 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 191 | + |
| 192 | +</section> |
| 193 | + |
| 194 | +<!-- /.links --> |
0 commit comments