I declare a very simple rule.

rule = -x_*Log[a_] :> x*Log[1/a]

Now I give two expressions,

-x Log[d] /. rule

which gives me $x\log(1/d)$ but now if I give

-2 Log[d]/. rule

doesn't give me anything. Same problems for rules don't occur for +ve sign, for instance

rule = x_*Log[a_] + x_*Log[b_]:> x Log[a*b]

works well for constants and functions. Somehow "-" sign is causing problems. Any ideas ?

  • 1
    $\begingroup$ Look at the FullForm of your rule and various expressions. $\endgroup$ – chuy May 6 at 20:48
  • $\begingroup$ I did, fullform of the rule is Rule[Times[-1,Log[Pattern[a,Blank[]]],Pattern[x,Blank[]]],Times[x,Log[Power[a,-1]]]] but -2Log[d] is Times[-2,Log[d]]. So is there way to pull out -2 and make it -1 to match the pattern ? $\endgroup$ – Jaswin May 6 at 20:59

I find it is best to keep the left hand side of your rules as simple as possible, so I might write

rule = x_ Log[a_] -> Abs[x] Log[a^Sign[x]]

You need to be careful that your rules are valid for all the values that your variables could take. We can use Assuming to enforce this

Assuming[x > 0, -x Log[d] /. rule // Refine]
(* x Log[1/d] *)

As desired

-2 Log[d] /. rule
(* 2 Log[1/d] *)

We might create more complex rules by applying conditions on the right hand side. For example:

rule2 = u1_*Log[v_] + u2_*Log[w_] :> u2*Log[w/v] /; u1 == -u2;

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.