All files main.js

98.47% Statements 129/131
93.75% Branches 15/16
100% Functions 1/1
98.47% Lines 129/131

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 1321x 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 1x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 22x 22x 41x 41x 41x 41x 41x 41x 41x 63x     41x 63x 34x 34x 24x 24x 63x 7x 7x 17x 17x 17x 63x 8x 8x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 63x 6x 6x 6x 6x 3x 3x 3x 3x 63x 12x 12x 12x 12x 3x 63x 1x 1x 1x 1x 1x  
/**
* @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 isFloat32ndarrayLike = require( '@stdlib/assert/is-float32ndarray-like' );
var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive;
var without = require( '@stdlib/array/base/without' );
var numel = require( '@stdlib/ndarray/base/numel' );
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' );
var nditerStacks = require( '@stdlib/ndarray/iter/stacks' );
var empty = require( '@stdlib/ndarray/empty' );
var base = require( '@stdlib/blas/base/snrm2' ).ndarray;
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Computes the L2-norm of a single-precision floating-point vector.
*
* @param {ndarrayLike} x - input array
* @param {NegativeInteger} [dim=-1] - dimension for which to compute the L2-norm
* @throws {TypeError} first argument must be a ndarray containing single-precision floating-point numbers
* @throws {TypeError} first argument must have at least one dimension
* @throws {TypeError} second argument must be a negative integer
* @throws {RangeError} second argument is out-of-bounds
* @returns {ndarray} ndarray containing the L2-norm
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( new Float32Array( [ 1.0, -2.0, 2.0 ] ) );
*
* var y = snrm2( x );
* // returns <ndarray>[ 3.0 ]
*/
function snrm2( x ) {
	var dim;
	var xsh;
	var osh;
	var xit;
	var out;
	var xc;
	var vx;
	var dm;
	var S;
	var v;
	var i;
 
	if ( !isFloat32ndarrayLike( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an ndarray containing single-precision floating-point numbers. Value: `%s`.', x ) );
	}
	// Convert the input array to a "base" ndarray:
	xc = ndarraylike2ndarray( x );
 
	// Resolve the input array shape:
	xsh = xc.shape;
 
	// Validate that we've been provided a non-zero-dimensional array...
	if ( xsh.length < 1 ) {
		throw new TypeError( format( 'invalid argument. First argument must have at least one dimension.' ) );
	}
	// Validate that the dimension argument is a negative integer...
	if ( arguments.length > 1 ) {
		dim = arguments[ 1 ];
		if ( !isNegativeInteger( dim ) ) {
			throw new TypeError( format( 'invalid argument. Second argument must be a negative integer. Value: `%s`.', dim ) );
		}
	} else {
		dim = -1;
	}
	// Validate that a provided dimension index is within bounds...
	dm = xsh.length - 1;
	dim = normalizeIndex( dim, dm );
	if ( dim === -1 ) {
		throw new RangeError( format( 'invalid argument. Second argument must be a value on the interval: [%d,%d]. Value: `%d`.', -( dm+1 ), -1, arguments[ 1 ] ) );
	}
	// Resolve the size of the dimension over which to compute the L2-norm:
	S = xsh[ dim ];
 
	// Resolve the output array shape by excluding the reduced dimension:
	osh = without( xc.shape, dim );
 
	// Allocate an empty output array:
	out = empty( osh, {
		'dtype': xc.dtype,
		'order': xc.order
	});
 
	// If we are only provided a one-dimensional input array, we can skip iterating over stacks...
	if ( osh.length === 0 ) {
		v = base( S, xc.data, xc.strides[0], xc.offset );
		out.iset( v );
		return out;
	}
	// Create an iterator for iterating over stacks of vectors:
	xit = nditerStacks( xc, [ dim ] );
 
	// Compute the L2-norm for each vector...
	for ( i = 0; i < numel( osh ); i++ ) {
		vx = xit.next().value;
		v = base( S, vx.data, vx.strides[0], vx.offset );
		out.iset( i, v );
	}
	return out;
}
 
 
// EXPORTS //
 
module.exports = snrm2;