# [SciPy-User] qr decompostion gives negative q, r ?

Charles R Harris charlesr.harris@gmail....
Tue Nov 20 16:57:40 CST 2012

```On Tue, Nov 20, 2012 at 3:49 PM, Virgil Stokes <vs@it.uu.se> wrote:

> On 2012-11-20 23:13, Skipper Seabold wrote:
> > On Tue, Nov 20, 2012 at 5:03 PM, Virgil Stokes <vs@it.uu.se> wrote:
> >> On 2012-11-20 22:33, Daπid wrote:
> >>> The QR descomposition is finding two matrices with certain properties
> such that:
> >>>
> >>> A = Q·R
> >>>
> >>> But, if both Q and R are multiplied by -1, (-Q)·(-R) = Q·R = A, still
> >>> the same matrix. If Q is orthogonal, -Q is also. The sign is,
> >>> therefore, arbitrary.
> >>>
> >>> On Tue, Nov 20, 2012 at 12:01 AM, Virgil Stokes <vs@it.uu.se> wrote:
> >>>> 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 to what I get from MATLAB (R2010B)
> >>>>
> >>>> [q,r] = qr(A)
> >>>>
> >>>> The q,r returned from numpy are both the negative of the q,r returned
> >>>> from MATLAB for the same matrix A. I believe that theq,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.
> >>>>
> >>>> _______________________________________________
> >>>> SciPy-User mailing list
> >>>> SciPy-User@scipy.org
> >>>> http://mail.scipy.org/mailman/listinfo/scipy-user
> >>> _______________________________________________
> >>> SciPy-User mailing list
> >>> SciPy-User@scipy.org
> >>> http://mail.scipy.org/mailman/listinfo/scipy-user
> >> Thanks David,
> >> I am well aware of this; but, I am using the QR decomposition for a
> >> convariance (PD matrix) and the negative R is not very useful in this
> >> case and the numpy result, IMHO should not be the default.
> >>
> > Can't you guard against this in your code?
> >
> >> Why is numpy/Python different from that returned by MATLAB and
> >> MATHEMATICA? This makes translations rather tricky and one begins to
> >> wonder if there are other differences.
> > It can often depend on the version of the underlying LAPACK functions
> > used (or maybe even where/how it was compiled). In my experience, I've
> > seen linear algebra functions in MATLAB give different results up to
> > an arbitrary sign when I know for a fact they were using the same
> > underling LAPACK routine. I later upgraded the LAPACK that I used to
> > build scipy and the signs agreed. I do not know if MATLAB does any
> > kind of normalization after the fact, but you could file an issue or
> > better yet provide a PR for the sign check in scipy if it's something
> > you don't want to check for in your code in the future. The beauty of
> > scipy is that you can look at the code to see why you're getting the
> > results you're getting. You can find out the LAPACK version and then
> > look at the helper functions that calls these routines to see what's
> > going on. Good luck figuring that out with MATLAB, etc.
> >
> > Skipper
> >
> > Skipper
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User@scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> Ok Skipper,
> Unfortunately, things are worse than I had hoped, numpy sometimes
> returns the negative of the q,r and other times the same as MATLAB!
> Thus, as someone has already mentioned in this discussion, the "sign"
> seems to depend on the matrix being decomposed. This could be a
> nightmare to track down.
>
> I hope that I can return to some older versions of numpy/scipy to work
> around this problem until this problem is fixed. Any suggestions on how
> to recover earlier versions would be appreciated.
>

But why is it a problem? Why is Matlab "right"? What is the property that
you
need to have in the decomposition?

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20121120/9542c990/attachment-0001.html
```