I have the following simple code


V = v0^2/2*Log[x^2 + (y/q)^2 + rc^2];
sub = {(v0^2/(x^2 + (y/q)^2 + rc^2)) -> (arg)};

Vx = D[V, x];
Vy = D[V, y];

Vxx = Simplify[D[V, {x, 2}]];
Vxy = Simplify[D[Vx, y]];
Vyy = Simplify[D[V, {y, 2}]];
Vyx = Simplify[D[Vy, x]];

Print["Vx = ", Vx //. sub]
Print["Vy = ", Vy //. sub]
Print["Vxx = ", Vxx //. sub]
Print["Vxy = ", Vxy //. sub]
Print["Vyx = ", Vyx //. sub]
Print["Vyy = ", Vyy //. sub]

which calculates the first and second derivatives the function $V$. The printed results I added a rule sub in order to make the output much simpler. However, for a strange reason, the rule applies correctly to the first derivatives $Vx$ and $Vy$ but it has no effect to the second ones $(Vxx, Vxy, Vyx, Vyy)$. Any ideas why?

  • 3
    $\begingroup$ Yes, because rules apply to the FullForm of an expression. If the FullForm does not match then nothing will be replaced. The pattern you use for the replacement is pretty complicated so it is not very surprising that it only matches in a few special cases. Rewrite your rule such that the left hand side is simpler and try again. $\endgroup$ – sebhofer Jul 25 '13 at 16:27

One of the best ways to diagnose pattern matching problems is look at the FullForm or TreeForm of the left-hand side of the pattern as well as the object you are attempting to match:

sub[[1, 1]] // TreeForm

enter image description here

Vxx // TreeForm

enter image description here

For the pattern to match the structure must be the same (or appear within the structure), and as you can see they are very different.

For the mathematical transformation I believe you are attempting these related questions may be helpful:



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.