All files base.js

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

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 1983x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 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 6x 6x 6x 6x 6x 32x 32x 38x 86x 86x 86x 32x 32x 38x 88x 88x 88x 247x 247x 247x 247x 88x 88x 32x 32x 32x 32x 38x 86x 86x 86x 86x 32x 32x 38x 4x 4x 4x 7x 4x 4x 3x 3x 38x 28x 28x 28x 76x 76x 76x 28x 28x 28x 28x 28x 38x 81x 81x 81x 28x 28x 28x 38x 81x 81x 81x 228x 228x 228x 228x 81x 81x 81x 28x 28x 28x 28x 38x 81x 81x 81x 81x 38x 3x 3x 3x 6x 3x 3x 3x 3x 38x 25x 25x 25x 72x 72x 72x 25x 25x 25x 25x 25x 25x 38x 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 matrix `A` and reduce its condition number.
*
* ## Notes
*
* -   The row scale factors are stored in `R` (which should have `M` indexed elements) and the column scale factors are stored in `C` (which should have `N` indexed elements).
* -   If the status code is greater than `M`, `R`, `ROWCND`, and `AMAX` contain valid values, but `C` and `COLCND` do not. If the status code is greater than zero and less than or equal to `M`, only `AMAX` contains a valid value. Elements which do not contain valid values are left unmodified.
*
* @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 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 - 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, 2.0, 3.0, 4.0 ] );
* var R = new Float64Array( 2 );
* var C = new Float64Array( 2 );
* var out = new Float64Array( 3 );
*
* dgeequ( 2, 2, A, 2, 1, 0, R, 1, 0, C, 1, 0, out, 1, 0 );
* // R => <Float64Array>[ 0.5, 0.25 ]
* // C => <Float64Array>[ ~1.333, 1.0 ]
* // out => <Float64Array>[ 0.5, 0.75, 4.0 ]
*/
function dgeequ( 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;
	}
	// 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...
	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;
		}
		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 = dgeequ;