All files / lapack/base/dlanv2/lib base.js

100% Statements 259/259
100% Branches 20/20
100% Functions 1/1
100% Lines 259/259

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 2603x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 3x 3x 24x 3x 3x 3x 3x 3x 3x 3x 21x 3x 3x 18x 15x 15x 15x 15x 15x 15x 15x 15x 3x 3x 3x 3x 3x 3x 3x 3x 15x 12x 12x 12x 18x 18x 18x 3x 3x 3x 3x 3x 18x 3x 3x 3x 3x 3x 18x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 3x 3x 3x 3x 3x 12x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 12x 12x 15x 24x 24x 24x 24x 21x 21x 24x 3x 3x 3x 24x 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 dlamch = require( '@stdlib/lapack/base/dlamch' );
var dlapy2 = require( '@stdlib/lapack/base/dlapy2' );
var int = require( '@stdlib/math/base/special/floor' );
var pow = require( '@stdlib/math/base/special/pow' );
var ln = require( '@stdlib/math/base/special/ln' );
var sign = require( '@stdlib/math/base/special/copysign' );
var max = require( '@stdlib/math/base/special/fast/max' );
var min = require( '@stdlib/math/base/special/fast/min' );
var abs = require( '@stdlib/math/base/special/fast/abs' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
 
 
// VARIABLES //
 
var safmin = dlamch( 'safe minimum' );
var eps = dlamch( 'precision' );
var safmin2 = pow( dlamch( 'base' ), int( ln( safmin / eps ) / ln( dlamch('base') ) / 2.0 ) );
var safmax2 = 1.0 / safmin2;
var multpl = 4.0;
 
 
// MAIN //
 
/**
* Computes the Schur factorization of a real 2-by-2 nonsymmetric matrix `A` in standard form.
*
* Given a real 2×2 matrix:
*
* ```tex
* \begin{bmatrix}
* A & B \\
* C & D
* \end{bmatrix}
* ```
*
* this routine computes an orthogonal matrix:
*
* ```tex
* \begin{bmatrix}
* \text{CS} & \text{SN} \\
* -\text{SN} & \text{CS}
* \end{bmatrix}
* ```
*
* such that the matrix is reduced to Schur (quasi-triangular) form:
*
* ```tex
* \begin{bmatrix}
* \text{AA} & \text{BB} \\
* 0 & \text{DD}
* \end{bmatrix}
* ```
*
* @private
* @param {Float64Array} A - array containing the element A(1,1)
* @param {NonNegativeInteger} offsetA - index in `A` of the element A(1,1)
* @param {Float64Array} B - array containing the element A(1,2)
* @param {NonNegativeInteger} offsetB - index in `B` of the element A(1,2)
* @param {Float64Array} C - array containing the element A(2,1)
* @param {NonNegativeInteger} offsetC - index in `C` of the element A(2,1)
* @param {Float64Array} D - array containing the element A(2,2)
* @param {NonNegativeInteger} offsetD - index in `D` of the element A(2,2)
* @param {Float64Array} RT1R - output array for the real part of the first eigenvalue
* @param {NonNegativeInteger} offsetRT1R - index in `RT1R` at which to store the value
* @param {Float64Array} RT1I - output array for the imaginary part of the first eigenvalue
* @param {NonNegativeInteger} offsetRT1I - index in `RT1I` at which to store the value
* @param {Float64Array} RT2R - output array for the real part of the second eigenvalue
* @param {NonNegativeInteger} offsetRT2R - index in `RT2R` at which to store the value
* @param {Float64Array} RT2I - output array for the imaginary part of the second eigenvalue
* @param {NonNegativeInteger} offsetRT2I - index in `RT2I` at which to store the value
* @param {Float64Array} CS - output array for cosine of the rotation
* @param {NonNegativeInteger} offsetCS - index in `CS` at which to store the value
* @param {Float64Array} SN - output array for sine of the rotation
* @param {NonNegativeInteger} offsetSN - index in `SN` at which to store the value
* @returns {void}
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( [ 4.0 ] );
* var B = new Float64Array( [ -5.0 ] );
* var C = new Float64Array( [ 2.0 ] );
* var D = new Float64Array( [ -3.0 ] );
* var RT1R = new Float64Array( 1 );
* var RT1I = new Float64Array( 1 );
* var RT2R = new Float64Array( 1 );
* var RT2I = new Float64Array( 1 );
* var CS = new Float64Array( 1 );
* var SN = new Float64Array( 1 );
*
* dlanv2( A, 0, B, 0, C, 0, D, 0, RT1R, 0, RT1I, 0, RT2R, 0, RT2I, 0, CS, 0, SN, 0 );
* // A => <Float64Array>[ 2.0 ]
* // B => <Float64Array>[ -7.0 ]
* // C => <Float64Array>[ 0.0 ]
* // D => <Float64Array>[ -1.0 ]
* // RT1R => <Float64Array>[ 2.0 ]
* // RT1I => <Float64Array>[ 0.0 ]
* // RT2R => <Float64Array>[ -1.0 ]
* // RT2I => <Float64Array>[ 0.0 ]
* // CS => <Float64Array>[ ~0.93 ]
* // SN => <Float64Array>[ ~0.34 ]
*/
function dlanv2( A, offsetA, B, offsetB, C, offsetC, D, offsetD, RT1R, offsetRT1R, RT1I, offsetRT1I, RT2R, offsetRT2R, RT2I, offsetRT2I, CS, offsetCS, SN, offsetSN ) {
	var bcmax;
	var bcmis;
	var sigma;
	var scale;
	var count;
	var temp;
	var tau;
	var sab;
	var sac;
	var sn1;
	var cs1;
	var aa;
	var bb;
	var cc;
	var dd;
	var p;
	var z;
 
	if ( C[ offsetC ] === 0.0 ) {
		CS[ offsetCS ] = 1.0;
		SN[ offsetSN ] = 0.0;
	} else if ( B[ offsetB ] === 0.0 ) {
		CS[ offsetCS ] = 0.0;
		SN[ offsetSN ] = 1.0;
		temp = D[ offsetD ];
		D[ offsetD ] = A[ offsetA ];
		A[ offsetA ] = temp;
		B[ offsetB ] = -C[ offsetC ];
		C[ offsetC ] = 0.0;
	} else if ( A[ offsetA ] === D[ offsetD ] && ( sign( 1.0, B[ offsetB ] ) !== sign( 1.0, C[ offsetC ] ) ) ) {
		CS[ offsetCS ] = 1.0;
		SN[ offsetSN ] = 0.0;
	} else {
		temp = A[ offsetA ] - D[ offsetD ];
		p = 0.5 * temp;
		bcmax = max( abs( B[ offsetB ] ), abs( C[ offsetC ] ) );
		bcmis = min( abs( B[ offsetB ] ), abs( C[ offsetC ] ) ) * sign( 1.0, B[ offsetB ] ) * sign( 1.0, C[ offsetC ] );
		scale = max( abs( p ), bcmax );
		z = ( ( p / scale ) * p ) + ( ( bcmax / scale ) * bcmis );
 
		if ( z > multpl * eps ) {
			z = p + sign( sqrt( scale ) * sqrt( z ), p );
			A[ offsetA ] = D[ offsetD ] + z;
			D[ offsetD ] -= ( bcmax / z ) * bcmis;
			tau = dlapy2( C[ offsetC ], z );
			CS[ offsetCS ] = z / tau;
			SN[ offsetSN ] = C[ offsetC ] / tau;
			B[ offsetB ] -= C[ offsetC ];
			C[ offsetC ] = 0.0;
		} else {
			count = 0;
			sigma = B[ offsetB ] + C[ offsetC ];
			while ( true ) {
				scale = max( abs( temp ), abs( sigma ) );
				if ( count <= 20 ) {
					if ( scale >= safmax2 ) {
						sigma *= safmin2;
						temp *= safmin2;
						count += 1;
						continue;
					}
					if ( scale <= safmin2 ) {
						sigma *= safmax2;
						temp *= safmax2;
						count += 1;
						continue;
					}
				}
				break;
			}
 
			p = 0.5 * temp;
			tau = dlapy2( sigma, temp );
 
			CS[ offsetCS ] = sqrt( 0.5 * ( 1.0 + ( abs( sigma ) / tau ) ) );
			SN[ offsetSN ] = -( p / ( tau * CS[ offsetCS ] ) ) * sign( 1.0, sigma );
 
			aa = ( A[ offsetA ] * CS[ offsetCS ] ) + ( B[ offsetB ] * SN[ offsetSN ] );
			bb = -( A[ offsetA ] * SN[ offsetSN ] ) + ( B[ offsetB ] * CS[ offsetCS ] );
			cc = ( C[ offsetC ] * CS[ offsetCS ] ) + ( D[ offsetD ] * SN[ offsetSN ] );
			dd = -( C[ offsetC ] * SN[ offsetSN ] ) + ( D[ offsetD ] * CS[ offsetCS ] );
 
			A[ offsetA ] = ( aa * CS[ offsetCS ] ) + ( cc * SN[ offsetSN ] );
			B[ offsetB ] = ( bb * CS[ offsetCS ] ) + ( dd * SN[ offsetSN ] );
			C[ offsetC ] = -( aa * SN[ offsetSN ] ) + ( cc * CS[ offsetCS ] );
			D[ offsetD ] = -( bb * SN[ offsetSN ] ) + ( dd * CS[ offsetCS ] );
 
			temp = 0.5 * ( A[ offsetA ] + D[ offsetD ] );
			A[ offsetA ] = temp;
			D[ offsetD ] = temp;
 
			if ( C[ offsetC ] !== 0.0 ) {
				if ( B[ offsetB ] === 0.0 ) {
					B[ offsetB ] -= C[ offsetC ];
					C[ offsetC ] = 0.0;
					temp = CS[ offsetCS ];
					CS[ offsetCS ] = -SN[ offsetSN ];
					SN[ offsetSN ] = temp;
				} else if ( sign( 1.0, B[ offsetB ] ) === sign( 1.0, C[ offsetC ] ) ) {
					sab = sqrt( abs( B[ offsetB ] ) );
					sac = sqrt( abs( C[ offsetC ] ) );
					p = sign( sab*sac, C[ offsetC ] );
					tau = 1.0 / sqrt( abs( B[ offsetB ]+C[ offsetC ] ) );
					A[ offsetA ] = temp + p;
					D[ offsetD ] = temp - p;
					B[ offsetB ] -= C[ offsetC ];
					C[ offsetC ] = 0.0;
					cs1 = sab*tau;
					sn1 = sac*tau;
					temp = ( CS[ offsetCS ] * cs1 ) - ( SN[ offsetSN ] * sn1 );
					SN[ offsetSN ] = ( CS[ offsetCS ] * sn1 ) + ( SN[ offsetSN ] * cs1 );
					CS[ offsetCS ] = temp;
				}
			}
		}
	}
	RT1R[ offsetRT1R ] = A[ offsetA ];
	RT2R[ offsetRT2R ] = D[ offsetD ];
 
	if ( C[ offsetC ] === 0.0 ) {
		RT1I[ offsetRT1I ] = 0.0;
		RT2I[ offsetRT2I ] = 0.0;
	} else {
		RT1I[ offsetRT1I ] = sqrt( abs( B[ offsetB ] * C[ offsetC ] ) );
		RT2I[ offsetRT2I ] = -RT1I[ offsetRT1I ];
	}
}
 
 
// EXPORTS //
 
module.exports = dlanv2;