Skip to content

Commit 405cce1

Browse files
committed
fix test fail issue
1. added one missed line and a few catches 2. replace function type hint with 3.9 compatible style
1 parent 71a7558 commit 405cce1

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

mathics/builtin/numbers/calculus.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111

1212
from itertools import product
13-
from typing import Optional
13+
from typing import Optional, Union
1414

1515
import numpy as np
1616
import sympy
@@ -2251,7 +2251,7 @@ def eval(self, eqs, vars, evaluation: Evaluation):
22512251
return
22522252
all_var_tuples = list(zip(vars, vars_sympy))
22532253

2254-
def cut_var_dimension(expressions: Expression | list[Expression]):
2254+
def cut_var_dimension(expressions: Union[Expression, list[Expression]]):
22552255
'''delete unused variables to avoid SymPy's PolynomialError
22562256
: Not a zero-dimensional system in e.g. Solve[x^2==1&&z^2==-1,{x,y,z}]'''
22572257
if not isinstance(expressions, list):
@@ -2266,7 +2266,7 @@ def cut_var_dimension(expressions: Expression | list[Expression]):
22662266
subset_vars_sympy.add(var_sympy)
22672267
return subset_vars, subset_vars_sympy
22682268

2269-
def solve_sympy(equations: Expression | list[Expression]):
2269+
def solve_sympy(equations: Union[Expression, list[Expression]]):
22702270
if not isinstance(equations, list):
22712271
equations = [equations]
22722272
equations_sympy = []
@@ -2287,10 +2287,11 @@ def solve_sympy(equations: Expression | list[Expression]):
22872287
equation_sympy = left - right
22882288
equation_sympy = sympy.together(equation_sympy)
22892289
equation_sympy = sympy.cancel(equation_sympy)
2290+
equations_sympy.append(equation_sympy)
22902291
numer, denom = equation_sympy.as_numer_denom()
22912292
denoms_sympy.append(denom)
22922293
try:
2293-
results = sympy.solve(equations_sympy, subset_vars_sympy, dict=True) # no transform needed with dict=True
2294+
results = sympy.solve(equations_sympy, subset_vars_sympy, dict=True) # no transform_dict needed with dict=True
22942295
# Filter out results for which denominator is 0
22952296
# (SymPy should actually do that itself, but it doesn't!)
22962297
results = [
@@ -2314,21 +2315,21 @@ def solve_recur(expression: Expression):
23142315
but including the translation from Mathics to sympy
23152316
23162317
returns:
2317-
solutions: a list of sympy solution dictionaries
2318+
solutions: a list of sympy solution dictionarys
23182319
conditions: a sympy condition object
23192320
23202321
note:
23212322
for And and List, should always return either (solutions, None) or ([], conditions)
2322-
for Or, all combinations are possible. if Or is root, should be handled outside'''
2323+
for Or, all combinations are possible. if Or is root, this should be handled outside'''
23232324
head = expression.get_head_name()
2324-
if head in ("System`And", "System`List"):
2325+
if head in ('System`And', 'System`List'):
23252326
solutions = []
23262327
equations: list[Expression] = []
23272328
inequations = []
23282329
for child in expression.elements:
23292330
if child.has_form("Equal", 2):
23302331
equations.append(child)
2331-
elif child.get_head_name() in ("System`And", "System`Or"):
2332+
elif child.get_head_name() in ('System`And', 'System`Or'):
23322333
sub_solution, sub_condition = solve_recur(child)
23332334
solutions.extend(sub_solution)
23342335
if sub_condition is not None:
@@ -2339,14 +2340,14 @@ def solve_recur(expression: Expression):
23392340
conditions = sympy.And(*inequations)
23402341
result = [sol for sol in solutions if conditions.subs(sol)]
23412342
return result, None if solutions else conditions
2342-
else: # should be System`Or then
2343-
assert head == "System`Or"
2343+
else: # assume should be System`Or
2344+
assert head == 'System`Or'
23442345
solutions = []
23452346
conditions = []
23462347
for child in expression.elements:
23472348
if child.has_form("Equal", 2):
23482349
solutions.extend(solve_sympy(child))
2349-
elif child.get_head_name() in ("System`And", "System`Or"): # List wouldn't be in here
2350+
elif child.get_head_name() in ('System`And', 'System`Or'): # I don't believe List would be in here
23502351
sub_solution, sub_condition = solve_recur(child)
23512352
solutions.extend(sub_solution)
23522353
if sub_condition is not None:
@@ -2360,17 +2361,20 @@ def solve_recur(expression: Expression):
23602361

23612362
if eqs.get_head_name() in ("System`List", "System`And", "System`Or"):
23622363
solutions, conditions = solve_recur(eqs)
2363-
# non True conditions are only accepted in subtrees, not root
2364+
# non True conditions are only accepted in subtrees only, not root
23642365
if conditions is not None:
23652366
evaluation.message("Solve", "fulldim")
2366-
return ListExpression(ListExpression())
23672367
else:
23682368
if eqs.has_form("Equal", 2):
23692369
solutions = solve_sympy(eqs)
23702370
else:
23712371
evaluation.message("Solve", "fulldim")
23722372
return ListExpression(ListExpression())
23732373

2374+
if solutions is None:
2375+
evaluation.message("Solve", "ivars")
2376+
return ListExpression(ListExpression())
2377+
23742378
if any(
23752379
sol and any(var not in sol for var in vars_sympy) for sol in solutions
23762380
):
@@ -2381,7 +2385,7 @@ def solve_recur(expression: Expression):
23812385
ListExpression(
23822386
*(
23832387
Expression(SymbolRule, var, from_sympy(sol[var_sympy]))
2384-
for var, var_sympy in zip(vars, all_var_tuples)
2388+
for var, var_sympy in all_var_tuples
23852389
if var_sympy in sol
23862390
),
23872391
)

0 commit comments

Comments
 (0)