[Numpy-discussion] Confused with qr decomposition function

Virgil Stokes vs at it.uu.se
Tue Nov 20 08:24:48 EST 2012


On 20-Nov-2012 00:30, Charles R Harris wrote:
>
>
> On Mon, Nov 19, 2012 at 4:17 PM, Virgil Stokes <vs at it.uu.se 
> <mailto:vs at it.uu.se>> wrote:
>
>     I am using the latest versions of numpy (from
>     numpy-1.7.0b2-win32-superpack-python2.7.exe) and scipy (from
>     scipy-0.11.0-win32-superpack-python2.7.exe ) on a windows 7 (32-bit)
>     platform.
>
>     I have used
>
>     import numpy as np
>     q,r = np.linalg.qr(A)
>
>     and compared the results obtained  from MATLAB (R2010B)
>
>     [q,r] = qr(A)
>
>     The q,r returned from numpy are both the negative of theq,r returned
>     from MATLAB for the same matrix A. I believe that the q,r returned from
>     MATLAB are correct. Why am I getting their negative from numpy?
>
>     Note, I have tried this on several different matrices ---  numpy always
>     gives the negative of MATLAB's q,r values.
>     [I mistakenly have already sent a similar email to the scipy list ---
>     please excuse this mistake.]
>
>
> They are both correct, the decomposition isn't unique. In particular, if both 
> algorithms use Housholder reflections there are two possible reflection planes 
> at each step, one of which is more numerically stable than the other, and the 
> two choices lead to different signs at each step. That said, MATLAB may be 
> normalizing the result in some way or using some other algorithm.
>
> Chuck
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
I am aware that they are both correct; but, if you are doing covariance QR 
decomposition then you almost surely are interested in the positive results 
(which is the default for MATLAB and most papers/books on this subject).

This problem came up recently when converting MATLAB code to python/numpy, with 
some Kalman Filter/Smooth code. It took some time to isolate the problem. It 
would useful if the documentation (numpy) stated explicitly what the default 
"signs" are for q,r.

Thanks for your interest in the problem, Chuck.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121120/9e0cf37c/attachment.html>


More information about the NumPy-Discussion mailing list