Skip to content

Commit

Permalink
Remove "sympy_name" in from_sympy() calls: (#1070)
Browse files Browse the repository at this point in the history
Generally, we can get sympy_name from the object.
Previously, we sometimes we had two-parameter "from_sympy()" calls and
three-parameter calls where sometiems a parameter might not be used.

Overall, I think this will simplifies things.

Some type annotations were added.
  • Loading branch information
rocky committed Aug 13, 2024
1 parent 222b686 commit e9e8fa7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions mathics/builtin/numbers/calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

from itertools import product
from typing import Optional
from typing import Iterable, Optional

import numpy as np
import sympy
Expand Down Expand Up @@ -442,7 +442,7 @@ class Derivative(PostfixOperator, SympyFunction):
The idea of the test is to set `Derivative[n][f]` to `nothing`. Then, the derivative is
evaluated. If it is not possible to find an explicit expression for the derivative,
then their occurencies are replaced by `nothing`. Therefore, if the resulting expression
if free of `nothing`, then we can use the result. Otherwise, the rule does not work.
if free of `nothing`, then we can use the result. Otherwise, the rule does not work.
Differently from `True` and `False`, `List` does not produce an infinite recurrence,
but since is a protected symbol, the following test produces error messages.
Expand Down Expand Up @@ -1042,7 +1042,7 @@ def prepare_sympy(self, elements):
return [elements[0]] + x.elements
return elements

def from_sympy(self, sympy_name, elements):
def from_sympy(self, elements: Iterable) -> Expression:
args = []
for element in elements[1:]:
if element.has_form("List", 1):
Expand Down
3 changes: 2 additions & 1 deletion mathics/builtin/numbers/exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from collections import namedtuple
from contextlib import contextmanager
from itertools import chain
from typing import Iterable

import mpmath

Expand Down Expand Up @@ -184,7 +185,7 @@ class Exp(MPMathFunction):
}
summary_text = "exponential function"

def from_sympy(self, sympy_name, elements):
def from_sympy(self, elements: Iterable) -> Expression:
return Expression(SymbolPower, SymbolE, elements[0])


Expand Down
6 changes: 4 additions & 2 deletions mathics/builtin/specialfns/gamma.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
Gamma and Related Functions
"""

import sys
from typing import Iterable

import mpmath
import sympy
Expand Down Expand Up @@ -326,8 +328,8 @@ class Gamma(MPMathMultiFunction):
def get_sympy_names(self):
return ["gamma", "uppergamma", "lowergamma"]

def from_sympy(self, sympy_name, elements):
if sympy_name == "lowergamma":
def from_sympy(self, elements: Iterable) -> Expression:
if self.sympy_name == "lowergamma":
# lowergamma(z, x) -> Gamma[z, 0, x]
z, x = elements
return Expression(Symbol(self.get_name()), z, Integer0, x)
Expand Down
9 changes: 5 additions & 4 deletions mathics/core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def get_constant(self, precision, evaluation, have_mpmath=False):
else:
return PrecisionReal(sympy_fn.n(d))

def get_sympy_function(self, elements=None):
def get_sympy_function(self, elements=None) -> Optional[Callable]:
if self.sympy_name:
return getattr(sympy, self.sympy_name)
return None
Expand All @@ -546,12 +546,13 @@ def to_sympy(self, expr, **kwargs):
if None in sympy_args:
return None
sympy_function = self.get_sympy_function(elements)
return sympy_function(*sympy_args)
if sympy_function is not None:
return sympy_function(*sympy_args)
except TypeError:
pass

def from_sympy(self, sympy_name, elements):
return to_expression(self.get_name(), *elements)
def from_sympy(self, elements: list) -> Expression:
return Expression(Symbol(self.get_name()), *elements)

def prepare_mathics(self, sympy_expr):
return sympy_expr
Expand Down
4 changes: 2 additions & 2 deletions mathics/core/convert/sympy.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def old_from_sympy(expr) -> BaseElement:
else:
margs.append(from_sympy(arg))
builtin = sympy_to_mathics.get(name)
return builtin.from_sympy(name, margs)
return builtin.from_sympy(margs)

elif isinstance(expr, sympy.sign):
name = "Sign"
Expand All @@ -517,7 +517,7 @@ def old_from_sympy(expr) -> BaseElement:
args = [from_sympy(arg) for arg in expr.args]
builtin = sympy_to_mathics.get(name)
if builtin is not None:
return builtin.from_sympy(name, args)
return builtin.from_sympy(args)
return Expression(Symbol(name), *args)

if isinstance(expr, sympy.Tuple):
Expand Down

0 comments on commit e9e8fa7

Please sign in to comment.