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