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 | 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 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 2x 2x 2x 2x 36x 38x 3x 3x 9x 9x 9x 3x 3x 3x 3x 3x 33x 33x 33x 33x 33x 38x 22x 22x 22x 30x 30x 30x 82x 82x 140x 62x 62x 62x 140x 82x 82x 20x 20x 8x 8x 8x 20x 20x 20x 20x 4x 4x 4x 4x 16x 16x 82x 30x 18x 18x 22x 22x 22x 22x 54x 54x 84x 50x 50x 50x 84x 54x 54x 4x 4x 4x 4x 4x 4x 4x 4x 54x 22x 22x 29x 29x 29x 38x 79x 79x 79x 29x 38x 3x 3x 3x 3x 26x 26x 26x 26x 26x 26x 26x 26x 38x 50x 50x 141x 141x 141x 141x 141x 141x 141x 141x 4x 4x 137x 141x 137x 137x 137x 137x 137x 141x 704x 704x 704x 704x 704x 704x 704x 137x 137x 137x 141x 41x 41x 41x 41x 41x 41x 41x 137x 137x 141x 93x 93x 44x 141x 44x 141x 8x 8x 44x 44x 44x 44x 44x 44x 44x 44x 50x 26x 26x 26x 26x 38x 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 */ '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; // MAIN // /** * Balances a general real matrix `A`. * * ## Notes * * The job parameter can be one of the following: * * - 'none': none, return immediately * - 'permutate': 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. * * @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 {Float64Array} 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` * @returns {integer} status code * * @example * var Float64Array = require( '@stdlib/array/float64' ); * * 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 Float64Array( 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 => <Float64Array>[ 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 sclfac; var factor; var ica; var ira; var ca; var ra; var is; var c; var r; var k; var l; var i; var j; var g; var f; var s; sclfac = 2.0; factor = 0.95; // 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; 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[ offsetScale + (l*strideScale) ] = i; if ( i !== l ) { dswap( l+1, A, strideA1, offsetA + (i*strideA2), A, strideA1, offsetA + (l*strideA2) ); dswap( N - k, A, strideA2, offsetA + (i*strideA1) + (k*strideA2), A, strideA2, offsetA + (l*strideA1) + (k*strideA2) ); } 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; } } } noconv = true; while ( noconv ) { // Search for columns isolating an eigenvalue and push them left noconv = false; 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[ offsetScale + (k*strideScale) ] = j; if ( j !== k ) { dswap( l+1, A, strideA1, offsetA + (j*strideA2), A, strideA1, offsetA + (k*strideA2) ); dswap( N-k, A, strideA2, offsetA + (j*strideA1), A, strideA2, offsetA + (k*strideA1) ); } noconv = true; k += 1; } } } } // Initialize `scale` for non-permuted submatrix is = offsetScale + (k*strideScale); for ( i = k; i <= l; i++ ) { scale[ is ] = 1.0; is += strideScale; } if ( job === 'permutate' ) { 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 ) { noconv = false; for ( i = k; i <= l; i++ ) { c = dnrm2( l-k+1, A, strideA1, offsetA + (k*strideA1) + (i*strideA2) ); r = dnrm2( l-k+1, A, strideA2, offsetA + (i*strideA1) + (k*strideA2) ); ica = idamax( l+1, A, strideA1, offsetA + (i*strideA2) ); ca = abs( A[ offsetA + (ica*strideA1) + (i*strideA2) ] ); ira = idamax( N-k+1, A, strideA2, offsetA + (i*strideA1) + (k*strideA2) ); ra = abs( A[ offsetA + (i*strideA1) + ((ira+k)*strideA2) ] ); if ( c === 0.0 || r === 0.0 ) { continue; } if ( isnan( c ) || isnan( r ) || isnan( ca ) || isnan( ra ) ) { return -3; } 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 ) { continue; } if ( f < 1.0 && scale[ offsetScale + (i*strideScale) ] < 1.0 ) { if ( f * scale[ offsetScale + (i*strideScale) ] <= sfmin1 ) { continue; } } if ( f > 1.0 && scale[ offsetScale + (i*strideScale) ] > 1.0 ) { if ( scale[ offsetScale + (i*strideScale) ] >= sfmax1 / f ) { continue; } } g = 1.0 / f; scale[ offsetScale + (i*strideScale) ] *= f; noconv = true; dscal( N-k, g, A, strideA2, offsetA + (i*strideA1) + (k*strideA2) ); dscal( l+1, f, A, strideA1, offsetA + (i*strideA2) ); } } out[ offsetOut ] = k; // ilo out[ offsetOut + strideOut ] = l; // ihi return 0; } // EXPORTS // module.exports = dgebal; |