+3 votes
in Programming Languages by (73.8k points)
I want to compute eigenvalues and eigenvectors of a 3x3 matrix. What Python function should I use for it?

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

You can use scipy's linear algebra function eig() to calculate eigenvalues and eigenvectors of a square matrix. You can set the parameters to get left or right eigenvectors. By default, it returns the right eigenvectors.

Here is an example:

>>> from scipy import linalg as LA
>>> import numpy as np
>>> a=np.array([[11,12,13],[91,2,3],[52,23,24]])
>>> a
array([[11, 12, 13],
       [91,  2,  3],
       [52, 23, 24]])
>>> evals, evecs = LA.eig(a, left=True, right=False)
>>> evals
array([ 61.69905021+0.j, -23.82135282+0.j,  -0.8776974 +0.j])
>>> evecs
array([[-0.89186516, -0.94421099, -0.86747631],
       [-0.30717734,  0.22261074, -0.15659578],
       [-0.33199189,  0.24271397,  0.47218917]])

>>> evals, evecs = LA.eig(a, left=False, right=True)
>>> evals
array([ 61.69905021+0.j, -23.82135282+0.j,  -0.8776974 +0.j])
>>> evecs
array([[-3.26538091e-01, -2.63826498e-01,  9.03547963e-04],
       [-5.36838695e-01,  9.49508315e-01, -7.35214463e-01],
       [-7.77931289e-01, -1.69792632e-01,  6.77833960e-01]])

You can also use Numpy's linear algebra function eig(). It returns eigenvalues and right eigenvectors of a square matrix.

Here is an example:

>>> from numpy import linalg as LA
>>> import numpy as np
>>> a=np.array([[11,12,13],[91,2,3],[52,23,24]])
>>> a
array([[11, 12, 13],
       [91,  2,  3],
       [52, 23, 24]])
>>> evals, evecs = LA.eig(a)
>>> evals
array([ 61.69905021, -23.82135282,  -0.8776974 ])
>>> evecs
array([[-3.26538091e-01, -2.63826498e-01,  9.03547963e-04],
       [-5.36838695e-01,  9.49508315e-01, -7.35214463e-01],
       [-7.77931289e-01, -1.69792632e-01,  6.77833960e-01]])
>>>


...