Why NumPy eigenvectors and SymPy eigenvectors are different?

70 views Asked by At

I'm working on a project that involves calculations with eigenvalues and eigenvectors. At the beginning I was doing everything in NumPy, but for diverse causes I had to change everything to SymPy. My main problem is that eigenvectors calculations does not match even when I'm calculating the same thing.

My original NumPy code is the following:

import numpy as np
from numpy.linalg import inv, eig

T_num = np.array([[-0.873242282149309, 0.858808525419568, 0.00866025403784438, 0.00866025403784438], 
        [-0.858808525419568, 0.873242282149309, -0.00866025403784438, -0.00866025403784438], 
        [0.0360843918243516, 0.0360843918243516, -0.909326673973660, 0.822724133595217], 
        [-0.0360843918243516, -0.0360843918243516, -0.822724133595217, 0.909326673973660]])

eigenvalues, eigenvectors = np.linalg.eig(T_num)
vr = np.diag(eigenvalues)
Kr = eigenvectors

And the Kr output was the following:

[[-0.16735621+0.00000000e+00j -0.54321448+5.43103641e-09j
  -0.54321448-5.43103641e-09j -0.10224205+0.00000000e+00j]
 [-0.10224205+0.00000000e+00j -0.54321448+0.00000000e+00j
  -0.54321448-0.00000000e+00j -0.16735621+0.00000000e+00j]
 [ 0.83678107+0.00000000e+00j -0.45267873+4.52586368e-09j
  -0.45267873-4.52586368e-09j  0.51121024+0.00000000e+00j]
 [ 0.51121024+0.00000000e+00j -0.45267873+4.96308368e-24j
  -0.45267873-4.96308368e-24j  0.83678107+0.00000000e+00j]]

I know that NumPy eigenvectors are normalized but SymPy are not so I normalized them using .normalized() . My SymPy code looks like this:


import sympy as sp

T_sym = sp.Matrix([[-0.873242282149309, 0.858808525419568, 0.00866025403784438, 0.00866025403784438], 
        [-0.858808525419568, 0.873242282149309, -0.00866025403784438, -0.00866025403784438], 
        [0.0360843918243516, 0.0360843918243516, -0.909326673973660, 0.822724133595217], 
        [-0.0360843918243516, -0.0360843918243516, -0.822724133595217, 0.909326673973660]])

vr_sym = list(T_sym.eigenvals().keys())
eigenvectores = T_sym.eigenvects()
lista_vectp = []

for _, _, eigenvectors in eigenvectores:
    for vector in eigenvectors:
        normalized_vector = vector.normalized()
        lista_vectp.append(normalized_vector.T)
            
    Kr_sym = sp.Matrix(lista_vectp).T

And my output looks like this:

[[-0.166635697352398 - 0.0155127875107584*I, -0.259982390116971 - 0.476960289513235*I, -0.214652107972921 + 0.499005456909564*I, 0.102242047382989 - 2.20934780343373e-64*I], 
[-0.101801866238621 - 0.00947714531910078*I, -0.259982395640837 - 0.476960299647248*I, -0.214652103412191 + 0.499005446307156*I, 0.167356213534108 - 2.62764866783283e-64*I], 
[0.83317848676198 + 0.077563937553791*I, -0.216651991764145 - 0.397466907927701*I, -0.178876756644103 + 0.415837880757976*I, -0.51121023691494 - 8.33146467644485e-65*I], 
[0.509009331193098 + 0.0473857265955033*I, -0.216651996367367 - 0.397466916372712*I, -0.178876752843495 + 0.415837871922635*I, -0.83678106767053 - 1.92452791847659e-64*I]])

So as you can see, SymPy calculations have a bigger imaginary part than the NumPy ones. Why does this happen?

0

There are 0 answers