Using Mathematica (v.7) basically I want to bring an expression like this
(x + x^2 + x^3)^4
to
x^4 (1 + x + x^2)^4
What would be the best way to take a term like the GCD out of an expression that is raised to a power and is in factored form; then place that term outside the brackets and retain that exponent value it was raised to. It would have to know that the value is raised to a power, before taking it out. Here is my attempt.
In[28]:= example = (x + x^2 + x^3)^4
Out[28]= (x + x^2 + x^3)^4
In[37]:= gcdVar = PolynomialGCD[Sequence @@ Level[example, {2}]]
Out[37]= x
In[40]:= step1 = Map[Divide[#, gcdVar] &, example, {2}]
Out[40]= (1 + x + x^2)^4
In[55]:= step2 = Times[step1, Power[gcdVar, Last[Level[example, {1}]]]]
Out[55]= x^4 (1 + x + x^2)^4
I have been looking at all the different functions related to this area like; Collect, Factor, Expand, Simplify, Solve. I don't think any of them can produce the output I wanted. Is there a built in, more efficient, scalable and shorter way to do this possibly using pattern / form matching?
This does what you do quick 'n' dirty style, but in one line:
example /. Power_[Plus_[f__], k_] :>
(PolynomialGCD@f)^k Simplify@(Plus@f/PolynomialGCD@f)^k
This is not very robust, and you'll be better of building your own small Module that checks for things like there actually being a greatest common divisor.
EDIT: You can add some inline checking like so:
example /. Power_[Plus_[f__], k_] /; !(PolynomialGCD@f === 1) :>
(PolynomialGCD@f)^k Simplify@(Plus@f/PolynomialGCD@f)^k