|
| 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 | +# factory |
| 22 | + |
| 23 | +> Return a function which interchange two vectors. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var factory = require( '@stdlib/blas/tools/swap-factory' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### factory( base, dtype ) |
| 40 | + |
| 41 | +Returns a function which interchanges two vectors. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; |
| 45 | + |
| 46 | +var swap = factory( dswap, 'float64' ); |
| 47 | +``` |
| 48 | + |
| 49 | +The function has the following parameters: |
| 50 | + |
| 51 | +- **base**: "base" function which interchanges two vectors. Must have an `ndarray` function signature (i.e., must support index offsets). |
| 52 | +- **dtype**: array data type. The function assumes that the data type of all provided arrays is the same. |
| 53 | + |
| 54 | +#### swap( x, y\[, dim] ) |
| 55 | + |
| 56 | +Interchanges two vectors. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 60 | +var array = require( '@stdlib/ndarray/array' ); |
| 61 | +var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; |
| 62 | + |
| 63 | +var swap = factory( dswap, 'float64' ); |
| 64 | + |
| 65 | +var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); |
| 66 | +var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); |
| 67 | + |
| 68 | +swap( x, y ); |
| 69 | + |
| 70 | +var xbuf = x.data; |
| 71 | +// returns <Float64Array>[ 2.0, 6.0, -1.0, -4.0, 8.0 ] |
| 72 | + |
| 73 | +var ybuf = y.data; |
| 74 | +// returns <Float64Array>[ 4.0, 2.0, -3.0, 5.0, -1.0 ] |
| 75 | +``` |
| 76 | + |
| 77 | +The returned function has the following parameters: |
| 78 | + |
| 79 | +- **x**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor]. Must have the same shape as `y`. |
| 80 | +- **y**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor]. Must have the same shape as `x`. |
| 81 | +- **dim**: dimension along which to interchange vectors. Must be a negative integer. Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. Default: `-1`. |
| 82 | + |
| 83 | +For multi-dimensional input [`ndarrays`][@stdlib/ndarray/ctor], the function performs batched computation, such that the function interchanges each pair of vectors in `x` and `y` according to the specified dimension index. |
| 84 | + |
| 85 | +```javascript |
| 86 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 87 | +var array = require( '@stdlib/ndarray/array' ); |
| 88 | +var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; |
| 89 | + |
| 90 | +var swap = factory( dswap, 'float64' ); |
| 91 | + |
| 92 | +var opts = { |
| 93 | + 'shape': [ 2, 3 ] |
| 94 | +}; |
| 95 | +var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 3.0 ] ), opts ); |
| 96 | +var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 2.0 ] ), opts ); |
| 97 | + |
| 98 | +var v1 = x.get( 0, 0 ); |
| 99 | +// returns 4.0 |
| 100 | + |
| 101 | +var v2 = y.get( 0, 0 ); |
| 102 | +// returns 2.0 |
| 103 | + |
| 104 | +swap( x, y ); |
| 105 | + |
| 106 | +v1 = x.get( 0, 0 ); |
| 107 | +// returns 2.0 |
| 108 | + |
| 109 | +v2 = y.get( 0, 0 ); |
| 110 | +// returns 4.0 |
| 111 | +``` |
| 112 | + |
| 113 | +</section> |
| 114 | + |
| 115 | +<!-- /.usage --> |
| 116 | + |
| 117 | +<section class="notes"> |
| 118 | + |
| 119 | +## Notes |
| 120 | + |
| 121 | +For the returned function, |
| 122 | + |
| 123 | +- Both input [`ndarrays`][@stdlib/ndarray/ctor] must have the same shape. |
| 124 | +- Negative indices are resolved relative to the last [`ndarray`][@stdlib/ndarray/ctor] dimension, with the last dimension corresponding to `-1`. |
| 125 | +- For multi-dimensional [`ndarrays`][@stdlib/ndarray/ctor], batched computation effectively means swapping all of `x` with all of `y`; however, the choice of `dim` will significantly affect performance. For best performance, specify a `dim` which best aligns with the [memory layout][@stdlib/ndarray/orders] of provided [`ndarrays`][@stdlib/ndarray/ctor]. |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.notes --> |
| 130 | + |
| 131 | +<section class="examples"> |
| 132 | + |
| 133 | +## Examples |
| 134 | + |
| 135 | +<!-- eslint no-undef: "error" --> |
| 136 | + |
| 137 | +```javascript |
| 138 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 139 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 140 | +var array = require( '@stdlib/ndarray/array' ); |
| 141 | +var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; |
| 142 | +var factory = require( '@stdlib/blas/tools/swap-factory' ); |
| 143 | + |
| 144 | +var swap = factory( dswap, 'float64' ); |
| 145 | + |
| 146 | +var opts = { |
| 147 | + 'dtype': 'float64' |
| 148 | +}; |
| 149 | + |
| 150 | +var x = array( discreteUniform( 10, 0, 100, opts ), { |
| 151 | + 'shape': [ 5, 2 ] |
| 152 | +}); |
| 153 | +console.log( ndarray2array( x ) ); |
| 154 | + |
| 155 | +var y = array( discreteUniform( 10, 0, 10, opts ), { |
| 156 | + 'shape': x.shape |
| 157 | +}); |
| 158 | +console.log( ndarray2array( y ) ); |
| 159 | + |
| 160 | +swap( x, y ); |
| 161 | +console.log( ndarray2array( x ) ); |
| 162 | +console.log( ndarray2array( y ) ); |
| 163 | +``` |
| 164 | + |
| 165 | +</section> |
| 166 | + |
| 167 | +<!-- /.examples --> |
| 168 | + |
| 169 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 170 | + |
| 171 | +<section class="related"> |
| 172 | + |
| 173 | +</section> |
| 174 | + |
| 175 | +<!-- /.related --> |
| 176 | + |
| 177 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 178 | + |
| 179 | +<section class="links"> |
| 180 | + |
| 181 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 182 | + |
| 183 | +[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders |
| 184 | + |
| 185 | +</section> |
| 186 | + |
| 187 | +<!-- /.links --> |
0 commit comments