Euler_Salter
Euler_Salter

Reputation: 3561

Why Numpy and Scipy QR decomposition give me different values?

I have the following vector.

x = np.array([[ 0.87695113],
              [ 0.3284933 ],
              [-0.35078323]])

When I call numpy version of qr

from numpy.linalg import qr as qr_numpy
qr_numpy(x)

I obtain

(array([[-0.87695113],
        [-0.3284933 ],
        [ 0.35078323]]), array([[-1.]]))

whereas when I run the scipy version I get something entirely different.

from scipy.linalg import qr as qr_scipy
qr_scipy(x)

With output

(array([[-0.87695113, -0.3284933 ,  0.35078323],
        [-0.3284933 ,  0.94250897,  0.06139208],
        [ 0.35078323,  0.06139208,  0.93444215]]), array([[-1.],
        [ 0.],
        [ 0.]]))

What is going on??

Upvotes: 2

Views: 1170

Answers (1)

aneroid
aneroid

Reputation: 15962

The default mode for numpy.linalg.qr() is 'reduced' whereas for scipy.linalg.qr() it's 'full'.

So to get the same results for both, either use 'economic' for scipy-qr or 'complete' for numpy-qr:

from numpy.linalg import qr as qr_numpy
qr_numpy(x)
(array([[-0.87695113],
        [-0.3284933 ],
        [ 0.35078323]]),
 array([[-1.]]))

Which matches with the output of scipy-qr:

from scipy.linalg import qr as qr_scipy
qr_scipy(x, mode='economic')
(array([[-0.87695113],
        [-0.3284933 ],
        [ 0.35078323]]),
 array([[-1.]]))

And to get the "complete" version with both:

from numpy.linalg import qr as qr_numpy
qr_numpy(x, mode='complete')
(array([[-0.87695113, -0.3284933 ,  0.35078323],
        [-0.3284933 ,  0.94250897,  0.06139208],
        [ 0.35078323,  0.06139208,  0.93444215]]),
 array([[-1.],
        [ 0.],
        [ 0.]]))
from scipy.linalg import qr as qr_scipy
qr_scipy(x)
(array([[-0.87695113, -0.3284933 ,  0.35078323],
        [-0.3284933 ,  0.94250897,  0.06139208],
        [ 0.35078323,  0.06139208,  0.93444215]]),
 array([[-1.],
        [ 0.],
        [ 0.]]))

Upvotes: 4

Related Questions