Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sum/Product fix #869

Merged
merged 3 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions mathics/builtin/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1836,8 +1836,13 @@ def get_result(self, items):
def to_sympy(self, expr, **kwargs):
if expr.has_form('Sum', 2) and expr.leaves[1].has_form('List', 3):
index = expr.leaves[1]
arg = expr.leaves[0].to_sympy()
bounds = (index.leaves[0].to_sympy(), index.leaves[1].to_sympy(), index.leaves[2].to_sympy())
arg_kwargs = kwargs.copy()
arg_kwargs['convert_all_global_functions'] = True
arg = expr.leaves[0].to_sympy(**arg_kwargs)
bounds = (index.leaves[0].to_sympy(**kwargs),
index.leaves[1].to_sympy(**kwargs),
index.leaves[2].to_sympy(**kwargs))

if arg is not None and None not in bounds:
return sympy.summation(arg, bounds)

Expand Down Expand Up @@ -1900,7 +1905,9 @@ def to_sympy(self, expr, **kwargs):
if expr.has_form('Product', 2) and expr.leaves[1].has_form('List', 3):
index = expr.leaves[1]
try:
e = expr.leaves[0].to_sympy(**kwargs)
e_kwargs = kwargs.copy()
e_kwargs['convert_all_global_functions'] = True
e = expr.leaves[0].to_sympy(**e_kwargs)
i = index.leaves[0].to_sympy(**kwargs)
start = index.leaves[1].to_sympy(**kwargs)
stop = index.leaves[2].to_sympy(**kwargs)
Expand Down
21 changes: 15 additions & 6 deletions mathics/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,18 +631,27 @@ def has_symbol(self, symbol_name) -> bool:
return self.head.has_symbol(symbol_name) or any(
leaf.has_symbol(symbol_name) for leaf in self.leaves)

def _as_sympy_function(self, **kwargs) -> sympy.Function:
sym_args = [leaf.to_sympy(**kwargs) for leaf in self.leaves]

if None in sym_args:
return None

f = sympy.Function(str(sympy_symbol_prefix + self.get_head_name()))
return f(*sym_args)

def to_sympy(self, **kwargs):
from mathics.builtin import mathics_to_sympy

if 'convert_all_global_functions' in kwargs:
if len(self.leaves) > 0 and kwargs['convert_all_global_functions']:
if self.get_head_name().startswith('Global`'):
return self._as_sympy_function(**kwargs)

if 'converted_functions' in kwargs:
functions = kwargs['converted_functions']
if len(self.leaves) > 0 and self.get_head_name() in functions:
sym_args = [leaf.to_sympy() for leaf in self.leaves]
if None in sym_args:
return None
func = sympy.Function(str(
sympy_symbol_prefix + self.get_head_name()))(*sym_args)
return func
return self._as_sympy_function(**kwargs)

lookup_name = self.get_lookup_name()
builtin = mathics_to_sympy.get(lookup_name)
Expand Down