# Expand modulus squared

Is it possible to make a function in Mathematica that expands expressions of the form

$$|z + w|^2 = |z|^2 + 2\text{Re} \overline{z}w + |w|^2?$$

Preferably it should also be able to handle things like $$\left |\sum_{i = 1}^n z_i \right |^2.$$ The last thing can obviously be mathematically deduced from the first one by consecutively applying the first equality.

• ComplexExpand[] with a proper setting of TargetFunctions is supposed to be able to do this, but I can't figure out how to have Mathematica produce the form you want... Feb 2, 2012 at 14:21
• ComplexExpand won't do it alone, in this case. Also, the OPs formula is incorrect the RHS should be $$\newcommand{asq}{|#1|^2} \asq{z} + 2\,\Re(\bar{z} w) + \asq{w} .$$ Feb 2, 2012 at 14:31
• @rcollyer Sorry, typo. I have corrected it. Feb 2, 2012 at 14:32
• @Szabolcs the second argument of ComplexExpand lets you tell it that some vars are complex
– acl
Feb 2, 2012 at 14:36
• ComplexExpand[Expand[ComplexExpand[ee, {z,w}, TargetFunctions->{Conjugate}]], {z,w}, TargetFunctions->{Abs}] might come within shouting distance Feb 2, 2012 at 14:36

Something like (ComplexExpand with all three arguments, Expand and a rule) :

rule = {Im[x_]^2 + Re[x_]^2 -> Abs[x]^2, f_ Re[x_] Re[y_] + f_ Im[x_] Im[y_] -> f Re[Conjugate[x] y]};

Expand[ComplexExpand[Abs[Subscript[z, 1] + Subscript[z, 2]]^2, {Subscript[z, 1],Subscript[z, 2]}, TargetFunctions -> {Re, Im}]] //. rule

Abs[Subscript[z, 1]]^2 + Abs[Subscript[z, 2]]^2 + Re[Conjugate[Subscript[z, 1]] Subscript[z, 2]]

Expand[ComplexExpand[Abs[Subscript[z, 1] + Subscript[z, 2] + Subscript[z, 3]]^2, {Subscript[z, 1], Subscript[z, 2], Subscript[z, 3]}, TargetFunctions -> {Re, Im}]] //. rule

Abs[Subscript[z, 1]]^2 + Abs[Subscript[z, 2]]^2 + Abs[Subscript[z, 3]]^2 + Re[Conjugate[Subscript[z, 1]] Subscript[z, 2]] + Re[Conjugate[Subscript[z, 1]] Subscript[z, 3]] + Re[Conjugate[Subscript[z, 2]] Subscript[z, 3]]

• Your rule does a mathematically incorrect replacement. off by a factor of 2. Feb 2, 2012 at 14:46
• @Szabolcs You're right, thanks, edited my answer. Feb 2, 2012 at 14:51

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}{\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.

• @szabolcs I changed the conjugate to the overbar style, per the OP. Also, \newcommand works, so I used it; check out the edit. Feb 2, 2012 at 15:11
• Wow, MathJax is really good! The lazy person I am, I just copied the TraditionalForm from Mathematica. I too like overbar better for conjugate. Feb 2, 2012 at 15:14
• @Szabolcs, I prefer the asterisk form, myself, but to be consistent ... Feb 2, 2012 at 15:16
• Thanks! I have accepted the other answer as they are basically the same as far as I understand and because b.gatessucks was earlier. Feb 2, 2012 at 16:21