Roman
Roman

Reputation: 131088

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?

Upvotes: 3

Views: 18161

Answers (3)

Robert T. McGibbon
Robert T. McGibbon

Reputation: 5277

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 ])

Upvotes: 2

Robert Dodier
Robert Dodier

Reputation: 17576

Writing a program to solve an eigenvalue problem is about 100 times as much work as fixing the library mismatch problem.

Upvotes: 0

alko
alko

Reputation: 48317

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.

Upvotes: 2

Related Questions