Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 66x 66x 66x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 20x 20x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 8x 8x 8x 4x 2x 2x 4x 8x 8x 8x 8x 8x 8x 8x 8x 12x 12x 12x 12x 12x 12x 12x 12x 20x 2x 2x 2x 2x 2x 12x 12x 20x 4x 2x 2x 4x 12x 12x 12x 12x 12x 12x 12x 12x 66x 66x 66x 66x 66x 86x 8x 8x 86x 58x 58x 58x 86x 32x 32x 32x 32x 32x 32x 128x 128x 64x 64x 64x 128x 32x 32x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 32x 2x 2x 2x 32x 18x 18x 32x 12x 12x 12x 30x 30x 32x 6x 2x 2x 6x 30x 30x 32x 10x 10x 32x 20x 20x 20x 30x 30x 30x 32x 14x 2x 2x 2x 2x 2x 2x 14x 30x 30x 30x 30x 34x 34x 34x 34x 34x 34x 34x 34x 34x 86x 136x 136x 58x 58x 58x 136x 34x 86x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 86x 16x 16x 14x 14x 14x 16x 2x 2x 2x 2x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 32x 32x 32x 86x 2x 2x 2x 2x 86x 14x 14x 14x 14x 56x 18x 18x 18x 18x 18x 32x 32x 32x 86x 8x 2x 2x 2x 2x 2x 2x 8x 32x 32x 32x 32x 32x 86x 14x 14x 14x 14x 46x 18x 18x 18x 18x 18x 32x 32x 32x 86x 28x 2x 2x 2x 2x 2x 2x 2x 2x 28x 32x 32x 32x 86x 3x 3x 3x 3x 3x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
/* eslint-disable max-statements */
// MODULES //
var Float64Array = require( '@stdlib/array/float64' );
var dladiv = require( '@stdlib/lapack/base/dladiv' ).ndarray;
var dlamch = require( '@stdlib/lapack/base/dlamch' );
var max = require( '@stdlib/math/base/special/max' );
var abs = require( '@stdlib/math/base/special/abs' );
// VARIABLES //
var SMLNUM = 2.0 * dlamch( 'S' );
var BIGNUM = 1.0 / SMLNUM;
// Scratch arrays holding the real and imaginary parts of the (at most) 2x2 matrix `C = ca*A - w*D` in column-major order (i.e., `[ C11, C21, C12, C22 ]`):
var CRV = new Float64Array( 4 );
var CIV = new Float64Array( 4 );
// Scratch array for the result of complex division:
var TMP = new Float64Array( 2 );
// Complete pivoting lookup tables (indexed by the position of the largest element in `C`):
var IPIVOT = [
[ 0, 1, 2, 3 ],
[ 1, 0, 3, 2 ],
[ 2, 3, 0, 1 ],
[ 3, 2, 1, 0 ]
];
var RSWAP = [ false, true, false, true ];
var ZSWAP = [ false, false, true, true ];
// FUNCTIONS //
/**
* Tests whether a provided string indicates to transpose a matrix.
*
* @private
* @param {string} str - input string
* @returns {boolean} boolean indicating whether to transpose a matrix
*/
function isTransposed( str ) {
return ( str !== 'no-transpose' );
}
// MAIN //
/**
* Solves a system of the form `(ca*A - w*D) X = s*B` or `(ca*A^T - w*D) X = s*B` with possible scaling and perturbation of `A`, where `A` is an `NA` by `NA` real matrix (`NA` is 1 or 2), `ca` is a real scalar, `D` is an `NA` by `NA` real diagonal matrix, and `w` is a real or complex scalar.
*
* ## Notes
*
* - If `w` is complex (`NW = 2`), `X` and `B` are `NA` by `2` matrices whose first column contains the real part and whose second column contains the imaginary part.
* - `s` is a scaling factor (`<= 1`) chosen so that `X` can be computed without overflow. `X` is further scaled if necessary to assure that `norm(ca*A - w*D) * norm(X)` is less than overflow.
* - The function writes the scaling factor `s` and the infinity-norm of `X` to the first and second elements of `out`, respectively.
* - The function returns `1` if `ca*A - w*D` had to be perturbed to make its smallest (or only) singular value greater than `smin` and `0` otherwise.
* - In the interests of speed, the function does not validate input arguments.
*
* @private
* @param {string} trans - specifies whether `A` should be transposed
* @param {PositiveInteger} NA - size of the matrix `A` (either `1` or `2`)
* @param {PositiveInteger} NW - `1` if `w` is real and `2` if `w` is complex
* @param {number} smin - desired lower bound on the singular values of `A`
* @param {number} ca - coefficient by which `A` is multiplied
* @param {Float64Array} A - input matrix
* @param {integer} strideA1 - stride of the first dimension of `A`
* @param {integer} strideA2 - stride of the second dimension of `A`
* @param {NonNegativeInteger} offsetA - starting index for `A`
* @param {number} d1 - first diagonal element of `D`
* @param {number} d2 - second diagonal element of `D` (not used if `NA = 1`)
* @param {Float64Array} B - right-hand side matrix
* @param {integer} strideB1 - stride of the first dimension of `B`
* @param {integer} strideB2 - stride of the second dimension of `B`
* @param {NonNegativeInteger} offsetB - starting index for `B`
* @param {number} wr - real part of `w`
* @param {number} wi - imaginary part of `w` (not used if `NW = 1`)
* @param {Float64Array} X - output matrix
* @param {integer} strideX1 - stride of the first dimension of `X`
* @param {integer} strideX2 - stride of the second dimension of `X`
* @param {NonNegativeInteger} offsetX - starting index for `X`
* @param {Float64Array} out - output array containing the scaling factor and the infinity-norm of `X`
* @param {integer} strideOut - stride length for `out`
* @param {NonNegativeInteger} offsetOut - starting index for `out`
* @returns {integer} status code
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( [ 5.0, 1.0, 1.0, 2.0 ] );
* var B = new Float64Array( [ 1.0, 2.0 ] );
* var X = new Float64Array( 2 );
* var out = new Float64Array( 2 );
*
* var info = dlaln2( 'no-transpose', 2, 1, 1.0e-3, 1.0, A, 1, 2, 0, 1.0, 1.0, B, 1, 2, 0, 0.5, 0.0, X, 1, 2, 0, out, 1, 0 );
* // returns 0
*
* // X => <Float64Array>[ ~-0.087, ~1.391 ]
* // out => <Float64Array>[ 1.0, ~1.391 ]
*/
function dlaln2( trans, NA, NW, smin, ca, A, strideA1, strideA2, offsetA, d1, d2, B, strideB1, strideB2, offsetB, wr, wi, X, strideX1, strideX2, offsetX, out, strideOut, offsetOut ) { // eslint-disable-line max-len, max-params
var u22abs;
var icmax;
var smini;
var bnorm;
var cnorm;
var xnorm;
var scale;
var ur11r;
var ui11r;
var ur12s;
var ui12s;
var bbnd;
var cmax;
var temp;
var ur11;
var ui11;
var ur12;
var ui12;
var ur22;
var ui22;
var cr21;
var ci21;
var cr22;
var ci22;
var lr21;
var li21;
var info;
var csr;
var csi;
var br1;
var br2;
var bi1;
var bi2;
var xr1;
var xr2;
var xi1;
var xi2;
var ib1;
var ib2;
var ib3;
var ib4;
var ix1;
var ix2;
var ix3;
var ix4;
var piv;
var v;
var j;
smini = max( smin, SMLNUM );
info = 0;
scale = 1.0;
// Indices of `B(1,1)`, `B(2,1)`, `B(1,2)`, and `B(2,2)`:
ib1 = offsetB;
ib2 = ib1 + strideB1;
ib3 = ib1 + strideB2;
ib4 = ib3 + strideB1;
// Indices of `X(1,1)`, `X(2,1)`, `X(1,2)`, and `X(2,2)`:
ix1 = offsetX;
ix2 = ix1 + strideX1;
ix3 = ix1 + strideX2;
ix4 = ix3 + strideX1;
if ( NA === 1 ) {
// 1x1 (i.e., scalar) system: C X = B
if ( NW === 1 ) {
// Real 1x1 system...
// C = ca A - w D
csr = ( ca*A[ offsetA ] ) - ( wr*d1 );
cnorm = abs( csr );
// If |C| < SMINI, use C = SMINI...
if ( cnorm < smini ) {
csr = smini;
cnorm = smini;
info = 1;
}
// Check scaling for X = B / C...
bnorm = abs( B[ ib1 ] );
if ( cnorm < 1.0 && bnorm > 1.0 ) {
if ( bnorm > BIGNUM*cnorm ) {
scale = 1.0 / bnorm;
}
}
// Compute X...
X[ ix1 ] = ( B[ ib1 ]*scale ) / csr;
xnorm = abs( X[ ix1 ] );
out[ offsetOut ] = scale;
out[ offsetOut+strideOut ] = xnorm;
return info;
}
// Complex 1x1 system (w is complex)...
// C = ca A - w D
csr = ( ca*A[ offsetA ] ) - ( wr*d1 );
csi = -wi * d1;
cnorm = abs( csr ) + abs( csi );
// If |C| < SMINI, use C = SMINI...
if ( cnorm < smini ) {
csr = smini;
csi = 0.0;
cnorm = smini;
info = 1;
}
// Check scaling for X = B / C...
bnorm = abs( B[ ib1 ] ) + abs( B[ ib3 ] );
if ( cnorm < 1.0 && bnorm > 1.0 ) {
if ( bnorm > BIGNUM*cnorm ) {
scale = 1.0 / bnorm;
}
}
// Compute X...
dladiv( scale*B[ ib1 ], scale*B[ ib3 ], csr, csi, X, ix1, X, ix3 );
xnorm = abs( X[ ix1 ] ) + abs( X[ ix3 ] );
out[ offsetOut ] = scale;
out[ offsetOut+strideOut ] = xnorm;
return info;
}
// 2x2 system...
// Compute the real part of C = ca A - w D (or ca A^T - w D):
CRV[ 0 ] = ( ca*A[ offsetA ] ) - ( wr*d1 );
CRV[ 3 ] = ( ca*A[ offsetA+strideA1+strideA2 ] ) - ( wr*d2 );
if ( isTransposed( trans ) ) {
CRV[ 2 ] = ca * A[ offsetA+strideA1 ];
CRV[ 1 ] = ca * A[ offsetA+strideA2 ];
} else {
CRV[ 1 ] = ca * A[ offsetA+strideA1 ];
CRV[ 2 ] = ca * A[ offsetA+strideA2 ];
}
if ( NW === 1 ) {
// Real 2x2 system (w is real)...
// Find the largest element in C...
cmax = 0.0;
icmax = -1;
for ( j = 0; j < 4; j++ ) {
v = abs( CRV[ j ] );
if ( v > cmax ) {
cmax = v;
icmax = j;
}
}
// If norm(C) < SMINI, use SMINI*identity...
if ( cmax < smini ) {
bnorm = max( abs( B[ ib1 ] ), abs( B[ ib2 ] ) );
if ( smini < 1.0 && bnorm > 1.0 ) {
if ( bnorm > BIGNUM*smini ) {
scale = 1.0 / bnorm;
}
}
temp = scale / smini;
X[ ix1 ] = temp * B[ ib1 ];
X[ ix2 ] = temp * B[ ib2 ];
xnorm = temp * bnorm;
out[ offsetOut ] = scale;
out[ offsetOut+strideOut ] = xnorm;
return 1;
}
// Gaussian elimination with complete pivoting...
piv = IPIVOT[ icmax ];
ur11 = CRV[ icmax ];
cr21 = CRV[ piv[ 1 ] ];
ur12 = CRV[ piv[ 2 ] ];
cr22 = CRV[ piv[ 3 ] ];
ur11r = 1.0 / ur11;
lr21 = ur11r * cr21;
ur22 = cr22 - ( ur12*lr21 );
// If smaller pivot < SMINI, use SMINI...
if ( abs( ur22 ) < smini ) {
ur22 = smini;
info = 1;
}
if ( RSWAP[ icmax ] ) {
br1 = B[ ib2 ];
br2 = B[ ib1 ];
} else {
br1 = B[ ib1 ];
br2 = B[ ib2 ];
}
br2 -= lr21 * br1;
bbnd = max( abs( br1*( ur22*ur11r ) ), abs( br2 ) );
if ( bbnd > 1.0 && abs( ur22 ) < 1.0 ) {
if ( bbnd >= BIGNUM*abs( ur22 ) ) {
scale = 1.0 / bbnd;
}
}
xr2 = ( br2*scale ) / ur22;
xr1 = ( ( scale*br1 )*ur11r ) - ( xr2*( ur11r*ur12 ) );
if ( ZSWAP[ icmax ] ) {
X[ ix1 ] = xr2;
X[ ix2 ] = xr1;
} else {
X[ ix1 ] = xr1;
X[ ix2 ] = xr2;
}
xnorm = max( abs( xr1 ), abs( xr2 ) );
// Further scaling if norm(A) norm(X) > overflow...
if ( xnorm > 1.0 && cmax > 1.0 ) {
if ( xnorm > BIGNUM/cmax ) {
temp = cmax / BIGNUM;
X[ ix1 ] *= temp;
X[ ix2 ] *= temp;
xnorm *= temp;
scale *= temp;
}
}
out[ offsetOut ] = scale;
out[ offsetOut+strideOut ] = xnorm;
return info;
}
// Complex 2x2 system (w is complex)...
// Find the largest element in C...
CIV[ 0 ] = -wi * d1;
CIV[ 1 ] = 0.0;
CIV[ 2 ] = 0.0;
CIV[ 3 ] = -wi * d2;
cmax = 0.0;
icmax = -1;
for ( j = 0; j < 4; j++ ) {
v = abs( CRV[ j ] ) + abs( CIV[ j ] );
if ( v > cmax ) {
cmax = v;
icmax = j;
}
}
// If norm(C) < SMINI, use SMINI*identity...
if ( cmax < smini ) {
bnorm = max( abs( B[ ib1 ] )+abs( B[ ib3 ] ), abs( B[ ib2 ] )+abs( B[ ib4 ] ) ); // eslint-disable-line max-len
if ( smini < 1.0 && bnorm > 1.0 ) {
if ( bnorm > BIGNUM*smini ) {
scale = 1.0 / bnorm;
}
}
temp = scale / smini;
X[ ix1 ] = temp * B[ ib1 ];
X[ ix2 ] = temp * B[ ib2 ];
X[ ix3 ] = temp * B[ ib3 ];
X[ ix4 ] = temp * B[ ib4 ];
xnorm = temp * bnorm;
out[ offsetOut ] = scale;
out[ offsetOut+strideOut ] = xnorm;
return 1;
}
// Gaussian elimination with complete pivoting...
piv = IPIVOT[ icmax ];
ur11 = CRV[ icmax ];
ui11 = CIV[ icmax ];
cr21 = CRV[ piv[ 1 ] ];
ci21 = CIV[ piv[ 1 ] ];
ur12 = CRV[ piv[ 2 ] ];
ui12 = CIV[ piv[ 2 ] ];
cr22 = CRV[ piv[ 3 ] ];
ci22 = CIV[ piv[ 3 ] ];
if ( icmax === 0 || icmax === 3 ) {
// Code when off-diagonals of pivoted C are real...
if ( abs( ur11 ) > abs( ui11 ) ) {
temp = ui11 / ur11;
ur11r = 1.0 / ( ur11*( 1.0+( temp*temp ) ) );
ui11r = -temp * ur11r;
} else {
temp = ur11 / ui11;
ui11r = -1.0 / ( ui11*( 1.0+( temp*temp ) ) );
ur11r = -temp * ui11r;
}
lr21 = cr21 * ur11r;
li21 = cr21 * ui11r;
ur12s = ur12 * ur11r;
ui12s = ur12 * ui11r;
ur22 = cr22 - ( ur12*lr21 );
ui22 = ci22 - ( ur12*li21 );
} else {
// Code when diagonals of pivoted C are real...
ur11r = 1.0 / ur11;
ui11r = 0.0;
lr21 = cr21 * ur11r;
li21 = ci21 * ur11r;
ur12s = ur12 * ur11r;
ui12s = ui12 * ur11r;
ur22 = cr22 - ( ur12*lr21 ) + ( ui12*li21 );
ui22 = -( ur12*li21 ) - ( ui12*lr21 );
}
u22abs = abs( ur22 ) + abs( ui22 );
// If smaller pivot < SMINI, use SMINI...
if ( u22abs < smini ) {
ur22 = smini;
ui22 = 0.0;
info = 1;
}
if ( RSWAP[ icmax ] ) {
br2 = B[ ib1 ];
br1 = B[ ib2 ];
bi2 = B[ ib3 ];
bi1 = B[ ib4 ];
} else {
br1 = B[ ib1 ];
br2 = B[ ib2 ];
bi1 = B[ ib3 ];
bi2 = B[ ib4 ];
}
br2 = br2 - ( lr21*br1 ) + ( li21*bi1 );
bi2 = bi2 - ( li21*br1 ) - ( lr21*bi1 );
bbnd = max( ( abs( br1 )+abs( bi1 ) ) * ( u22abs*( abs( ur11r )+abs( ui11r ) ) ), abs( br2 )+abs( bi2 ) ); // eslint-disable-line max-len
if ( bbnd > 1.0 && u22abs < 1.0 ) {
if ( bbnd >= BIGNUM*u22abs ) {
scale = 1.0 / bbnd;
br1 *= scale;
bi1 *= scale;
br2 *= scale;
bi2 *= scale;
}
}
dladiv( br2, bi2, ur22, ui22, TMP, 0, TMP, 1 );
xr2 = TMP[ 0 ];
xi2 = TMP[ 1 ];
xr1 = ( ur11r*br1 ) - ( ui11r*bi1 ) - ( ur12s*xr2 ) + ( ui12s*xi2 );
xi1 = ( ui11r*br1 ) + ( ur11r*bi1 ) - ( ui12s*xr2 ) - ( ur12s*xi2 );
if ( ZSWAP[ icmax ] ) {
X[ ix1 ] = xr2;
X[ ix2 ] = xr1;
X[ ix3 ] = xi2;
X[ ix4 ] = xi1;
} else {
X[ ix1 ] = xr1;
X[ ix2 ] = xr2;
X[ ix3 ] = xi1;
X[ ix4 ] = xi2;
}
xnorm = max( abs( xr1 )+abs( xi1 ), abs( xr2 )+abs( xi2 ) );
// Further scaling if norm(A) norm(X) > overflow...
if ( xnorm > 1.0 && cmax > 1.0 ) {
if ( xnorm > BIGNUM/cmax ) {
temp = cmax / BIGNUM;
X[ ix1 ] *= temp;
X[ ix2 ] *= temp;
X[ ix3 ] *= temp;
X[ ix4 ] *= temp;
xnorm *= temp;
scale *= temp;
}
}
out[ offsetOut ] = scale;
out[ offsetOut+strideOut ] = xnorm;
return info;
}
// EXPORTS //
module.exports = dlaln2;
|