All files ndarray.js

100% Statements 154/154
100% Branches 26/26
100% Functions 1/1
100% Lines 154/154

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 1553x 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 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 2x 2x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 17x 56x 56x 8x 8x 56x 24x 24x 24x 24x 24x 24x 56x 56x 4x 4x 56x 12x 12x 12x 52x 40x 40x 56x 56x 15x 17x 2x 2x 1x 1x 2x 2x 17x 3x 3x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 3x 1x 1x 1x 13x 10x 10x 10x 10x 15x 17x 3x 3x 3x 3x 3x  
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 f32 = require( '@stdlib/number/float64/base/to-float32' );
var FLOAT32_MAX = require( '@stdlib/constants/float32/max' );
var absf = require( '@stdlib/math/base/special/absf' );
var abs2f = require( '@stdlib/math/base/special/abs2f' );
var sqrtf = require( '@stdlib/math/base/special/sqrtf' );
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex64' );
 
 
// VARIABLES //
 
// Blue's scaling constants:
var tsml = 1.08420217E-19;
var tbig = 4.50359963E+15;
var ssml = 3.77789319E+22;
var sbig = 1.32348898E-23;
 
 
// MAIN //
 
/**
* Computes the L2-norm of a single-precision complex floating-point vector.
*
* @param {PositiveInteger} N - number of indexed elements
* @param {Complex64Array} x - input array
* @param {integer} strideX - `x` stride length
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @returns {number} L2-norm
*
* @example
* var Complex64Array = require( '@stdlib/array/complex64' );
*
* var x = new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2, 2.0, 3.0 ] );
*
* var norm = scnrm2( 4, x, 1, 0 );
* // returns ~0.8
*/
function scnrm2( N, x, strideX, offsetX ) {
	var notbig;
	var sumsq;
	var viewX;
	var abig;
	var amed;
	var asml;
	var ymax;
	var ymin;
	var scl;
	var ax;
	var ix;
	var sx;
	var i;
 
	if ( N <= 0 ) {
		return 0.0;
	}
	// Reinterpret the input array as a real-valued array comprised of interleaved real and imaginary components:
	viewX = reinterpret( x, 0 );
	sx = strideX * 2;
	ix = offsetX * 2;
 
	// Initialize loop values for accumulation:
	notbig = true;
 
	sumsq = 0.0;
	abig = 0.0;
	amed = 0.0;
	asml = 0.0;
	scl = 1.0;
 
	// Compute the sum of squares using 3 accumulators--`abig` (sum of squares scaled down to avoid overflow), `asml` (sum of squares scaled up to avoid underflow), `amed` (sum of squares that do not require scaling)--and thresholds and multipliers--`tbig` (values bigger than this are scaled down by `sbig`) and `tsml` (values smaller than this are scaled up by `ssml`)...
	for ( i = 0; i < N; i++ ) {
		ax = absf( viewX[ ix ] );
		if ( ax > tbig ) {
			abig = f32( abig + abs2f( ax * sbig ) );
			notbig = false;
		} else if ( ax < tsml ) {
			if ( notbig ) {
				asml = f32( asml + abs2f( ax * ssml ) );
			}
		} else {
			amed = f32( amed + f32( ax * ax ) );
		}
		ax = absf( viewX[ ix + 1 ] );
		if ( ax > tbig ) {
			abig = f32( abig + abs2f( ax * sbig ) );
			notbig = false;
		} else if ( ax < tsml ) {
			if ( notbig ) {
				asml = f32( asml + abs2f( ax * ssml ) );
			}
		} else {
			amed = f32( amed + f32( ax * ax ) );
		}
		ix += sx;
	}
	// Combine `abig` and `amed` or `amed` and `asml` if more than one accumulator was used...
	if ( abig > 0.0 ) {
		// Combine `abig` and `amed` if `abig` > 0...
		if ( amed > 0.0 || ( amed > FLOAT32_MAX ) || ( amed !== amed ) ) {
			abig = f32( abig + f32( f32( amed * sbig ) * sbig ) );
		}
		scl = f32( 1.0 / sbig );
		sumsq = abig;
	} else if ( asml > 0.0 ) {
		// Combine `amed` and `asml` if `asml` > 0...
		if ( amed > 0.0 || amed > FLOAT32_MAX || ( amed !== amed ) ) {
			amed = sqrtf( amed );
			asml = f32( sqrtf( asml ) / ssml );
			if ( asml > amed ) {
				ymin = amed;
				ymax = asml;
			} else {
				ymin = asml;
				ymax = amed;
			}
			scl = 1.0;
			sumsq = f32( f32( ymax * ymax ) * f32( 1.0 + abs2f( ymin / ymax ) ) ); // eslint-disable-line max-len
		} else {
			scl = f32( 1.0 / ssml );
			sumsq = asml;
		}
	} else {
		// All values are mid-range...
		scl = 1.0;
		sumsq = amed;
	}
	return f32( sqrtf( sumsq ) * scl );
}
 
 
// EXPORTS //
 
module.exports = scnrm2;