All files / blas/base/zrotg/lib main.js

82.3% Statements 107/130
50% Branches 5/10
100% Functions 1/1
82.3% Lines 107/130

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 1311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x                           3x 3x 3x 3x 3x 3x           3x 3x 3x 3x       3x     3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x  
/**
* @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.
*/
 
'use strict';
 
// MODULES //
 
var Float64Array = require( '@stdlib/array/float64' );
var abs = require( '@stdlib/math/base/special/abs' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var cabs2 = require( '@stdlib/math/base/special/cabs2' );
var EPS = require( '@stdlib/constants/float64/eps' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );
var cmul = require( '@stdlib/complex/float64/base/mul' );
var conj = require( '@stdlib/complex/float64/conj' );
 
 
// MAIN //
 
/**
* Constructs a Givens plane rotation.
*
* @param {Complex128} za - rotational elimination parameter
* @param {Complex128} zb - rotational elimination parameter
* @returns {Float64Array} output array
*
* @example
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var za = new Complex128( 4.0, 3.0 );
* // returns <Complex128>
*
* var zb = new Complex128( 0.0, 0.0 );
* // returns <Complex128>
*
* var out = zrotg( za, zb );
* // out => <Float64Array>[ 4.0, 3.0, 1.0, 0.0, 0.0 ]
*/
function zrotg( za, zb ) {
	var scalar;
	var scale;
	var temp;
	var za2;
	var zb2;
	var out;
	var c2;
	var c;
	var s;
	var r;
	var d;
 
	out = new Float64Array( 5 );
 
	s = new Complex128( 0.0, 0.0 );
	r = new Complex128( 0.0, 0.0 );
	if ( real( zb ) === 0.0 && imag( zb ) === 0.0 ) {
		c = 1.0;
		r = za;
	} else if ( real( za ) === 0.0 && imag( za ) === 0.0 ) {
		c = 0;
		if ( real( zb ) === 0 ) {
			r = abs( imag( zb ) );
			s = conj( zb ) / r;
		} else if ( imag( zb ) === 0 ) {
			r = abs( real( zb ) );
			s = conj( zb ) / r;
		} else {
			zb2 = cabs2( zb );
			d = sqrt( zb2 );
			s = conj( zb ) / d;
			r = d;
		}
	} else {
		za2 = cabs2( za );
		zb2 = cabs2( zb );
		c2 = za2 + zb2;
 
		if ( za2 >= c2 * EPS ) {
			c = sqrt( za2 / c2);
			r = new Complex128( real( za ) / c, imag( za ) / c );
			scale = 1 / sqrt(za2 * c2);
			temp = new Complex128(real(za) * scale, imag(za) * scale);
			s = cmul(conj(zb), temp);
		} else {
			d = sqrt( za2 * c2 );
			c = za2 / d;
			while ( c < EPS ) {
				d *= 2;
				c = za2 / d;
			}
			while ( c === 0.0 ) {
				c = 1;
			}
			r = new Complex128( real( za ) / c, imag( za ) / c );
			scalar = new Complex128( 1 / d, 0.0 );
			s = cmul( conj( zb ), cmul( za, scalar ) );
		}
	}
 
	za = r;
 
	out[ 0 ] = real( za );
	out[ 1 ] = imag( za );
	out[ 2 ] = c;
	out[ 3 ] = real( s );
	out[ 4 ] = imag( s );
	return out;
}
 
 
// EXPORTS //
 
module.exports = zrotg;