All files base.js

100% Statements 200/200
100% Branches 22/22
100% Functions 1/1
100% Lines 200/200

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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 2012x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 8x 7x 7x 7x 7x 8x 8x 8x 8x 8x 8x 1x 1x 7x 8x 1x 1x 6x 6x 8x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 8x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 1x 1x 1x 1x 1x 2x 4x 8x 1x 1x 5x 8x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 34x 34x 34x 3x 1x 32x 32x 32x 1x 3x 5x 5x 5x 5x 5x 5x 8x 2x 2x 2x 2x 2x  
/**
* @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, max-params */
 
'use strict';
 
// MODULES //
 
var dgemm = require( '@stdlib/blas/base/dgemm' ).ndarray;
var min = require( '@stdlib/math/base/special/min' );
var max = require( '@stdlib/math/base/special/max' );
var floor = require( '@stdlib/math/base/special/floor' );
var dgebd2 = require( './dgebd2.js' );
var dlabrd = require( './dlabrd.js' );
 
 
// MAIN //
 
/**
* Reduces a general real `M-by-N` matrix `A` to upper or lower bi-diagonal form `B` by an orthogonal transformation `Q**T * A * P = B`.
*
* ## Notes
*
* -   If `M >= N`,
*
*     -   `B` is upper bi-diagonal form.
*     -   The diagonal and the first superdiagonal are overwritten with the upper bi-diagonal matrix `B`.
*     -   Elements below the diagonal, with the array `TAUQ`, represent the orthogonal matrix `Q` as a product of elementary reflectors.
*     -   Elements above the first super-diagonal, with the array `TAUP`, represent the orthogonal matrix `P` as a product of elementary reflectors.
*
* -   If `M < N`,
*
*     -   `B` is lower bi-diagonal form.
*     -   The diagonal and the first sub-diagonal are overwritten with the lower bi-diagonal matrix `B`.
*     -   Elements below the first sub-diagonal, with the array `TAUQ`, represent the orthogonal matrix `Q` as a product of elementary reflectors.
*     -   Elements on and above the diagonal, with the array `TAUP`, represent the orthogonal matrix `P` as a product of elementary reflectors.
*
* @private
* @param {NonNegativeInteger} M - number of rows in `A`
* @param {NonNegativeInteger} N - number of columns in `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`
* @param {Float64Array} D - diagonal elements of the bi-diagonal matrix `B` (length `min(M,N)`)
* @param {integer} strideD - stride length for `D`
* @param {NonNegativeInteger} offsetD - starting index for `D`
* @param {Float64Array} E - off-diagonal elements of the bi-diagonal matrix `B` (length `min(M,N)-1`)
* @param {integer} strideE - stride length for `E`
* @param {NonNegativeInteger} offsetE - starting index for `E`
* @param {Float64Array} TAUQ - scalars factors of the elementary reflectors that represent the orthogonal matrix `Q` (length `NB`)
* @param {integer} strideTAUQ - stride length for `TAUQ`
* @param {NonNegativeInteger} offsetTAUQ - starting index for `TAUQ`
* @param {Float64Array} TAUP - scalars factors of the elementary reflectors that represent the orthogonal matrix `P` (length `NB`)
* @param {integer} strideTAUP - stride length for `TAUP`
* @param {NonNegativeInteger} offsetTAUP - starting index for `TAUP`
* @param {Float64Array} WORK - workspace array (length >= `max(1,LWORK)`)
* @param {integer} strideWORK - stride length of `WORK`
* @param {NonNegativeInteger} offsetWORK - starting index for `WORK`
* @param {integer} LWORK - length of WORK array
* @returns {integer} status code
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( [ 4.0, -1.0, 2.5, 7.0, 8.5, -2.0 ] );
* var D = new Float64Array( [ 0.0, 0.0 ] );
* var E = new Float64Array( [ 0.0 ] );
* var TAUQ = new Float64Array( [ 0.0, 0.0 ] );
* var TAUP = new Float64Array( [ 0.0, 0.0 ] );
* var WORK = new Float64Array( 100 );
*
* dgebrd( 3, 2, A, 1, 3, 0, D, 1, 0, E, 1, 0, TAUQ, 1, 0, TAUP, 1, 0, WORK, 1, 0, 100 );
* // A => <Float64Array>[ ~-4.822, ~-0.113, ~0.283, ~-3.007, ~-10.78, ~-0.237 ]
* // D => <Float64Array>[ ~-4.822, ~-10.78 ]
* // E => <Float64Array>[ ~-3.007 ]
* // TAUQ => <Float64Array>[ ~1.83, ~1.894 ]
* // TAUP => <Float64Array>[ 0.0, 0.0 ]
* // WORK[ 0 ] => 3.0
*/
function dgebrd( M, N, A, strideA1, strideA2, offsetA, D, strideD, offsetD, E, strideE, offsetE, TAUQ, strideTAUQ, offsetTAUQ, TAUP, strideTAUP, offsetTAUP, WORK, strideWORK, offsetWORK, LWORK ) { // eslint-disable-line stdlib/jsdoc-doctest-decimal-point
	var ldwrkx;
	var ldwrky;
	var lwkmin;
	var lwkopt;
	var lquery;
	var minmn;
	var nbmin;
	var nb;
	var nx;
	var ws;
	var i;
	var j;
 
	minmn = min( M, N );
	if ( minmn === 0 ) {
		lwkmin = 1;
		lwkopt = 1;
	} else {
		lwkmin = max( M, N );
		nb = 32; // The optimal blocksize derived from the Fortran LAPACK call `ilaenv( 1, 'DGEBRD', ' ', m, n, -1, -1 )`
		lwkopt = ( M + N ) * nb;
	}
 
	WORK[ offsetWORK ] = lwkopt;
 
	lquery = ( LWORK === -1 );
 
	if ( ( LWORK < lwkmin ) && !lquery ) {
		return -10;
	}
 
	if ( lquery ) {
		return 0;
	}
 
	// Quick return if possible
	if ( minmn === 0 ) {
		WORK[ offsetWORK ] = 1;
		return 0;
	}
 
	// Determine the block size
	ldwrkx = M;
	ldwrky = N;
	ws = max( M, N );
	nx = minmn;
 
	if ( nb > 1 && nb < minmn ) {
		// Set the crossover point NX
		nx = max( nb, 128 ); // The crossover point derived from the Fortran LAPACK call `ilaenv( 3, 'DGEBRD', ' ', m, n, -1, -1 )`
 
		// Determine when to switch from blocked to unblocked code
		if ( nx < minmn ) {
			ws = lwkopt;
			if ( LWORK < ws ) {
				// Not enough workspace for optimal NB,consider using a smaller block size
				nbmin = 2; // The minimum block size derived from the Fortran LAPACK call `ilaenv( 2, 'DGEBRD', ' ', m, n, -1, -1 )`
				if ( LWORK >= ( M + N ) * nbmin ) {
					nb = floor( LWORK / ( M + N ) );
				} else {
					nb = 1;
					nx = minmn;
				}
			}
		}
	} else {
		nx = minmn;
	}
 
	for ( i = 0; i < minmn - nx; i += nb ) {
		// Reduce rows and columns i:i+nb-1 to bi-diagonal form and return the matrices X and Y which are needed to update the unreduced part of the matrix.
		dlabrd( M - i, N - i, nb, A, strideA1, strideA2, offsetA + ( i * strideA1 ) + ( i * strideA2 ), D, strideD, offsetD + ( i * strideD ), E, strideE, offsetE + ( i * strideE ), TAUQ, strideTAUQ, offsetTAUQ + ( i * strideTAUQ ), TAUP, strideTAUP, offsetTAUP + ( i * strideTAUP ), WORK, strideWORK, ldwrkx, offsetWORK, WORK, strideWORK, ldwrky, offsetWORK + ( ldwrkx*nb ) );
 
		//  Update the trailing submatrix A(i+nb:m,i+nb:n), using an update of the form  A := A - V*Y**T - X*U**T
		dgemm('no-transpose', 'transpose', M - i - nb, N - i - nb, nb, -1.0, A, strideA1, strideA2, offsetA + ( ( i + nb ) * strideA1 ) + ( i * strideA2 ), WORK, strideWORK, ldwrky, offsetWORK + ( ldwrkx * nb ) + nb, 1.0, A, strideA1, strideA2, offsetA + ( ( i + nb ) * strideA1 ) + ( ( i + nb ) * strideA2 ) );
 
		dgemm('no-transpose', 'no-transpose', M - i - nb, N - i - nb, nb, -1.0, WORK, strideWORK, ldwrkx, offsetWORK + nb, A, strideA1, strideA2, offsetA + ( i * strideA1 ) + (( i + nb ) * strideA2), 1.0, A, strideA1, strideA2, offsetA + ( ( i + nb ) * strideA1 ) + ( ( i + nb ) * strideA2 ) );
 
		// Copy diagonal and off-diagonal elements of B back into A
		if ( M >= N ) {
			for ( j = i; j < i + nb; j++ ) {
				A[ offsetA + ( j * strideA1 ) + ( j * strideA2 ) ] = D[ offsetD + ( j * strideD ) ];
				A[ offsetA + ( j * strideA1 ) + ( ( j + 1 ) * strideA2 ) ] = E[ offsetE + ( j * strideE ) ];
			}
		} else {
			for ( j = i; j < i + nb; j++ ) {
				A[ offsetA + ( j * strideA1 ) + ( j * strideA2 ) ] = D[ offsetD + ( j * strideD ) ];
				A[ offsetA + ( ( j + 1 ) * strideA1 ) + ( j * strideA2 ) ] = E[ offsetE + ( j * strideE ) ];
			}
		}
	}
 
	// Use unblocked code to reduce the remainder of the matrix
	dgebd2( M - i, N - i, A, strideA1, strideA2, offsetA + ( i * strideA1 ) + ( i * strideA2 ), D, strideD, offsetD + ( i * strideD ), E, strideE, offsetE + ( i * strideE ), TAUQ, strideTAUQ, offsetTAUQ + ( i * strideTAUQ ), TAUP, strideTAUP, offsetTAUP + ( i * strideTAUP ), WORK, strideWORK, offsetWORK );
	WORK[ offsetWORK ] = ws;
 
	return 0;
}
 
 
// EXPORTS //
 
module.exports = dgebrd;