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

Adding pytests for from_sympy / to_sympy. #828

Merged
merged 6 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@ -237,8 +239,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 @@ -767,7 +768,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 @@ -784,9 +785,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
2 changes: 2 additions & 0 deletions mathics/core/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,8 @@ def is_zero(self) -> bool:


RationalOneHalf = Rational(1, 2)
MATHICS3_COMPLEX_I = Complex(Integer0, Integer1)
MATHICS3_COMPLEX_I_NEG = Complex(Integer0, IntegerM1)


class String(Atom, BoxElementMixin):
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)
}
35 changes: 13 additions & 22 deletions mathics/core/convert/mpmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@
import mpmath
import sympy

from mathics.core.atoms import (
Complex,
Integer0,
Integer1,
IntegerM1,
MachineReal,
MachineReal0,
PrecisionReal,
)
from mathics.core.atoms import Complex, MachineReal, MachineReal0, PrecisionReal
from mathics.core.element import BaseElement
from mathics.core.expression import Expression
from mathics.core.systemsymbols import SymbolDirectedInfinity, SymbolIndeterminate

ExpressionInfinity = Expression(SymbolDirectedInfinity, Integer1)
ExpressionMInfinity = Expression(SymbolDirectedInfinity, IntegerM1)
ExpressionIInfinity = Expression(SymbolDirectedInfinity, Complex(Integer0, Integer1))
ExpressionMIInfinity = Expression(SymbolDirectedInfinity, Complex(Integer0, IntegerM1))

ExpressionComplexInfinity = Expression(SymbolDirectedInfinity)
from mathics.core.expression import (
MATHICS3_COMPLEX_INFINITY,
MATHICS3_I_INFINITY,
MATHICS3_I_NEG_INFINITY,
MATHICS3_INFINITY,
MATHICS3_NEG_INFINITY,
)
from mathics.core.systemsymbols import SymbolIndeterminate


@lru_cache(maxsize=1024)
Expand All @@ -41,7 +32,7 @@ def from_mpmath(
return SymbolIndeterminate
if isinstance(value, mpmath.mpf):
if mpmath.isinf(value):
return ExpressionInfinity if value > 0 else ExpressionMInfinity
return MATHICS3_INFINITY if value > 0 else MATHICS3_NEG_INFINITY
if precision is None:
return MachineReal(float(value))
# If the error if of the order of the number, the number
Expand All @@ -56,10 +47,10 @@ def from_mpmath(
val_re, val_im = value.real, value.imag
if mpmath.isinf(val_re):
if mpmath.isinf(val_im):
return ExpressionComplexInfinity
return ExpressionInfinity if val_re > 0 else ExpressionMInfinity
return MATHICS3_COMPLEX_INFINITY
return MATHICS3_INFINITY if val_re > 0 else MATHICS3_NEG_INFINITY
elif mpmath.isinf(val_im):
return ExpressionIInfinity if val_im > 0 else ExpressionMIInfinity
return MATHICS3_I_INFINITY if val_im > 0 else MATHICS3_I_NEG_INFINITY
real = from_mpmath(val_re, precision=precision)
imag = from_mpmath(val_im, precision=precision)
return Complex(real, imag)
Expand Down
Loading