简体   繁体   中英

How to find eigenvectors and eigenvalues without numpy and scipy?

I need to calculate eigenvalues and eigenvectors in python. numpy and scipy do not work. They both write Illegal instruction (core dumped) . I found out that to resolve the problem I need to check my blas/lapack. So, I thought that may be an easier way is to write/find a small function to solve the eigenvalue problem. Does anybody know if such solutions exist?

Any efficient solution would use internally the same blas/lapack library. I still think that it won't be so hard to fix your libs.

But in case you find it easier, you can implement yourself any of those http://en.wikipedia.org/wiki/List_of_numerical_analysis_topics#Eigenvalue_algorithms .

I suppose easiest to implement would be power algorithm , but doubt it would be efficient.

You can use sympy, the python computer algebra system, to solve the eigenvalue problem without native libraries using the Berkowitz method. It's not fast, but if you have a small number of small matrices, that won't be a problem.

Example:

>>> from sympy import Matrix
>>> m = Matrix([[10,2,3], [3,12,5], [5,5,8]])
>>> print m.eigenvals()
# this gets the eigenvalues along with their multiplicity
{10 - (-77/2 + sqrt(1019751)*I/18)**(1/3) - 50/(3*(-77/2 + sqrt(1019751)*I/18)**(1/3)): 1,
 10 - (-77/2 + sqrt(1019751)*I/18)**(1/3)*(-1/2 + sqrt(3)*I/2) - 50/(3*(-77/2 + sqrt(1019751)*I/18)**(1/3)*(-1/2 + sqrt(3)*I/2)): 1,
 10 - 50/(3*(-77/2 + sqrt(1019751)*I/18)**(1/3)*(-1/2 - sqrt(3)*I/2)) - (-77/2 + sqrt(1019751)*I/18)**(1/3)*(-1/2 - sqrt(3)*I/2): 1}
>>> print map(complex, m.eigenvals().keys())
[(8.374025140524024+2.117582368135751e-22j), (3.8835463038416105-2.117582368135751e-22j), (17.742428555634365-1.0587911840678754e-22j)]

# check with numpy
>>> import numpy as np
>>> print np.linalg.eigvals(np.array(m.tolist(), dtype=float))
array([ 17.74242856,   8.37402514,   3.8835463 ])

编写解决特征值问题的程序所需的工作是解决库不匹配问题的100倍。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM