2
$\begingroup$

Suppose I have terms which are symbolic derivatives of an undefined function:

f'[x]
(* Derivative[1][f][x] *)

If I later find out that this function can be described better by another function such as:

$$ f(x) = x^4 F(x) $$

I would want to be able to make the following substitution (left abstracted in case there were more terms):

func[x] = x^4 F[x]^4

f'[x] /. Derivative[a_][f][x] -> D[func[x], {x, a}]

(* 
Inactive[Sum][x^(4 - K[1])*Binomial[1, K[1]]*
     D[F[x]^4, {x, 1 - K[1]}]*FactorialPower[4, K[1]], 
   {K[1], 0, 1}]
*)

but we see that this gives a very strange term that includes a summation over a variable $K(1)$. This feels like a very strange behaviour that I can't make sense of expecially considering the following behaviour:

D[func[x], {x, 1}]
(* 4 x^3 F[x]^4 + 4 x^4 F[x]^3 Derivative[1][F][x] *)

Any advice on this would be appreciated.

$\endgroup$
3
  • 5
    $\begingroup$ Try it with :> instead of -> in the replacement rule. The indefinite a-th derivative is evaluated and then the value of a is substituted in your code $\endgroup$
    – Michael E2
    Commented Mar 29, 2022 at 22:08
  • 6
    $\begingroup$ I would use a pure function: f'[x] /. f -> Function[#^4 F[#]^4] $\endgroup$
    – Carl Woll
    Commented Mar 29, 2022 at 22:11
  • $\begingroup$ @MichaelE2 that worked perfectly!! $\endgroup$
    – akozi
    Commented Mar 29, 2022 at 22:33

1 Answer 1

2
$\begingroup$

With the OP's code, we get, as shown in the OP,

func[x] = x^4 F[x]^4;
f'[x] /. Derivative[a_][f][x] -> D[func[x], {x, a}]

(* 
Inactive[Sum][x^(4 - K[1])*Binomial[1, K[1]]*
     D[F[x]^4, {x, 1 - K[1]}]*FactorialPower[4, K[1]], 
   {K[1], 0, 1}]
*)

where the output is the result of replacing a by 1 in

D[func[x], {x, a}]
(*
Inactive[Sum][x^(4 - K[1]) * Binomial[a, K[1]] *
     D[F[x]^4, {x, a - K[1]}] * FactorialPower[4, K[1]],
   {K[1], 0, a}]
*)

which is a rule from calculus (maybe due to Leibniz, but I forget at the moment).

Here are several fixes:

Activate the sum:

f'[x] /. Derivative[a_][f][x] -> D[func[x], {x, a}] // Activate

Use RuleDelayed, which will hold the derivative D[] from being evaluated until a has been replaced:

f'[x] /. Derivative[a_][f][x] :> D[func[x], {x, a}]

Carl Woll's suggestion, a variation on:

f'[x] /. f -> Function[x, Evaluate@func[x]]

If we redefine func as a proper function, there are more:

ClearAll[func];
func[x_] := x^4 F[x]^4;  (* the key is x_ instead of x;
                            for := vs. = see below  *)
f'[x] /. f -> func         (* best way, overall? *)
f'[x] /. f -> (func[#] &)  (* a somewhat pointless embellishment
                              of f -> func  *)

All yield:

(*  4 x^3 F[x]^4 + 4 x^4 F[x]^3 Derivative[1][F][x]  *)

On defining functions and SetDelayed (:=) versus Set (=), see points #6 and #9 and the links contained therein of this answer:

$\endgroup$
1
  • 2
    $\begingroup$ One reason I like using the replacement for f is when the expression is f'[x] + f[x] $\endgroup$
    – Carl Woll
    Commented Mar 30, 2022 at 16:49

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.