# Named pattern not recognized in replacement rule

Suppose I have two systems of coordinates:{x1,x2} and {y1,y2}. I want to transform an expression containing $\frac{\partial yi}{\partial xj}$'s to those in terms of $\frac{\partial xj}{\partial yi}$'s. I wrote the following code:

expr = D[y2[x1, x2], {x1, 2}, {x2, 0}] + D[y1[x1, x2], {x1, 0}, {x2, 2}];
expr /. Derivative[n_, m_][y : y1 | y2][x1, x2] -> Nest[(1/D[x2[y1, y2],
y1])*D[#1, y1] + (1/D[x2[y1, y2], y2])* D[#1, y2] &, Nest[(1/D[x1[y1, y2],
y1])*D[#1, y1] + (1/D[x1[y1, y2], y2])* D[#1, y2] &, 1/D[x1[y1, y2], y], n], m]


Why is this not working? It seems n,m and y are not recognized in the RHS of my replacement rule.

Edit 1:(Special case of expr where Nest is not evaluated)

The above was a snippet of a bigger problem I have been working on. There expr is a pure function. And then the above rule doesn't evaluate the Nest functions:

expr=Derivative[0, 1][y1][x1, x2]*D[#1, {y1, 1}, {y2, 0}] &
expr/.Derivative[n_, m_][y : y1 | y2][x1, x2] :>
Nest[(1/D[x2[y1, y2], y1])*D[#1, y1] + (1/D[x2[y1, y2], y2])*
D[#1, y2] &,
Nest[(1/D[x1[y1, y2], y1])*D[#1, y1] + (1/D[x1[y1, y2], y2])*
D[#1, y2] &, 1/D[x1[y1, y2], y], n], m]


How to force evaluation here?

• What tests have you performed to validate your claims that the pattern matching is not working? – m_goldberg Mar 5 '18 at 6:44
• Also Ctrl+Shift+I (convert to input form) before copying. – Kuba Mar 5 '18 at 6:58
• @ m_goldberg Could you look at edit 1 of this post? – Subho Mar 5 '18 at 10:17
• It would be good to have cross-post links between this thread and the related one on Wolfram Community – Daniel Lichtblau Mar 5 '18 at 16:01

Here is your code expressed in input form. I have made two modifications.

• I have replaced Rule withe RuleDelayed. This corrects an error on your part.
• I have requested the output to be shown in input form, which I believe is more readable in this case

expr = D[y2[x1, x2], {x1, 2}, {x2, 0}] + D[y1[x1, x2], {x1, 0}, {x2, 2}];

expr /.
Derivative[n_, m_][y : y1 | y2][x1, x2] :>
Nest[
(1/D[x2[y1, y2], y1])*D[#1, y1] + (1/D[x2[y1, y2], y2])*D[#1, y2] &,
Nest[
(1/D[x1[y1, y2], y1])*D[#1, y1] + (1/D[x1[y1, y2], y2])*D[#1, y2] &,
1/D[x1[y1, y2], y],
n],
m] // InputForm


I get the following result. It's messy, but so is your replacement rule. I see no sign that the pattern matching is not working.

• Thanks for the correction. I had done a similar thing earlier, but that had somehow not worked as well. I will check once again. – Subho Mar 5 '18 at 8:38