Reputation: 1769
Suppose I have the following:
x=Symbol('x')
f=Function('f')(x)
type(f)
f
Why is type(f)=f
?
Why it is not UndefinedFunction?
How can I identify this kind of functions more generally?
Upvotes: 2
Views: 772
Reputation:
It's an instance of AppliedUndef, because it's been "applied" (to variable x). Before that, it was an UndefinedFunction.
Let's consider two steps separately: creating a function and applying it. When they are mixed together, we lose the difference between functions and expressions, which is a common source of confusion in SymPy and in math generally.
>>> f = Function('f')
>>> type(f)
<class 'sympy.core.function.UndefinedFunction'>
This is an undefined function. Notably, it's a class and not an object. Perhaps it should be an object (a long-standing issue) but on the other hand, this behavior is consistent with defined functions (sin
, log
) being classes. You can test for f being an UndefinedFunction:
>>> from sympy.core.function import UndefinedFunction
>>> isinstance(f, UndefinedFunction)
True
When you apply f to x, creating f(x)
, the result is a SymPy expression of class f
, just as sin(x)
is an expression of class sin
. How can we test this expression for being an undefined function applied to something? let's see:
>>> type(f(x)).__mro__
(f, AppliedUndef, Function, Application, <class 'sympy.core.expr.Expr'>, <class 'sympy.core.basic.Basic'>, <class 'sympy.core.evalf.EvalfMixin'>, <class 'object'>)
So, AppliedUndef is the class you want to check here.
>>> from sympy.core.function import AppliedUndef
>>> isinstance(f(x), AppliedUndef)
True
Test with isinstance(..., ...)
, not with type(...) == ...
Upvotes: 1