# Replace multiple variables using a list?

I want to calculate sum of derivatives of expression

F = Exp[(x*z - 1) f*b + (x - 1) (1 - f) b]


from n-th to (n-k)-th, say k=4. The dummy way which works is:

der1 = D[F, {x, n}] + D[F, {x, k1}] + D[F, {x, k2}] + D[F, {x, k3}] + D[F, {x, k4}]
der1 = der1 /. {k1 -> n - 1, k2 -> n - 2, k3 -> n - 3, k4 -> n - 4}


However I'd like to do it automatically. I tried as follows:

der2 = Sum[D[F, {x, n - k}], {k, 0, 4}]


but the problem is that it doesn't evaluate derivatives, leaving partial derivative symbol. Whereas the same method works if it is not (n-k)-th derivative but k-th derivative:

der3 = Sum[D[F, {x, k}], {k, 0, 4}]


My question is how to write der2 in order to give the same output as der1 but in the way of der3? Sorry if question is stupid. Please note, I'm rather new to Mathematica.

• Your der2 code works for me when I first define b to be an integer, like b=10. Did you perhaps mean n instead of b? Jul 23, 2020 at 11:54
• Yes, I corrected. I meant this. I want the order of derivative to be positive integer but I don't want to precise value - I'm looking for general formula Jul 23, 2020 at 12:16

For a generic number of derivatives m, D does return a result

D[F, {x, m}]

E^(10*(1 - f)*(-1 + x) + 10*f*(-1 + x*z))*(10 - 10*f + 10*f*z)^m


I suppose Mathematica returns the partial derivatives, because it does not know whether your n is less than 4. There may be a way to solve this with appropriate assumptions, but in the meantime you could use some thing like

Sum[# /. {m -> n - k}, {k, 0, 4}]& @ D[F, {x, m}] // Simplify

10^(-4 + n) E^(10 (-1 + x + f x (-1 + z))) (1 + f (-1 + z))^(-4 + n)
(11111 + 43210 f (-1 + z) + 63100 f^2 (-1 + z)^2 + 41000 f^3 (-1 + z)^3 +
10000 f^4 (-1 + z)^4)

• Thank you! Mixing your solution and my attempts I managed to find even simpler one Sum[D[F, {x, m}] /. {m -> n - k}, {k, 0, 4}] Jul 23, 2020 at 14:18
• In your case sum is recomputing D[F, {x,m}] for every summand, so overall five times. In my example it is only computed once, and sum only sums over the replacements. It does not make a difference here, but there are cases where this can lead to large hits in performance. Jul 23, 2020 at 14:39