# [Numpy-discussion] Eigenvectors in Matlab vs. Numpy

Charles R Harris charlesr.harris@gmail....
Sun Jun 29 20:10:24 CDT 2008

```On Sun, Jun 29, 2008 at 6:47 PM, Saket <saketn@gmail.com> wrote:

> Hi,
>
> I'm having this weird problem when computing eigenvalues/vectors with
> Numpy. I have the following symmetric matrix, B:
>
>   -0.3462    0.6538    0.5385   -0.4615    0.6538   -0.3462   -0.3462
> -0.3462
>    0.6538   -0.3462    0.5385   -0.4615    0.6538   -0.3462   -0.3462
> -0.3462
>    0.5385    0.5385   -0.6154    0.3846    0.5385   -0.4615   -0.4615
> -0.4615
>   -0.4615   -0.4615    0.3846   -0.6154   -0.4615    0.5385    0.5385
>  0.5385
>    0.6538    0.6538    0.5385   -0.4615   -0.3462   -0.3462   -0.3462
> -0.3462
>   -0.3462   -0.3462   -0.4615    0.5385   -0.3462   -0.3462    0.6538
>  0.6538
>   -0.3462   -0.3462   -0.4615    0.5385   -0.3462    0.6538   -0.3462
>  0.6538
>   -0.3462   -0.3462   -0.4615    0.5385   -0.3462    0.6538    0.6538
> -0.3462
>
> I compute the eigenvalues and eigenvectors of B using
> numpy.linalg.eig(B). I get the following eigenvalues:
>
> [  2.79128785e+00  -1.79128785e+00   1.64060486e-16  -3.07692308e-01
>  -1.00000000e+00  -1.00000000e+00  -1.00000000e+00  -1.00000000e+00]
>
> I do the same thing in Matlab and get the SAME eigenvalues. However,
> my eigenVECTORS in Matlab versus numpy are different. It makes no
> sense to me. In general, the following relationship should hold: Bx =
> Lx, where B is my matrix, x is an eigenvector, and L is the
> corresponding eigenvalue. For the eigenvectors that Matlab returns, I
> have confirmed that the relationship does hold. But for the Numpy
> eigenvectors, it doesn't!
>
> Any idea why this might be happening? I did some computations myself
> and it looks like the Matlab output is correct. Just seems like the
> eigenvectors that Numpy is returning are wrong...
>
> Thanks for any suggestions.
>

Works for me:

In [16]: d,v = linalg.eig(A)

In [17]: abs(dot(A,v) - dot(v,diag(d))).max()
Out[17]: 1.1102230246251565e-15

Perhaps you are not applying the results correctly. You should also use eigh
for symmetric matrices. Note that Matlab, IIRC, returns the eigenvalues as a
diagonal matrix when you ask for the eigenvectors, while numpy returns a 1D
array that needs to be made into a diagonal array or simply multiplied
pointwise from the right, i.e.,

In [21]: abs(dot(A,v) - v*d).max()
Out[21]: 1.1102230246251565e-15

This is with arrays, matrices will be slightly different. If your problem

In [28]: numpy.__config__.show()
libraries = ['lapack', 'ptf77blas', 'ptcblas', 'atlas']
library_dirs = ['/usr/local/atlas/lib']
language = f77
include_dirs = ['/usr/local/atlas/include']

blas_opt_info:
libraries = ['ptf77blas', 'ptcblas', 'atlas']
library_dirs = ['/usr/local/atlas/lib']
define_macros = [('ATLAS_INFO', '"\\"3.7.35\\""')]
language = c
include_dirs = ['/usr/local/atlas/include']

libraries = ['ptf77blas', 'ptcblas', 'atlas']
library_dirs = ['/usr/local/atlas/lib']
language = c
include_dirs = ['/usr/local/atlas/include']

lapack_opt_info:
libraries = ['lapack', 'ptf77blas', 'ptcblas', 'atlas']
library_dirs = ['/usr/local/atlas/lib']
define_macros = [('ATLAS_INFO', '"\\"3.7.35\\""')]
language = f77
include_dirs = ['/usr/local/atlas/include']

lapack_mkl_info:
NOT AVAILABLE

blas_mkl_info:
NOT AVAILABLE

mkl_info:
NOT AVAILABLE

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080629/0d824394/attachment-0001.html
```