6
$\begingroup$

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)
$\endgroup$
0

1 Answer 1

3
$\begingroup$
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]]

enter image description here

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]

enter image description here

(% // 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)]

enter image description here

format[(a1^3 a2^6 a3^9 a4^6 a5^6)/(b1^3 b2^12 b3^15 b4^9 b5^6)]

enter image description here

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Erosannin
    Aug 20, 2020 at 2:25
  • $\begingroup$ what's the meaning of dot in gcd = GCD @@ Cases[expr, x_^p_. :> p, 1]? $\endgroup$
    – PureLine
    Aug 20, 2020 at 4:51
  • $\begingroup$ @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. $\endgroup$
    – Bob Hanlon
    Aug 20, 2020 at 5:02
  • $\begingroup$ 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?? $\endgroup$
    – Erosannin
    Aug 20, 2020 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.