[Math] “correct” QR factorization result

matrices

I am implementing a Givens Rotation QR factorization algorithm and I'm trying to check the solution of the output to make sure it is correct.

I am not very familiar with QR factorization, so I may be missing some key ideas. I am writing the initial code for a high performance computing class with the goal of trying to optimize its performance as much as I can.

If I give the program the MxN matrix sizes on the command line, I am returned a result of the A matrix, R matrix, Transposed Q, and Transposed Q * A.

Here is a sample output:

bash-4.1$ ./qr 5 5
Matrix A:
  0.15797  0.71033  0.13588 -0.47781  0.43762
 -0.77366  0.69873  0.35560 -0.77672 -0.02941
  0.57911 -0.84310  0.76622 -0.84151 -0.77572
 -0.87032  0.96510  0.68374  0.79453  0.66207
  0.25023 -0.93697  0.86126  0.87264 -0.21894

Matrix R:
 -1.33378  1.49278  0.14207  0.32616  0.74101
  0.00000 -1.13339  0.58250  0.62677 -0.60196
  0.00000  0.00000  1.25769 -0.08152 -0.02853
  0.00000  0.00000  0.00000  1.55777  0.60007
  0.00000  0.00000  0.00000  0.00000  0.08965

Matrix Q Transposed:
 -0.11843  0.58005 -0.43419  0.65253 -0.18761
 -0.78272  0.14749  0.17201  0.00793  0.57961
  0.48393  0.14891  0.57861  0.46626  0.43754
  0.05833 -0.67161 -0.48822  0.39463  0.38916
  0.36841  0.41055 -0.45688 -0.44834  0.53476

Q^T*A:
 -1.33378  1.49278  0.14207  0.32616  0.74101
  0.00000 -1.13339  0.58250  0.62677 -0.60196
  0.00000  0.00000  1.25769 -0.08152 -0.02853
  0.00000  0.00000  0.00000  1.55777  0.60007
  0.00000  0.00000  0.00000  0.00000  0.08965

When I go to check the accuracy of my solution against an online QR factorization calculator I get different results (granted, my Q is already transposed):

Matrix Q:
  -0.1184  -0.7827  -0.4839  -0.0583   0.3684
   0.5801   0.1475  -0.1489   0.6716   0.4105
  -0.4342   0.1720  -0.5786   0.4882  -0.4569
   0.6525   0.0079  -0.4663  -0.3946  -0.4483
  -0.1876   0.5796  -0.4375  -0.3892   0.5348

Matrix R:
  -1.3338   1.4928   0.1421   0.3262   0.7410
   0.0000  -1.1334   0.5825   0.6268  -0.6020
   0.0000   0.0000  -1.2577   0.0815   0.0285
   0.0000   0.0000   0.0000  -1.5578  -0.6001
   0.0000   0.0000   0.0000   0.0000   0.0896

Do different methods of QR factorization result in different Q and R?

If so, is there any way to prove my answer is "correct" other than what I have already done? (Find Q and R. Multiply A by transposed Q and verify answer is R)

Best Answer

You can flip the sign of any column of $Q$ (and make corresponding changes to the same row of $R$) and keep $A=QR$ and $Q$ orthogonal. In the example you've given, you can easily see that the two solutions are equivalent except for this sign change on some columns of $Q$ and corresponding rows of $R$.

In that sense, the QR factorization is not unique. Most codes for computing the QR factorization don't worry about this and let the diagonal elements of $R$ have arbitrary signs. Some codes will arrange the computation so that $R$ has non-negative entries on the diagonal.

If you want to get such a QR factorization after using a routine that doesn't care about the sign of the diagonal entries of $R$, you can easily correct it- for each row $i$ in which $R_{i,i}<0$, flip the sign of row $i$ of $R$ and flip the sign of column $i$ of $Q$.

If $A$ has full column rank, and if we follow the convention that diagonal entries of $R$ are constrained to be positive, then the QR factorization is unique- it's a good exercise to prove this.

Related Question