Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have the following simple code

Clear["Global`*"];

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["******************************"]
Print["Vx = ", Vx //. sub]
Print["Vy = ", Vy //. sub]
Print["Vxx = ", Vxx //. sub]
Print["Vxy = ", Vxy //. sub]
Print["Vyx = ", Vyx //. sub]
Print["Vyy = ", Vyy //. sub]
Print["*****************************"]

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?

share|improve this question
3  
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. –  sebhofer Jul 25 '13 at 16:27

1 Answer 1

up vote 10 down vote accepted

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:

Replacing term by variable with Mathematica

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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