Skip to content
ForCADDocumentation

solve_least_squares_qr Function

public pure function solve_least_squares_qr(A, B) result(X)

Solve an overdetermined full-rank least-squares problem by pivoted QR.

For , , and one or more right-hand sides, Householder transformations with column pivoting form

Columns are pivoted by recomputed trailing two-norms. Recomputing rather than downdating the norms costs additional work but avoids accumulated downdate error in this fallback path. The matrix and right-hand side are independently multiplied by exact radix powers before factorization; the solution scale is restored afterward. A diagonal of not exceeding 64*max(m,n)*epsilon(rk)*max(initial column norm) is classified as numerically rank deficient. Invalid shapes, nonfinite inputs, rank deficiency, or a nonfinite result return X(0,0).

This routine is intended as the accuracy fallback after a structured normal-equation solve fails. Its storage is , and it deliberately returns no arbitrary solution for a rank-deficient design.

References: LAPACK DGELS uses QR for overdetermined full-rank least squares; DGEQP3 computes QR with column pivoting; DGELSY extends that factorization to rank-deficient minimum-norm problems. DGELS, DGEQP3, DGELSY.

Arguments

Type IntentOptional Attributes Name
real(kind=rk), intent(in), contiguous :: A(:,:)

Design matrix [m,n] with m>=n>=1.

real(kind=rk), intent(in) :: B(:,:)

Right-hand sides [m,nrhs].

Return Value real(kind=rk), allocatable, (:,:)