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