symengine.py icon indicating copy to clipboard operation
symengine.py copied to clipboard

creating a Function results in a segfault

Open mlangiu opened this issue 5 years ago • 2 comments

I am currently trying to implement a custom function creation that works with both sympy and symengine as a backend. For this I looked at the example at https://docs.sympy.org/latest/modules/functions/index.html, but for the equivalent implementation in symengine I get a segmentation fault!

>>> import sympy, symengine
>>> for backend in sympy, symengine:
...     print(backend.__name__, backend.__version__)
...     
...     class my_func(backend.Function):
...         @classmethod
...         def eval(cls, x):
...             if x.is_Number:
...                 if x.is_zero:
...                     return backend.S.One
...                 elif x is backend.S.Infinity:
...                     return backend.S.Zero
...         def _eval_is_real(self):
...             return self.args[0].is_real
...     print(my_func(0))
... 
sympy 1.6.2
1
symengine 0.6.1
Segmentation fault: 11

mlangiu avatar Nov 12 '20 11:11 mlangiu

If instead I change from the class definition to a call to backend.Function and subsequently add the corresponding methods to the new type I don't get the segfault but simplification doesn't work for symengine.

>>> import sympy, symengine
>>> for backend in sympy, symengine:
...     print(backend.__name__, backend.__version__)
...     
...     # class my_func(backend.Function):
...     @classmethod
...     def eval(cls, x):
...         if x.is_Number:
...             if x.is_zero:
...                 return backend.S.One
...             elif x is backend.S.Infinity:
...                 return backend.S.Zero
...     def _eval_is_real(self):
...         return self.args[0].is_real
...     my_func = backend.Function('my_func')
...     my_func.eval = eval
...     my_func._eval_is_real = _eval_is_real
...     print(my_func(0))
... 
sympy 1.6.2
1
symengine 0.6.1
my_func(0)

mlangiu avatar Nov 12 '20 11:11 mlangiu

The _eval_is_real and eval are currently not supported by symengine.

rikardn avatar Jul 21 '21 13:07 rikardn