differentiation between defined and undefined functions

144 views
Skip to first unread message

vweber

unread,
Apr 13, 2012, 7:57:51 AM4/13/12
to sympy
Dear All
How is it possible to differentiate between defined(internal) and
undefined functions?
For example:

x = symbols('x')
f = Function('f')(x)
eq=cos(x)+f
s=eq.atoms(Function)
>>> s
set(f(x), cos(x))

so I would like

for i in s:
IS_INTERNAL(i) or IS_DEFINED(i) or whatever

and get something like that

f(x) -> False
cos(x) -> True

Thanks
V

Tom Bachmann

unread,
Apr 13, 2012, 8:15:58 AM4/13/12
to sy...@googlegroups.com
Hi,

On 13.04.2012 12:57, vweber wrote:
> Dear All
> How is it possible to differentiate between defined(internal) and
> undefined functions?
> For example:
>
> x = symbols('x')
> f = Function('f')(x)
> eq=cos(x)+f
> s=eq.atoms(Function)
>>>> s
> set(f(x), cos(x))
>
> so I would like
>
> for i in s:
> IS_INTERNAL(i) or IS_DEFINED(i) or whatever
>

AppliedUndef may help:

In [1]: x = symbols('x')
In [2]: f = Function('f')(x)
In [3]: eq=cos(x)+f
In [4]: from sympy.core.function import AppliedUndef

In [5]: eq.atoms(AppliedUndef)
Out[5]: set(f(x))

In [6]: isinstance(cos(x), AppliedUndef)
Out[6]: False

In [7]: isinstance(f, AppliedUndef)
Out[7]: True

Chris Smith

unread,
Apr 13, 2012, 8:37:38 AM4/13/12
to sy...@googlegroups.com

see also the identical question

https://mail.google.com/mail/u/0/#search/AppliedUndef/13697af357f5cfa3

```
> What is the best way to test if a sympy expression contains a function,
> either a named function such as sin(x) or a general function such as
> f(x,y,z). I would prefer not to differentiate with respect to all possible
> arguments and see if all the results are zero.
>>> eq=f(x)+sin(x)
>>> eq.has(Function) # best way to see if it's there
True
>>> eq.atoms(Function) # use this when you need to know what they are

set([f(x), sin(x)])
f(x) is an instance of AppliedUndef
f is an instance of UndefinedFunction
sin(x) is an instance of Function
sin is an instance of FunctionClass
In retrospect, more symmetry in naming would have been nice, e.g. f(x)
being UndefinedFunc and f being UndefinedFuncClass.
```

(If this is not in the FAQ, it should be.)

/c

Aaron Meurer

unread,
Apr 13, 2012, 12:56:53 PM4/13/12
to sy...@googlegroups.com
Note that if your definition of internal does not include your own
custom subclasses of Function, you'll have to do an additional check
(checking .__module__ should be enough).

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

Reply all
Reply to author
Forward
0 new messages