From 2537dad0870df62b02b602b2b9b52d3b6cc2c279 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Thu, 26 Nov 2026 16:24:08 -0700 Subject: [PATCH] Keep SymPy symbol assumptions when converting to SymEngine SymPy symbols with assumptions are converted to SympySymbol, a PySymbol backed Symbol subclass that stores the original SymPy object. Converting back returns the original symbol and assumption queries like is_real are answered by SymPy. Fixes #522 --- symengine/lib/symengine_wrapper.in.pyx | 58 +++++++++++++++++++++++++- symengine/tests/test_sympy_conv.py | 30 +++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/symengine/lib/symengine_wrapper.in.pyx b/symengine/lib/symengine_wrapper.in.pyx index 5548e9a4..1d0e7897 100644 --- a/symengine/lib/symengine_wrapper.in.pyx +++ b/symengine/lib/symengine_wrapper.in.pyx @@ -281,7 +281,9 @@ def sympy2symengine(a, raise_error=False): if isinstance(a, sympy.Dummy): return Dummy(a.name, a.dummy_index) elif isinstance(a, sympy.Symbol): - return Symbol(a.name) + if type(a) is sympy.Symbol and a.assumptions0 == {'commutative': True}: + return Symbol(a.name) + return SympySymbol(a) elif isinstance(a, sympy.Mul): return mul(*[sympy2symengine(x, raise_error) for x in a.args]) elif isinstance(a, sympy.Add): @@ -1440,6 +1442,60 @@ cdef class Symbol(Expr): return self.__class__ +class SympySymbol(Symbol): + """ + A Symbol that wraps a SymPy Symbol (or a subclass) so that information + not representable in SymEngine, like assumptions, is kept. Converting + back to SymPy returns the original SymPy object and assumption queries + like ``is_real`` are answered by SymPy. + """ + + def __new__(cls, sympy_obj): + return Symbol.__new__(cls, sympy_obj.name) + + def __init__(self, sympy_obj): + self._sympy_obj = sympy_obj + super().__init__(sympy_obj.name, store_pickle=True) + + def __reduce__(self): + return (SympySymbol, (self._sympy_obj,)) + + def _sympy_(self): + return self._sympy_obj + + @property + def is_zero(self): + return self._sympy_obj.is_zero + + @property + def is_positive(self): + return self._sympy_obj.is_positive + + @property + def is_negative(self): + return self._sympy_obj.is_negative + + @property + def is_nonpositive(self): + return self._sympy_obj.is_nonpositive + + @property + def is_nonnegative(self): + return self._sympy_obj.is_nonnegative + + @property + def is_real(self): + return self._sympy_obj.is_real + + @property + def is_integer(self): + return self._sympy_obj.is_integer + + @property + def is_complex(self): + return self._sympy_obj.is_complex + + cdef class Dummy(Symbol): def __init__(Basic self, name=None, dummy_index=None, *args, **kwargs): diff --git a/symengine/tests/test_sympy_conv.py b/symengine/tests/test_sympy_conv.py index 598a5ff4..8d137b37 100644 --- a/symengine/tests/test_sympy_conv.py +++ b/symengine/tests/test_sympy_conv.py @@ -41,6 +41,36 @@ def test_conv1(): assert x._sympy_() == sympy.Symbol("y") +@unittest.skipIf(not have_sympy, "SymPy not installed") +def test_conv_symbol_assumptions(): + import pickle + x = sympy.Symbol("x", real=True) + p = sympy.Symbol("p", positive=True) + t = sympy.Symbol("t") + + # Plain SymPy symbols are still converted to plain SymEngine symbols + assert type(sympify(t)) == Symbol + + xs = sympify(x) + assert isinstance(xs, Symbol) + assert xs.name == "x" + assert xs.is_real is True + assert sympify(p).is_positive is True + assert xs._sympy_() is x + + # assumptions survive arithmetic and conversion back to SymPy + e = sympify(x**2 + p + t) + assert sympy.sympify(e) == x**2 + p + t + assert sympy.sympify(e - t - p) == x**2 + assert sympy.sympify(e).subs(x, 2) == 4 + p + t + assert {s.is_real for s in e.free_symbols if s.name == "x"} == {True} + + # pickling keeps the assumptions + e2 = pickle.loads(pickle.dumps(e)) + assert e2 == e + assert sympy.sympify(e2) == x**2 + p + t + + @unittest.skipIf(not have_sympy, "SymPy not installed") def test_conv1b(): x = sympy.Symbol("x")