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

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?


2 Answers 2


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

Mathematica graphics

This will work with different variable name

 (f'[y] /. f[y] -> HoldForm[f[y]])

Mathematica graphics

 (1 + D[f[x], x]) /. f[x] -> HoldForm[f[x]]

Mathematica graphics

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

Mathematica graphics

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.

  • 1
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    May 17, 2020 at 21:59
  • $\begingroup$ 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. $\endgroup$
    – atlas
    May 17, 2020 at 22:04
  • $\begingroup$ @Nasser btw I upvoted your anwer, but it doesn't show until I gather more karma :) $\endgroup$
    – atlas
    May 17, 2020 at 22:08
  • $\begingroup$ @Nasser Following up on my comment above: is there a reason you chose to use DownValues[f][[1,2]] rather than simply f[x]? $\endgroup$
    – atlas
    May 17, 2020 at 22:09
  • 1
    $\begingroup$ @atlas no reason. I was trying to be cool. I will change it now. $\endgroup$
    – Nasser
    May 17, 2020 at 22:13

I would just give f an UpValues definition for Derivative:

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


D[f[x] + x, x]

1 + 2 x f[x]

Higher order derivatives work as well:

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

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]



20 E


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.