Skip to content

Commit

Permalink
Merge pull request #466 from isuruf/func
Browse files Browse the repository at this point in the history
Raise error on wrong number of arguments to Function and add name property
  • Loading branch information
isuruf authored Jan 23, 2024
2 parents a79eeee + a165060 commit 2223b99
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 13 additions & 2 deletions symengine/lib/symengine_wrapper.in.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2415,8 +2415,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 @@ -2837,6 +2844,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

0 comments on commit 2223b99

Please sign in to comment.