All files base.js

100% Statements 166/166
100% Branches 47/47
100% Functions 1/1
100% Lines 166/166

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 1673x 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 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 4x 4x 73x 20x 20x 20x 14x 51x 124x 124x 40x 40x 124x 51x 20x 6x 24x 60x 60x 18x 18x 60x 24x 6x 20x 20x 73x 25x 25x 25x 2x 25x 23x 80x 80x 23x 25x 18x 63x 63x 87x 87x 87x 87x 63x 63x 18x 63x 63x 49x 49x 63x 25x 7x 25x 25x 36x 36x 36x 36x 25x 19x 19x 25x 7x 25x 25x 73x 23x 23x 23x 23x 19x 42x 42x 42x 42x 23x 4x 12x 12x 12x 12x 4x 23x 23x 23x 23x 1x 73x 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.
*/
 
/* eslint-disable max-len */
 
'use strict';
 
// MODULES //
 
var dlassq = require( '@stdlib/lapack/base/dlassq' ).ndarray;
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var Float64Array = require( '@stdlib/array/float64' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var abs = require( '@stdlib/math/base/special/abs' );
 
 
// VARIABLES //
 
var out = new Float64Array( 2 );
var work = new Float64Array( 0 );
 
 
// MAIN //
 
/**
* Returns the value of the one-norm, Frobenius norm, infinity-norm, or the largest absolute value of any element of a real symmetric matrix `A`.
*
* @private
* @param {string} norm - specifies the norm: 'M' (max abs), '1'/'O' (one-norm), 'I' (infinity-norm), 'F'/'E' (Frobenius)
* @param {string} uplo - specifies whether the upper or lower triangular part of `A` is referenced ('upper' or 'lower')
* @param {NonNegativeInteger} N - order of the matrix `A`
* @param {Float64Array} A - input matrix
* @param {integer} strideA1 - stride of the first dimension of `A`
* @param {integer} strideA2 - stride of the second dimension of `A`
* @param {NonNegativeInteger} offsetA - starting index for `A`
* @returns {number} matrix norm
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* // A = [ 1.0, 2.0, 2.0, 5.0 ] (2x2 symmetric, row-major)
* var A = new Float64Array( [ 1.0, 2.0, 2.0, 5.0 ] );
*
* var norm = dlansy( 'M', 'upper', 2, A, 2, 1, 0 );
* // returns 5.0
*/
function dlansy( norm, uplo, N, A, strideA1, strideA2, offsetA ) {
	var scale;
	var sumsq;
	var anorm;
	var absa;
	var temp;
	var i;
	var j;
 
	if ( N <= 0 ) {
		return 0.0;
	}
	if ( norm === 'M' || norm === 'm' ) {
		// Find max(abs(A(i,j)))
		anorm = 0.0;
		if ( uplo === 'upper' ) {
			for ( j = 0; j < N; j++ ) {
				for ( i = 0; i <= j; i++ ) {
					temp = abs( A[ offsetA + (i*strideA1) + (j*strideA2) ] );
					if ( anorm < temp || isnan( temp ) ) {
						anorm = temp;
					}
				}
			}
		} else {
			for ( j = 0; j < N; j++ ) {
				for ( i = j; i < N; i++ ) {
					temp = abs( A[ offsetA + (i*strideA1) + (j*strideA2) ] );
					if ( anorm < temp || isnan( temp ) ) {
						anorm = temp;
					}
				}
			}
		}
		return anorm;
	}
	if ( norm === 'I' || norm === 'i' || norm === 'O' || norm === 'o' || norm === '1' ) {
		// Find normI(A) ( = norm1(A), since A is symmetric)
		anorm = 0.0;
		if ( work.length < N ) {
			work = new Float64Array( N );
		} else {
			for ( i = 0; i < N; i++ ) {
				work[ i ] = 0.0;
			}
		}
		if ( uplo === 'upper' ) {
			for ( j = 0; j < N; j++ ) {
				temp = 0.0;
				for ( i = 0; i < j; i++ ) {
					absa = abs( A[ offsetA + (i*strideA1) + (j*strideA2) ] );
					temp += absa;
					work[ i ] += absa;
				}
				work[ j ] = temp + abs( A[ offsetA + (j*strideA1) + (j*strideA2) ] );
			}
			for ( i = 0; i < N; i++ ) {
				temp = work[ i ];
				if ( anorm < temp || isnan( temp ) ) {
					anorm = temp;
				}
			}
		} else {
			for ( j = 0; j < N; j++ ) {
				temp = work[ j ] + abs( A[ offsetA + (j*strideA1) + (j*strideA2) ] );
				for ( i = j + 1; i < N; i++ ) {
					absa = abs( A[ offsetA + (i*strideA1) + (j*strideA2) ] );
					temp += absa;
					work[ i ] += absa;
				}
				if ( anorm < temp || isnan( temp ) ) {
					anorm = temp;
				}
			}
		}
		return anorm;
	}
	if ( norm === 'F' || norm === 'f' || norm === 'E' || norm === 'e' ) {
		// Find normF(A) = Frobenius norm
		scale = 0.0;
		sumsq = 1.0;
		if ( uplo === 'upper' ) {
			for ( j = 1; j < N; j++ ) {
				dlassq( j, A, strideA1, offsetA + (j*strideA2), scale, sumsq, out, 1, 0 );
				scale = out[ 0 ];
				sumsq = out[ 1 ];
			}
		} else {
			for ( j = 0; j < N - 1; j++ ) {
				dlassq( N - j - 1, A, strideA1, offsetA + ((j+1)*strideA1) + (j*strideA2), scale, sumsq, out, 1, 0 );
				scale = out[ 0 ];
				sumsq = out[ 1 ];
			}
		}
		sumsq *= 2;
		dlassq( N, A, strideA1 + strideA2, offsetA, scale, sumsq, out, 1, 0 );
		return out[ 0 ] * sqrt( out[ 1 ] );
	}
	return 0.0;
}
 
 
// EXPORTS //
 
module.exports = dlansy;