All files base.js

100% Statements 204/204
100% Branches 31/31
100% Functions 1/1
100% Lines 204/204

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 2053x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 4x 4x 4x 4x 4x 30x 30x 34x 103x 103x 103x 30x 34x 103x 103x 103x 103x 103x 103x 103x 267x 267x 267x 267x 103x 30x 30x 30x 30x 34x 103x 103x 103x 103x 30x 30x 34x 2x 2x 2x 4x 2x 2x 2x 2x 2x 28x 28x 34x 97x 97x 97x 28x 28x 28x 28x 28x 34x 97x 97x 97x 28x 28x 34x 97x 97x 97x 97x 97x 253x 253x 253x 253x 97x 97x 28x 28x 28x 28x 34x 97x 97x 97x 97x 34x 2x 2x 2x 4x 2x 2x 2x 2x 2x 26x 26x 34x 91x 91x 91x 26x 26x 26x 34x 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';
 
// MODULES //
 
var FLOAT64_SMALLEST_NORMAL = require( '@stdlib/constants/float64/smallest-normal' );
var abs = require( '@stdlib/math/base/special/abs' );
var max = require( '@stdlib/math/base/special/max' );
var min = require( '@stdlib/math/base/special/min' );
 
 
// VARIABLES //
 
var SMLNUM = FLOAT64_SMALLEST_NORMAL;
var BIGNUM = 1.0 / SMLNUM;
 
 
// MAIN //
 
/**
* Computes row and column scale factors intended to equilibrate an `M`-by-`N` band matrix `A` and reduce its condition number.
*
* ## Notes
*
* -   The band matrix `A` is stored in the band array `AB`, which has `KL+KU+1` rows and `N` columns, such that the element `A(i,j)` (zero-based) is stored in row `KU+i-j` and column `j` of `AB`, for `max(0,j-KU) <= i <= min(M-1,j+KL)`.
* -   `R` returns the row scale factors and `C` returns the column scale factors, chosen such that each element of the scaled matrix `B(i,j) = R(i)*A(i,j)*C(j)` has an absolute value of at most one and each row and column of `B` has at least one element with an absolute value of one (barring underflow and overflow).
* -   `out` is set to `[ ROWCND, COLCND, AMAX ]`, where `ROWCND` is the ratio of the smallest `R(i)` to the largest `R(i)`, `COLCND` is the ratio of the smallest `C(j)` to the largest `C(j)`, and `AMAX` is the absolute value of the largest matrix element.
* -   The status code is `0` on success, `i` (one-based) if row `i` of `A` is exactly zero, and `M+j` (one-based) if column `j` of `A` is exactly zero.
*
* @private
* @param {NonNegativeInteger} M - number of rows in the matrix `A`
* @param {NonNegativeInteger} N - number of columns in the matrix `A`
* @param {NonNegativeInteger} KL - number of subdiagonals within the band of `A`
* @param {NonNegativeInteger} KU - number of superdiagonals within the band of `A`
* @param {Float64Array} AB - band array containing the band matrix `A`
* @param {integer} strideAB1 - stride of the first dimension of `AB` (i.e., between band rows)
* @param {integer} strideAB2 - stride of the second dimension of `AB` (i.e., between columns)
* @param {NonNegativeInteger} offsetAB - starting index for `AB`
* @param {Float64Array} R - output array for the row scale factors
* @param {integer} strideR - stride length for `R`
* @param {NonNegativeInteger} offsetR - starting index for `R`
* @param {Float64Array} C - output array for the column scale factors
* @param {integer} strideC - stride length for `C`
* @param {NonNegativeInteger} offsetC - starting index for `C`
* @param {Float64Array} out - output array for `[ ROWCND, COLCND, AMAX ]`
* @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' );
*
* // A = [ [ 1, 2, 0 ], [ 3, 4, 5 ], [ 0, 6, 8 ] ] (KL = 1, KU = 1), stored as a column-major band array:
* var AB = new Float64Array( [ 0.0, 1.0, 3.0, 2.0, 4.0, 6.0, 5.0, 8.0, 0.0 ] );
* var R = new Float64Array( 3 );
* var C = new Float64Array( 3 );
* var out = new Float64Array( 3 );
*
* var info = dgbequ( 3, 3, 1, 1, AB, 1, 3, 0, R, 1, 0, C, 1, 0, out, 1, 0 );
* // returns 0
*
* // R => <Float64Array>[ 0.5, 0.2, 0.125 ]
* // C => <Float64Array>[ ~1.67, 1.0, 1.0 ]
* // out => <Float64Array>[ 0.25, ~0.6, 8.0 ]
*/
function dgbequ( M, N, KL, KU, AB, strideAB1, strideAB2, offsetAB, R, strideR, offsetR, C, strideC, offsetC, out, strideOut, offsetOut ) { // eslint-disable-line max-len, max-params
	var rcmin;
	var rcmax;
	var iab;
	var ir;
	var ic;
	var i0;
	var i1;
	var i;
	var j;
 
	// Quick return if possible...
	if ( M === 0 || N === 0 ) {
		out[ offsetOut ] = 1.0;
		out[ offsetOut + strideOut ] = 1.0;
		out[ offsetOut + ( 2*strideOut ) ] = 0.0;
		return 0;
	}
	// Initialize the row scale factors...
	ir = offsetR;
	for ( i = 0; i < M; i++ ) {
		R[ ir ] = 0.0;
		ir += strideR;
	}
	// Find the maximum element in each row, visiting only the elements within the band of each column `j`, which lie in rows `i0` through `i1` (zero-based)...
	for ( j = 0; j < N; j++ ) {
		i0 = max( j-KU, 0 );
		i1 = min( j+KL, M-1 );
 
		// `A(i0,j)` is stored in band row `KU+i0-j` of column `j`, and moving down a column of `A` moves down a row of `AB`...
		iab = offsetAB + ( ( KU+i0-j ) * strideAB1 ) + ( j * strideAB2 );
		ir = offsetR + ( i0 * strideR );
		for ( i = i0; i <= i1; i++ ) {
			R[ ir ] = max( R[ ir ], abs( AB[ iab ] ) );
			iab += strideAB1;
			ir += strideR;
		}
	}
	// Find the maximum and minimum row scale factors...
	rcmin = BIGNUM;
	rcmax = 0.0;
	ir = offsetR;
	for ( i = 0; i < M; i++ ) {
		rcmax = max( rcmax, R[ ir ] );
		rcmin = min( rcmin, R[ ir ] );
		ir += strideR;
	}
	out[ offsetOut + ( 2*strideOut ) ] = rcmax; // AMAX
 
	if ( rcmin === 0.0 ) {
		// Find the first zero scale factor and return an error code...
		ir = offsetR;
		for ( i = 0; i < M; i++ ) {
			if ( R[ ir ] === 0.0 ) {
				return i + 1;
			}
			ir += strideR;
		}
	}
	// Invert the row scale factors...
	ir = offsetR;
	for ( i = 0; i < M; i++ ) {
		R[ ir ] = 1.0 / min( max( R[ ir ], SMLNUM ), BIGNUM );
		ir += strideR;
	}
	// Compute `ROWCND = min(R)/max(R)`...
	out[ offsetOut ] = max( rcmin, SMLNUM ) / min( rcmax, BIGNUM );
 
	// Initialize the column scale factors...
	ic = offsetC;
	for ( j = 0; j < N; j++ ) {
		C[ ic ] = 0.0;
		ic += strideC;
	}
	// Find the largest (row-scaled) element in each column...
	ic = offsetC;
	for ( j = 0; j < N; j++ ) {
		i0 = max( j-KU, 0 );
		i1 = min( j+KL, M-1 );
		iab = offsetAB + ( ( KU+i0-j ) * strideAB1 ) + ( j * strideAB2 );
		ir = offsetR + ( i0 * strideR );
		for ( i = i0; i <= i1; i++ ) {
			C[ ic ] = max( C[ ic ], abs( AB[ iab ] ) * R[ ir ] );
			iab += strideAB1;
			ir += strideR;
		}
		ic += strideC;
	}
	// Find the maximum and minimum column scale factors...
	rcmin = BIGNUM;
	rcmax = 0.0;
	ic = offsetC;
	for ( j = 0; j < N; j++ ) {
		rcmin = min( rcmin, C[ ic ] );
		rcmax = max( rcmax, C[ ic ] );
		ic += strideC;
	}
	if ( rcmin === 0.0 ) {
		// Find the first zero scale factor and return an error code...
		ic = offsetC;
		for ( j = 0; j < N; j++ ) {
			if ( C[ ic ] === 0.0 ) {
				return M + j + 1;
			}
			ic += strideC;
		}
	}
	// Invert the column scale factors...
	ic = offsetC;
	for ( j = 0; j < N; j++ ) {
		C[ ic ] = 1.0 / min( max( C[ ic ], SMLNUM ), BIGNUM );
		ic += strideC;
	}
	// Compute `COLCND = min(C)/max(C)`...
	out[ offsetOut + strideOut ] = max( rcmin, SMLNUM ) / min( rcmax, BIGNUM );
	return 0;
}
 
 
// EXPORTS //
 
module.exports = dgbequ;