Skip to content

Commit

Permalink
Merge branch 'master' into arithmetic_refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed May 27, 2023
2 parents 4cf85ac + 4d0bcb9 commit 279ae2c
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 103 deletions.
4 changes: 2 additions & 2 deletions mathics/builtin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@


def add_builtins(new_builtins):
from mathics.core.convert.sympy import mathics_to_sympy, sympy_to_mathics

for var_name, builtin in new_builtins:
name = builtin.get_name()
if hasattr(builtin, "python_equivalent"):
Expand Down Expand Up @@ -255,8 +257,6 @@ def name_is_builtin_symbol(module, name: str) -> Optional[type]:
_builtins_list.append((instance.get_name(), instance))
builtins_by_module[module.__name__].append(instance)

mathics_to_sympy = {} # here we have: name -> sympy object
sympy_to_mathics = {}

new_builtins = _builtins_list

Expand Down
11 changes: 10 additions & 1 deletion mathics/builtin/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
IterationFunction,
Predefined,
SympyFunction,
SympyObject,
Test,
)
from mathics.builtin.inference import evaluate_predicate, get_assumptions_list
Expand Down Expand Up @@ -788,7 +789,7 @@ def eval_Element_alternatives(
return Element(Expression(elems.head, *unknown), domain)


class I_(Predefined):
class I_(Predefined, SympyObject):
"""
<url>:Imaginary unit:https://en.wikipedia.org/wiki/Imaginary_unit</url> \
(<url>:WMA:https://reference.wolfram.com/language/ref/I.html</url>)
Expand All @@ -805,9 +806,17 @@ class I_(Predefined):
"""

name = "I"
sympy_name = "I"
sympy_obj = sympy.I
summary_text = "imaginary unit"
python_equivalent = 1j

def is_constant(self) -> bool:
return True

def to_sympy(self, symb, **kwargs):
return self.sympy_obj

def evaluate(self, evaluation: Evaluation):
return Complex(Integer0, Integer1)

Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
String,
)
from mathics.core.attributes import A_HOLD_ALL, A_NO_ATTRIBUTES, A_PROTECTED
from mathics.core.convert.expression import to_expression, to_numeric_sympy_args
from mathics.core.convert.expression import to_expression
from mathics.core.convert.op import ascii_operator_to_symbol
from mathics.core.convert.python import from_bool
from mathics.core.convert.sympy import from_sympy
from mathics.core.convert.sympy import from_sympy, to_numeric_sympy_args
from mathics.core.definitions import Definition
from mathics.core.evaluation import Evaluation
from mathics.core.exceptions import MessageException
Expand Down
3 changes: 1 addition & 2 deletions mathics/builtin/specialfns/elliptic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
from mathics.builtin.base import SympyFunction
from mathics.core.atoms import Integer
from mathics.core.attributes import A_LISTABLE, A_NUMERIC_FUNCTION, A_PROTECTED
from mathics.core.convert.expression import to_numeric_sympy_args
from mathics.core.convert.sympy import from_sympy
from mathics.core.convert.sympy import from_sympy, to_numeric_sympy_args
from mathics.eval.numerify import numerify


Expand Down
17 changes: 0 additions & 17 deletions mathics/core/convert/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,6 @@ def to_numeric_args(mathics_args: Type[BaseElement], evaluation) -> list:
)


def to_numeric_sympy_args(mathics_args: Type[BaseElement], evaluation) -> list:
"""
Convert Mathics arguments, such as the arguments in an evaluation
method a Python list that is sutiable for feeding as arguments
into SymPy.
We make use of fast conversions for literals.
"""
if mathics_args.is_literal:
sympy_args = [mathics_args.value]
else:
args = numerify(mathics_args, evaluation).get_sequence()
sympy_args = [a.to_sympy() for a in args]

return sympy_args


expression_constructor_map = {
SymbolList: lambda head, *args, **kwargs: ListExpression(*args, **kwargs)
}
Loading

0 comments on commit 279ae2c

Please sign in to comment.