All files base.js

100% Statements 168/168
100% Branches 14/14
100% Functions 1/1
100% Lines 168/168

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 1692x 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 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 1x 1x 1x 1x 2x 2x 2x 4x 4x 6x 1x 1x 1x 3x 3x 3x 3x 3x 6x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 3x 3x 3x 3x 3x 3x 3x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 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.
*/
 
'use strict';
 
// MODULES //
 
var min = require( '@stdlib/math/base/special/min' );
var floor = require( '@stdlib/math/base/special/floor' );
var dgeqr2 = require( './dgeqr2.js' );
var dlarfb = require( './dlarfb.js' );
var dlarft = require( './dlarft.js' );
 
 
// MAIN //
 
/**
* Compute a QR factorization of a real M-by-N matrix `A`.
*
* ## Notes
*
* -   On exit, the elements on and above the diagonal of the array contain the `min(M,N)-by-N` upper trapezoidal matrix `R` (`R` is upper triangular if `m >= n`).
* -   On exit, the elements below the diagonal, with the array `TAU`, represent the orthogonal matrix `Q` as a product of `min(m,n)` elementary reflectors.
* -   `LWORK >= 1`, if `MIN(M,N) = 0`, and `LWORK >= N`, otherwise. For optimum performance `LWORK >= 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 {NonNegativeInteger} M - number of rows in `A`
* @param {NonNegativeInteger} N - number of columns in `A`
* @param {Float64Array} A - input/output 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} TAU - output array of scalar factors (length `min(M,N)`)
* @param {integer} strideTAU - stride for TAU
* @param {NonNegativeInteger} offsetTAU - starting index for TAU
* @param {Float64Array} WORK - workspace array (length >= `M`)
* @param {integer} strideWORK - stride for `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, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12 ] );
* var TAU = new Float64Array( 3 );
* var WORK = new Float64Array( 4 );
*
* dgeqrf( 3, 4, A, 1, 3, 0, TAU, 1, 0, WORK, 1, 0, 4 );
* // A => <Float64Array>[ ~-10.344, ~0.441, ~0.793, ~-11.794, ~0.947, ~0.919, ~-13.244, ~1.894, ~0.0, ~-14.694, ~2.842, ~0.0 ]
* // TAU => <Float64Array>[ ~1.097, ~1.084, 0.0 ]
* // WORK => <Float64Array>[ 4.0, ~-2.842, ~17.046, 0.0 ]
*/
function dgeqrf( M, N, A, strideA1, strideA2, offsetA, TAU, strideTAU, offsetTAU, WORK, strideWORK, offsetWORK, LWORK ) { // eslint-disable-line stdlib/jsdoc-doctest-decimal-point, max-len, max-params
	var ldwork;
	var lquery;
	var inctau;
	var lwkopt;
	var nbmin;
	var inca;
	var taui;
	var aii;
	var iws;
	var ib;
	var nb;
	var nx;
	var i;
	var k;
 
	k = min( M, N );
	nb = 32; // The optimal block size derived from fortran LAPACK call `ilaenv( 1, 'DGEQRF', ' ', m, n, -1, -1 )`
	lquery = ( LWORK === -1 );
 
	if ( lquery ) {
		if ( k === 0 ) {
			lwkopt = 1;
		} else {
			lwkopt = N * nb;
		}
		WORK[ offsetWORK ] = lwkopt;
		return 0;
	}
 
	// Quick return if possible
	if ( k === 0 ) {
		WORK[ offsetWORK ] = 1;
		return 0;
	}
 
	nbmin = 2;
	nx = 0;
	iws = N;
 
	if ( nb > 1 && nb < k ) {
		// Determine when to cross over from blocked to unblocked code.
		nx = 128; // The crossover point derived from fortran LAPACK call `ilaenv( 3, 'DGEQRF', ' ', m, n, -1, -1 )`
 
		if ( nx < k ) {
			// Determine if workspace is large enough for blocked code.
			ldwork = N;
			iws = ldwork * nb;
			if ( LWORK < iws ) {
				// Not enough workspace to use optimal NB: reduce NB and determine the minimum value of NB.
				nb = floor( LWORK / ldwork );
				nbmin = 2; // The minimal block size derived from fortran LAPACK call `ilaenv( 2, 'DGEQRF', ' ', m, n, -1, -1 )`
			}
		}
	}
 
	inca = nb * ( strideA1 + strideA2 );
	inctau = nb * strideTAU;
 
	aii = offsetA; // Index of A(i,i)
	taui = offsetTAU; // Index of TAU(i)
 
	if ( nb >= nbmin && nb < k && nx < k ) {
		// Use blocked code initially
		for ( i = 0; i < k - nx; i += nb ) {
			ib = min( k - i, nb );
 
			// Compute the QR factorization of the current block A(i:m,i:i+ib-1)
			dgeqr2( M - i, ib, A, strideA1, strideA2, aii, TAU, strideTAU, taui, WORK, strideWORK, offsetWORK );
 
			if ( i + ib < N ) {
				// Form the triangular factor of the block reflector: H = H(i) H(i+1) . . . H(i+ib-1)
				dlarft( 'forward', 'columns', M - i, ib, A, strideA1, strideA2, aii, TAU, strideTAU, taui, WORK, 1, N, offsetWORK );
 
				// Apply H**T to A(i:m,i+ib:n) from the left
				dlarfb( 'left', 'transpose', 'forward', 'columns', M - i, N - i - ib, ib, A, strideA1, strideA2, aii, WORK, 1, N, offsetWORK, A, strideA1, strideA2, aii + ( ib * strideA2 ), WORK, 1, ldwork, offsetWORK + ( ib * strideWORK ) );
			}
 
			aii += inca;
			taui += inctau;
		}
	} else {
		i = 0;
	}
 
	// Use unblocked code to factor the last or only block
	if ( i <= k - 1 ) {
		dgeqr2( M - i, N - i, A, strideA1, strideA2, aii, TAU, strideTAU, taui, WORK, strideWORK, offsetWORK );
	}
 
	WORK[ offsetWORK ] = iws;
 
	return 0;
}
 
 
// EXPORTS //
 
module.exports = dgeqrf;