Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error on wrong number of arguments to Function and add name property #466

Merged
merged 2 commits into from
Jan 23, 2024
Merged
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
15 changes: 13 additions & 2 deletions symengine/lib/symengine_wrapper.in.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2412,8 +2412,15 @@ class Pow(Expr):
class Function(Expr):

def __new__(cls, *args, **kwargs):
if cls == Function and len(args) == 1:
return UndefFunction(args[0])
if cls == Function:
nargs = len(args)
if nargs == 0:
raise TypeError("Required at least one argument to Function")
elif nargs == 1:
return UndefFunction(args[0])
elif nargs > 1:
raise TypeError(f"Unexpected extra arguments {args[1:]}.")

return super(Function, cls).__new__(cls)

@property
Expand Down Expand Up @@ -2834,6 +2841,10 @@ class FunctionSymbol(Function):
name = deref(X).get_name().decode("utf-8")
return str(name)

@property
def name(Basic self):
return self.get_name()

def _sympy_(self):
import sympy
name = self.get_name()
Expand Down
12 changes: 12 additions & 0 deletions symengine/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ def test_derivative():
assert i == fxy.diff(y, 1, x)


def test_function():
x = Symbol("x")
fx = Function("f")(x)
assert fx == function_symbol("f", x)

raises(TypeError, lambda: Function("f", "x"))
raises(TypeError, lambda: Function("f", x))
raises(TypeError, lambda: Function())

assert fx.name == "f"


def test_abs():
x = Symbol("x")
e = abs(x)
Expand Down