Finding what function is used in an expression

15 views
Skip to first unread message

Ben Lucato

unread,
Jan 16, 2013, 11:23:49 PM1/16/13
to sy...@googlegroups.com
An expression like this returns like so:

(2 * sin(3 * x) + 3).find(sympy.Function)
>>> sin(3 * x)

how do I get the output 

>>> sympy.sin 

instead? i.e. find just the function being used? I don't want to do (2 * sin(3 * x) + 3).has(sympy.sin) for each possible function, unless I have to.


Chris Smith

unread,
Jan 16, 2013, 11:38:36 PM1/16/13
to sy...@googlegroups.com
On Thu, Jan 17, 2013 at 10:08 AM, Ben Lucato <ben.l...@gmail.com> wrote:
> An expression like this returns like so:
>
> (2 * sin(3 * x) + 3).find(sympy.Function)
>>>> sin(3 * x)
>
> how do I get the output
>

>>> eq
sin(x)
>>> [f.func for f in eq.atoms(Function)]
[sin]

If you know there is only one function then you can append [0] to the
above list comprehension to get only that function...but be careful in
case there were no functions.

Ben Lucato

unread,
Jan 16, 2013, 11:44:11 PM1/16/13
to sy...@googlegroups.com
Perfect - exactly what I was looking for. With what I am doing there will only be one function (never less, never more) so this solution works well with [0]
Reply all
Reply to author
Forward
0 new messages