# Derivative of an expression containing a symbolic sum [duplicate]

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[λ, β]?

• appears explicitly in the numerator, and implicitly in the denominator due to the sum I do not see it. The denominator has no Subscript[\[Lambda], \[Alpha]] in it at all. The sum is over beta, not alpha. Here is screen shot of your expression !Mathematica graphics Oct 19 '14 at 0:19
• The idea is that \[Alpha] is a non-negative integer in [1,k], so there is going to be a term in the series in the denominator where \[Alpha]=\[Beta]. Oct 19 '14 at 0:31
• so there is going to be a term in the series in the denominator where \[Alpha]=\[Beta] This I do not understand at all. You are doing symbolic differentiation? How is Mathematica supposed to know that Alpha and Beta are the same symbol at one point? Oct 19 '14 at 0:35
• Well that's my question exactly. As I pointed out in my question I could explicitly set \[Alpha] to 1 for example and explicitly code the corresponding term in the denominator and get the right answer. But I'm wondering if there's some way to state the assumption that \[Alpha] is a non-negative integer that is in the same range as the index for the sum which would force Mathematica to differentiate the denominator. Oct 19 '14 at 0:41
• This is probably a duplicate of how to differentiate formally. You only have to get rid of the Subscripts as variable names because they bury the meaning of the actual labels $\alpha$ and $\beta$ too deeply. Replace Subscript[lambda][alpha] by lambda[alpha] etc.
– Jens
Oct 19 '14 at 0:45

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:

$$\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 -> {λ}]


$$\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 )}\right){}^2}$$

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? Oct 19 '14 at 19:09
• Actually this is what I get when I run your code: $$-\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. 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? Oct 19 '14 at 23:47
• Yes, that's right.
– Jens
Oct 20 '14 at 0:31