is it an appropriate way to compute eigendecomposition by using this command `np.linalg.eig(H*H)`?

Viewed 62

I am learning this post and this post.

I am trying to reproduce the computation as this post with Python, NumPy.

H = np.array([[0.1, 0.3, .4],[0.5 , 0.5, 0.9],[0.1, 0.4, 0.5]])
u, s, vh = np.linalg.svd(H)
w, v = np.linalg.eig(H*H)

np.linalg.eig(H*H) gives a very different result from this post.

why does that?

1 Answers

There are two issues. Firstly, you're not transposing the multiplier. Secondly, you're using elementwise multiplication instead of matrix multiplication.

Here is how you can fix both issues:

In [18]: np.linalg.eig(np.matmul(H, H.T))
Out[18]:
(array([1.94501343e+00, 1.14315435e-05, 4.49751401e-02]),
 array([[-0.35979589, -0.82953709,  0.42710084],
        [-0.81600749,  0.05780546, -0.57514373],
        [-0.4524143 ,  0.55545183,  0.69770664]]))

Alternatively, you could use np.matrix to make the * perform matrix multiplication:

In [22]: H = np.matrix([[0.1, 0.3, .4],[0.5 , 0.5, 0.9],[0.1, 0.4, 0.5]])

In [23]: np.linalg.eig(H*H.T)
Out[23]:
(array([1.94501343e+00, 1.14315435e-05, 4.49751401e-02]),
 matrix([[-0.35979589, -0.82953709,  0.42710084],
         [-0.81600749,  0.05780546, -0.57514373],
         [-0.4524143 ,  0.55545183,  0.69770664]]))

If the matrix contains complex numbers you should be using conjugate transpose (.H) instead of transpose (.T). I've chosen to not do this to avoid confusing notation (H*H.H).

Related