(Q, R, tau, Q2) = Matrices.QR(A);
This function returns the QR decomposition of a rectangular matrix A (the number of columns of A must be less than or equal to the number of rows):
Q * R = A
where Q is a rectangular matrix that has orthonormal columns and has the same size as A (QTQ=I), R is a square, upper triangular matrix. Matrix R has the following important properties:
This means that if abs(R[i,i]) ≤ ε then abs(R[j,k]) ≤ ε for j ≥ i, i.e., the i-th row up to the last row of R have small elements and can be treated as being zero. This allows to, e.g., estimate the row-rank of R (which is the same row-rank as A). Furthermore, R can be partitioned in two parts
A = Q * [R1, R2; 0, 0]
where R1 is a regular, upper triangular matrix.
Note, the solution is computed with the LAPACK function "dgeqrf",
i.e., by a QR factorization without column pivoting.
If Q is not needed, the function may be
called as: (,R) = QR(A)
.
Real A[3,3] = [1, 2, 3; 3, 4, 5; 2, 1, 4]; Real R[3,3]; algorithm (,R) := Matrices.QR(A); // R = [-3.74.., -4.27.., -6.94..; 0.0 , -1.64.., -0.17..; 0.0 , 0.0 , -1.29..]
function QR extends Modelica.Icons.Function; input Real A[:, :] "Rectangular matrix with size(A,1) >= size(A,2)"; output Real Q[size(A, 1), size(A, 2)] "Rectangular matrix with orthonormal columns such that Q*R=A[:,p]"; output Real R[min(size(A, 1), size(A, 2)), size(A, 2)] "Square upper triangular matrix"; output Real tau[min(size(A, 1), size(A, 2))] "Scalar factors of the elementary reflectors"; output Real Q2[size(A, 1), size(A, 1)] "Orthogonal matrix defined as the product of elementary reflectors"; end QR;