10

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.

JT_NL
  • 983
  • 1
  • 6
  • 16

2 Answers2

7

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]]
b.gates.you.know.what
  • 20,103
  • 2
  • 43
  • 84
7

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.

rcollyer
  • 33,976
  • 7
  • 92
  • 191
  • @szabolcs I changed the conjugate to the overbar style, per the OP. Also, \newcommand works, so I used it; check out the edit. – rcollyer Feb 02 '12 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. – Szabolcs Feb 02 '12 at 15:14
  • @Szabolcs, I prefer the asterisk form, myself, but to be consistent ... – rcollyer Feb 02 '12 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. – JT_NL Feb 02 '12 at 16:21