I am trying to do the following:

Expand[(A g + n)^2 Conjugate[A g + n]^2, Element[{A, g, n}, Complexes]]

The goal is to expand and get all the terms of $(A g + n)^2 ((A g + n)^*)^2$ where the variables are complex numbers. Ideally, in the form of either $A A^*gg^* + \ldots$ or $|A|^2 |g|^2 + \ldots$.

Unfortunately, all I get as an output is:

==> (A g+n)^2 ((A g+n)^*)^2

How do I get Mathematica to actually expand this expression in terms of the complex variables?

ComplexExpand[] is not useful because it splits up the real and imaginary parts which is going too far for what I want.

  • 1
    $\begingroup$ "ComplexExpand[] is not useful..." - ...because you did not supply the second argument and the TargetFunctions setting: ComplexExpand[(A g + n)^2 Conjugate[A g + n]^2, {A, g, n}, TargetFunctions -> Conjugate] $\endgroup$ Jan 28, 2020 at 17:12
  • $\begingroup$ Thanks, that did the trick! Is there anyway to get Mathematica to collect $AA^*$ into $|A|^2$? $\endgroup$
    – XYZT
    Jan 28, 2020 at 17:15

1 Answer 1


As pointed out in comment by J.M., use the option TargetFunctions


expr = (A g + n)^2 Conjugate[A g + n]^2;

expr2 = expr // ComplexExpand[#, {A, g, n},
    TargetFunctions -> Conjugate] &

(* A^2 g^2 Conjugate[A]^2 Conjugate[g]^2 + 
 2 A g n Conjugate[A]^2 Conjugate[g]^2 + n^2 Conjugate[A]^2 Conjugate[g]^2 + 
 2 A^2 g^2 Conjugate[A] Conjugate[g] Conjugate[n] + 
 4 A g n Conjugate[A] Conjugate[g] Conjugate[n] + 
 2 n^2 Conjugate[A] Conjugate[g] Conjugate[n] + A^2 g^2 Conjugate[n]^2 + 
 2 A g n Conjugate[n]^2 + n^2 Conjugate[n]^2 *)

To convert sym * Conjugate[sym] to Abs[sym]^2

expr3 = expr2 //. Times[a___*(sym_)^m_.*Conjugate[sym_]^n_.] :>
   Times[a*Abs[sym]^2*sym^(m - 1)*Conjugate[sym]^(n - 1)]

(* Abs[A]^4 Abs[g]^4 + 4 Abs[A]^2 Abs[g]^2 Abs[n]^2 + Abs[n]^4 + 
 2 n Abs[A]^2 Abs[g]^2 Conjugate[A] Conjugate[g] + 
 2 n Abs[n]^2 Conjugate[A] Conjugate[g] + n^2 Conjugate[A]^2 Conjugate[g]^2 + 
 2 A g Abs[A]^2 Abs[g]^2 Conjugate[n] + 2 A g Abs[n]^2 Conjugate[n] + 
 A^2 g^2 Conjugate[n]^2 *)

Verifying equivalency

expr == expr2 == expr3 // FullSimplify

(* True *)
  • $\begingroup$ Wow! Thanks. There's a lot to unpack in there (since I am not very familiar with the Mathematica language). $\endgroup$
    – XYZT
    Jan 28, 2020 at 20:05

Your Answer

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

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