Skip to content

Commit

Permalink
Merge pull request #804 from GarkGarcia/eigenvalues-eigenvectors-fix
Browse files Browse the repository at this point in the history
Fixed the behaviour of Eigenvalues and Eigenvectors
  • Loading branch information
wolfv authored Aug 26, 2020
2 parents 1927ada + 57fe993 commit 3944289
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions mathics/builtin/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,17 @@ def apply(self, m, evaluation):
eigenvalues = matrix.eigenvals()
try:
eigenvalues = sorted(eigenvalues.items(),
key=lambda v_c: (abs(v_c[0]), -v_c[0]), reverse=True)
except TypeError as e:
if not str(e).startswith('cannot determine truth value of'):
raise e
eigenvalues = list(eigenvalues.items())
key=lambda v_c: (abs(v_c[0]), -v_c[0]),
reverse=True)
# Try to sort the results as complex numbers
except TypeError:
try:
eigenvalues = sorted(eigenvalues.items(),
key=lambda v_c: -abs(v_c[0]))
# Don't sort the results at all
except TypeError:
pass

return from_sympy([v for (v, c) in eigenvalues for _ in range(c)])


Expand Down Expand Up @@ -996,7 +1002,19 @@ def apply(self, m, evaluation):
'Eigenvectors', 'eigenvecnotimplemented', m)

# The eigenvectors are given in the same order as the eigenvalues.
eigenvects = sorted(eigenvects, key=lambda val_c_vect: (abs(val_c_vect[0]), -val_c_vect[0]), reverse=True)
try:
eigenvects = sorted(eigenvects,
key=lambda v_c: (abs(v_c[0]), -v_c[0]),
reverse=True)
# Try to sort the results as complex numbers
except TypeError:
try:
eigenvects = sorted(eigenvects,
key=lambda v_c: -abs(v_c[0]))
# Don't sort the results at all
except TypeError:
pass

result = []
for val, count, basis in eigenvects:
# Select the i'th basis vector, convert matrix to vector,
Expand Down

0 comments on commit 3944289

Please sign in to comment.