|
| 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 | +# slaswp |
| 22 | + |
| 23 | +> Perform a series of row interchanges on an input matrix. |
| 24 | +
|
| 25 | +<section class = "usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var slaswp = require( '@stdlib/lapack/base/slaswp' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### slaswp( N, A, LDA, k1, k2, IPIV, incx ) |
| 34 | + |
| 35 | +Performs a series of row interchanges on an input matrix `A` using pivot indices stored in `IPIV`. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 39 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 40 | + |
| 41 | +var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] |
| 42 | +var IPIV = new Int32Array( [ 2, 0, 1 ] ); |
| 43 | + |
| 44 | +slaswp( 'row-major', 2, A, 2, 0, 2, IPIV, 1 ); |
| 45 | +// A => <Float32Array>[ 3.0, 4.0, 1.0, 2.0, 5.0, 6.0 ] |
| 46 | +``` |
| 47 | + |
| 48 | +The function has the following parameters: |
| 49 | + |
| 50 | +- **order**: storage layout. |
| 51 | +- **N**: number of columns in `A`. |
| 52 | +- **A**: input matrix stored in linear memory as a [`Float32Array`][mdn-Float32Array]. |
| 53 | +- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`). |
| 54 | +- **k1**: index of first row to interchange when `incx` is positive and the index of the last row to interchange when `incx` is negative. |
| 55 | +- **k2**: index of last row to interchange when `incx` is positive and the index of the first row to interchange when `incx` is negative. |
| 56 | +- **IPIV**: vector of pivot indices as an [`Int32Array`][mdn-int32array]. Must contain at least `k1+(k2-k1)*abs(incx)` elements. Only the elements in positions `k1` through `k1+(k2-k1)*abs(incx)` are accessed. |
| 57 | +- **incx**: increment between successive values of `IPIV`. Elements from `IPIV` are accessed according to `IPIV[k1+(k-k1)*abs(incx)] = j`, thus implying that rows `k` and `j` should be interchanged. If `incx` is negative, the pivots are applied in reverse order. |
| 58 | + |
| 59 | +The sign of the increment parameter `incx` determines the order in which pivots are applied. For example, to apply pivots in reverse order, |
| 60 | + |
| 61 | +```javascript |
| 62 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 63 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 64 | + |
| 65 | +var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] |
| 66 | +var IPIV = new Int32Array( [ 2, 0, 1 ] ); |
| 67 | + |
| 68 | +slaswp( 'row-major', 2, A, 2, 0, 2, IPIV, -1 ); |
| 69 | +// A => <Float32Array>[ 3.0, 4.0, 1.0, 2.0, 5.0, 6.0 ] |
| 70 | +``` |
| 71 | + |
| 72 | +To perform strided access over `IPIV`, provide an `abs(incx)` value greater than one. For example, to access every other element in `IPIV`, |
| 73 | + |
| 74 | +```javascript |
| 75 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 76 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 77 | + |
| 78 | +var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] |
| 79 | +var IPIV = new Int32Array( [ 2, 999, 0, 999, 1 ] ); |
| 80 | + |
| 81 | +slaswp( 'row-major', 2, A, 2, 0, 2, IPIV, 2 ); |
| 82 | +// A => <Float32Array>[ 3.0, 4.0, 1.0, 2.0, 5.0, 6.0 ] |
| 83 | +``` |
| 84 | + |
| 85 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 86 | + |
| 87 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 88 | + |
| 89 | +```javascript |
| 90 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 91 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 92 | + |
| 93 | +// Initial arrays... |
| 94 | +var A0 = new Float32Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 95 | +var IPIV0 = new Int32Array( [ 0, 2, 0, 1] ); |
| 96 | + |
| 97 | +// Create offset views... |
| 98 | +var A1 = new Float32Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 99 | +var IPIV1 = new Int32Array( IPIV0.buffer, IPIV0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 100 | + |
| 101 | +slaswp( 'row-major', 2, A1, 2, 0, 2, IPIV1, 1 ); |
| 102 | +// A0 => <Float32Array>[ 0.0, 3.0, 4.0, 1.0, 2.0, 5.0, 6.0 ] |
| 103 | +``` |
| 104 | + |
| 105 | +#### slaswp.ndarray( N, A, sa1, sa2, oa, k1, k2, inck, IPIV, si, oi ) |
| 106 | + |
| 107 | +Performs a series of row interchanges on the matrix `A` using pivot indices stored in `IPIV` and alternative indexing semantics. |
| 108 | + |
| 109 | +```javascript |
| 110 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 111 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 112 | + |
| 113 | +var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] |
| 114 | +var IPIV = new Int32Array( [ 2, 0, 1 ] ); |
| 115 | + |
| 116 | +slaswp.ndarray( 2, A, 2, 1, 0, 0, 2, 1, IPIV, 1, 0 ); |
| 117 | +// A => <Float32Array>[ 3.0, 4.0, 1.0, 2.0, 5.0, 6.0 ] |
| 118 | +``` |
| 119 | + |
| 120 | +The function has the following additional parameters: |
| 121 | + |
| 122 | +- **N**: number of columns in `A`. |
| 123 | +- **A**: input matrix stored in linear memory as a [`Float32Array`][mdn-float32array]. |
| 124 | +- **sa1**: stride of the first dimension of `A`. |
| 125 | +- **sa2**: stride of the second dimension of `A`. |
| 126 | +- **oa**: starting index for `A`. |
| 127 | +- **k1**: index of first row to interchange when `inck` is positive and the index of the last row to interchange when `inck` is negative. |
| 128 | +- **k2**: index of last row to interchange when `inck` is positive and the index of the first row to interchange when `inck` is negative. |
| 129 | +- **inck**: direction in which to apply pivots (-1 to apply pivots in reverse order; otherwise, apply in provided order). |
| 130 | +- **IPIV**: vector of pivot indices as an [`Int32Array`][mdn-int32array]. |
| 131 | +- **si**: index increment for `IPIV`. |
| 132 | +- **oi**: starting index for `IPIV`. |
| 133 | + |
| 134 | +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, |
| 135 | + |
| 136 | +<!-- eslint-disable max-len --> |
| 137 | + |
| 138 | +```javascript |
| 139 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 140 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 141 | + |
| 142 | +var A = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 143 | +var IPIV = new Int32Array( [ 0, 0, 2, 0, 1 ] ); |
| 144 | + |
| 145 | +slaswp.ndarray( 2, A, 2, 1, 4, 0, 2, 1, IPIV, 1, 2 ); |
| 146 | +// A => <Float32Array>[ 0.0, 0.0, 0.0, 0.0, 3.0, 4.0, 1.0, 2.0, 5.0, 6.0 ] |
| 147 | +``` |
| 148 | + |
| 149 | +</section> |
| 150 | + |
| 151 | +<!-- /.usage --> |
| 152 | + |
| 153 | +<section class="notes"> |
| 154 | + |
| 155 | +## Notes |
| 156 | + |
| 157 | +- Both functions access `k2-k1+1` elements from `IPIV`. |
| 158 | +- While `slaswp` conflates the order in which pivots are applied with the order in which elements in `IPIV` are accessed, the `ndarray` method delineates control of those behaviors with separate parameters `inck` and `si`. |
| 159 | +- `slaswp()` corresponds to the [LAPACK][LAPACK] level 1 function [`slaswp`][lapack-slaswp]. |
| 160 | + |
| 161 | +</section> |
| 162 | + |
| 163 | +<!-- /.notes --> |
| 164 | + |
| 165 | +<section class="examples"> |
| 166 | + |
| 167 | +## Examples |
| 168 | + |
| 169 | +<!-- eslint no-undef: "error" --> |
| 170 | + |
| 171 | +```javascript |
| 172 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 173 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 174 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 175 | +var slaswp = require( '@stdlib/lapack/base/slaswp' ); |
| 176 | + |
| 177 | +// Specify matrix meta data: |
| 178 | +var shape = [ 4, 2 ]; |
| 179 | +var strides = [ 1, 4 ]; |
| 180 | +var offset = 0; |
| 181 | +var order = 'column-major'; |
| 182 | + |
| 183 | +// Create a matrix stored in linear memory: |
| 184 | +var A = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); |
| 185 | +console.log( ndarray2array( A, shape, strides, offset, order ) ); |
| 186 | + |
| 187 | +// Define a vector of pivot indices: |
| 188 | +var IPIV = new Int32Array( [ 2, 0, 3, 1 ] ); |
| 189 | + |
| 190 | +// Interchange rows: |
| 191 | +slaswp( order, shape[ 1 ], A, strides[ 1 ], 0, shape[ 0 ]-1, IPIV, 1 ); |
| 192 | +console.log( ndarray2array( A, shape, strides, offset, order ) ); |
| 193 | +``` |
| 194 | + |
| 195 | +</section> |
| 196 | + |
| 197 | +<!-- /.examples --> |
| 198 | + |
| 199 | +<!-- C interface documentation. --> |
| 200 | + |
| 201 | +* * * |
| 202 | + |
| 203 | +<section class="c"> |
| 204 | + |
| 205 | +## C APIs |
| 206 | + |
| 207 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 208 | + |
| 209 | +<section class="intro"> |
| 210 | + |
| 211 | +</section> |
| 212 | + |
| 213 | +<!-- /.intro --> |
| 214 | + |
| 215 | +<!-- C usage documentation. --> |
| 216 | + |
| 217 | +<section class="usage"> |
| 218 | + |
| 219 | +### Usage |
| 220 | + |
| 221 | +```c |
| 222 | +TODO |
| 223 | +``` |
| 224 | + |
| 225 | +#### TODO |
| 226 | + |
| 227 | +TODO. |
| 228 | + |
| 229 | +```c |
| 230 | +TODO |
| 231 | +``` |
| 232 | + |
| 233 | +TODO |
| 234 | + |
| 235 | +```c |
| 236 | +TODO |
| 237 | +``` |
| 238 | + |
| 239 | +</section> |
| 240 | + |
| 241 | +<!-- /.usage --> |
| 242 | + |
| 243 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 244 | + |
| 245 | +<section class="notes"> |
| 246 | + |
| 247 | +</section> |
| 248 | + |
| 249 | +<!-- /.notes --> |
| 250 | + |
| 251 | +<!-- C API usage examples. --> |
| 252 | + |
| 253 | +<section class="examples"> |
| 254 | + |
| 255 | +### Examples |
| 256 | + |
| 257 | +```c |
| 258 | +TODO |
| 259 | +``` |
| 260 | + |
| 261 | +</section> |
| 262 | + |
| 263 | +<!-- /.examples --> |
| 264 | + |
| 265 | +</section> |
| 266 | + |
| 267 | +<!-- /.c --> |
| 268 | + |
| 269 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 270 | + |
| 271 | +<section class="related"> |
| 272 | + |
| 273 | +</section> |
| 274 | + |
| 275 | +<!-- /.related --> |
| 276 | + |
| 277 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 278 | + |
| 279 | +<section class="links"> |
| 280 | + |
| 281 | +[lapack]: https://www.netlib.org/lapack/explore-html/ |
| 282 | + |
| 283 | +[lapack-slaswp]: https://www.netlib.org/lapack/explore-html/d1/d7e/group__laswp_ga0c231ab9280d3cf32c10855ae741fc00.html#ga0c231ab9280d3cf32c10855ae741fc00 |
| 284 | + |
| 285 | +[mdn-float32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array |
| 286 | + |
| 287 | +[mdn-int32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array |
| 288 | + |
| 289 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 290 | + |
| 291 | +</section> |
| 292 | + |
| 293 | +<!-- /.links --> |
0 commit comments