programmatically determining the terms in an expression

21 views
Skip to first unread message

Nathan Thern

unread,
Nov 4, 2016, 5:25:04 PM11/4/16
to sympy
I have performed a long series of symbolic calculations to arrive at an expression:

(xaa*(-λaa - λab + 1) - xaa + xabaa + xacab)
/
sqrt
((-xaa*(-λaa - λab + 1) - xabaa - xacab + xba*(-λba - λbb + 1) + xbbba + xbcbb)**2 +
     
(-yaa*(-λaa - λab + 1) - yabaa - yacab + yba*(-λba - λbb + 1) + ybbba + ybcbb)**2 +
     
(-zaa*(-λaa - λab + 1) - zabaa - zacab + zba*(-λba - λbb + 1) + zbbba + zbcbb)**2)


which is of the form

(Fab + G)/sqrt(Aab**2 + Bab + C)

Given the input, how do I assign the right expressions to A,B,C,F & G?

This seems like a common theme for a newbie question, and I'm sorry if it's been asked before; but I couldn't find anything in this list that helped me. I'd rather have a few hints or pointers to the right place in the manual than a complete solution.

thanks,
NT


Nathan Thern

unread,
Nov 4, 2016, 5:49:21 PM11/4/16
to sympy
Actually, I think I can do it with the args property. If I'm not on the right track, someone let me know.
I'm still figuring out how I can determine the power terms

Nathan Thern

unread,
Nov 4, 2016, 6:02:32 PM11/4/16
to sympy
OK,

Poly(expr.args[0].args[0],λab).coeffs()


gets me the A, B & C coefficients inside the square root.
Sorry if I wasted everyone's time, but I'm going to leave the post here in case it helps someone else out.

Aaron Meurer

unread,
Nov 4, 2016, 6:19:37 PM11/4/16
to sy...@googlegroups.com
I think you are looking for pattern matching.
http://docs.sympy.org/dev/modules/core.html#sympy.core.basic.Basic.match

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 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/134c2253-a5a4-4102-993e-2029604b0c09%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages