All files base.js

100% Statements 209/209
100% Branches 33/33
100% Functions 1/1
100% Lines 209/209

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 2103x 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 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 4x 4x 4x 4x 4x 25x 25x 29x 88x 88x 88x 25x 25x 29x 92x 92x 92x 328x 328x 328x 328x 92x 92x 25x 25x 29x 88x 84x 84x 88x 88x 25x 25x 25x 25x 29x 88x 88x 88x 88x 25x 25x 29x 4x 4x 4x 8x 4x 4x 4x 4x 29x 21x 21x 21x 76x 76x 76x 21x 21x 21x 21x 21x 29x 80x 80x 80x 21x 21x 21x 21x 29x 80x 80x 80x 292x 292x 292x 292x 80x 76x 76x 80x 80x 80x 21x 21x 21x 21x 29x 80x 80x 80x 80x 29x 4x 4x 4x 8x 4x 4x 4x 4x 29x 17x 17x 17x 68x 68x 68x 17x 17x 17x 17x 17x 17x 29x 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 dlamch = require( '@stdlib/lapack/base/dlamch' );
var abs = require( '@stdlib/math/base/special/abs' );
var pow = require( '@stdlib/math/base/special/pow' );
var ln = require( '@stdlib/math/base/special/ln' );
var trunc = require( '@stdlib/math/base/special/trunc' );
var max = require( '@stdlib/math/base/special/fast/max' );
var min = require( '@stdlib/math/base/special/fast/min' );
 
 
// VARIABLES //
 
var SMLNUM = dlamch( 'S' );
var BIGNUM = 1.0 / SMLNUM;
var RADIX = dlamch( 'B' );
var LOGRDX = ln( RADIX );
 
 
// MAIN //
 
/**
* Computes row and column scale factors intended to equilibrate a matrix `A` and reduce its condition number (using the base of the machine as the scaling restriction).
*
* @private
* @param {NonNegativeInteger} M - number of rows in `A`
* @param {NonNegativeInteger} N - number of columns in `A`
* @param {Float64Array} A - input matrix
* @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} R - output vector for the row scale factors
* @param {integer} strideR - stride length for `R`
* @param {NonNegativeInteger} offsetR - starting index for `R`
* @param {Float64Array} C - output vector for the column scale factors
* @param {integer} strideC - stride length for `C`
* @param {NonNegativeInteger} offsetC - starting index for `C`
* @param {Float64Array} out - three element output array whose elements are set to `[ 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' );
*
* var A = new Float64Array( [ 1.0, 0.0, 0.0, 4.0 ] );
* var R = new Float64Array( 2 );
* var C = new Float64Array( 2 );
* var out = new Float64Array( 3 );
*
* dgeequb( 2, 2, A, 2, 1, 0, R, 1, 0, C, 1, 0, out, 1, 0 );
* // R => <Float64Array>[ 1.0, 0.25 ]
* // C => <Float64Array>[ 1.0, 1.0 ]
* // out => <Float64Array>[ 0.25, 1.0, 4.0 ]
*/
function dgeequb( M, N, A, strideA1, strideA2, offsetA, R, strideR, offsetR, C, strideC, offsetC, out, strideOut, offsetOut ) { // eslint-disable-line stdlib/jsdoc-doctest-decimal-point, max-len, max-params
	var rcmin;
	var rcmax;
	var ia;
	var ja;
	var ir;
	var ic;
	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...
	ja = offsetA;
	for ( j = 0; j < N; j++ ) {
		ia = ja;
		ir = offsetR;
		for ( i = 0; i < M; i++ ) {
			R[ ir ] = max( R[ ir ], abs( A[ ia ] ) );
			ia += strideA1;
			ir += strideR;
		}
		ja += strideA2;
	}
	// Restrict each row scale factor to a power of the radix...
	ir = offsetR;
	for ( i = 0; i < M; i++ ) {
		if ( R[ ir ] > 0.0 ) {
			R[ ir ] = pow( RADIX, trunc( ln( R[ ir ] ) / LOGRDX ) );
		}
		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;
		}
	} else {
		// 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...
	// Restrict each column scale factor to a power of the radix...
	ja = offsetA;
	ic = offsetC;
	for ( j = 0; j < N; j++ ) {
		ia = ja;
		ir = offsetR;
		for ( i = 0; i < M; i++ ) {
			C[ ic ] = max( C[ ic ], abs( A[ ia ] ) * R[ ir ] );
			ia += strideA1;
			ir += strideR;
		}
		if ( C[ ic ] > 0.0 ) {
			C[ ic ] = pow( RADIX, trunc( ln( C[ ic ] ) / LOGRDX ) );
		}
		ja += strideA2;
		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;
		}
	} else {
		// 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)`...
		rcmin = max( rcmin, SMLNUM );
		rcmax = min( rcmax, BIGNUM );
		out[ offsetOut + strideOut ] = rcmin / rcmax;
	}
	return 0;
}
 
 
// EXPORTS //
 
module.exports = dgeequb;