# Collecting common powers of terms

How to collect the common powers appearing in the following function using MATHEMATICA, such that if I input

(4 m^6)/((m - n)^2 (m + n)^4 (2m+n)^4)


then the output should be displayed as

((2 m^3)/((m - n) (m + n)^2 (2m+n)^2))^2



Edit: In general if there are many terms with different powers in both the numerator and the denominator then what is required is the GCD of all the powers of the terms in both the numerator and the denominator. For example if we have follows

(a1^(n1)a2^(n2)a3^(n3)a4^(n4)...a^(nn))/(b1^(m1)b2^(m2)b3^(m3)b4^(m4)...bm^(mm))



The what could be taken as the common power is the

GCD(n1,n2,...,nn,m1,m2,m3....mm)


expr = (4 m^6)/((m - n)^2 (m + n)^4 (2 m + n)^4);


The form that you requested will automatically simplify to the original expression.

((2 m^3)/((m - n) (m + n)^2 (2 m + n)^2))^2

(* (4 m^6)/((m - n)^2 (m + n)^4 (2 m + n)^4) *&)


To keep the requested form you need to prevent the automatic simplification

expr2 = Module[{$$a}, Inactive[Power][ ($$a /. Solve[expr == $$a^2,$$a][[-1]]), 2]] expr2 // Activate

(* (4 m^6)/((m - n)^2 (m + n)^4 (2 m + n)^4) *)


EDIT:

expr = (a1^3 a2^6 a3^9 a4^6 a5^6)/(b1^3 b2^12 b3^15 b4^9 b5^6);

gcd = GCD @@ Cases[expr, x_^p_. :> p, 1]

(* 3 *)

Inactive[Power][(expr /. x_^p_ :> x^(p/gcd)), gcd] (% // Activate) === expr

(* True *)


EDIT 2:

format[expr_] := Module[{coef, gcd},
coef = expr /. {a_Integer r_ :> a, r_ :> 1};
gcd = GCD @@
Cases[expr/coef, (a : _Integer : 1) x_^p_. :> p, 1];
Inactive[Power][
(expr/(coef^(1/gcd)) /. x_^p_ :> x^(p/gcd)), gcd]]

format[(4 m^6)/((m - n)^2 (m + n)^4 (2 m + n)^4)] format[(a1^3 a2^6 a3^9 a4^6 a5^6)/(b1^3 b2^12 b3^15 b4^9 b5^6)] • this works fine but what if the common power is something different. In more general cases the common power would be the GCD of all the powers in both the numerator and denominator. BDW I have edited the question to make this thing more clear as I suspected it wasn't before. Aug 20, 2020 at 2:25
• what's the meaning of dot in gcd = GCD @@ Cases[expr, x_^p_. :> p, 1]? Aug 20, 2020 at 4:51
• @PureLine, "_. represents an optional argument to a function, with a default value specified by Default." Compare results from x /. t_^p_ :> p with those from x /. t_^p_. :> p In general, highlight unknown element ( _. in this case), then press F1 for help. Aug 20, 2020 at 5:02
• The second code that you gave doesn't seem to work for the original expression that I gave. The problem is with the number 4 appearing in the numerator. During the pattern matching it code doesn't seem to recognize 4. Can you suggest a way out to that?? Aug 20, 2020 at 5:32