Any way to define a function with parameters?

41 views
Skip to first unread message

Jisoo Song

unread,
Mar 27, 2020, 5:09:10 AM3/27/20
to sympy
Let's think of general quadratic function: a*x**2 + b*x + c. Commonly, this function is represented as f(x) = a*x**2 + b*x + c.
However, we can also write it as f(x; a,b,c), which implies that a,b and c can also be replaced by other values.
Does sympy have algorithm for such notation, so that `f(x, evaluate=False)` returns `f(x; a,b,c)`?

S.Y. Lee

unread,
Mar 27, 2020, 11:01:34 AM3/27/20
to sympy
I don't think that a*x**2 + b*+ c.is actually considered a 'Function', but just as an 'Expression' in sympy.
And literally you can just apply (a*x**2 + b*+ c).subs({a: 1}) to do evaluations with respect to any variables. 

Jisoo Song

unread,
Mar 28, 2020, 2:14:55 AM3/28/20
to sympy


2020년 3월 28일 토요일 오전 12시 1분 34초 UTC+9, S.Y. Lee 님의 말:
 a*x**2 + b*+ c is clearly not a function, but `f(x)` which returns ` a*x**2 + b*+ c` is, a function.
We can write a code like this:

class f(Function):
    @classmethod
    def eval(cls, x):
        return a*x**2 + b*x + c

What I wanted to do was to generate another function class from f, which will have 1 for `a` so that it can return x**2 + b*x + c when called.
I think this will need some metaclass-related tweaks by now.
Reply all
Reply to author
Forward
0 new messages