Skip to content
Closed
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
59 changes: 53 additions & 6 deletions symengine/lib/symengine_wrapper.in.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2545,9 +2545,9 @@ class Function(Expr):
def is_Function(self):
return True

def func(self, *values):
import sys
return getattr(sys.modules[__name__], self.__class__.__name__.lower())(*values)
@property
def func(self):
return self.__class__


class OneArgFunction(Function):
Expand Down Expand Up @@ -2973,9 +2973,10 @@ class AppliedUndef(Function):
name = self.get_name()
return sage.function(name)(*self.args_as_sage())

def func(self, *values):
name = self.get_name()
return function_symbol(name, *values)
@property
def func(self):
# Like SymPy's, the undefined function itself: f(x).func is f.
return UndefFunction(self.get_name())


FunctionSymbol = AppliedUndef # an alias
Expand All @@ -2985,9 +2986,22 @@ class UndefFunction(object):
def __init__(self, name):
self.name = name

@property
def __name__(self):
return self.name

def __call__(self, *values):
return function_symbol(self.name, *values)

def __eq__(self, other):
return isinstance(other, UndefFunction) and self.name == other.name

def __hash__(self):
return hash((UndefFunction, self.name))

def __repr__(self):
return self.name


cdef rcp_const_basic pynumber_to_symengine(PyObject* o1):
cdef Basic X = sympify(<object>o1)
Expand Down Expand Up @@ -3082,9 +3096,42 @@ class PyFunction(FunctionSymbol):
import sage.all as sage
return sage.SR(self.pyobject())

@property
def func(self):
"""Rebuilds the wrapped object with new arguments through its own
``func``, so the result is again a PyFunction of the same class."""
pyfunc = getattr(self.pyobject(), "func", None)
if pyfunc is None:
return UndefFunction(self.get_name())
return _PyFunctionFunc(pyfunc)

def pyobject(Basic self):
return <object>deref(symengine.rcp_static_cast_PyFunction(self.thisptr)).get_py_object()

class _PyFunctionFunc(object):
"""``func`` of a PyFunction: its wrapped (SymPy) class, applied to
SymEngine arguments and converted back."""

def __init__(self, pyfunc):
self.pyfunc = pyfunc

@property
def __name__(self):
return self.pyfunc.__name__

def __call__(self, *values):
return sympify(self.pyfunc(*[_sympify(v)._sympy_() for v in values]))

def __eq__(self, other):
return isinstance(other, _PyFunctionFunc) and self.pyfunc == other.pyfunc

def __hash__(self):
return hash((_PyFunctionFunc, self.pyfunc))

def __repr__(self):
return repr(self.pyfunc)


cdef class PyFunctionClass(object):

def __cinit__(self, function, PyModule module not None):
Expand Down
16 changes: 16 additions & 0 deletions symengine/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ def test_f():
assert f == g


def test_func():
x = Symbol("x")
y = Symbol("y")
for e in (sin(x), log(x), log(x, 2), floor(x), ceiling(x), LambertW(x),
KroneckerDelta(x, y), zeta(x, y), exp(x), sign(x)):
assert e.func is type(e)
assert e.func(*e.args) == e
# An undefined function's func is the function itself, as in SymPy
f = function_symbol("f", x, y)
assert f.func(*f.args) == f
assert f.func == function_symbol("f", y).func
assert f.func != function_symbol("g", x).func
assert f.func.__name__ == "f"
assert f.func(y) == function_symbol("f", y)


def test_derivative():
x = Symbol("x")
y = Symbol("y")
Expand Down
22 changes: 22 additions & 0 deletions symengine/tests/test_sympy_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,3 +863,25 @@ def test_evalf_symbolic_pyfunction():
f = sympify(sympy.besselj(x, 1))
assert isinstance(f, PyFunction)
assert f.n() == f


@unittest.skipIf(not have_sympy, "SymPy not installed")
def test_pyfunction_func():
class price(sympy.Function):
@staticmethod
def _imp_(a):
return 3 * a

x = Symbol("x")
y = Symbol("y")
p = sympify(price(sympy.Symbol("x")))
assert isinstance(p, PyFunction)
assert p.func.__name__ == "price"
assert p.func(*p.args) == p
# Rebuilding with new arguments keeps the wrapped SymPy class
for q in (p.func(y), p.subs(x, y),
sympify(price(sympy.floor(sympy.Symbol("y")))).replace(
lambda e: isinstance(e, floor), lambda e: e.args[0])):
assert isinstance(q, PyFunction)
assert q == sympify(price(sympy.Symbol("y")))
assert type(q.pyobject())._imp_(2) == 6
Loading