[R] Sign differences amoung QR solutions.

rkevinburton at charter.net rkevinburton at charter.net
Thu Feb 12 22:00:33 CET 2009

```I was noticing mainly sign differences amoung the solutions to QR decomposition. For example R:

> x <- matrix(c(12,-51,4,6,167,-68,-4,24,-41),nrow=3,byrow=T)
> x
[,1] [,2] [,3]
[1,]   12  -51    4
[2,]    6  167  -68
[3,]   -4   24  -41
> r <- qr(x)
> r\$qr
[,1]         [,2] [,3]
[1,] -14.0000000  -21.0000000   14
[2,]   0.4285714 -175.0000000   70
[3,]  -0.2857143    0.1107692  -35

> qr.Q(r)
[,1]       [,2]        [,3]
[1,] -0.8571429  0.3942857  0.33142857
[2,] -0.4285714 -0.9028571 -0.03428571
[3,]  0.2857143 -0.1714286  0.94285714
>

When I feed this matrix into http://www.bluebit.gr/matrix-calculator/calculate.aspx I get:

R
-14.000  -21.000   14.000
0.000 -175.000   70.000
0.000    0.000  -35.000

Q
-0.857  0.394  0.331
-0.429 -0.903 -0.034
0.286 -0.171  0.943

Which seems to agree with the results of 'R'

This same matrix on http://en.wikipedia.org/wiki/QR_decomposition

R
14 -21 14
0 175 -70
0 0 35

Q
6/7 -69/175 -58/175
3/7 158/175 6/175
-2/7 6/36 -33/35

I have yet another algoithm that puts the R matrix as:
-14 -21 14
0 -175 70
0 0 35

And Q as:
Q:
-0.8571 0.3943 -0.3314
-0.4286 -0.9029 0.0343
0.2857 -0.1714 -0.9429

So Wikipedia shows different signs but the property X = QR holds in all cases. My question is are there any gotchas (similar to the sign for tan) that I need to be aware of when using the results of a QR decomposition? With the sign differences it seems that there could be many possible decompositions for one matrix depending on the algoithem.

Thank you.

Kevin

```