2
$\begingroup$

Please explain why in the following mathematica code, the cos term was not replaced in the first try, but got replaced in the second try?

g[x]/x Cos[x]
% /. {Cos[x] -> Integrate[Cos[x], x], g[x]/x -> D[g[x]/x, x]}
% /. Cos[x] -> Integrate[Cos[x], x]

Following is the output I am getting

(Cos[x] g[x])/x
Cos[x] (-(g[x]/x^2) + Derivative[1][g][x]/x)
Sin[x] (-(g[x]/x^2) + Derivative[1][g][x]/x)

What I tried:

The following code gives expected result

g[x] Cos[x]
% /. {Cos[x] -> Integrate[Cos[x], x], g[x] -> D[g[x], x]}

Output:

Cos[x] g[x]
Sin[x] Derivative[1][g][x]

I guess the 1/x factor is messing something up?

Extra points: Now, the following code (using :> instead of ->) does not work

g[x]/x Cos[x] /. {Cos[x] :> Integrate[Cos[x], x], 
  g[x]/x :> D[g[x]/x, x]}

Next, the following code each work. First, as suggested by @edinorog2196 , using g[x] -> x D[g[x]/x, x] instead of g[x] -> x D[g[x]/x, x] works

g[x]/x Cos[x] /. {Cos[x] -> Integrate[Cos[x], x], 
  g[x] -> x D[g[x]/x, x]}

Also using ReplaceRepeated (//.) (suggested by @BobHanlon and @RolandF) works, irrespective of using :> or ->:

g[x]/x Cos[x] //. {Cos[x] -> Integrate[Cos[x], x], 
  g[x]/x -> D[g[x]/x, x]}
g[x]/x Cos[x] //. {Cos[x] :> Integrate[Cos[x], x], 
  g[x]/x :> D[g[x]/x, x]}
$\endgroup$
6
  • $\begingroup$ I don't know why it doesn't work, but you can use ReplaceRepeated (//.) instead of ReplaceAll (/.) $\endgroup$
    – Bob Hanlon
    Commented Jul 15 at 14:38
  • $\begingroup$ 1/x // FullForm gives Power[x,-1] $\endgroup$ Commented Jul 15 at 14:39
  • 1
    $\begingroup$ A workaround is to replace g[x] -> x D[g[x]/x, x]. In my experience it is better to just substitute a single thing rather than an expression g[x]/x which could not be found in the code (you have (Cos[x]g[x])/x) $\endgroup$ Commented Jul 15 at 15:17
  • 2
    $\begingroup$ These results are not a bug. Replace documentation explains that when a list of rules is given, the result with the first rule that applies is returned. Your expected result is given by Replace Repeated, which repeatedly performs replacements until the expression no longer changes. $\endgroup$
    – creidhne
    Commented Jul 15 at 16:43
  • $\begingroup$ @creidhne Do you mean that Replace will only work once for one of the rule that is found to apply? I tested this using following code 1 + x + y /. {x -> x1, y -> y1}. The result is 1 + x1 + y1. Both the replacements happened here without any problem. $\endgroup$
    – Prem
    Commented Jul 16 at 8:18

2 Answers 2

4
$\begingroup$

The problem is that the expression doesn't have g[x]/x just isolated from Cos[x]:

expr = g[x]/x  Cos[x];
expr // FullForm

Times[Power[x, -1], Cos[x], g[x]]

Because everything is bound together inside Times, the rule for g matches the whole expression rather than just an isolated part. That's why the Cos rule will no longer fire. Compare:

expr2 = {g[x]/x , Cos[x]};
expr /. {Cos[x] -> Integrate[Cos[x], x], g[x]/x -> D[g[x]/x, x]}
expr2 /. {Cos[x] -> Integrate[Cos[x], x], g[x]/x -> D[g[x]/x, x]}

enter image description here

As you can see, when the two terms are separated elements in a list, there's no overlap between the factors and both rules can work independently. I'm not sure why ReplaceAll decides to prioritize the rule for g in expr, but I guess the top-level replacement simply takes priority here.

$\endgroup$
1
  • 2
    $\begingroup$ To add to this answer, the reason why Times matches like that is because it has the Flat attribute. $\endgroup$
    – Greg Hurst
    Commented Jul 16 at 12:40
2
$\begingroup$

Three errors: Execute cells separately if you refer its result.

Use :> instead of -> to avoid evaluation of rules already at input time.

Don't use %, it was created for the math.exe text input.

Give names to results, so you can refer them in any order of executing commands.

Cell output image

$\endgroup$
1
  • $\begingroup$ I will look into the suggestions you gave, but even if I use multiple cells and do not use % or ->, the expected result only turns up when we use //. The following works g[x]/x Cos[x] //. {Cos[x] -> Integrate[Cos[x], x], g[x]/x -> D[g[x]/x, x]} $\endgroup$
    – Prem
    Commented Jul 16 at 8:25

Your Answer

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

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