Derivative of an expression containing a symbolic sum [duplicate]

This question already has an answer here:

how to differentiate formally?

5 answers

Is there a general pattern for working with symbolic sums in Mathematica?

For example here’s a derivative I want to compute:

D[Exp[Subscript[λ, α] x]/Sum[Exp[Subscript[λ, β] x],{β, 1, k}], Subscript[λ, α]]

The derivative is with respect to a parameter Subscript[λ, α], and it appears explicitly in the numerator, and implicitly in the denominator due to the sum. Mathematica differentiates the numerator where the parameter is explicitly present, but it misses the fact that the sum over the denominator contains an instance of Subscript[λ, α].

I can hack a solution to this since without loss of generality I could assume that the subscript α takes the value 1 and separate that term from the symbolic sum in the denominator.

But is there a way to represent the sum in the denominator so that Mathematica understands that Subscript[λ, α] is contained in the set Subscript[λ, β]?



1 Answer


In order to get the expression yo simplify to the fullest extent, one could make the additional assumption that the label α\alpha always falls within the range of the summation whose index is β\beta. To do this, we have to add some rules which are easier to write if the starting expression is brought into a slightly different form:

∂∂λ(α)exp(λ(α)x)∑βexp(λ(β)x)\frac{\partial }{\partial \lambda (\alpha )}\frac{\exp
(\lambda (\alpha ) x)}{\sum _{\beta } \exp (\lambda
(\beta ) x)}

Here I omitted the summation indices because I want to ignore them anyway (equivalent to the assumption stated above). I also eliminated the subscripts because they are never a good idea as a method of indexing variable names.

Now we’re ready to apply the method outlined in my answer here, adding one more assumption (again to implement the assumption stated above) – which is that sums should be killed provided that they contain a Kronecker delta in which the summation index appears. I chose the simplest route here and associated this rule with the symbol β so that this brute-force simplification only applies when you choose β as the summation variable. That way, you could still allow similar sums where you do not wish to make the assumption that α\alpha is in the summation range. Those would then use a summation index different from β and not get simplified.

λ /:
D[λ[i_], λ[j_], NonConstants -> {λ}] :=
KroneckerDelta[i, j]

β /: Sum[KroneckerDelta[α_, β] rest___, β] :=
Times[rest] /. β :> α

D[Exp[λ[α] x]/
Sum[Exp[λ[β] x], β], λ[α],
NonConstants -> {λ}]

xexλ(α)∑βexλ(β)−xe2xλ(α)(∑βexλ(β))2\frac{x e^{x \lambda (\alpha )}}{\sum _{\beta } e^{x
\lambda (\beta )}}-\frac{x e^{2 x \lambda (\alpha
)}}{\left(\sum _{\beta } e^{x \lambda (\beta

This is the answer you are probably looking for.



Thanks! I had no idea about the flexibility of evaluation in Mathematica. If you don’t mind me asking a follow up question, can you expand on what the TagSetDelayed rule for \[Beta] is doing? My understanding (which must be wrong) is that when evaluating the Sum expression in the denominator Mathematica encounters the expression \[Beta] and then it trys to apply the rule you defined. But the lhs of the definition in your rule has KroneckerDelta and the Sum in the denominator doesn’t so how does the match happen?
– Stripes Plaid
Oct 19 ’14 at 19:09



Actually this is what I get when I run your code: −exλ[α]D[∑βexλ[β],λ[α],NonConstants→{λ}](∑βexλ[β])2+exλ[α]x∑βexλ[β]-\frac{e^{x \lambda [\alpha ]} D\left[\sum _{\beta } e^{x \lambda [\beta ]},\lambda [\alpha ],\text{NonConstants}\to \{\lambda \}\right]}{\left(\sum _{\beta } e^{x \lambda [\beta ]}\right){}^2}+\frac{e^{x \lambda [\alpha ]} x}{\sum _{\beta } e^{x \lambda [\beta ]}} It has the right structure, but there’s an unevaluated derivative still.
– Stripes Plaid
Oct 19 ’14 at 20:11



@StripesPlaid Indeed, the last simplification works in Mathematica version 10.0.1, but not in version 8. I’m guessing you’re using an older version. The TagSetDelayed rule for β\beta only gets applied in the 10.0.1 version after the derivative has been done under the sum. Then what the rule says is that whenever β\beta appears as the summation index without any additional range specification, and there is a Kronecker delta involving β\beta, you’re supposed to reduce the sum to the rest multiplying the delta.
– Jens
Oct 19 ’14 at 20:53



I see, so the TagSetDelayed rule for λ\lambda results in what I’m seeing, and then in Mathematica 10 the remaining derivative is carried out under the sum and leaves a KroneckerDelta which triggers the evaluation of the rule for β\beta. Am I getting that right?
– Stripes Plaid
Oct 19 ’14 at 23:47



Yes, that’s right.
– Jens
Oct 20 ’14 at 0:31