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 | 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 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 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 2x 2x 2x 2x 96x 98x 7x 7x 21x 21x 21x 7x 7x 7x 7x 7x 89x 89x 89x 89x 89x 98x 57x 57x 57x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 219x 219x 384x 159x 159x 159x 384x 219x 219x 60x 60x 24x 24x 24x 60x 60x 60x 60x 12x 12x 12x 12x 48x 48x 48x 48x 48x 48x 207x 207x 207x 207x 81x 45x 45x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 135x 135x 216x 123x 123x 123x 216x 135x 135x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 135x 135x 135x 135x 57x 57x 77x 77x 77x 98x 203x 203x 203x 77x 98x 9x 9x 9x 9x 68x 68x 68x 68x 68x 68x 68x 68x 98x 120x 120x 120x 120x 120x 120x 120x 325x 325x 325x 325x 325x 325x 325x 325x 12x 12x 12x 12x 12x 12x 313x 325x 313x 313x 313x 313x 313x 325x 808x 808x 808x 808x 808x 808x 808x 313x 313x 313x 325x 125x 125x 125x 125x 125x 125x 125x 313x 313x 325x 225x 225x 225x 225x 225x 225x 88x 325x 88x 325x 8x 8x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 120x 68x 68x 68x 68x 98x 3x 3x 3x 3x 3x | /** * @license Apache-2.0 * * Copyright (c) 2025 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, max-statements, max-lines-per-function */ 'use strict'; // MODULES // var dswap = require( '@stdlib/blas/base/dswap' ).ndarray; var dlamch = require( '@stdlib/lapack/base/dlamch' ); var dnrm2 = require( '@stdlib/blas/base/dnrm2' ).ndarray; var idamax = require( '@stdlib/blas/base/idamax' ).ndarray; var abs = require( '@stdlib/math/base/special/abs' ); var isnan = require( '@stdlib/assert/is-nan' ); var max = require( '@stdlib/math/base/special/maxn' ); var min = require( '@stdlib/math/base/special/minn' ); var dscal = require( '@stdlib/blas/base/dscal' ).ndarray; // VARIABLES // var sclfac = 2.0; var factor = 0.95; // MAIN // /** * Balances a general real matrix `A`. * * ## Notes * * The job parameter can be one of the following: * * - 'none': none, return immediately * - 'permute': permute only * - 'scale': scale only * - 'both': both permute and scale * * The matrix `A` is overwritten by the balanced matrix. Scale factors are stored in the `scale` array. If `P[ j ]` is the index of the row and column interchanged with row and column j (zero-based) and `D[ j ]` is the scaling factor applied to row and column j, then: * * - `scale[ j ]` = `P[ j ]` for j = `0`,...,`out[ 0 ]-1` * - `scale[ j ]` = `D[ j ]` for j = `out[ 0 ]`,...,`out[ 1 ]` * - `scale[ j ]` = `P[ j ]` for j = `out[ 1 ]+1`,...,`N-1`. * * The order in which the interchanges are made is `N-1` to `out[ 1 ]+1`, then `0` to `out[ 0 ]-1`. * * @private * @param {string} job - indicates the operations to be performed * @param {NonNegativeInteger} N - number of rows/columns in matrix `A` * @param {Float64Array} A - input matrix to be balanced * @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 {Int32Array} out - stores the first and last row/column of the balanced submatrix * @param {integer} strideOut - stride of `out` * @param {NonNegativeInteger} offsetOut - starting index for `out` * @param {Float64Array} scale - array containing permutation and scaling information * @param {integer} strideScale - stride of `scale` * @param {NonNegativeInteger} offsetScale - starting index for `scale` * @throws {RangeError} should not return NaN * @returns {integer} status code * * @example * var Float64Array = require( '@stdlib/array/float64' ); * var Int32Array = require( '@stdlib/array/int32' ); * * var A = new Float64Array( [ 1.0, 100.0, 0.0, 2.0, 200.0, 0.0, 0.0, 0.0, 3.0 ] ); * var out = new Int32Array( 2 ); * var scale = new Float64Array( 3 ); * * dgebal( 'both', 3, A, 3, 1, 0, out, 1, 0, scale, 1, 0 ); * // A => <Float64Array>[ 1, 12.5, 0, 16, 200, 0, 0, 0, 3 ] * // out => <Int32Array>[ 0, 1 ] * // scale => <Float64Array>[ 8, 1, 2 ] */ function dgebal( job, N, A, strideA1, strideA2, offsetA, out, strideOut, offsetOut, scale, strideScale, offsetScale ) { var canSwap; var noconv; var sfmin1; var sfmin2; var sfmax1; var sfmax2; var ica; var ira; var ia1; var ia2; var ia3; var ia4; var ca; var ra; var is; var c; var r; var k; var l; var i; var j; var g; var f; var s; // Quick return if possible if ( N === 0 ) { out[ offsetOut ] = 0.0; // ilo out[ offsetOut + strideOut ] = -1.0; // ihi (invalid) return 0; } if ( job === 'none' ) { is = offsetScale; for ( i = 0; i < N; i++ ) { scale[ is ] = 1.0; is += strideScale; } out[ offsetOut ] = 0.0; // ilo out[ offsetOut + strideOut ] = N - 1; // ihi return 0; } // Permutation to isolate eigenvalues if possible k = 0; l = N - 1; if ( job !== 'scale' ) { // Row and column exchange noconv = true; while ( noconv ) { // Search for rows isolating an eigenvalue and push them down noconv = false; is = offsetScale + ( l * strideScale ); // Follows scale ia1 = offsetA + ( l * strideA2 ); // Follows `i`th column of A ia2 = offsetA + ( l * strideA2 ); // Follows `l`th column of A ia3 = offsetA + (l*strideA1) + (k*strideA2); // Follows `i`th row of A ia4 = offsetA + (l*strideA1) + (k*strideA2); // Follows `l`th row of A for ( i = l; i >= 0; i-- ) { canSwap = true; for ( j = 0; j <= l; j++ ) { if ( i !== j && A[ offsetA + (i*strideA1) + (j*strideA2) ] !== 0.0 ) { canSwap = false; break; } } if ( canSwap ) { scale[ is ] = i; if ( i !== l ) { dswap( l+1, A, strideA1, ia1, A, strideA1, ia2 ); dswap( N - k, A, strideA2, ia3, A, strideA2, ia4 ); } noconv = true; // Check if remaining submatrix is empty and return if ( l === 0.0 ) { out[ offsetOut ] = 0.0; // ilo out[ offsetOut + strideOut ] = 0.0; // ihi return 0; } l -= 1; is -= strideScale; ia2 -= strideA2; ia4 -= strideA1; } ia1 -= strideA2; ia3 -= strideA1; } } noconv = true; while ( noconv ) { // Search for columns isolating an eigenvalue and push them left noconv = false; is = offsetScale + ( k * strideScale ); // Follows scale ia1 = offsetA + ( k * strideA2 ); // Follows `j`th column of A ia2 = offsetA + ( k * strideA2 ); // Follows `k`th column of A ia3 = offsetA + ( k * strideA1 ); // Follows `j`th row of A ia4 = offsetA + ( k * strideA1 ); // Follows `k`th row of A for ( j = k; j <= l; j++ ) { canSwap = true; for ( i = k; i <= l; i++ ) { if ( i !== j && A[ offsetA + (i*strideA1) + (j*strideA2) ] !== 0.0 ) { canSwap = false; break; } } if ( canSwap ) { scale[ is ] = j; if ( j !== k ) { dswap( l+1, A, strideA1, ia1, A, strideA1, ia2 ); dswap( N-k, A, strideA2, ia3, A, strideA2, ia4 ); } noconv = true; k += 1; is += strideScale; ia2 += strideA2; ia4 += strideA1; } ia1 += strideA2; ia3 += strideA1; } } } // Initialize scale for non-permuted submatrix is = offsetScale + ( k * strideScale ); for ( i = k; i <= l; i++ ) { scale[ is ] = 1.0; is += strideScale; } if ( job === 'permute' ) { out[ offsetOut ] = k; // ilo out[ offsetOut + strideOut ] = l; // ihi return 0; } // Balance the submatrix in rows K to L, iterative loop for norm reduction (job = 'B') sfmin1 = dlamch( 'S' ) / dlamch( 'P' ); sfmax1 = 1.0 / sfmin1; sfmin2 = sfmin1 * sclfac; sfmax2 = 1.0 / sfmin2; noconv = true; while ( noconv ) { is = offsetScale + ( k * strideScale ); // Follows scale ia1 = offsetA + ( k * strideA1 ) + ( k * strideA2 ); // Follows A[ k, i ] ia2 = offsetA + ( k * strideA1 ) + ( k * strideA2 ); // Follows A[ i, k ] ia3 = offsetA + ( k * strideA2 ); // follows `i`th column of A noconv = false; for ( i = k; i <= l; i++ ) { c = dnrm2( l-k+1, A, strideA1, ia1 ); r = dnrm2( l-k+1, A, strideA2, ia2 ); ica = idamax( l+1, A, strideA1, ia3 ); ca = abs( A[ offsetA + (ica*strideA1) + (i*strideA2) ] ); ira = idamax( N-k+1, A, strideA2, ia2 ); ra = abs( A[ offsetA + (i*strideA1) + ((ira+k)*strideA2) ] ); if ( c === 0.0 || r === 0.0 ) { ia1 += strideA2; ia2 += strideA1; is += strideScale; ia3 += strideA2; continue; } if ( isnan( c ) || isnan( r ) || isnan( ca ) || isnan( ra ) ) { throw new RangeError( 'should not return NaN' ); } g = r / sclfac; f = 1.0; s = c + r; while ( c < g && max( f, c, ca ) < sfmax2 && min( r, g, ra ) > sfmin2 ) { f *= sclfac; c *= sclfac; ca *= sclfac; r /= sclfac; g /= sclfac; ra /= sclfac; } g = c / sclfac; while ( g >= r && max( r, ra ) < sfmax2 && min( f, c, g, ca ) > sfmin2 ) { f /= sclfac; c /= sclfac; g /= sclfac; ca /= sclfac; r *= sclfac; ra *= sclfac; } // Now balance if ( ( c + r ) >= factor * s ) { ia1 += strideA2; ia2 += strideA1; is += strideScale; ia3 += strideA2; continue; } if ( f < 1.0 && scale[ is ] < 1.0 ) { if ( f * scale[ is ] <= sfmin1 ) { ia1 += strideA2; ia2 += strideA1; is += strideScale; ia3 += strideA2; continue; } } if ( f > 1.0 && scale[ is ] > 1.0 ) { if ( scale[ is ] >= sfmax1 / f ) { ia1 += strideA2; ia2 += strideA1; is += strideScale; ia3 += strideA2; continue; } } g = 1.0 / f; scale[ is ] *= f; noconv = true; dscal( N-k, g, A, strideA2, ia2 ); dscal( l+1, f, A, strideA1, ia3 ); ia1 += strideA2; ia2 += strideA1; is += strideScale; ia3 += strideA2; } } out[ offsetOut ] = k; // ilo out[ offsetOut + strideOut ] = l; // ihi return 0; } // EXPORTS // module.exports = dgebal; |