10
$\begingroup$

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.

$\endgroup$
8
  • 1
    $\begingroup$ 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... $\endgroup$ Feb 2, 2012 at 14:21
  • 1
    $\begingroup$ 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$
    – rcollyer
    Feb 2, 2012 at 14:31
  • $\begingroup$ @rcollyer Sorry, typo. I have corrected it. $\endgroup$
    – JT_NL
    Feb 2, 2012 at 14:32
  • 3
    $\begingroup$ @Szabolcs the second argument of ComplexExpand lets you tell it that some vars are complex $\endgroup$
    – acl
    Feb 2, 2012 at 14:36
  • 1
    $\begingroup$ ComplexExpand[Expand[ComplexExpand[ee, {z,w}, TargetFunctions->{Conjugate}]], {z,w}, TargetFunctions->{Abs}] might come within shouting distance $\endgroup$ Feb 2, 2012 at 14:36

2 Answers 2

7
$\begingroup$

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]]
$\endgroup$
2
  • $\begingroup$ Your rule does a mathematically incorrect replacement. off by a factor of 2. $\endgroup$
    – Szabolcs
    Feb 2, 2012 at 14:46
  • $\begingroup$ @Szabolcs You're right, thanks, edited my answer. $\endgroup$ Feb 2, 2012 at 14:51
7
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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