Skip to content

Commit 5f32b4d

Browse files
authored
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 5e89a3e commit 5f32b4d

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

mathics/builtin/numbers/calculus.py

Lines changed: 18 additions & 14 deletions
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
@@ -2249,7 +2249,7 @@ def eval(self, eqs, vars, evaluation: Evaluation):
22492249
return
22502250
all_var_tuples = list(zip(vars, vars_sympy))
22512251

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

2267-
def solve_sympy(equations: Expression | list[Expression]):
2267+
def solve_sympy(equations: Union[Expression, list[Expression]]):
22682268
if not isinstance(equations, list):
22692269
equations = [equations]
22702270
equations_sympy = []
@@ -2285,10 +2285,11 @@ def solve_sympy(equations: Expression | list[Expression]):
22852285
equation_sympy = left - right
22862286
equation_sympy = sympy.together(equation_sympy)
22872287
equation_sympy = sympy.cancel(equation_sympy)
2288+
equations_sympy.append(equation_sympy)
22882289
numer, denom = equation_sympy.as_numer_denom()
22892290
denoms_sympy.append(denom)
22902291
try:
2291-
results = sympy.solve(equations_sympy, subset_vars_sympy, dict=True) # no transform needed with dict=True
2292+
results = sympy.solve(equations_sympy, subset_vars_sympy, dict=True) # no transform_dict needed with dict=True
22922293
# Filter out results for which denominator is 0
22932294
# (SymPy should actually do that itself, but it doesn't!)
22942295
results = [
@@ -2312,21 +2313,21 @@ def solve_recur(expression: Expression):
23122313
but including the translation from Mathics to sympy
23132314
23142315
returns:
2315-
solutions: a list of sympy solution dictionaries
2316+
solutions: a list of sympy solution dictionarys
23162317
conditions: a sympy condition object
23172318
23182319
note:
23192320
for And and List, should always return either (solutions, None) or ([], conditions)
2320-
for Or, all combinations are possible. if Or is root, should be handled outside'''
2321+
for Or, all combinations are possible. if Or is root, this should be handled outside'''
23212322
head = expression.get_head_name()
2322-
if head in ("System`And", "System`List"):
2323+
if head in ('System`And', 'System`List'):
23232324
solutions = []
23242325
equations: list[Expression] = []
23252326
inequations = []
23262327
for child in expression.elements:
23272328
if child.has_form("Equal", 2):
23282329
equations.append(child)
2329-
elif child.get_head_name() in ("System`And", "System`Or"):
2330+
elif child.get_head_name() in ('System`And', 'System`Or'):
23302331
sub_solution, sub_condition = solve_recur(child)
23312332
solutions.extend(sub_solution)
23322333
if sub_condition is not None:
@@ -2337,14 +2338,14 @@ def solve_recur(expression: Expression):
23372338
conditions = sympy.And(*inequations)
23382339
result = [sol for sol in solutions if conditions.subs(sol)]
23392340
return result, None if solutions else conditions
2340-
else: # should be System`Or then
2341-
assert head == "System`Or"
2341+
else: # assume should be System`Or
2342+
assert head == 'System`Or'
23422343
solutions = []
23432344
conditions = []
23442345
for child in expression.elements:
23452346
if child.has_form("Equal", 2):
23462347
solutions.extend(solve_sympy(child))
2347-
elif child.get_head_name() in ("System`And", "System`Or"): # List wouldn't be in here
2348+
elif child.get_head_name() in ('System`And', 'System`Or'): # I don't believe List would be in here
23482349
sub_solution, sub_condition = solve_recur(child)
23492350
solutions.extend(sub_solution)
23502351
if sub_condition is not None:
@@ -2358,17 +2359,20 @@ def solve_recur(expression: Expression):
23582359

23592360
if eqs.get_head_name() in ("System`List", "System`And", "System`Or"):
23602361
solutions, conditions = solve_recur(eqs)
2361-
# non True conditions are only accepted in subtrees, not root
2362+
# non True conditions are only accepted in subtrees only, not root
23622363
if conditions is not None:
23632364
evaluation.message("Solve", "fulldim")
2364-
return ListExpression(ListExpression())
23652365
else:
23662366
if eqs.has_form("Equal", 2):
23672367
solutions = solve_sympy(eqs)
23682368
else:
23692369
evaluation.message("Solve", "fulldim")
23702370
return ListExpression(ListExpression())
23712371

2372+
if solutions is None:
2373+
evaluation.message("Solve", "ivars")
2374+
return ListExpression(ListExpression())
2375+
23722376
if any(
23732377
sol and any(var not in sol for var in vars_sympy) for sol in solutions
23742378
):
@@ -2379,7 +2383,7 @@ def solve_recur(expression: Expression):
23792383
ListExpression(
23802384
*(
23812385
Expression(SymbolRule, var, from_sympy(sol[var_sympy]))
2382-
for var, var_sympy in zip(vars, all_var_tuples)
2386+
for var, var_sympy in all_var_tuples
23832387
if var_sympy in sol
23842388
),
23852389
)

0 commit comments

Comments
 (0)