3
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ 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 ! $\endgroup$
    – Sumit
    Commented Apr 23, 2015 at 18:04
  • $\begingroup$ 1) Yes, I just fixed the code/output. 2) Moving the function definition does not seem to change the output. $\endgroup$ Commented Apr 23, 2015 at 18:08
  • $\begingroup$ Can you use derivative in this way expression := D[F[x, y], y] for the second one? $\endgroup$
    – Sumit
    Commented Apr 23, 2015 at 18:27
  • $\begingroup$ No, in my case expression will involve Derivative and not D. $\endgroup$ Commented Apr 23, 2015 at 18:29
  • 1
    $\begingroup$ Jens' answer is the one to use; however, there is another workaround: F[u_, v_] = func /. {x -> u, y -> v} $\endgroup$ Commented Apr 23, 2015 at 18:41

1 Answer 1

9
$\begingroup$

Cleaning up your syntax (which involves a non-localized varable F and an unnecessary Return) the second case coud be written like this:

test[func_] := Module[
  {expression, f},
  expression = Derivative[0, 1][f][x, y];
  f = Function[{x, y}, #] &[func];
  expression]

Then the expected result comes out:

test[y - x^2 + 1]

(* ==> 1 *)

What I did here is to define f as a Function because that's the only way you can substitute it into expression when it's defined with Derivative. The difference between Derivative and D is that the former does derivatives on the function slots without any reference to the names of the variables that go into those slots, and then substitutes the variables into the resulting derivative afterwards. By contrast, D uses the variable names as a way to identify what the correct "slots" of the expression are in the first place.

So therefore, Derivative needs a Function to operate on. Since func is an expression and not a function, we have to create a function ourselves. But that leaves the final step: Function has attribute HoldAll, so I cannot put the dummy argument func into the body of Function before it gets evaluated. This is circumvented by leaving the body open with # and applying that "empty" Function as a function to the dummy argument at the time you call test.

$\endgroup$
2
  • $\begingroup$ Perfect, that makes a lot of sense now. Thanks! $\endgroup$ Commented Apr 23, 2015 at 18:47
  • $\begingroup$ Get here from this post, since codes like test[func_] := Module[{expression, f}, f[x_, y_] = func; expression = Derivative[0, 1][f][x, y]; expression] not working. I do not quite understand what is happening here, would you please give me a hint? $\endgroup$
    – Kattern
    Commented Jun 5, 2015 at 11:27

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.