Skip to content

Commit 2223b99

Browse files
authored
Merge pull request #466 from isuruf/func
Raise error on wrong number of arguments to Function and add name property
2 parents a79eeee + a165060 commit 2223b99

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

symengine/lib/symengine_wrapper.in.pyx

+13-2
Original file line numberDiff line numberDiff line change
@@ -2415,8 +2415,15 @@ class Pow(Expr):
24152415
class Function(Expr):
24162416

24172417
def __new__(cls, *args, **kwargs):
2418-
if cls == Function and len(args) == 1:
2419-
return UndefFunction(args[0])
2418+
if cls == Function:
2419+
nargs = len(args)
2420+
if nargs == 0:
2421+
raise TypeError("Required at least one argument to Function")
2422+
elif nargs == 1:
2423+
return UndefFunction(args[0])
2424+
elif nargs > 1:
2425+
raise TypeError(f"Unexpected extra arguments {args[1:]}.")
2426+
24202427
return super(Function, cls).__new__(cls)
24212428

24222429
@property
@@ -2837,6 +2844,10 @@ class FunctionSymbol(Function):
28372844
name = deref(X).get_name().decode("utf-8")
28382845
return str(name)
28392846

2847+
@property
2848+
def name(Basic self):
2849+
return self.get_name()
2850+
28402851
def _sympy_(self):
28412852
import sympy
28422853
name = self.get_name()

symengine/tests/test_functions.py

+12
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ def test_derivative():
103103
assert i == fxy.diff(y, 1, x)
104104

105105

106+
def test_function():
107+
x = Symbol("x")
108+
fx = Function("f")(x)
109+
assert fx == function_symbol("f", x)
110+
111+
raises(TypeError, lambda: Function("f", "x"))
112+
raises(TypeError, lambda: Function("f", x))
113+
raises(TypeError, lambda: Function())
114+
115+
assert fx.name == "f"
116+
117+
106118
def test_abs():
107119
x = Symbol("x")
108120
e = abs(x)

0 commit comments

Comments
 (0)