5
$\begingroup$

I have the following expression

-3 I Ez Re[Ex] + 3 I Ex Re[Ez]

And I wish to write this in the form -3 Re[I Ez Conjugate[Ex]]. So I have made the rule

f_ I x_ Re[y_] - f_ I y_ Re[x_] -> f Real[I x Conjugate[y]]

But my expression does not seem to match that expression. What is wrong?

$\endgroup$
3

1 Answer 1

10
$\begingroup$

Pattern matching with complex numbers is notoriously difficult because Complex numbers are atomic yet have non-trivial FullForm.

{AtomQ[-3 I], FullForm[-3 I]}
{True,Complex[0,-3]}

Examining the FullForm of your expression, perhaps you want the following.

rule = a_Complex*x_*Re[y_] + b_Complex*y_*Re[x_] :>
  Abs[a]*Re[I x Conjugate[y]] /; a == -b;
-3 I Ez Re[Ex] + 3 I Ex Re[Ez] /. rule
-3 Im[Ex Conjugate[Ez]]
$\endgroup$
6
  • $\begingroup$ @Mr.Wizard What is the correct way to add textual Mathematica output, as you have done in your edit? $\endgroup$ Feb 10, 2012 at 12:12
  • $\begingroup$ I don't know about "correct" but I am putting the output in quote blocks (which just means it starts with > Sometimes I think it looks better to combine code and quote types, which means starting each line with > followed by five spaces. I hoped you wouldn't mind my edit; I guess you don't. $\endgroup$
    – Mr.Wizard
    Feb 10, 2012 at 12:15
  • $\begingroup$ You can see the raw text of the message at any time by starting to edit it. $\endgroup$
    – Mr.Wizard
    Feb 10, 2012 at 12:16
  • $\begingroup$ @Mr.Wizard I was hoping someone would edit it so I could see formatted output. I see that R.M. has just added a further edit. I really don't think that either of these actually quite right. The standard quote block really looks more like a PrintUsage cell in Mathematica. Perhaps, this is discussion for Meta. $\endgroup$ Feb 10, 2012 at 12:54
  • $\begingroup$ Mark, it's already there. I am interested to know what you think looks best. $\endgroup$
    – Mr.Wizard
    Feb 10, 2012 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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