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 | 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 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 729x 729x 17x 17x 17x 17x 17x 17x 17x 50x 50x 50x 50x 50x 50x 50x 50x 35x 35x 50x 15x 15x 50x 2x 2x 2x 2x 2x 50x 48x 31x 31x 48x 17x 17x 17x 17x 29x 25x 25x 25x 29x 29x 17x 17x 11x 11x 11x 17x 2x 2x 17x 4x 4x 15x 11x 11x 11x 11x 17x 48x 48x 11x 11x 48x 9x 9x 9x 9x 7x 7x 7x 7x 7x 9x 9x 9x 9x 5x 5x 5x 5x 9x 48x 48x 37x 37x 37x 37x 48x 9x 9x 9x 9x 9x 9x 9x 21x 21x 21x 38x 38x 21x 21x 21x 9x 48x 50x 50x 39x 50x 11x 11x 11x 50x 50x 50x 50x 17x 17x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 498x 498x 11x 11x 11x 11x 11x 11x 11x 11x 40x 40x 40x 40x 40x 40x 40x 40x 29x 29x 40x 11x 11x 40x 2x 2x 2x 2x 2x 40x 38x 26x 26x 38x 12x 12x 12x 12x 22x 18x 18x 18x 22x 22x 12x 12x 8x 8x 8x 12x 2x 2x 12x 4x 4x 10x 6x 6x 6x 6x 12x 38x 38x 6x 6x 38x 8x 8x 4x 4x 8x 8x 6x 6x 6x 6x 6x 8x 8x 8x 8x 4x 4x 4x 4x 8x 38x 38x 32x 32x 21x 21x 21x 21x 38x 6x 6x 6x 6x 6x 6x 6x 12x 12x 12x 20x 20x 12x 12x 12x 6x 38x 40x 40x 34x 40x 6x 6x 6x 40x 40x 40x 40x 11x 11x 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 30x 30x 2x 2x 28x 28x 30x 14x 30x 17x 17x 11x 30x 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.
*/
/* eslint-disable max-len, max-statements */
'use strict';
// MODULES //
var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' );
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' );
var idamax = require( '@stdlib/blas/base/idamax' ).ndarray;
var dscal = require( '@stdlib/blas/base/dscal' ).ndarray;
var dswap = require( '@stdlib/blas/base/dswap' ).ndarray;
var dspr = require( '@stdlib/blas/base/dspr' ).ndarray;
var abs = require( '@stdlib/math/base/special/abs' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var max = require( '@stdlib/math/base/special/max' );
// VARIABLES //
// Factor used to determine the pivot block size (see LAPACK reference DSPTRF):
var ALPHA = ( 1.0 + sqrt( 17.0 ) ) / 8.0;
// FUNCTIONS //
/**
* Factorizes a real symmetric matrix `A = U*D*U^T` using the upper triangle of `A` supplied in column-major packed form.
*
* @private
* @param {NonNegativeInteger} N - order of the matrix `A`
* @param {Float64Array} AP - packed form of a symmetric matrix `A`
* @param {integer} sa - stride length for `AP`
* @param {NonNegativeInteger} oa - starting index for `AP`
* @param {Int32Array} IPIV - vector of pivot indices
* @param {integer} si - stride length for `IPIV`
* @param {NonNegativeInteger} oi - starting index for `IPIV`
* @returns {integer} status code
*/
function factorU( N, AP, sa, oa, IPIV, si, oi ) {
var absakk;
var colmax;
var rowmax;
var kstep;
var info;
var imax;
var jmax;
var wkm1;
var d11;
var d12;
var d22;
var knc;
var kpc;
var r1;
var wk;
var kc;
var kk;
var kp;
var kx;
var i;
var j;
var k;
var t;
/**
* Resolves the physical index of the (one-based) packed element `idx`.
*
* @private
* @param {PositiveInteger} idx - one-based packed index
* @returns {integer} physical index into `AP`
*/
function pos( idx ) {
return oa + ( ( idx-1 ) * sa );
}
info = 0;
// `k` is the main loop index, decreasing from `N` to `1` in steps of `1` or `2`:
k = N;
kc = ( ( N-1 )*N / 2 ) + 1;
while ( k >= 1 ) {
knc = kc;
kstep = 1;
// Determine rows and columns to be interchanged and whether a 1-by-1 or 2-by-2 pivot block will be used...
absakk = abs( AP[ pos( kc+k-1 ) ] );
// `imax` is the row-index of the largest off-diagonal element in column `k`, and `colmax` is its absolute value...
if ( k > 1 ) {
imax = idamax( k-1, AP, sa, pos( kc ) ) + 1;
colmax = abs( AP[ pos( kc+imax-1 ) ] );
} else {
colmax = 0.0;
}
if ( max( absakk, colmax ) === 0.0 ) {
// Column `k` is zero: set `info` and continue...
if ( info === 0 ) {
info = k;
}
kp = k;
} else {
if ( absakk >= ALPHA*colmax ) {
// No interchange, use 1-by-1 pivot block...
kp = k;
} else {
rowmax = 0.0;
jmax = imax;
kx = ( imax*( imax+1 ) / 2 ) + imax;
for ( j = imax+1; j <= k; j++ ) {
if ( abs( AP[ pos( kx ) ] ) > rowmax ) {
rowmax = abs( AP[ pos( kx ) ] );
jmax = j;
}
kx += j;
}
kpc = ( ( imax-1 )*imax / 2 ) + 1;
if ( imax > 1 ) {
jmax = idamax( imax-1, AP, sa, pos( kpc ) ) + 1;
rowmax = max( rowmax, abs( AP[ pos( kpc+jmax-1 ) ] ) );
}
if ( absakk >= ALPHA*colmax*( colmax / rowmax ) ) {
// No interchange, use 1-by-1 pivot block...
kp = k;
} else if ( abs( AP[ pos( kpc+imax-1 ) ] ) >= ALPHA*rowmax ) {
// Interchange rows and columns `k` and `imax`, use 1-by-1 pivot block...
kp = imax;
} else {
// Interchange rows and columns `k-1` and `imax`, use 2-by-2 pivot block...
kp = imax;
kstep = 2;
}
}
kk = k - kstep + 1;
if ( kstep === 2 ) {
knc = knc - k + 1;
}
if ( kp !== kk ) {
// Interchange rows and columns `kk` and `kp` in the leading submatrix `A(1:k,1:k)`...
dswap( kp-1, AP, sa, pos( knc ), AP, sa, pos( kpc ) );
kx = kpc + kp - 1;
for ( j = kp+1; j <= kk-1; j++ ) {
kx = kx + j - 1;
t = AP[ pos( knc+j-1 ) ];
AP[ pos( knc+j-1 ) ] = AP[ pos( kx ) ];
AP[ pos( kx ) ] = t;
}
t = AP[ pos( knc+kk-1 ) ];
AP[ pos( knc+kk-1 ) ] = AP[ pos( kpc+kp-1 ) ];
AP[ pos( kpc+kp-1 ) ] = t;
if ( kstep === 2 ) {
t = AP[ pos( kc+k-2 ) ];
AP[ pos( kc+k-2 ) ] = AP[ pos( kc+kp-1 ) ];
AP[ pos( kc+kp-1 ) ] = t;
}
}
// Update the leading submatrix...
if ( kstep === 1 ) {
// 1-by-1 pivot block `D(k)`: column `k` now holds `W(k) = U(k)*D(k)`, where `U(k)` is the k-th column of `U`. Perform a rank-1 update of `A(1:k-1,1:k-1)` as `A := A - U(k)*D(k)*U(k)^T = A - W(k)*(1/D(k))*W(k)^T` and store `U(k)` in column `k`...
r1 = 1.0 / AP[ pos( kc+k-1 ) ];
dspr( 'column-major', 'upper', k-1, -r1, AP, sa, pos( kc ), AP, sa, oa );
dscal( k-1, r1, AP, sa, pos( kc ) );
} else if ( k > 2 ) {
// 2-by-2 pivot block `D(k)`. Perform a rank-2 update of `A(1:k-2,1:k-2)`...
d12 = AP[ pos( k-1 + ( ( k-1 )*k / 2 ) ) ];
d22 = AP[ pos( k-1 + ( ( k-2 )*( k-1 ) / 2 ) ) ] / d12;
d11 = AP[ pos( k + ( ( k-1 )*k / 2 ) ) ] / d12;
t = 1.0 / ( ( d11*d22 ) - 1.0 );
d12 = t / d12;
for ( j = k-2; j >= 1; j-- ) {
wkm1 = d12 * ( ( d11*AP[ pos( j + ( ( k-2 )*( k-1 ) / 2 ) ) ] ) - AP[ pos( j + ( ( k-1 )*k / 2 ) ) ] );
wk = d12 * ( ( d22*AP[ pos( j + ( ( k-1 )*k / 2 ) ) ] ) - AP[ pos( j + ( ( k-2 )*( k-1 ) / 2 ) ) ] );
for ( i = j; i >= 1; i-- ) {
AP[ pos( i + ( ( j-1 )*j / 2 ) ) ] = AP[ pos( i + ( ( j-1 )*j / 2 ) ) ] - ( AP[ pos( i + ( ( k-1 )*k / 2 ) ) ]*wk ) - ( AP[ pos( i + ( ( k-2 )*( k-1 ) / 2 ) ) ]*wkm1 );
}
AP[ pos( j + ( ( k-1 )*k / 2 ) ) ] = wk;
AP[ pos( j + ( ( k-2 )*( k-1 ) / 2 ) ) ] = wkm1;
}
}
}
// Store details of the interchanges in `IPIV`...
if ( kstep === 1 ) {
IPIV[ oi + ( ( k-1 )*si ) ] = kp;
} else {
IPIV[ oi + ( ( k-1 )*si ) ] = -kp;
IPIV[ oi + ( ( k-2 )*si ) ] = -kp;
}
// Decrease `k` and return to the start of the main loop...
k -= kstep;
kc = knc - k;
}
return info;
}
/**
* Factorizes a real symmetric matrix `A = L*D*L^T` using the lower triangle of `A` supplied in column-major packed form.
*
* @private
* @param {NonNegativeInteger} N - order of the matrix `A`
* @param {Float64Array} AP - packed form of a symmetric matrix `A`
* @param {integer} sa - stride length for `AP`
* @param {NonNegativeInteger} oa - starting index for `AP`
* @param {Int32Array} IPIV - vector of pivot indices
* @param {integer} si - stride length for `IPIV`
* @param {NonNegativeInteger} oi - starting index for `IPIV`
* @returns {integer} status code
*/
function factorL( N, AP, sa, oa, IPIV, si, oi ) {
var absakk;
var colmax;
var rowmax;
var kstep;
var info;
var imax;
var jmax;
var wkp1;
var npp;
var d11;
var d21;
var d22;
var knc;
var kpc;
var r1;
var wk;
var kc;
var kk;
var kp;
var kx;
var i;
var j;
var k;
var t;
/**
* Resolves the physical index of the (one-based) packed element `idx`.
*
* @private
* @param {PositiveInteger} idx - one-based packed index
* @returns {integer} physical index into `AP`
*/
function pos( idx ) {
return oa + ( ( idx-1 ) * sa );
}
info = 0;
// `k` is the main loop index, increasing from `1` to `N` in steps of `1` or `2`:
k = 1;
kc = 1;
npp = N*( N+1 ) / 2;
while ( k <= N ) {
knc = kc;
kstep = 1;
// Determine rows and columns to be interchanged and whether a 1-by-1 or 2-by-2 pivot block will be used...
absakk = abs( AP[ pos( kc ) ] );
// `imax` is the row-index of the largest off-diagonal element in column `k`, and `colmax` is its absolute value...
if ( k < N ) {
imax = k + idamax( N-k, AP, sa, pos( kc+1 ) ) + 1;
colmax = abs( AP[ pos( kc+imax-k ) ] );
} else {
colmax = 0.0;
}
if ( max( absakk, colmax ) === 0.0 ) {
// Column `k` is zero: set `info` and continue...
if ( info === 0 ) {
info = k;
}
kp = k;
} else {
if ( absakk >= ALPHA*colmax ) {
// No interchange, use 1-by-1 pivot block...
kp = k;
} else {
// `jmax` is the column-index of the largest off-diagonal element in row `imax`, and `rowmax` is its absolute value...
rowmax = 0.0;
kx = kc + imax - k;
for ( j = k; j <= imax-1; j++ ) {
if ( abs( AP[ pos( kx ) ] ) > rowmax ) {
rowmax = abs( AP[ pos( kx ) ] );
jmax = j;
}
kx = kx + N - j;
}
kpc = npp - ( ( N-imax+1 )*( N-imax+2 ) / 2 ) + 1;
if ( imax < N ) {
jmax = imax + idamax( N-imax, AP, sa, pos( kpc+1 ) ) + 1;
rowmax = max( rowmax, abs( AP[ pos( kpc+jmax-imax ) ] ) );
}
if ( absakk >= ALPHA*colmax*( colmax / rowmax ) ) {
// No interchange, use 1-by-1 pivot block...
kp = k;
} else if ( abs( AP[ pos( kpc ) ] ) >= ALPHA*rowmax ) {
// Interchange rows and columns `k` and `imax`, use 1-by-1 pivot block...
kp = imax;
} else {
// Interchange rows and columns `k+1` and `imax`, use 2-by-2 pivot block...
kp = imax;
kstep = 2;
}
}
kk = k + kstep - 1;
if ( kstep === 2 ) {
knc = knc + N - k + 1;
}
if ( kp !== kk ) {
// Interchange rows and columns `kk` and `kp` in the trailing submatrix `A(k:n,k:n)`...
if ( kp < N ) {
dswap( N-kp, AP, sa, pos( knc+kp-kk+1 ), AP, sa, pos( kpc+1 ) );
}
kx = knc + kp - kk;
for ( j = kk+1; j <= kp-1; j++ ) {
kx = kx + N - j + 1;
t = AP[ pos( knc+j-kk ) ];
AP[ pos( knc+j-kk ) ] = AP[ pos( kx ) ];
AP[ pos( kx ) ] = t;
}
t = AP[ pos( knc ) ];
AP[ pos( knc ) ] = AP[ pos( kpc ) ];
AP[ pos( kpc ) ] = t;
if ( kstep === 2 ) {
t = AP[ pos( kc+1 ) ];
AP[ pos( kc+1 ) ] = AP[ pos( kc+kp-k ) ];
AP[ pos( kc+kp-k ) ] = t;
}
}
// Update the trailing submatrix...
if ( kstep === 1 ) {
// 1-by-1 pivot block `D(k)`: column `k` now holds `W(k) = L(k)*D(k)`, where `L(k)` is the k-th column of `L`. Perform a rank-1 update of `A(k+1:n,k+1:n)` as `A := A - L(k)*D(k)*L(k)^T = A - W(k)*(1/D(k))*W(k)^T` and store `L(k)` in column `k`...
if ( k < N ) {
r1 = 1.0 / AP[ pos( kc ) ];
dspr( 'column-major', 'lower', N-k, -r1, AP, sa, pos( kc+1 ), AP, sa, pos( kc+N-k+1 ) );
dscal( N-k, r1, AP, sa, pos( kc+1 ) );
}
} else if ( k < N-1 ) {
// 2-by-2 pivot block `D(k)`. Perform a rank-2 update of `A(k+2:n,k+2:n)`...
d21 = AP[ pos( k+1 + ( ( k-1 )*( ( 2*N )-k ) / 2 ) ) ];
d11 = AP[ pos( k+1 + ( k*( ( 2*N )-k-1 ) / 2 ) ) ] / d21;
d22 = AP[ pos( k + ( ( k-1 )*( ( 2*N )-k ) / 2 ) ) ] / d21;
t = 1.0 / ( ( d11*d22 ) - 1.0 );
d21 = t / d21;
for ( j = k+2; j <= N; j++ ) {
wk = d21 * ( ( d11*AP[ pos( j + ( ( k-1 )*( ( 2*N )-k ) / 2 ) ) ] ) - AP[ pos( j + ( k*( ( 2*N )-k-1 ) / 2 ) ) ] );
wkp1 = d21 * ( ( d22*AP[ pos( j + ( k*( ( 2*N )-k-1 ) / 2 ) ) ] ) - AP[ pos( j + ( ( k-1 )*( ( 2*N )-k ) / 2 ) ) ] );
for ( i = j; i <= N; i++ ) {
AP[ pos( i + ( ( j-1 )*( ( 2*N )-j ) / 2 ) ) ] = AP[ pos( i + ( ( j-1 )*( ( 2*N )-j ) / 2 ) ) ] - ( AP[ pos( i + ( ( k-1 )*( ( 2*N )-k ) / 2 ) ) ]*wk ) - ( AP[ pos( i + ( k*( ( 2*N )-k-1 ) / 2 ) ) ]*wkp1 );
}
AP[ pos( j + ( ( k-1 )*( ( 2*N )-k ) / 2 ) ) ] = wk;
AP[ pos( j + ( k*( ( 2*N )-k-1 ) / 2 ) ) ] = wkp1;
}
}
}
// Store details of the interchanges in `IPIV`...
if ( kstep === 1 ) {
IPIV[ oi + ( ( k-1 )*si ) ] = kp;
} else {
IPIV[ oi + ( ( k-1 )*si ) ] = -kp;
IPIV[ oi + ( k*si ) ] = -kp;
}
// Increase `k` and return to the start of the main loop...
k += kstep;
kc = knc + N - k + 2;
}
return info;
}
// MAIN //
/**
* Computes the factorization of a real symmetric matrix `A` stored in packed format using the Bunch-Kaufman diagonal pivoting method.
*
* @private
* @param {string} order - storage layout
* @param {string} uplo - specifies whether the upper or lower triangular part of the symmetric matrix `A` is supplied
* @param {NonNegativeInteger} N - order of the matrix `A`
* @param {Float64Array} AP - packed form of a symmetric matrix `A`
* @param {integer} strideAP - stride length for `AP`
* @param {NonNegativeInteger} offsetAP - starting index for `AP`
* @param {Int32Array} IPIV - vector of pivot indices
* @param {integer} strideIPIV - stride length for `IPIV`
* @param {NonNegativeInteger} offsetIPIV - starting index for `IPIV`
* @returns {integer} status code
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var Int32Array = require( '@stdlib/array/int32' );
*
* var AP = new Float64Array( [ 4.0, 1.0, 2.0, -2.0, 0.0, 3.0, 2.0, 1.0, -2.0, -1.0 ] );
* var IPIV = new Int32Array( 4 );
*
* dsptrf( 'column-major', 'upper', 4, AP, 1, 0, IPIV, 1, 0 );
* // IPIV => <Int32Array>[ 1, 2, 3, 1 ]
*/
function dsptrf( order, uplo, N, AP, strideAP, offsetAP, IPIV, strideIPIV, offsetIPIV ) {
if ( N === 0 ) {
return 0;
}
// For a symmetric matrix, an upper triangle stored in row-major order is identical in memory to a lower triangle stored in column-major order (and vice versa), so resolve `order`+`uplo` to the equivalent column-major factorization branch...
if (
( isColumnMajor( order ) && uplo === 'upper' ) ||
( isRowMajor( order ) && uplo === 'lower' )
) {
return factorU( N, AP, strideAP, offsetAP, IPIV, strideIPIV, offsetIPIV );
}
return factorL( N, AP, strideAP, offsetAP, IPIV, strideIPIV, offsetIPIV );
}
// EXPORTS //
module.exports = dsptrf;
|