On Fri, Jul 22, 2022 at 10:51 AM Anton Makarov <[email protected]> wrote:
>
> Hi, I am new to sympy. I am trying to understand what is the best practice to 
> check head of expression? For example:
> expr = sympify("somefunc(1-x)")
> Is this a best practice or there is a better way to do this?

Undefined functions created from sympify will have the
UndefinedFunction metaclass, which you can test directly with

>>> from sympy.core.function import UndefinedFunction
>>> isinstance(sympify("somefunc(1-x)").func, UndefinedFunction)
True
>>> sympify("somefunc(1-x)").func.name == 'somefunc'
True

Or alternately you can just check

>>> sympify("somefunc(1-x)").func == Function('somefunc')
True

Note that this won't apply to functions that are predefined in SymPy like sin().

> How to understand that head of expr is somefunc? I can do it like this:
> if str(expr.func) == "somefunc"

> And by the way, if I have the expression:
> expr = sympify("x")
> How can I check that expr is some variable (not necessary "x", but in more 
> broader sense)?

isinstance(expr, Symbol)

> Or:
> expr = sympify("5.5777")
> how to check that expr is some number?

isinstance(expr, Number)

Aaron Meurer

>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/654926aa-8c56-4a27-8ca6-68f389273475n%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6KnDmRkb_rPBgWBr1V1dFF4yx%3DS%2Bccw81yVMJ98LLARmQ%40mail.gmail.com.

Reply via email to