Here's my solution using ComplexExpand
and ReplaceRepeated
(//.
):
res = Expand@ComplexExpand[ Abs[w + z]^2, {w, z},
TargetFunctions -> {Re, Im}]
(*
==> Im[w]^2 + 2 Im[w] Im[z] + Im[z]^2 + Re[w]^2 + 2 Re[w] Re[z] + Re[z]^2
*)
res //. {Im[a_]^2 + Re[a_]^2 :> Abs[a]^2,
c_ Im[a_] Im[b_] + c_ Re[a_] Re[b_] :> c Re[Conjugate[a] b] }
(*
==> Abs[w]^2 + Abs[z]^2 + 2 Re[z Conjugate[w]]
*)
or, mathematically
$$
\newcommand{asq}[1]{\left|#1\right|^2} \asq{w} + \asq{z} + 2\, \Re(z \bar{w})
$$
This also works with more than 2 variables:
(Expand@ComplexExpand[Abs[z + w + x]^2, {z, w, x},
TargetFunctions -> {Re, Im}]) //.
{Im[a_]^2 + Re[a_]^2 :> Abs[a]^2,
c_ Im[a_] Im[b_] + c_ Re[a_] Re[b_] :> c Re[Conjugate[a] b] }
(*
==> Abs[w]^2 + Abs[x]^2 + Abs[z]^2 + 2 Re[x Conjugate[w]]
+ 2 Re[z Conjugate[w]] + 2 Re[z Conjugate[x]]
*)
In mathematical notation:
$$
\asq{w}+\asq{x}+\asq{z}+2\, \Re(x \bar{w})+2\, \Re(z \bar{w})+2\, \Re(z \bar{x}).
$$
Note, ReplaceRepeated
performs a structural transformation, not a mathematical one, so it is inherently dangerous if you're not careful.
ComplexExpand[]
with a proper setting ofTargetFunctions
is supposed to be able to do this, but I can't figure out how to have Mathematica produce the form you want... $\endgroup$ComplexExpand
won't do it alone, in this case. Also, the OPs formula is incorrect the RHS should be $$\newcommand{asq}[1]{|#1|^2} \asq{z} + 2\,\Re(\bar{z} w) + \asq{w} .$$ $\endgroup$ComplexExpand
lets you tell it that some vars are complex $\endgroup$