simplification using substitution

49 views
Skip to first unread message

Dave M

unread,
Apr 4, 2014, 12:14:48 PM4/4/14
to sy...@googlegroups.com
Hi
I've tried subs() for this but it's not working for me. I have the following expression:

j*(3*x**4 + 6*x**2*y**2 - 24*x**2*z**2 + 3*y**4 - 24*y**2*z**2 + 8*z**4)/(2*(x**2 + y**2 + z**2)**(9/2))

I want to simplify it by making the substitutions r = sympy.sqrt(x*x + y*y + z*z) and s = sympy.sqrt(x*x + y*y) so that it becomes something like

j/r**9*(4*z**2*(z**2 - 3*s**2) + 3/2*s**4)

can this be done with sympy, and if so how?
Thanks

      

Aaron Meurer

unread,
Apr 4, 2014, 6:22:48 PM4/4/14
to sy...@googlegroups.com
The subs for s = sqrt(x**2 + y**2 + z**2) should work.

To make the other one work, try subs(x**2, s**2 - y**2), followed by a call to factor().

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 http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/a1c4dbb4-0263-495b-9a54-18fd33c4d431%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave M

unread,
Apr 6, 2014, 12:35:35 PM4/6/14
to sy...@googlegroups.com
Thanks for responding Aaron. Here is the relevant part of my session for the attempted r substitution. Can you tell me what I'm doing incorrectly and how to do it right?

In [113]: f = j*(3*x**4 + 6*x**2*y**2 - 24*x**2*z**2 + 3*y**4 - 24*y**2*z**2 + 8*z**4)/(2*(x**2 + y**2 + z**2)**(9/2))

In [114]: f
Out[114]: j*(x**2 + y**2 + z**2)**(-4.5)*(3*x**4 + 6*x**2*y**2 - 24*x**2*z**2 + 3*y**4 - 24*y**2*z**2 + 8*z**4)/2

In [115]: f.subs(sympy.sqrt(x**2+y**2+z**2),r)
Out[115]: j*(x**2 + y**2 + z**2)**(-4.5)*(3*x**4 + 6*x**2*y**2 - 24*x**2*z**2 + 3*y**4 - 24*y**2*z**2 + 8*z**4)/2




Aaron Meurer

unread,
Apr 6, 2014, 1:31:36 PM4/6/14
to sy...@googlegroups.com
First off, make sure not to have 9/2, as Python converts this into a Float. Use Rational(9, 2) or S(9)/2.

What is your r? It works for me:

In [10]: r = Symbol('r')

In [11]: f = j*(3*x**4 + 6*x**2*y**2 - 24*x**2*z**2 + 3*y**4 - 24*y**2*z**2 + 8*z**4)/(2*(x**2 + y**2 + z**2)**(9/2))

In [12]: print(f.subs(sqrt(x**2+y**2+z**2),r))
j*(3*x**4 + 6*x**2*y**2 - 24*x**2*z**2 + 3*y**4 - 24*y**2*z**2 + 8*z**4)/(2*r**9)


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 http://groups.google.com/group/sympy.

Dave M

unread,
Apr 6, 2014, 3:23:50 PM4/6/14
to sy...@googlegroups.com
Thanks for the tips. My problem seems to have been I had r already defined (to sympy.sqrt(x**2 + y**2 + z**2). I guess it cannot be pre-defined for subs to work correctly. Works now. Thanks!

BTW I really like sympy. I just stumbled across it last week by accident and had to try it out. And very cool that it has special functions. The predefined Legendre polynomials are saving me lots of time from having to key them in.

Aaron Meurer

unread,
Apr 6, 2014, 5:12:55 PM4/6/14
to sy...@googlegroups.com
Yes, this is what I figured. Read the page I referred you to. It discusses this. You need to be clear about the difference between Python variables and SymPy symbols.

Aaron Meurer


On Sun, Apr 6, 2014 at 2:23 PM, Dave M <daveme...@gmail.com> wrote:
Thanks for the tips. My problem seems to have been I had r already defined (to sympy.sqrt(x**2 + y**2 + z**2). I guess it cannot be pre-defined for subs to work correctly. Works now. Thanks!

BTW I really like sympy. I just stumbled across it last week by accident and had to try it out. And very cool that it has special functions. The predefined Legendre polynomials are saving me lots of time from having to key them in.

--
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 http://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
0 new messages