From d0b070118f2eb2451614228088b391ee0a593e81 Mon Sep 17 00:00:00 2001 From: Juan Mauricio Matera Date: Tue, 20 Aug 2024 09:18:51 -0300 Subject: [PATCH] fix tests for sympy conversions (#1073) This PR just fixes the pytest, and marks some issues in the conversion of Lambda functions. --------- Co-authored-by: rocky --- Makefile | 8 ++- admin-tools/make-op-tables.sh | 2 +- admin-tools/make-operator-tables.sh | 8 +++ mathics/core/builtin.py | 25 +++++++-- mathics/core/symbols.py | 4 +- test/core/test_sympy_python_convert.py | 71 +++++++++++++++++--------- 6 files changed, 85 insertions(+), 33 deletions(-) create mode 100755 admin-tools/make-operator-tables.sh diff --git a/Makefile b/Makefile index 423e35ba2..d01a2937f 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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] @@ -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 diff --git a/admin-tools/make-op-tables.sh b/admin-tools/make-op-tables.sh index 77faabf0d..03c1d0a5d 100755 --- a/admin-tools/make-op-tables.sh +++ b/admin-tools/make-op-tables.sh @@ -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 \ diff --git a/admin-tools/make-operator-tables.sh b/admin-tools/make-operator-tables.sh new file mode 100755 index 000000000..9ebc6afbf --- /dev/null +++ b/admin-tools/make-operator-tables.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# Create ASCII operator to Unicode tables +bs=${BASH_SOURCE[0]} +mydir=$(dirname $bs) +PYTHON=${PYTHON:-python} + +cd $mydir/../mathics/data +mathics3-generate-operator-json-table -o operator-tables.json diff --git a/mathics/core/builtin.py b/mathics/core/builtin.py index 598972e19..3de0acff5 100644 --- a/mathics/core/builtin.py +++ b/mathics/core/builtin.py @@ -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 ( @@ -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: """ @@ -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 @@ -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 = { diff --git a/mathics/core/symbols.py b/mathics/core/symbols.py index 1be4ce772..ed00a4988 100644 --- a/mathics/core/symbols.py +++ b/mathics/core/symbols.py @@ -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 diff --git a/test/core/test_sympy_python_convert.py b/test/core/test_sympy_python_convert.py index a6f4668e3..d9bd2a241 100644 --- a/test/core/test_sympy_python_convert.py +++ b/test/core/test_sympy_python_convert.py @@ -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): @@ -81,15 +88,15 @@ 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"), ), ) @@ -97,8 +104,8 @@ 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"), ), ) @@ -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) @@ -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): @@ -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, )