# How do I expand expressions involving complex numbers?

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.

• "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] – J. M.'s torpor Jan 28 '20 at 17:12
• Thanks, that did the trick! Is there anyway to get Mathematica to collect $AA^*$ into $|A|^2$? – XYZT Jan 28 '20 at 17:15

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

Clear["Global*"]

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 *)
`
• Wow! Thanks. There's a lot to unpack in there (since I am not very familiar with the Mathematica language). – XYZT Jan 28 '20 at 20:05