Reputation: 3672
I have an expression where three terms involve r**2
, I'd like to collect those together and factor out the r**2
:
>>> foo = a**2*r**2 + 2*a*b*x*r + 2*a*b*r**2 + b**2 + 2*b**2*r + b**2*r**2
>>> foo
2 2 2 2 2 2 2
a ⋅r + 2⋅a⋅b⋅r + 2⋅a⋅b⋅r⋅x + b ⋅r + 2⋅b ⋅r + b
simplify
doesn't alter the expression at all.
In my actual use case, foo
is actually a subexpression: I'm dividing by the coefficient of r**2, so that will cancel off for that term, and everything is under a square root.
So the general question is: if I want to effect a transformation of a subexpression, e.g. factoring certain terms, how do I do that on a sub expression, as opposed to the whole expression?
Upvotes: 0
Views: 170
Reputation: 14480
You can use collect
:
In [84]: collect(foo, r**2)
Out[84]:
2 2 2 ⎛ 2 2⎞
2⋅a⋅b⋅r⋅x + 2⋅b ⋅r + b + r ⋅⎝a + 2⋅a⋅b + b ⎠
Upvotes: 1