How to implement a function with Python(Sympy), realizing the same as '_' and replacement rule in Wolfram Mathematica?

53 views
Skip to first unread message

Xuemei Gu

unread,
Dec 11, 2018, 3:03:43 AM12/11/18
to sympy
hey, everyone.

hello, I recently played with sympy for comparing with wolfram language. I wonder whether there is a simple way to do the _ (blank) as mathematica do.

Here is a link: 


Can anyone know? Or give any hints? Thanks a lot!

Ishan Anirudh Joshi

unread,
Dec 14, 2018, 9:54:31 AM12/14/18
to sympy
I think using Wild symbols along with replace could be used for the purpose.
Wild symbols are used for pattern matching.

>>> l1 = Wild('l1')
>>> l2 = Wild('l2')

>>> testexpr = p1[MM]*p2[NN] + p1[XX]*p2[MM] + p1[XX]**2

>>> t = testexpr.replace(p1[l1]*p2[l2], FF1[l1]*FF2[l2])
>>> t =  t.replace(p1[l1]**2, 0)
>>> t
FF1[MM]*FF2[NN] + FF1[XX]*FF2[MM]


Hope that helps!

Xuemei Gu

unread,
Dec 14, 2018, 10:04:39 AM12/14/18
to sy...@googlegroups.com
hey,

Thank you very much for the kind reply. I have already solve it as the way you suggested. 


I have another question, let's say:

expression:   a[1]*b[2]*c[3]*d[4]+a[1]**2*b[3]*c[1]*d[4]+a[1]*b[2]**2*c[3]+b[2]*c[3]+...

I only want to take the cases for terms containing b[xxx]*c[xxx]*d[xxx], other cases are all becoming to 0.

Do you have any ideas? I thought about match, but seems wrong.

Thank you very much!
Best regards,
Xuemei Gu


--
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/DXfsFNYbjW0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/74c91bfa-9c28-4c5b-871b-f77afb75b493%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ishan Anirudh Joshi

unread,
Dec 14, 2018, 2:39:46 PM12/14/18
to sympy
For this particular expression I guess, something like this can be done:

>>> w = Wild('w')
>>> x = Wild('x')
>>> y = Wild('y')
>>> z = wild('z')

>>> expr = a[1]*b[2]*c[3]*d[4] + a[1]**2*b[3]*c[1]*d[4] + a[1]*b[2]**2*c[3] + b[2]*c[3]

>>> p = expr.replace(w*b[x]*c[y]*d[z], 0)                                                              # p has the terms which are not needed
>>> sol = expr - p

>>> sol
a[1]**2*b[3]*c[1]*d[4] + a[1]*b[2]*c[3]*d[4]

>>> p
Reply all
Reply to author
Forward
0 new messages