Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
1  
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... –  J. M. Feb 2 '12 at 14:21
1  
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} .$$ –  rcollyer Feb 2 '12 at 14:31
    
@rcollyer Sorry, typo. I have corrected it. –  Jonas Teuwen Feb 2 '12 at 14:32
3  
@Szabolcs the second argument of ComplexExpand lets you tell it that some vars are complex –  acl Feb 2 '12 at 14:36
1  
ComplexExpand[Expand[ComplexExpand[ee, {z,w}, TargetFunctions->{Conjugate}]], {z,w}, TargetFunctions->{Abs}] might come within shouting distance –  Daniel Lichtblau Feb 2 '12 at 14:36
show 3 more comments

2 Answers

up vote 7 down vote accepted

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]]
share|improve this answer
    
Your rule does a mathematically incorrect replacement. off by a factor of 2. –  Szabolcs Feb 2 '12 at 14:46
    
@Szabolcs You're right, thanks, edited my answer. –  b.gatessucks Feb 2 '12 at 14:51
add comment

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.

share|improve this answer
    
@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 2 '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 2 '12 at 15:14
    
@Szabolcs, I prefer the asterisk form, myself, but to be consistent ... –  rcollyer Feb 2 '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. –  Jonas Teuwen Feb 2 '12 at 16:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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