QR factorizations in Julia¶
Julia provides access to both the thin and full forms of the QR factorization.
using LinearAlgebra
A = rand(1.:9.,6,4)
@show m,n = size(A);
(m, n) = size(A) = (6, 4)
Here is a standard call:
Q,R = qr(A);
Q
6×6 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:
-0.666667 0.106601 0.318448 -0.423568 0.119768 0.499027
-0.416667 -0.300207 -0.187934 0.0460732 -0.824902 -0.135496
-0.583333 0.234365 -0.113155 0.635183 0.318998 -0.294575
-0.166667 -0.0297855 0.00915739 -0.589747 0.186044 -0.767357
-0.0833333 -0.917865 0.0841625 0.120783 0.355792 0.0481651
-0.0833333 -0.0148928 -0.918314 -0.22939 0.205506 0.233839
R
4×4 Array{Float64,2}:
-12.0 -10.4167 -5.58333 -10.5
0.0 -8.85963 -4.49683 -3.0052
0.0 0.0 -7.04307 0.161705
0.0 0.0 0.0 -5.35655
If you look carefully, you see that we got a full \(Q\) but a thin \(R\). Moreover, the \(Q\) above is not a standard matrix type. If you convert it to a true matrix, then it reverts to the thin form.
Matrix(Q)
6×4 Array{Float64,2}:
-0.666667 0.106601 0.318448 -0.423568
-0.416667 -0.300207 -0.187934 0.0460732
-0.583333 0.234365 -0.113155 0.635183
-0.166667 -0.0297855 0.00915739 -0.589747
-0.0833333 -0.917865 0.0841625 0.120783
-0.0833333 -0.0148928 -0.918314 -0.22939
We can test that \(\mathbf{Q}\) is orthogonal.
QTQ = Q'*Q
norm(QTQ - I)
8.41217758417149e-16
The thin \(Q\) cannot be an orthogonal matrix, because it is not even square, but it is still ONC.
Matrix(Q)'*Matrix(Q) - I
4×4 Array{Float64,2}:
2.22045e-16 -4.80008e-17 -2.38854e-17 5.30075e-17
-4.80008e-17 -3.33067e-16 2.38779e-17 3.4396e-18
-2.38854e-17 2.38779e-17 -4.44089e-16 6.05498e-18
5.30075e-17 3.4396e-18 6.05498e-18 2.22045e-16