All files base.js

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

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 1792x 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 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 6x 4x 6x 1x 1x 9x 1x 3x 1x 1x 9x 9x 9x 9x 9x 1x 1x 1x 9x 2x 2x 2x 6x 9x 3x 3x 3x 1x 1x 3x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 3x 1x 1x 3x 2x 2x 2x 3x 3x 2x 3x 3x 3x 3x 3x 3x 2x 2x 1x 1x 1x 2x 6x 6x 6x 9x 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 max = require( '@stdlib/math/base/special/max' );
var min = require( '@stdlib/math/base/special/min' );
var floor = require( '@stdlib/math/base/special/floor' );
var dorglq = require( './dorglq.js' );
var dorgqr = require( './dorgqr.js' );
 
 
// MAIN //
 
/**
* Generates one of the real orthogonal matrices `Q` or `P^T` determined by `DGEBRD` when reducing a real matrix `A` to bi-diagonal form: `A = Q * B * P^T`.
*
* ## Notes
*
* -   `Q` and `P^T` are defined as products of elementary reflectors `H(i)` or `G(i)` respectively.
*
* -   If `vect = 'Q'`,
*
*     -   A is assumed to have been an M-by-K matrix, and Q is of order M.
*     -   if m >= k, Q = H(1) H(2) . . . H(k) and DORGBR returns the first n columns of Q, where m >= n >= k;
*     -   if m < k, Q = H(1) H(2) . . . H(m-1) and DORGBR returns Q as an M-by-M matrix.
*
* -   If `vect = 'P'`,
*
*     -   A is assumed to have been a K-by-N matrix, and P**T is of order N
*     -   if k < n, P**T = G(k) . . . G(2) G(1) and DORGBR returns the first m rows of P**T, where n >= m >= k
*     -   if k >= n, P**T = G(n-1) . . . G(2) G(1) and DORGBR returns P**T as an N-by-N matrix.
*
* -   LWORK >= max(1,min(M,N)). For optimum performance LWORK >= min(M,N)*NB, where NB is the optimal blocksize.
*
* -   If LWORK = -1, then a workspace query is assumed; the routine only calculates the optimal size of the WORK array, returns this value as the first entry of the WORK array
*
* @private
* @param {string} vect - specifies whether the matrix Q or the matrix P**T is returned in `A`
* @param {NonNegativeInteger} M - number of rows of `Q` or `P^T`
* @param {NonNegativeInteger} N - number of columns of `Q` or `P^T`
* @param {NonNegativeInteger} K - number of elementary reflectors
* @param {Float64Array} A - input/output matrix
* @param {integer} strideA1 - stride length of the first dimension of `A`
* @param {integer} strideA2 - stride length of the second dimension of `A`
* @param {NonNegativeInteger} offsetA - starting index for `A`
* @param {Float64Array} TAU - scalar factors of elementary reflectors
* @param {integer} strideTAU - stride length of `TAU`
* @param {NonNegativeInteger} offsetTAU - starting index for `TAU`
* @param {Float64Array} WORK - workspace array
* @param {integer} strideWORK - stride length of `WORK`
* @param {NonNegativeInteger} offsetWORK - starting index for `WORK`
* @param {NonNegativeInteger} LWORK - dimension of the array `WORK`
* @returns {integer} status code
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( [ 1, 0, 0, -1, -2, 0 ] );
* var TAU = new Float64Array( [ 1, 1 ] );
* var WORK = new Float64Array( 10 );
*
* var info = dorgbr( `P`, 2, 3, 2, A, 1, 2, 0, TAU, 1, 0, WORK, 1, 0, 10 );
* // A => <Float64Array>[ 0, 0, 0, 0, 2, 0 ]
* // info => 0
* // WORK[ 0 ] => 96
*/
function dorgbr( vect, M, N, K, A, strideA1, strideA2, offsetA, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, LWORK ) {
	var lquery;
	var lwkopt;
	var wantq;
	var mn;
	var i;
	var j;
 
	wantq = ( vect === 'Q' );
	mn = min( M, N );
	lquery = ( LWORK === -1 );
 
	WORK[ offsetWORK ] = 1;
	if ( wantq ) {
		if ( M >= K ) {
			dorgqr( M, N, K, A, strideA1, strideA2, offsetA, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, -1 );
		} else if ( M > 1 ) {
			dorgqr( M - 1, M - 1, M - 1, A, strideA1, strideA2, offsetA, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, -1 );
		}
	} else if ( K < N ) {
		dorglq( M, N, K, A, strideA1, strideA2, offsetA, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, -1 );
	} else if ( N > 1 ) {
		dorglq( N - 1, N - 1, N - 1, A, strideA1, strideA2, offsetA, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, -1 );
	}
 
	lwkopt = floor( WORK[ offsetWORK ] );
	lwkopt = max( lwkopt, mn );
 
	if ( lquery ) {
		WORK[ offsetWORK ] = lwkopt;
		return 0;
	}
	if ( M === 0 || N === 0 ) {
		WORK[ offsetWORK ] = 1;
		return 0;
	}
 
	if ( wantq ) {
		// Form Q, determined by a call to DGEBRD to reduce an `M-by-K` matrix...
 
		if ( M >= K ) {
			// If M >= K, assume M >= N >= K:
			dorgqr( M, N, K, A, strideA1, strideA2, offsetA, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, LWORK );
		} else {
			// If M < K, assume M = N; Shift the vectors which define the elementary reflectors one column to the right, and set the first row and column of Q to those of the unit matrix...
			for ( j = M - 1; j >= 1; j-- ) {
				A[ offsetA + ( j * strideA2 ) ] = 0.0;
 
				for ( i = j + 1; i < M; i++ ) {
					A[ offsetA + ( i * strideA1 ) + ( j * strideA2 ) ] = A[ offsetA + ( i * strideA1 ) + ( ( j - 1 ) * strideA2 ) ];
				}
			}
			A[ offsetA ] = 1.0;
			for ( i = 1; i < M; i++ ) {
				A[ offsetA + ( i * strideA1 ) ] = 0.0;
			}
			if ( M > 1 ) {
				// Form Q(2:m,2:m):
				dorgqr( M - 1, M - 1, M - 1, A, strideA1, strideA2, offsetA + strideA1 + strideA2, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, LWORK );
			}
		}
	} else if ( K < N ) {
		// If k < n, assume k <= m <= n:
		dorglq( M, N, K, A, strideA1, strideA2, offsetA, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, LWORK );
	} else {
		// If k >= n, assume m = n; Shift the vectors which define the elementary reflectors one row downward, and set the first row and column of P**T to those of the unit matrix...
		A[ offsetA ] = 1.0;
		for ( i = 1; i < N; i++ ) {
			A[ offsetA + ( i * strideA1 ) ] = 0.0;
		}
		for ( j = 1; j < N; j++ ) {
			for ( i = j - 1; i >= 1; i-- ) {
				A[ offsetA + ( i * strideA1 ) + ( j * strideA2 ) ] = A[ offsetA + ( ( i - 1 ) * strideA1 ) + ( j * strideA2 ) ];
			}
			// A(0, j) = 0
			A[ offsetA + ( j * strideA2 ) ] = 0.0;
		}
 
		if ( N > 1 ) {
			// Form P**T(2:n,2:n)
			dorglq( N - 1, N - 1, N - 1, A, strideA1, strideA2, offsetA + strideA1 + strideA2, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, LWORK );
		}
	}
	WORK[ offsetWORK ] = lwkopt;
 
	return 0;
}
 
 
// EXPORTS //
 
module.exports = dorgbr;