XLPack 7.0
XLPack Numerical Library (Excel VBA) Reference Manual
Loading...
Searching...
No Matches

◆ Dgelsy()

Sub Dgelsy ( M As  Long,
N As  Long,
A() As  Double,
B() As  Double,
Jpvt() As  Long,
RCond As  Double,
Rank As  Long,
Info As  Long,
Optional Nrhs As  Long = 1 
)

Solution to overdetermined or underdetermined linear equations Ax = b using a complete orthogonal factorization

Purpose
This routine computes the minimum-norm solution to a real linear least squares problem:
minimize || A * X - B ||
using a complete orthogonal factorization of A. A is an m x n matrix which may be rank-deficient.
Several right hand side vectors b and solution vectors x can be handled in a single call; they are stored as the columns of the m x nrhs right hand side matrix B and the n x nrhs solution matrix X.

The routine first computes a QR factorization with column pivoting:
A * P = Q * [ R11 R12 ]
[ 0 R22 ]
with R11 defined as the largest leading submatrix whose estimated condition number is less than 1/RCond. The order of R11, rank, is the effective rank of A.
Then, R22 is considered to be negligible, and R12 is annihilated by orthogonal transformations from the right, arriving at the complete orthogonal factorization:
A * P = Q * [ T11 0 ] * Z
[ 0 0 ]
The minimum-norm solution is then
X = P * Z^T [ T11^(-1)*Q1^T*B ]
[ 0 ]
where Q1 consists of the first rank columns of Q.
Parameters
[in]MNumber of rows of the matrix A. (M >= 0) (If M = 0, returns with Rank = 0)
[in]NNumber of columns of the matrix A. (N >= 0) (If N = 0, returns with Rank = 0)
[in,out]A()Array A(LA1 - 1, LA2 - 1) (LA1 >= M, LA2 >= N)
[in] M x N matrix A.
[out] A() has been overwritten by details of the complete orthogonal factorization.
[in,out]B()Array B(LB1 - 1, LB2 - 1) (LB1 >= max(M, N), LB2 >= Nrhs) (2D array) or B(LB - 1) (LB >= max(M, N), Nrhs = 1) (1D array)
[in] M x Nrhs right hand side matrix B.
[out] N x Nrhs solution matrix X.
[in,out]Jpvt()Array Jpvt(LJpvt - 1) (LJpvt >= N)
[in] If Jpvt(i-1) <> 0, the i-th column of A is permuted to the front of A*P, otherwise column i is a free column.
[out] If Jpvt(i) = k, then the i-th column of A*P was the k-th column of A.
[in]RCondRCond is used to determine the effective rank of A, which is defined as the order of the largest leading triangular submatrix R11 in the QR factorization with pivoting of A, whose estimated condition number < 1/RCond.
[out]RankThe effective rank of A, i.e., the order of the submatrix R11. This is the same as the order of the submatrix T11 in the complete orthogonal factorization of A.
[out]Info= 0: Successful exit.
= -1: The argument M had an illegal value. (M < 0)
= -2: The argument N had an illegal value. (N < 0)
= -3: The argument A() is invalid.
= -4: The argument B() is invalid.
= -5: The argument Jpvt() is invalid.
= -9: The argument Nrhs had an illegal value. (Nrhs < 0)
[in]Nrhs(Optional)
Number of right hand sides, i.e., number of columns of the matrices B and X. (Nrhs >= 0) (If Nrhs = 0, returns with Rank = 0) (default = 1)
Reference
LAPACK
Example Program
Compute the least squares solution of the overdetermined linear equations Ax = b and its variance, where
( -1.06 0.48 -0.04 )
A = ( -1.19 0.73 -0.24 )
( 1.97 -0.89 0.56 )
( 0.68 -0.53 0.08 )
( 0.3884 )
B = ( 0.1120 )
( -0.3644 )
( -0.0002 )
Sub Ex_Dgelsy()
Const M = 4, N = 3
Dim A(M - 1, N - 1) As Double, B(M - 1) As Double, Ci(N - 1) As Double
Dim Jpvt(N - 1) As Long, RCond As Double, Rank As Long, Info As Long
Dim I As Long
A(0, 0) = -1.06: A(0, 1) = 0.48: A(0, 2) = -0.04
A(1, 0) = -1.19: A(1, 1) = 0.73: A(1, 2) = -0.24
A(2, 0) = 1.97: A(2, 1) = -0.89: A(2, 2) = 0.56
A(3, 0) = 0.68: A(3, 1) = -0.53: A(3, 2) = 0.08
B(0) = 0.3884: B(1) = 0.112: B(2) = -0.3644: B(3) = -0.0002
For I = 0 To N - 1
Jpvt(I) = 0
Next
RCond = 0.0001
Call Dgelsy(M, N, A(), B(), Jpvt(), RCond, Rank, Info)
If Info <> 0 Then
Debug.Print "Error in Dgelsy: Info =", Info
Exit Sub
End If
Debug.Print "X =", B(0), B(1), B(2)
Call Dgecovy(0, N, A(), Jpvt(), Ci(), Info)
Debug.Print "Var =", Ci(0), Ci(1), Ci(2)
Debug.Print "Rank =", Rank, "Info =", Info
End Sub
Function Ci(X As Double, Optional Info As Long) As Double
Cosine integral Ci(x)
Sub Dgecovy(Job As Long, N As Long, A() As Double, Jpvt() As Long, Ci() As Double, Info As Long)
Unscaled covariance matrix of linear least squares problem solved by Dgelsy
Sub Dgelsy(M As Long, N As Long, A() As Double, B() As Double, Jpvt() As Long, RCond As Double, Rank As Long, Info As Long, Optional Nrhs As Long=1)
Solution to overdetermined or underdetermined linear equations Ax = b using a complete orthogonal fac...
Example Results
X = -0.820000000000001 -0.940000000000001 0.740000000000001
Var = 6.46959967542666 16.7350408218919 18.7177532773229
Rank = 3 Info = 0