3

Can someone help me understand the evaluation procedure in the following two pieces of code?

test[] := Module[{expression},
  Clear[F];
  expression = Derivative[0,1][F][x,y];
  F[x_,y_] = y-x^2+1;
  Return[expression];
];

test[func_] := Module[{expression},
  Clear[F];
  expression = Derivative[0,1][F][x,y];
  F[x_,y_] = func;
  Return[expression];
];

test[]
test[y - x^2 + 1]

1

0

I expected the output to be 1 in the second case.

I believe the major issue (which we can identify using Trace) is that when test is called without an argument, F is assigned as expected:

F[x_,y_]=y-x^2+1;

However, when test is called with an argument, F receives a different assignment using locally defined variables:

F[x$_,y$_]=y-x^2+1;

If I can figure out how to have F assigned as in the former, then I believe my issue will be resolved and the outputs will match.

Context: I know there is an easier way to compute a partial derivative with respect to a variable, however that is not my ultimate goal. In the end, expression will be much more complex and involve mixed partial derivatives of F. I want Mathematica to replace all the instances of these Derivatives. I do not think I can use a ReplaceAll since I am not sure what Derivatives will actually show up.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
James Rohal
  • 1,404
  • 10
  • 23
  • First by (F^(0,1))[x,y], do you mean D[F[x,y],y] ? And second, what happens if you put the definition (F[x_,y_]) before the evaluation of expression? I think that will work ! – Sumit Apr 23 '15 at 18:04
  • Yes, I just fixed the code/output. 2) Moving the function definition does not seem to change the output.
  • – James Rohal Apr 23 '15 at 18:08
  • Can you use derivative in this way expression := D[F[x, y], y] for the second one? – Sumit Apr 23 '15 at 18:27
  • No, in my case expression will involve Derivative and not D. – James Rohal Apr 23 '15 at 18:29
  • 1
    Jens' answer is the one to use; however, there is another workaround: F[u_, v_] = func /. {x -> u, y -> v} – 2012rcampion Apr 23 '15 at 18:41