# Replace rule does not match

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?

-
This thread: groups.google.com/group/comp.soft-sys.math.mathematica/…, should give you an exhaustive answer :) – Leonid Shifrin Feb 10 '12 at 12:00
@Leonid Thanks. I was starting to feel too good about the day. – Daniel Lichtblau Feb 10 '12 at 18:29
@Daniel Most welcome :) – Leonid Shifrin Feb 10 '12 at 18:38

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]]

-
@Mr.Wizard What is the correct way to add textual Mathematica output, as you have done in your edit? – Mark McClure Feb 10 '12 at 12:12
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. – Mr.Wizard Feb 10 '12 at 12:15
You can see the raw text of the message at any time by starting to edit it. – Mr.Wizard Feb 10 '12 at 12:16
@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. – Mark McClure Feb 10 '12 at 12:54
Mark, it's already there. I am interested to know what you think looks best. – Mr.Wizard Feb 10 '12 at 13:01