Differentiating a sum in Sympy

118 Views Asked by At

I would like to differentiate the entropy H(T) of a discrete random variable T with respect to one of the masses q(t') using Sympy. The following implements the expression for H(T):

import sympy as sym
sym.init_printing(use_latex=True)

N_T = sym.Symbol("N_T")
qT = sym.Function("q")

t = sym.Symbol("t")
ht_expr = sym.summation( qT(t) * sym.log(qT(t)), (t, 1, N_T) )
ht_expr

This prints the correct expression for H(T), as expected:

enter image description here

Now, differentiating with respect to q(t') should result in the derivative of q(t)log(q(t)) evaluated at t' if t' is in 1...N_T, and 0 otherwise (this is a known result, easy to check). I.e. it should return:

enter image description here

Instead I get 0:

tprime = sym.Symbol("t'")
sym.diff(ht_expr, qT(tprime))
[Out]: 0

How can I make Sympy handle sums like this and give the correct result?

1

There are 1 best solutions below

0
On

I found that the answer in sympy - taking derivative of sum of symbolic number of elements also applies to this case.

In short, we need to use IndexedBase instead of Function. For completeness, here is the code solving this problem.

N_T = sym.Symbol("N_T")
qT = sym.IndexedBase("q")
t, k = sym.symbols("t k")

ht_expr = sym.summation( qT[t] * sym.log(qT[t]), (t, 1, N_T) )
sym.diff(ht_expr, k)

Output:

enter image description here