\nabla symbol and detect args in function

63 views
Skip to first unread message

Yang Liu

unread,
Sep 16, 2022, 8:26:54 AM9/16/22
to sympy
Hi, 

I want to represent the \nabla symbolically without calculation.  Is there a way to declare such a symbol? Also for \triangle?

Moreover, I want to write a function say F(\nabla u) take \nabla*u as an input. Is there a way to detect if I pass the "\nabla" into the function? Pseudo code would be something like:

F(expression):
    if \nabla in expression:
         do something
     else:
 
Many Thanks!

Aaron Meurer

unread,
Sep 16, 2022, 3:27:10 PM9/16/22
to sy...@googlegroups.com
You can name a symbol any string you want, so Symbol(r'\nabla') will
create a symbol named \nabla.

To test if a symbol is in an expression, use .has():

>>> (x + 1).has(x)
True
>>> (y + 1).has(x)
False

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 sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/cab2a16d-8fa0-4875-971e-7e39a10077d2n%40googlegroups.com.

Yang Liu

unread,
Sep 16, 2022, 5:36:56 PM9/16/22
to sympy
Hi Aron,

Many thanks for your answers. I previously use say f.args[0] to access input like \nabla*f to check. But has() is more error free.

Is there any more robust way to get the specific args symbol? For instance, if the input is like (expr=x*y*z). But I want to get the x from this expression. I would say expr.arg[0]. But if I change the expression order. This doesn't work. 

Thanks!

Aaron Meurer

unread,
Sep 16, 2022, 6:02:49 PM9/16/22
to sy...@googlegroups.com
On Fri, Sep 16, 2022 at 3:37 PM Yang Liu <mica...@gmail.com> wrote:
>
> Hi Aron,
>
> Many thanks for your answers. I previously use say f.args[0] to access input like \nabla*f to check. But has() is more error free.
>
> Is there any more robust way to get the specific args symbol? For instance, if the input is like (expr=x*y*z). But I want to get the x from this expression. I would say expr.arg[0]. But if I change the expression order. This doesn't work.

expr.free_symbols will give you a set of all the symbols in an
expression. Does that give you what you need?

If not, I'm not clear what you mean by "the x from this expression".
Note that if you just want a symbol named "x" you can create another
one with Symbol('x'). SymPy treats any two symbols with the same name
and assumptions as equal, so recreating a symbol with the same name is
equivalent to reusing the same symbol object.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/4fe989e9-6104-4026-a3e1-4d112a07f3bfn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages