# Why sparse QR decomposition yield different results from dense QR decomposition in R?

A simple example is given below:

mat <- diag(1:6)
qr.R(qr(as(mat,"Matrix")))
qr.R(qr(dd))


the diagnal values are of opposite signs. There is a warning message when doing sparse QR decomposition:

Warning message:
In qr.R(qr(as(dd, "Matrix"))) :
qr.R(<sparse>) may differ from qr.R(<dense>) because of permutations


However, if I check qr.R(qr(as(mat,"Matrix")))@q, it is 0 1 2 3 4 5, it seems that there is no permutations being done.

Moreover, in Matlab, if I want to do a sparse matrix decomposition:

mat=sparse(1:6,1:6,1:6)
p = colamd(mat)
R = qr(mat(:,p))


this result(R and p) should be equal to the R output @R and @q, am I right?

1) "The" QR decomposition of a matrix is only unique if the sign of all diagonal entries of R is specified. As I read, it is somewhat "common" to specify them as all non-negative.

2) The SparseQR algorithm currently in use in the Matrix package (of which I am co-maintainer) does follow the "common" convention, whereas R's qr() itself does not:

> qr.R(qr(diag(1:6)))

[,1] [,2] [,3] [,4] [,5] [,6]
[1,]   -1    0    0    0    0    0
[2,]    0   -2    0    0    0    0
[3,]    0    0   -3    0    0    0
[4,]    0    0    0   -4    0    0
[5,]    0    0    0    0   -5    0
[6,]    0    0    0    0    0    6


However, that is not a problem, really, is it? All important properties of the QR decomposition do not depend on which signs are chosen for R, right?

• Thanks for the answer @Martin, I want to compare the performance of a Matlab optimization program with R, so I need a qr to produce exact same result. I agree that usually the signs do not matter, but I will use qr for sparse matrix to replace qr for the time being. Apr 10 '13 at 11:24