Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
58 changes: 57 additions & 1 deletion symengine/lib/symengine_wrapper.in.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
30 changes: 30 additions & 0 deletions symengine/tests/test_sympy_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading