Skip to content

Commit

Permalink
fix tests for sympy conversions (#1073)
Browse files Browse the repository at this point in the history
This PR just fixes the pytest, and marks some issues in the conversion
of Lambda functions.

---------

Co-authored-by: rocky <[email protected]>
  • Loading branch information
mmatera and rocky committed Aug 29, 2024
1 parent fce0ddc commit d539620
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 33 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ build:
# because pip install doesn't handle
# INSTALL_REQUIRES properly
#: Set up to run from the source tree
develop: mathics/data/op-tables.json
develop: mathics/data/op-tables.json mathics/data/operators.json
$(PIP) install -e .[dev]

# See note above on ./setup.py
Expand All @@ -71,7 +71,7 @@ develop-full: mathics/data/op-tables.json

# See note above on ./setup.py
#: Set up to run from the source tree with full dependencies and Cython
develop-full-cython: mathics/data/op-tables.json
develop-full-cython: mathics/data/op-tables.json mathics/data/operators.json
$(PIP) install -e .[dev,full,cython]


Expand Down Expand Up @@ -145,6 +145,10 @@ latexdoc texdoc doc:
mathics/data/op-tables.json:
$(BASH) ./admin-tools/make-op-tables.sh

#: Build JSON ASCII to operator tables
mathics/data/operators.json:
$(BASH) ./admin-tools/make-operator-tables.sh

#: Remove ChangeLog
rmChangeLog:
$(RM) ChangeLog || true
Expand Down
2 changes: 1 addition & 1 deletion admin-tools/make-op-tables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mydir=$(dirname $bs)
PYTHON=${PYTHON:-python}

cd $mydir/../mathics/data
mathics-generate-json-table \
mathics3-generate-json-table \
--field=ascii-operator-to-symbol \
--field=ascii-operator-to-unicode \
--field=ascii-operator-to-wl-unicode \
Expand Down
25 changes: 21 additions & 4 deletions mathics/core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
"""

import importlib
import os.path as osp
import re
from functools import lru_cache, total_ordering
from itertools import chain
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union, cast

import mpmath
import pkg_resources
import sympy

from mathics.core.atoms import (
Expand Down Expand Up @@ -67,6 +69,21 @@
from mathics.eval.numerify import numerify
from mathics.eval.scoping import dynamic_scoping

try:
import ujson
except ImportError:
import json as ujson

ROOT_DIR = pkg_resources.resource_filename("mathics", "")

# Load the conversion tables from disk
characters_path = osp.join(ROOT_DIR, "data", "operator-tables.json")
assert osp.exists(
characters_path
), f"ASCII operator to Unicode tables are missing from {characters_path}"
with open(characters_path, "r") as f:
operator_data = ujson.load(f)


class Builtin:
"""
Expand Down Expand Up @@ -982,16 +999,16 @@ def __init__(self, format_function, *args, **kwargs):
super().__init__(*args, **kwargs)
name = self.get_name()
if self.needs_verbatim:
name = "Verbatim[%s]" % name
name = f"Verbatim[{name}"
if self.default_formats:
op_pattern = "%s[item_]" % name
op_pattern = f"{name}[item_]"
if op_pattern not in self.formats:
operator = self.get_operator_display()
if operator is not None:
form = '%s[{HoldForm[item]},"%s",%d]' % (
format_function,
operator,
self.precedence,
operator_data["operator-precedence"].get(name, self.precedence),
)
self.formats[op_pattern] = form

Expand Down Expand Up @@ -1032,7 +1049,7 @@ def __init__(self, *args, **kwargs):
formatted = "MakeBoxes[Infix[{%s}, %s, %d,%s], form]" % (
replace_items,
operator,
self.precedence,
operator_data["operator-precedence"].get(name, self.precedence),
self.grouping,
)
default_rules = {
Expand Down
4 changes: 2 additions & 2 deletions mathics/core/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
# I put this constants here instead of inside `mathics.core.convert.sympy`
# to avoid a circular reference. Maybe they should be in its own module.

sympy_symbol_prefix = "_Mathics_User_"
sympy_slot_prefix = "_Mathics_Slot_"
sympy_symbol_prefix = "_mu_"
sympy_slot_prefix = "_ms_"


# FIXME: This is repeated below
Expand Down
71 changes: 47 additions & 24 deletions test/core/test_sympy_python_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,39 @@
from mathics.core.convert.sympy import from_sympy
from mathics.core.expression import Expression
from mathics.core.list import ListExpression
from mathics.core.symbols import Symbol, SymbolPlus
from mathics.core.symbols import (
Symbol,
SymbolPlus,
sympy_slot_prefix,
sympy_symbol_prefix,
)
from mathics.core.systemsymbols import (
SymbolD,
SymbolDerivative,
SymbolFunction,
SymbolGamma,
SymbolIntegrate,
SymbolSin,
SymbolSlot,
)


class SympyConvert(unittest.TestCase):
def compare_to_sympy(self, mathics_expr, sympy_expr, **kwargs):
mathics_expr.to_sympy(**kwargs) == sympy_expr
assert mathics_expr.to_sympy(**kwargs) == sympy_expr

def compare_to_mathics(self, mathics_expr, sympy_expr, **kwargs):
mathics_expr == from_sympy(sympy_expr, **kwargs)
assert mathics_expr == from_sympy(sympy_expr, **kwargs)

def compare(self, mathics_expr, sympy_expr, **kwargs):
self.compare_to_sympy(mathics_expr, sympy_expr, **kwargs)
self.compare_to_mathics(mathics_expr, sympy_expr)

def testSymbol(self):
self.compare(Symbol("Global`x"), sympy.Symbol("_Mathics_User_Global`x"))
self.compare(Symbol("Global`x"), sympy.Symbol(f"{sympy_symbol_prefix}Global`x"))
self.compare(
Symbol("_Mathics_User_x"),
sympy.Symbol("_Mathics_User_System`_Mathics_User_x"),
sympy.Symbol(f"{sympy_symbol_prefix}System`_Mathics_User_x"),
)

def testReal(self):
Expand Down Expand Up @@ -81,24 +88,24 @@ def testString(self):
def testAdd(self):
self.compare(
Expression(SymbolPlus, Integer1, Symbol("Global`x")),
sympy.Add(sympy.Integer(1), sympy.Symbol("_Mathics_User_Global`x")),
sympy.Add(sympy.Integer(1), sympy.Symbol(f"{sympy_symbol_prefix}Global`x")),
)

def testIntegrate(self):
self.compare(
Expression(SymbolIntegrate, Symbol("Global`x"), Symbol("Global`y")),
sympy.Integral(
sympy.Symbol("_Mathics_User_Global`x"),
sympy.Symbol("_Mathics_User_Global`y"),
sympy.Symbol(f"{sympy_symbol_prefix}Global`x"),
sympy.Symbol(f"{sympy_symbol_prefix}Global`y"),
),
)

def testDerivative(self):
self.compare(
Expression(SymbolD, Symbol("Global`x"), Symbol("Global`y")),
sympy.Derivative(
sympy.Symbol("_Mathics_User_Global`x"),
sympy.Symbol("_Mathics_User_Global`y"),
sympy.Symbol(f"{sympy_symbol_prefix}Global`x"),
sympy.Symbol(f"{sympy_symbol_prefix}Global`y"),
),
)

Expand All @@ -111,11 +118,13 @@ def testDerivative2(self):
)
expr = Expression(head, Symbol("Global`x"), Symbol("Global`y"))

sfxy = sympy.Function(str("_Mathics_User_Global`f"))(
sympy.Symbol("_Mathics_User_Global`x"),
sympy.Symbol("_Mathics_User_Global`y"),
sfxy = sympy.Function(str(f"{sympy_symbol_prefix}Global`f"))(
sympy.Symbol(f"{sympy_symbol_prefix}Global`x"),
sympy.Symbol(f"{sympy_symbol_prefix}Global`y"),
)
sym_expr = sympy.Derivative(
sfxy, sympy.Symbol(f"{sympy_symbol_prefix}Global`x")
)
sym_expr = sympy.Derivative(sfxy, sympy.Symbol("_Mathics_User_Global`x"))

self.compare_to_sympy(expr, sym_expr, **kwargs)
# compare_to_mathics fails because Derivative becomes D (which then evaluates to Derivative)
Expand All @@ -124,28 +133,28 @@ def testConvertedFunctions(self):
kwargs = {"converted_functions": set(["Global`f"])}

marg1 = Expression(Symbol("Global`f"), Symbol("Global`x"))
sarg1 = sympy.Function(str("_Mathics_User_Global`f"))(
sympy.Symbol("_Mathics_User_Global`x")
sarg1 = sympy.Function(str(f"{sympy_symbol_prefix}Global`f"))(
sympy.Symbol(f"{sympy_symbol_prefix}Global`x")
)
self.compare(marg1, sarg1, **kwargs)

marg2 = Expression(Symbol("Global`f"), Symbol("Global`x"), Symbol("Global`y"))
sarg2 = sympy.Function(str("_Mathics_User_Global`f"))(
sympy.Symbol("_Mathics_User_Global`x"),
sympy.Symbol("_Mathics_User_Global`y"),
sarg2 = sympy.Function(str(f"{sympy_symbol_prefix}Global`f"))(
sympy.Symbol(f"{sympy_symbol_prefix}Global`x"),
sympy.Symbol(f"{sympy_symbol_prefix}Global`y"),
)
self.compare(marg2, sarg2, **kwargs)

self.compare(
Expression(SymbolD, marg2, Symbol("Global`x")),
sympy.Derivative(sarg2, sympy.Symbol("_Mathics_User_Global`x")),
sympy.Derivative(sarg2, sympy.Symbol(f"{sympy_symbol_prefix}Global`x")),
**kwargs,
)

def testExpression(self):
self.compare(
Expression(SymbolSin, Symbol("Global`x")),
sympy.sin(sympy.Symbol("_Mathics_User_Global`x")),
sympy.sin(sympy.Symbol(f"{sympy_symbol_prefix}Global`x")),
)

def testConstant(self):
Expand All @@ -155,14 +164,28 @@ def testConstant(self):
def testGamma(self):
self.compare(
Expression(SymbolGamma, Symbol("Global`z")),
sympy.gamma(sympy.Symbol("_Mathics_User_Global`z")),
sympy.gamma(sympy.Symbol(f"{sympy_symbol_prefix}Global`z")),
)
self.compare(
Expression(SymbolGamma, Symbol("Global`z"), Symbol("Global`x")),
sympy.uppergamma(
sympy.Symbol("_Mathics_User_Global`z"),
sympy.Symbol("_Mathics_User_Global`x"),
sympy.Symbol(f"{sympy_symbol_prefix}Global`z"),
sympy.Symbol(f"{sympy_symbol_prefix}Global`x"),
),
)

def testSlots(self):
"""check the conversion of slots in anonymous functions."""
sympy_symbol = sympy.Symbol("x")
sympy_lambda_expr = sympy.Lambda(sympy_symbol, sympy_symbol + 1)
# compare_to_sympy does not pass because Slot[1] are translated as
# functions
self.compare_to_mathics(
Expression(
SymbolFunction,
Expression(SymbolPlus, Integer1, Expression(SymbolSlot, Integer1)),
),
sympy_lambda_expr,
)


Expand Down

0 comments on commit d539620

Please sign in to comment.