# Replacement rule only matches part of expression

I have the result of a calculation (which is too long to mention here) and it gives

-I (t2 - t4) (((Hy vx^2 - Hx vx vy + vz (Hz vy - Hy vz)) Re[Ex] +
Hx vz^2 Re[Ey] - Ey vz^2 Re[Hx] +
vx (Hy vy Re[Ey] - Hz vz Re[Ey] + Ex vy Re[Hx]) +
vy (3 Hx vz Re[Ez] - 3 Ez vz Re[Hx]) + (-Ex vx^2 + Ex vz^2 +
vx (-Ey vy + 3 Ez vz)) Re[Hy] - Ex vy vz Re[Hz] +
vx (-3 Hy vz Re[Ez] + Ey vz Re[Hz]) +
vy^2 Real[I Ey Conjugate[Hx]]))


So, I have the following replacement rule

{f_ x_ + f_ y_ -> f (x + y), f_ x_ - f_ y_ -> f (x - y)}


If I now do

3 Hx Re[Ez] vz - 3 Ez Re[Hx] vz //. {f_ x_ + f_ y_ -> f (x + y), f_ x_ - f_ y_ -> f (x - y)}


This gives me

(3 Hx Re[Ez] - 3 Ez Re[Hx]) vz


If I do the same on the above equation the rule just gives the same thing back to me. What I want is (I have manually manipulated this to give what I want to obtain as requested.)

-I (t2 - t4) (-vx vy Abs[Ex]^2) +
vz^2 (Hx Re[Ey] - Ey Re[Hx]) +
vx vy (Hy Re[Ey] + Ex Re[Hx]) +
vy vz 3 (Hx Re[Ez] - Ez Re[Hx]) +
(-vx^2 + vz^2) (Ex Re[Hy] - Hy Re[Ex])
- vy vx Abs[Hy]^2
- vy vz (Ex Re[Hz] + Hz Re[Ex])
vx vz 3 (Hy Re[Ez] + Ez Re[Hy])
+ vx vz (Ey Re[Hz] - Hz Re[Ey]) +
vy^2 Real[I Ey Conjugate[Hx]]


My substitution rules are

 replrule1 = {f_ x_ + f_ y_ -> f (x + y), f_ x_ - f_ y_ -> f (x - y)}
replrule2 = a_Complex x_ Re[y_] + b_Complex y_ Re[x_] :>
Abs[a] Real[I x Conjugate[y]] /; a == -b
replrule4 = x_ Re[y_] - y_ Re[x_] -> Real[I x Conjugate[y]]


After that substitution I also want to apply

b_Complex y_ Re[x_] + a_Complex x_ Re[y_] :>  Abs[a] Real[I x Conjugate[y]] /; a == -b


Which has been a previous question of mine here.

• Jonas, your edit confuses me a bit. If you were originally using //. then I guess that is not producing the result you want. Would you please include the exact output that you desire? – Mr.Wizard Feb 11 '12 at 13:35
• @Mr.Wizard Done. I hope it is clearer now. – Jonas Teuwen Feb 11 '12 at 13:41
• Jonas, pardon me if I am being slow, but I don't see 3 Hx Re[Ez] vz - 3 Ez Re[Hx] vz in the output after doing the replacement. Again I have to ask, what is the compete result that you are expecting? I am asking you to manually manipulate that complete expression into the form that you desire. I cannot otherwise understand what you expect to end up where. – Mr.Wizard Feb 11 '12 at 13:53
• @Mr.Wizard Sorry about that. I have added some more. There is 3 Hx vz Re[Ez] - 3 Ez vz Re[Hx] in the output which is to me the same but maybe Mathematica sees it differently internally. – Jonas Teuwen Feb 11 '12 at 17:07
• I should still implement the DelayedRule. First I need to fully understand what is so different ;-). – Jonas Teuwen Feb 11 '12 at 17:33

In reply to the remaining issue addressed in the comments, here is the reason the 3 is not taken out of the brackets.

You have

rule = {f_ x_ + f_ y_ -> f (x + y), f_ x_ - f_ y_ -> f (x - y)};

3 Hx Re[Ez] vz - 3 Ez Re[Hx] vz //. rule


vz (3 Hx Re[Ez] - 3 Ez Re[Hx])

leaving the multiple 3 inside. Taking a simplified case, this works:

3 a + 3 b /. f_ x_ + f_ y_ :> f (x + y)


3 (a + b)

But this does not work, for the reason stated by Mr. Wizard:

3 a - 3 b /. f_ x_ - f_ y_ -> f (x - y)


3 a - 3 b

And neither does this:

3 a - 3 b /. f_ x_ + f_ y_ -> f (x + y)


3 a - 3 b

for reason that FullForm[3 a - 3 b] is

Plus[Times[3, a], Times[-3, b]

so f cannot ever match 3 and -3.

An awkward solution to this simple case is:

3 a - 3 b /.
f_ x_ + g_ y_ :>
Which[f == g, f (x + y), f == -g, f (x - y), True, f x + g y]


3 (a - b)

But this does not work for more complicated inputs.

Simplify on the other hand works ok:

Simplify[3 a - 3 b]


3 (a - b)

3 Hx Re[Ez] vz - 3 Ez Re[Hx] vz // Simplify


3 vz (Hx Re[Ez] - Ez Re[Hx])

And perhaps rule = f_ x_ + g_ y_ :> Simplify[f x + g y] would work for your original case, depending on what you are specifically seeking.

• I forgot to come back to this. Good thing you remembered. (+1). – Mr.Wizard Feb 12 '12 at 11:44

Try ReplaceRepeated (//.)

On examination

The rule is progressively implemented

rule = {f_ x_ + f_ y_ -> f (x + y), f_ x_ - f_ y_ -> f (x - y)}


on a sample selection

 (Hx vz^2 Re[Ey] - Ey vz^2 Re[Hx] +
vx (Hy vy Re[Ey] - Hz vz Re[Ey] + Ex vy Re[Hx]) +
vy (3 Hx vz Re[Ez] - 3 Ez vz Re[Hx])) /. rule


Output of which is further modified by % /. rule to a final form.

ReplaceRepeated goes straight to the final form:

 (Hx vz^2 Re[Ey] - Ey vz^2 Re[Hx] +
vx (Hy vy Re[Ey] - Hz vz Re[Ey] + Ex vy Re[Hx]) +
vy (3 Hx vz Re[Ez] - 3 Ez vz Re[Hx])) //. rule


Simple Case

ReplaceAll matches one match:

a + b + c /. x_ + y_ -> x y


a (b + c)

ReplaceRepeated matches all occurrences

a + b + c //. x_ + y_ -> x y


a b c

• I actually have used ReplaceRepeated but it does not change the output. – Jonas Teuwen Feb 11 '12 at 13:13
• It changes your original expression when I try it. ReplaceAll takes five interations to arrive at the final form that ReplaceRepeated produces in a single evaluation. – Chris Degnen Feb 11 '12 at 18:44

I still believe this kind of transformation is often misguided. Nevertheless, here are some tips.

You should usually use RuleDelayed (:>) when using named patterns:

rule = {f_ x_ + f_ y_ :> f (x + y)};


Mathematica represents a - b as a + (-1 b), therefore:

{f x + f y, f x - f y - f z, f x - f y + f z} //. rule

{f (x + y), f (x - y - z), f (x - y + z)}

• Two questions: 1. why are these kinds of transformations misguided? And, 2. why should you use RuleDelayed with named patterns, it seems to work with just Rule? The second question isn't meant to be facetious, it is more about why use the one, when the other obviously works. And, when do you use named patterns with Rule. – rcollyer Feb 11 '12 at 4:00
• @rcollyer I suppose that you are asking on the OP's behalf. (1) as has been stated elsewhere using replacement rules for mathematical operations is often, but by no means always, problematic, and often, but by no means always, better done differently. (2) RuleDelayed is a matter of good habit; in this case the symbols are undefined and it doesn't matter, but the habit of using { h[x_] -> x } is a pitfall because you are mixing the local and global meanings of x. – Mr.Wizard Feb 11 '12 at 4:20
• For the first question, yes I was. For the second, I wasn't coming up with an example where it would have problems, so I asked. – rcollyer Feb 11 '12 at 4:22