I have a differential equation of the form


(where $g$ is has a known explicit form, e.g. $x f(x)$) and I have an expression which contains several derivatives of $f$. I want to use the differential equation to substitute all the derivatives in order to leave the expression as a function of $f(x)$ and $x$ only. For this purpose I have written the following rule:

ruleDerF = Derivative[n_][f][x_] -> D[x f[x], {x, n - 1}];

This works ok, e.g.,

f''[x] /. ruleDerF

(*f[x] + x f'[x]*)


f''[x] //. ruleDerF

(*f[x] + x^2 f[x]*)

The problem appears when I want to apply this rule when the function is evaluated for a value of $x$. If I try naively to apply this rule on $f(1)$ it gives an obvious error:

f''[1] /. ruleDerF

General::ivar: 1 is not a valid variable. >>

(* \!\(\*SubscriptBox[\(\[PartialD]\), \({1, 1}\)]\(f[1]\)\) *)

This is obvious because you cannot derive wrt 1. So I tried to redefine the rule in the following way,

ruleDerF = Derivative[n_][f][x_] -> (D[y f[y], {y, n - 1}]/.y:>x);

believing that the delayed rule would cause to first perform the derivation wrt the symbolic variable y and then evaluate it to the introduced value, but it doesn't work this way (I get the same result as before).

Any idea that could help me?

  • 1
    $\begingroup$ Perhaps this should be marked as a duplicate of (22917) or one of the many questions it is linked to? $\endgroup$
    – Mr.Wizard
    Commented Jul 21, 2016 at 11:17
  • $\begingroup$ You might be interested in DifferentialRoot[]: f = DifferentialRoot[Function[{y, x}, {y'[x] == x y[x], y[0] == 0}]] and then look at f'' for example. This will only work if you have given initial conditions and your DEs are linear. $\endgroup$ Commented Jul 21, 2016 at 11:58
  • $\begingroup$ Are you trying to prevent the derivative D[y f[y], {y,n-1}] from being carried out every time the rule is applied? One can imagine applying this rule thousands of times for a fairly complicated f[x], and wishing for the derivative to only be carried out once, and stored thereafter. This is how I interpreted in intent behind -> rather than :>, but perhaps it's simply a mistake that I'm overthinking. $\endgroup$
    – jjc385
    Commented Aug 6, 2016 at 1:56

1 Answer 1


Define your rule as a function:

ruleDerF = (# /. Module[{x}, 
   Derivative[n_][f][var_] :> (D[x f[x], {x, n - 1}] /. x -> var)

f''[x] // ruleDerF
f[x] + x Derivative[1][f][x]
 FixedPoint[ruleDerF, f''[x]]
f[x] + x^2 f[x]
FixedPoint[ruleDerF, f''[1]]
2 f[1]

(havent tested heavily)

  • $\begingroup$ Shouldn't you localize x? And wouldn't mentioning RuleDelayed be useful to the OP? $\endgroup$
    – Mr.Wizard
    Commented Jul 21, 2016 at 11:04
  • $\begingroup$ Maybe I'm crazy but at first blush it looks like he is using :> and -> backward, or at least haphazardly. Certainly you changed -> to :> following Derivative[n_][f][x_]. $\endgroup$
    – Mr.Wizard
    Commented Jul 21, 2016 at 11:08
  • 2
    $\begingroup$ Trying to understand why this solution works I realized that defining the rule as Derivative[n_][f][x_] :> (D[y f[y], {y, n - 1}]/.y->x) does the job. The only thing left, as mentioned, is to localize the dummy variable y. $\endgroup$
    – dpravos
    Commented Jul 21, 2016 at 11:11
  • $\begingroup$ Finally I have opted for defining the rule as Derivative[n_][f][var_] :> Module[{x}, (D[x f[x], {x, n - 1}] /. x -> var)]. I will mark the answer as right because it's equivalent. Thank you for your insights. $\endgroup$
    – dpravos
    Commented Jul 21, 2016 at 11:15
  • $\begingroup$ @DavidPravos no need to accept so quickly, I will add more explanations later. Some things are needed and some may be confusing, like using FixedPoint, which isn't necessary. $\endgroup$
    – Kuba
    Commented Jul 21, 2016 at 11:17

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.