Skip to content

Commit b9f8296

Browse files
committed
wrapping Sympy.simplify call in a Try/Except block. Splitting the test.
1 parent 50d1bd2 commit b9f8296

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

mathics/eval/numbers/algebra/simplify.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ def _default_complexity_function(x):
8080
.to_python()
8181
)
8282

83-
# At this point, ``complexity_function`` is a function that takes a
84-
# sympy expression and returns an integer.
85-
sympy_result = simplify(sympy_expr, measure=complexity_function, doit=False)
86-
sympy_result = sympy_result.doit(roots=False) # Don't expand RootSum
87-
88-
# and bring it back
89-
result = from_sympy(sympy_result).evaluate(evaluation)
83+
try:
84+
# At this point, ``complexity_function`` is a function that takes a
85+
# sympy expression and returns an integer.
86+
sympy_result = simplify(sympy_expr, measure=complexity_function, doit=False)
87+
sympy_result = sympy_result.doit(roots=False) # Don't expand RootSum
88+
# and bring it back
89+
result = from_sympy(sympy_result).evaluate(evaluation)
90+
except ValueError:
91+
return expr
9092
return result

test/builtin/numbers/test_linalg.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,32 @@ def test_inverse(str_expr, str_expected, fail_msg, warnings):
212212
None,
213213
),
214214
(
215-
"A = Array[a, {2,2}]; Eigenvalues[A.ConjugateTranspose[A]] // FullSimplify",
215+
"A = Array[a, {2,2}]; eigvals=Eigenvalues[A.ConjugateTranspose[A]][[1]]",
216216
None,
217-
"{-Sqrt[(a[1, 1] Conjugate[a[1, 1]] + a[1, 2] Conjugate[a[1, 2]] + a[2, 1] Conjugate[a[2, 1]] + a[2, 2] Conjugate[a[2, 2]]) ^ 2 - 4 a[1, 1] a[2, 2] Conjugate[a[1, 1]] Conjugate[a[2, 2]] - 4 a[1, 2] a[2, 1] Conjugate[a[1, 2]] Conjugate[a[2, 1]] + 4 a[1, 1] a[2, 2] Conjugate[a[1, 2]] Conjugate[a[2, 1]] + 4 a[1, 2] a[2, 1] Conjugate[a[1, 1]] Conjugate[a[2, 2]]] / 2 + a[1, 1] Conjugate[a[1, 1]] / 2 + a[1, 2] Conjugate[a[1, 2]] / 2 + a[2, 1] Conjugate[a[2, 1]] / 2 + a[2, 2] Conjugate[a[2, 2]] / 2, Sqrt[(a[1, 1] Conjugate[a[1, 1]] + a[1, 2] Conjugate[a[1, 2]] + a[2, 1] Conjugate[a[2, 1]] + a[2, 2] Conjugate[a[2, 2]]) ^ 2 - 4 a[1, 1] a[2, 2] Conjugate[a[1, 1]] Conjugate[a[2, 2]] - 4 a[1, 2] a[2, 1] Conjugate[a[1, 2]] Conjugate[a[2, 1]] + 4 a[1, 1] a[2, 2] Conjugate[a[1, 2]] Conjugate[a[2, 1]] + 4 a[1, 2] a[2, 1] Conjugate[a[1, 1]] Conjugate[a[2, 2]]] / 2 + a[1, 1] Conjugate[a[1, 1]] / 2 + a[1, 2] Conjugate[a[1, 2]] / 2 + a[2, 1] Conjugate[a[2, 1]] / 2 + a[2, 2] Conjugate[a[2, 2]] / 2}",
218-
"Sympy issue #1156",
217+
(
218+
"-Sqrt[(a[1, 1] Conjugate[a[1, 1]] + a[1, 2] Conjugate[a[1, 2]] "
219+
"+ a[2, 1] Conjugate[a[2, 1]] + a[2, 2] Conjugate[a[2, 2]]) ^ 2 "
220+
"- 4 (a[1, 1] Conjugate[a[1, 1]] + a[1, 2] Conjugate[a[1, 2]]) "
221+
"(a[2, 1] Conjugate[a[2, 1]] + a[2, 2] Conjugate[a[2, 2]]) + 4 "
222+
"(a[1, 1] Conjugate[a[2, 1]] + a[1, 2] Conjugate[a[2, 2]]) "
223+
"(a[2, 1] Conjugate[a[1, 1]] + a[2, 2] Conjugate[a[1, 2]])] / 2 "
224+
"+ a[1, 1] Conjugate[a[1, 1]] / 2 + a[1, 2] Conjugate[a[1, 2]] / 2 "
225+
"+ a[2, 1] Conjugate[a[2, 1]] / 2 + a[2, 2] Conjugate[a[2, 2]] / 2"
226+
),
227+
None, # "Sympy issue #1156",
228+
),
229+
(
230+
"eigvals[[1]] // FullSimplify",
231+
None,
232+
(
233+
"-Sqrt[(a[1, 1] Conjugate[a[1, 1]] + a[1, 2] Conjugate[a[1, 2]] + "
234+
"a[2, 1] Conjugate[a[2, 1]] + a[2, 2] Conjugate[a[2, 2]]) ^ 2 - "
235+
"4 a[1, 1] a[2, 2] Conjugate[a[1, 1]] Conjugate[a[2, 2]] - "
236+
"4 a[1, 2] a[2, 1] Conjugate[a[1, 2]] Conjugate[a[2, 1]] + "
237+
"4 a[1, 1] a[2, 2] Conjugate[a[1, 2]] Conjugate[a[2, 1]] + "
238+
"4 a[1, 2] a[2, 1] Conjugate[a[1, 1]] Conjugate[a[2, 2]]] / 2"
239+
),
240+
None, # "Sympy issue #1156",
219241
),
220242
],
221243
)

0 commit comments

Comments
 (0)