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 | 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 158x 158x 158x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 48x 48x 48x 48x 48x 48x 48x 48x 22x 22x 2x 2x 22x 22x 22x 22x 22x 22x 48x 26x 26x 12x 12x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 50x 50x 50x 50x 50x 50x 50x 26x 26x 26x 48x 18x 18x 18x 18x 18x 48x 48x 48x 48x 48x 48x 48x 22x 22x 2x 2x 22x 22x 48x 26x 26x 26x 26x 26x 12x 12x 26x 26x 26x 48x 18x 18x 3x 3x 3x 3x 3x 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 31x 31x 31x 31x 31x 31x 15x 15x 2x 2x 15x 15x 4x 4x 15x 15x 15x 15x 31x 16x 16x 9x 9x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 30x 30x 30x 30x 30x 30x 30x 16x 16x 16x 31x 11x 11x 11x 11x 11x 31x 31x 31x 31x 31x 31x 31x 31x 20x 20x 31x 15x 15x 2x 2x 15x 31x 16x 16x 16x 16x 16x 16x 9x 9x 16x 16x 16x 31x 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 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 33x 33x 4x 4x 29x 33x 16x 33x 18x 18x 11x 33x 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-params */
'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 dgemv = require( '@stdlib/blas/base/dgemv' ).ndarray;
var dger = require( '@stdlib/blas/base/dger' ).ndarray;
var dscal = require( '@stdlib/blas/base/dscal' ).ndarray;
var dswap = require( '@stdlib/blas/base/dswap' ).ndarray;
// FUNCTIONS //
/**
* Returns the one-based row index encoded by a pivot value.
*
* ## Notes
*
* - The pivot encoding follows `dsptrf`: a positive value `p` denotes a 1-by-1 diagonal block for which rows `k` and `p` were interchanged, and a negative value `-p` denotes a 2-by-2 diagonal block for which rows were interchanged with row `p`, where `p` is a one-based index.
*
* @private
* @param {integer} p - pivot value
* @returns {PositiveInteger} one-based row index
*/
function pivotRow( p ) {
return ( p > 0 ) ? p : -p;
}
/**
* Solves a system of linear equations `A * X = B` using the factorization `A = U * D * U^T` computed by `dsptrf`, where `U` is stored in column-major packed format.
*
* @private
* @param {PositiveInteger} N - order of the matrix `A`
* @param {PositiveInteger} nrhs - number of right-hand sides
* @param {Float64Array} AP - factored matrix in packed format
* @param {integer} sap - stride length for `AP`
* @param {NonNegativeInteger} oap - 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`
* @param {Float64Array} B - right-hand side matrix, overwritten by the solution matrix `X`
* @param {integer} sb1 - stride of the first dimension of `B`
* @param {integer} sb2 - stride of the second dimension of `B`
* @param {NonNegativeInteger} ob - starting index for `B`
* @returns {Float64Array} solution matrix `X`
*/
function solveU( N, nrhs, AP, sap, oap, IPIV, si, oi, B, sb1, sb2, ob ) {
var akm1k;
var denom;
var akm1;
var bkm1;
var ib1;
var ib2;
var ak;
var bk;
var kc;
var kp;
var ik;
var p;
var j;
var k;
// First, solve `U * D * X = B` (loop counters `k` and `kc` are one-based, as in the reference implementation)...
k = N;
kc = ( ( N * ( N+1 ) ) / 2 ) + 1;
while ( k >= 1 ) {
// Move `kc` to the start of column `k` of `U`...
kc -= k;
p = IPIV[ oi + ( ( k-1 ) * si ) ];
kp = pivotRow( p );
// Index of the first element in row `k` of `B` (zero-based row `k-1`)...
ik = ob + ( ( k-1 ) * sb1 );
if ( p > 0 ) {
// 1-by-1 diagonal block: interchange rows `k-1` and `kp-1` (zero-based)...
if ( kp !== k ) {
dswap( nrhs, B, sb2, ik, B, sb2, ob + ( ( kp-1 ) * sb1 ) );
}
// Multiply by `inv(U(k))`, where `U(k)` is the transformation stored in column `k-1` of `A` (zero-based)...
dger( k-1, nrhs, -1.0, AP, sap, oap + ( ( kc-1 ) * sap ), B, sb2, ik, B, sb1, sb2, ob );
// Multiply by the inverse of the diagonal block...
dscal( nrhs, 1.0 / AP[ oap + ( ( kc+k-2 ) * sap ) ], B, sb2, ik );
k -= 1;
} else {
// 2-by-2 diagonal block: interchange rows `k-2` and `kp-1` (zero-based)...
if ( kp !== k-1 ) {
dswap( nrhs, B, sb2, ik - sb1, B, sb2, ob + ( ( kp-1 ) * sb1 ) );
}
// Multiply by `inv(U(k))`, where `U(k)` is the transformation stored in columns `k-2` and `k-1` of `A` (zero-based)...
dger( k-2, nrhs, -1.0, AP, sap, oap + ( ( kc-1 ) * sap ), B, sb2, ik, B, sb1, sb2, ob );
dger( k-2, nrhs, -1.0, AP, sap, oap + ( ( kc-k ) * sap ), B, sb2, ik - sb1, B, sb1, sb2, ob );
// Multiply by the inverse of the diagonal block...
akm1k = AP[ oap + ( ( kc+k-3 ) * sap ) ];
akm1 = AP[ oap + ( ( kc-2 ) * sap ) ] / akm1k;
ak = AP[ oap + ( ( kc+k-2 ) * sap ) ] / akm1k;
denom = ( akm1 * ak ) - 1.0;
ib1 = ik - sb1;
ib2 = ik;
for ( j = 0; j < nrhs; j++ ) {
bkm1 = B[ ib1 ] / akm1k;
bk = B[ ib2 ] / akm1k;
B[ ib1 ] = ( ( ak * bkm1 ) - bk ) / denom;
B[ ib2 ] = ( ( akm1 * bk ) - bkm1 ) / denom;
ib1 += sb2;
ib2 += sb2;
}
kc -= k - 1;
k -= 2;
}
}
// Next, solve `U^T * X = B`...
k = 1;
kc = 1;
while ( k <= N ) {
p = IPIV[ oi + ( ( k-1 ) * si ) ];
kp = pivotRow( p );
ik = ob + ( ( k-1 ) * sb1 );
// Multiply by `inv(U^T(k))`, where `U(k)` is the transformation stored in column `k-1` of `A` (zero-based)...
dgemv( 'transpose', k-1, nrhs, -1.0, B, sb1, sb2, ob, AP, sap, oap + ( ( kc-1 ) * sap ), 1.0, B, sb2, ik );
if ( p > 0 ) {
// 1-by-1 diagonal block: interchange rows `k-1` and `kp-1` (zero-based)...
if ( kp !== k ) {
dswap( nrhs, B, sb2, ik, B, sb2, ob + ( ( kp-1 ) * sb1 ) );
}
kc += k;
k += 1;
} else {
// 2-by-2 diagonal block: also apply the transformation stored in column `k` of `A` (zero-based)...
dgemv( 'transpose', k-1, nrhs, -1.0, B, sb1, sb2, ob, AP, sap, oap + ( ( kc+k-1 ) * sap ), 1.0, B, sb2, ik + sb1 );
// Interchange rows `k-1` and `kp-1` (zero-based)...
if ( kp !== k ) {
dswap( nrhs, B, sb2, ik, B, sb2, ob + ( ( kp-1 ) * sb1 ) );
}
kc += ( 2 * k ) + 1;
k += 2;
}
}
return B;
}
/**
* Solves a system of linear equations `A * X = B` using the factorization `A = L * D * L^T` computed by `dsptrf`, where `L` is stored in column-major packed format.
*
* @private
* @param {PositiveInteger} N - order of the matrix `A`
* @param {PositiveInteger} nrhs - number of right-hand sides
* @param {Float64Array} AP - factored matrix in packed format
* @param {integer} sap - stride length for `AP`
* @param {NonNegativeInteger} oap - 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`
* @param {Float64Array} B - right-hand side matrix, overwritten by the solution matrix `X`
* @param {integer} sb1 - stride of the first dimension of `B`
* @param {integer} sb2 - stride of the second dimension of `B`
* @param {NonNegativeInteger} ob - starting index for `B`
* @returns {Float64Array} solution matrix `X`
*/
function solveL( N, nrhs, AP, sap, oap, IPIV, si, oi, B, sb1, sb2, ob ) {
var akm1k;
var denom;
var akm1;
var bkm1;
var ib1;
var ib2;
var ak;
var bk;
var kc;
var kp;
var ik;
var p;
var j;
var k;
// First, solve `L * D * X = B` (loop counters `k` and `kc` are one-based, as in the reference implementation)...
k = 1;
kc = 1;
while ( k <= N ) {
p = IPIV[ oi + ( ( k-1 ) * si ) ];
kp = pivotRow( p );
// Index of the first element in row `k` of `B` (zero-based row `k-1`)...
ik = ob + ( ( k-1 ) * sb1 );
if ( p > 0 ) {
// 1-by-1 diagonal block: interchange rows `k-1` and `kp-1` (zero-based)...
if ( kp !== k ) {
dswap( nrhs, B, sb2, ik, B, sb2, ob + ( ( kp-1 ) * sb1 ) );
}
// Multiply by `inv(L(k))`, where `L(k)` is the transformation stored in column `k-1` of `A` (zero-based)...
if ( k < N ) {
dger( N-k, nrhs, -1.0, AP, sap, oap + ( kc * sap ), B, sb2, ik, B, sb1, sb2, ik + sb1 );
}
// Multiply by the inverse of the diagonal block...
dscal( nrhs, 1.0 / AP[ oap + ( ( kc-1 ) * sap ) ], B, sb2, ik );
kc += N - k + 1;
k += 1;
} else {
// 2-by-2 diagonal block: interchange rows `k` and `kp-1` (zero-based)...
if ( kp !== k+1 ) {
dswap( nrhs, B, sb2, ik + sb1, B, sb2, ob + ( ( kp-1 ) * sb1 ) );
}
// Multiply by `inv(L(k))`, where `L(k)` is the transformation stored in columns `k-1` and `k` of `A` (zero-based)...
if ( k < N-1 ) {
dger( N-k-1, nrhs, -1.0, AP, sap, oap + ( ( kc+1 ) * sap ), B, sb2, ik, B, sb1, sb2, ik + ( 2*sb1 ) );
dger( N-k-1, nrhs, -1.0, AP, sap, oap + ( ( kc+N-k+1 ) * sap ), B, sb2, ik + sb1, B, sb1, sb2, ik + ( 2*sb1 ) );
}
// Multiply by the inverse of the diagonal block...
akm1k = AP[ oap + ( kc * sap ) ];
akm1 = AP[ oap + ( ( kc-1 ) * sap ) ] / akm1k;
ak = AP[ oap + ( ( kc+N-k ) * sap ) ] / akm1k;
denom = ( akm1 * ak ) - 1.0;
ib1 = ik;
ib2 = ik + sb1;
for ( j = 0; j < nrhs; j++ ) {
bkm1 = B[ ib1 ] / akm1k;
bk = B[ ib2 ] / akm1k;
B[ ib1 ] = ( ( ak * bkm1 ) - bk ) / denom;
B[ ib2 ] = ( ( akm1 * bk ) - bkm1 ) / denom;
ib1 += sb2;
ib2 += sb2;
}
kc += ( 2 * ( N-k ) ) + 1;
k += 2;
}
}
// Next, solve `L^T * X = B`...
k = N;
kc = ( ( N * ( N+1 ) ) / 2 ) + 1;
while ( k >= 1 ) {
// Move `kc` to the start of column `k` of `L`...
kc -= N - k + 1;
p = IPIV[ oi + ( ( k-1 ) * si ) ];
kp = pivotRow( p );
ik = ob + ( ( k-1 ) * sb1 );
// Multiply by `inv(L^T(k))`, where `L(k)` is the transformation stored in column `k-1` of `A` (zero-based)...
if ( k < N ) {
dgemv( 'transpose', N-k, nrhs, -1.0, B, sb1, sb2, ik + sb1, AP, sap, oap + ( kc * sap ), 1.0, B, sb2, ik );
}
if ( p > 0 ) {
// 1-by-1 diagonal block: interchange rows `k-1` and `kp-1` (zero-based)...
if ( kp !== k ) {
dswap( nrhs, B, sb2, ik, B, sb2, ob + ( ( kp-1 ) * sb1 ) );
}
k -= 1;
} else {
// 2-by-2 diagonal block: also apply the transformation stored in column `k-2` of `A` (zero-based)...
if ( k < N ) {
dgemv( 'transpose', N-k, nrhs, -1.0, B, sb1, sb2, ik + sb1, AP, sap, oap + ( ( kc-( N-k )-1 ) * sap ), 1.0, B, sb2, ik - sb1 );
}
// Interchange rows `k-1` and `kp-1` (zero-based)...
if ( kp !== k ) {
dswap( nrhs, B, sb2, ik, B, sb2, ob + ( ( kp-1 ) * sb1 ) );
}
kc -= N - k + 2;
k -= 2;
}
}
return B;
}
// MAIN //
/**
* Solves a system of linear equations `A * X = B` with a real symmetric matrix `A` stored in packed format using the factorization `A = U * D * U^T` or `A = L * D * L^T` computed by `dsptrf`.
*
* ## Notes
*
* - 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). Accordingly, as in `dsptrf`, when `order` is `'row-major'`, the function resolves `uplo` to the equivalent column-major factorization (i.e., `'row-major'` and `'upper'` is solved using `L * D * L^T` and `'row-major'` and `'lower'` is solved using `U * D * U^T`).
*
* @private
* @param {string} order - storage layout
* @param {string} uplo - specifies whether the factorization of the upper or lower triangular part of `A` is supplied
* @param {NonNegativeInteger} N - order of the matrix `A`
* @param {NonNegativeInteger} nrhs - number of right-hand sides
* @param {Float64Array} AP - factored matrix in packed format, as returned by `dsptrf`
* @param {integer} strideAP - stride length for `AP`
* @param {NonNegativeInteger} offsetAP - starting index for `AP`
* @param {Int32Array} IPIV - vector of pivot indices, as returned by `dsptrf`
* @param {integer} strideIPIV - stride length for `IPIV`
* @param {NonNegativeInteger} offsetIPIV - starting index for `IPIV`
* @param {Float64Array} B - right-hand side matrix, overwritten by the solution matrix `X`
* @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`
* @returns {Float64Array} solution matrix `X`
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var Int32Array = require( '@stdlib/array/int32' );
*
* // Factorization of A = [ [ 2, 1 ], [ 1, 2 ] ] (column-major, upper), as computed by `dsptrf`:
* var AP = new Float64Array( [ 1.5, 0.5, 2.0 ] );
* var IPIV = new Int32Array( [ 1, 2 ] );
* var B = new Float64Array( [ 3.0, 3.0 ] );
*
* dsptrs( 'column-major', 'upper', 2, 1, AP, 1, 0, IPIV, 1, 0, B, 1, 2, 0 );
* // B => <Float64Array>[ 1.0, 1.0 ]
*/
function dsptrs( order, uplo, N, nrhs, AP, strideAP, offsetAP, IPIV, strideIPIV, offsetIPIV, B, strideB1, strideB2, offsetB ) {
if ( N === 0 || nrhs === 0 ) {
return B;
}
if (
( isColumnMajor( order ) && uplo === 'upper' ) ||
( isRowMajor( order ) && uplo === 'lower' )
) {
return solveU( N, nrhs, AP, strideAP, offsetAP, IPIV, strideIPIV, offsetIPIV, B, strideB1, strideB2, offsetB );
}
return solveL( N, nrhs, AP, strideAP, offsetAP, IPIV, strideIPIV, offsetIPIV, B, strideB1, strideB2, offsetB );
}
// EXPORTS //
module.exports = dsptrs;
|