I'm confused by the following three different results involving pattern matching and condition.

aa[p]/(2 bb[p]) /. {((f_ /; (D[f, kk] == 0))/bb[kk_]) :> kk}
--> p
aa[p]/(2 bb[p]) /. {(f_/bb[kk_]) /; (D[f, kk] == 0) :> kk}
--> p aa[p]
aa[p]/(2 bb[p]) /. {(f_/bb[kk_]) /; (D[f, kk] == 0) :> cc[kk]}

--> aa[p] cc[p]

Intuitively I expect the first result, where one first check if the factor besides bb[p] depends on p, and if it doesn't, then extract the argument p.

However, I have completely no idea how the second and the third results arise (where from 1st to the 2nd the location of the condition is changed). Moreover, in the 2nd kk is matched with p aa[p], why on earth in the 3rd cc[kk] spits out NOT cc[p aa[p]], but instead a product aa[p] cc[p]?


1 Answer 1


Look at (especially: Print[...]):

aa[p]/(2 bb[p]) /. {((f_ /; (Print[{f, kk, D[f, kk]}]; D[f, kk] == 0))/
     bb[kk_]) :> kk}


We see, that when f_ is checked, kk has not yet a value. And therefore, "D[f, kk]D[f, kk]" is zero and the pattern matches and "p" is returned.

For the second case, consider:

aa[p]/(2 bb[
     p]) /. {(f_/bb[kk_]) /; (Print[{f, kk, D[f, kk], D[f, kk] == 0}];
      D[f, kk] == 0) :> kk}


Now 2 attempts for a match are done. The derivative in the first attempt is not zero and is discarded. Then f is matched against 1/2 (this time only part of the expression is matched, aa[p] is not matched and retained). The derivative is of course zero and p together with aa[p] is returned.

The third case is similar to the second case.


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.