# Defer evaluation of function but not of its derivative

Sometimes, the derivative of a function can be simplified nicely using the definition of the function itself. For example, if $$f(x) = e^{x^2}$$, then $$f'(x) = 2x \cdot f(x).$$ How can I perform this simplification in Mathematica?

For example, the input

f[x_] := Exp[x^2]
g[x_] := Defer[f][x] + x
g'[x]


results in the ouput:

1 + f'[x]


Instead, I'd like it to output the following:

1 + 2x f[x]


How can I use Defer (or a similar command) such that the evaluation of f[x] is deferred, but f'[x] is evaluated?

Does this work for you? I only tested it on your example

ClearAll[f, x];
f[x_] := Exp[x^2]
(f'[x] /. f[x] -> HoldForm[f[x]]) // Simplify This will work with different variable name

 (f'[y] /. f[y] -> HoldForm[f[y]]) (1 + D[f[x], x]) /. f[x] -> HoldForm[f[x]] To do f'[x]^2, make sure to do the derivative first then square the result, like this

 (D[f[x], x] /. f[x] -> HoldForm[f[x]])^2 You can also use Defer in place of HoldForm above. I do not see how this would work if you Defer the original f[x] at the source, since Mathematica will not be able to take its derivative in first place. So the idea is to do the derivative, then replace the downvalue of f[x] by its name back in the result.

If this does not work for you, will delete this answer.

• The problem with this is that, of course, it only finds literal occurrences of the value of f[x], so for instance it fails with (f'[x])^2. May 17, 2020 at 21:59
• Thanks! HoldForm was exactly what I was looking for. One issue was that it only works using the same variable name as the function definition, since otherwise DownValues doesn't recognize the occurence. E.g. it doesn't work with D[f[y], y]. I'm now using the rule f[x] -> HoldForm[f[x]] and it's working great. May 17, 2020 at 22:04
• @Nasser btw I upvoted your anwer, but it doesn't show until I gather more karma :) May 17, 2020 at 22:08
• @Nasser Following up on my comment above: is there a reason you chose to use DownValues[f][[1,2]] rather than simply f[x]? May 17, 2020 at 22:09
• @atlas no reason. I was trying to be cool. I will change it now. May 17, 2020 at 22:13

I would just give f an UpValues definition for Derivative:

f /: Derivative[f] = Function[x, 2 x f[x]];


Then:

D[f[x] + x, x]


1 + 2 x f[x]

Higher order derivatives work as well:

D[f[x], {x, 3}]
f'''[x]


12 x f[x] + 8 x^3 f[x]

12 x f[x] + 8 x^3 f[x]

You could also give f a numeric definition as well:

f[x_?NumericQ] := Exp[x^2]


Then:

f'''


20 E