# Correctly differentiate wrt product of variables

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?

• 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. – MassDefect Oct 1 '19 at 13:07
• 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. – Patrick.B Oct 1 '19 at 15:18
• 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). – MassDefect Oct 1 '19 at 15:38
• How is f defined? Can you just use f'[y z]? – Carl Woll Oct 1 '19 at 15:40

I recommend using Derivative.

Derivative[f][y*z]


A shorter way to write the very same is

f'[y*z]


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[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]] *)


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

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

(* 1 *)
`