2
$\begingroup$

In an expression like

expr = 2 g Pi - (2 I) G Pi - (2 I) Pi V0;

obviously $2 Pi$ is some sort of symbolic GCD. In such a simple case I could eyeball it and divide:

expr/(2 Pi) // Simplify
(* g - I(G + V0) *)

If the expression is more complex I would like to have a function that automatically identifies the symbolic GCD and then divide by it. I managed to write such a function (plus another auxiliary one). Here is the code:

Clear[gcdComplex, gcdPlusExpression];
gcdComplex[cp_Complex] /; And @@ IntegerQ /@ ReIm[4 + 8 I] := 
 With[{gcd = GCD @@ ReIm[cp]}, List[gcd, cp/gcd]]
gcdComplex[x_] := x
gcdPlusExpression[expr_Plus] := 
 Module[{l1 = List @@ expr, l2, l3, l4, gcd},
  l2 = (List @@ # &) /@ l1;
  l3 = Map[gcdComplex, l2, 2];
  l4 = Flatten /@ l3;
  gcd = Times @@ Intersection @@ l4;
  {gcd,expr/gcd // Simplify}]
gcdPlusExpression@expr
(* {2 Pi, g - I (G + V0)} *)

I wonder if there is a better/simpler way.

$\endgroup$
  • 2
    $\begingroup$ Wouldn't Factor help you? $\endgroup$ – halirutan Oct 9 '16 at 20:47
  • 4
    $\begingroup$ In two steps (easily combined into one, I wanted to make the process reasonably clear): gcd = PolynomialGCD @@ expr Out[10]= 2 Pi In[11]:= Together[expr/gcd] Out[11]= g - I G - I V0 $\endgroup$ – Daniel Lichtblau Oct 9 '16 at 21:04
  • $\begingroup$ @DanielLichtblau - Thank you! Your one-liner makes my oversized code look foolish. But at least it was good practice. $\endgroup$ – Soldalma Oct 10 '16 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.