From df1a32bb6fa4bab7695e3ac98cce3b72beb0854a Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Wed, 4 Nov 2026 22:11:44 +0000 Subject: [PATCH] Make Function.func the class, as in SymPy Function.func was a method that rebuilt the node through the module function named after the class in lowercase, so floor(x).func was never floor, and KroneckerDelta and LambertW, which have no such module function, could not be rebuilt at all. It is now a property returning the class, whose constructor every function class shares with func. An undefined function's func is the function itself, an UndefFunction, which now has a __name__ and compares by name, as SymPy's f(x).func is f. A PyFunction's func rebuilds the wrapped object through its own func, so that replace, subs and func keep it a PyFunction of the same class instead of turning it into an undefined function. --- symengine/lib/symengine_wrapper.in.pyx | 59 +++++++++++++++++++++++--- symengine/tests/test_functions.py | 16 +++++++ symengine/tests/test_sympy_conv.py | 22 ++++++++++ 3 files changed, 91 insertions(+), 6 deletions(-) diff --git a/symengine/lib/symengine_wrapper.in.pyx b/symengine/lib/symengine_wrapper.in.pyx index e25be87e..212ef5bd 100644 --- a/symengine/lib/symengine_wrapper.in.pyx +++ b/symengine/lib/symengine_wrapper.in.pyx @@ -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): @@ -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 @@ -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(o1) @@ -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 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): diff --git a/symengine/tests/test_functions.py b/symengine/tests/test_functions.py index 7c4573df..906387ec 100644 --- a/symengine/tests/test_functions.py +++ b/symengine/tests/test_functions.py @@ -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") diff --git a/symengine/tests/test_sympy_conv.py b/symengine/tests/test_sympy_conv.py index 598a5ff4..2b513433 100644 --- a/symengine/tests/test_sympy_conv.py +++ b/symengine/tests/test_sympy_conv.py @@ -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