1
$\begingroup$

I have a function f(x) for which I would need to differentiate and then evaluate it to some product x = y*z. Naively, Mathematica does not accept:

D[f[y*z], y*z]

Now, I can somewhat force it by using a rule like so:

D[f[x], x] /. x -> y*z

Now, the problem is that the substitution, for example

rule = f[y*z] -> y*z
D[f[x], x] /. x -> y*z /. rule

is not performed at all. Here, I would of course expect the answer to be 1. How can I make this work as intended?

$\endgroup$
  • $\begingroup$ Your rule is that f[y, z] should be replaced with y z but f[y, z] never shows up in the expression you show, only f[x] does. $\endgroup$ – MassDefect Oct 1 '19 at 13:07
  • $\begingroup$ Sorry, the notation is maybe not very clear. The space between "y" and "z" is understood as multiplication. I amended this in the question for clarity. $\endgroup$ – Patrick.B Oct 1 '19 at 15:18
  • $\begingroup$ Sorry, I meant to put spaces only but the commas snuck in. D[f[x], x] /. x -> y*z yields f'[y z]. There is nothing for your rule to replace because your rule only acts on plain f[y z] and not f'. Does something more like f = y z; D[f, y z]/.y z-> x work for you? This yields 1 (along with a warning, but that can be suppressed with Quiet or through some clever application of Hold perhaps). $\endgroup$ – MassDefect Oct 1 '19 at 15:38
  • $\begingroup$ How is f defined? Can you just use f'[y z]? $\endgroup$ – Carl Woll Oct 1 '19 at 15:40
4
$\begingroup$

I recommend using Derivative.

Derivative[1][f][y*z]

A shorter way to write the very same is

f'[y*z]
| improve this answer | |
$\endgroup$
1
$\begingroup$

Replacement rules act on the internal structure of the expression. You can use FullForm to see the structure.

Clear["Global`*"]

D[f[x], x] /. x -> y*z//FullForm

(* Derivative[1][f][Times[y, z]] *)

(rule = f[y*z] -> y*z)//FullForm

(* Rule[f[Times[y, z]], Times[y, z]] *)

The LHS of the rule does not appear in the expression, so no replacement is made.

D[f[x], x] /. x -> y*z /. rule

(* f'[y z] *)

If you use a pure function in the rule, you will get what you expect

(rule = f -> (#1 & ))//FullForm

(* Rule[f, Function[Slot[1]]] *)

The LHS of the rule (i.e., f) appears in the expression, so

D[f[x], x] /. x -> y*z /. rule

(* 1 *)
| improve this answer | |
$\endgroup$

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.