All files assign.js

96.73% Statements 148/153
90.9% Branches 20/22
100% Functions 1/1
96.73% Lines 148/153

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 1543x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 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 38x 38x 4x 4x 4x 4x 4x 4x 4x 38x 2x 2x 2x 2x 2x 2x 2x 32x 32x 32x 38x 4x 4x 38x 6x 6x 2x 2x 2x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 28x 22x 22x 22x 22x 22x 12x 12x 12x 12x 12x 12x 12x 22x 10x 10x 10x       10x     10x 10x 10x 10x 22x 32x 32x 32x 32x 32x 32x 32x 32x 32x 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 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 cdiv = require( '@stdlib/complex/float64/base/div' );
var conj = require( '@stdlib/complex/float64/conj' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
 
 
// MAIN //
 
/**
* Constructs a Givens plane rotation.
*
* @param {Complex128} za - rotational elimination parameter
* @param {Complex128} zb - rotational elimination parameter
* @param {Float64Array} out - output array
* @param {integer} stride - index increment
* @param {NonNegativeInteger} offset - starting index
* @returns {Float64Array} output array
*
* @example
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var za = new Complex128( 5.0, 6.0 );
* // returns <Complex128>
*
* var zb = new Complex128( 0.0, 0.0 );
* // returns <Complex128>
*
* var out = zrotg( za, zb, new Float64Array( 5 ), 1, 0 );
* // returns <Float64Array>[ 5.0, 6.0, 1.0, 0.0, 0.0 ]
*/
function zrotg(za, zb, out, stride, offset) {
	var scalar;
	var scale;
	var temp;
	var za2;
	var zb2;
	var c2;
	var c;
	var s;
	var r;
	var d;
 
	// Handle NaN inputs - if either input is NaN (not a Complex128), return array of NaNs
	if (typeof za === 'number' && isnan(za)) {
		out[0] = NaN;
		out[1] = NaN;
		out[2] = NaN;
		out[3] = NaN;
		out[4] = NaN;
		return out;
	}
	if (typeof zb === 'number' && isnan(zb)) {
		out[0] = NaN;
		out[1] = NaN;
		out[2] = NaN;
		out[3] = NaN;
		out[4] = NaN;
		return out;
	}
 
	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) {
			d = abs(imag(zb));
			r = new Complex128(d, 0.0);
			s = cdiv(conj(zb), new Complex128(d, 0.0));
		} else if (imag(zb) === 0) {
			d = abs(real(zb));
			r = new Complex128(d, 0.0);
			s = cdiv(conj(zb), new Complex128(d, 0.0));
		} else {
			zb2 = cabs2(zb);
			d = sqrt(zb2);
			r = new Complex128(d, 0.0);
			s = cdiv(conj(zb), new Complex128(d, 0.0));
		}
	} 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(c2);
 
			temp = new Complex128(real(za) / sqrt(za2), imag(za) / sqrt(za2));
			temp = new Complex128(real(temp) * scale, imag(temp) * 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[offset] = real(za);
	out[offset + stride] = imag(za);
	out[offset + (2 * stride)] = c;
	out[offset + (3 * stride)] = real(s);
	out[offset + (4 * stride)] = imag(s);
	return out;
}
 
 
// EXPORTS //
 
module.exports = zrotg;