2

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.

akozi
  • 853
  • 3
  • 10

1 Answers1

2

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:

Michael E2
  • 235,386
  • 17
  • 334
  • 747