I'm trying to write this expressionenter image description here

in Mathematica, and calculate the following quantity

enter image description here

However, when I tried the following

Clear[g, n, v,T,P,B,r,i,j,k,l,t,w,Q,g,a,f]
g = B*(Sum[Indexed[n, k]*Log[Indexed[n, k]*Indexed[v, k]],k])+ Sum[Indexed[n, f]*Indexed[w, f],f] + Sum[Sum[0.5*Indexed[Indexed[Q, l], j]*Indexed[n, l]*Indexed[n, j],j],l] + P;
a = Indexed[v, r] * (g - Sum[D[g, Indexed[n, t]]*Indexed[n, t],t]) + D[g,Indexed[n, r]]
Collect[a,B] //Simplify

If I run Clear[g] right before a, I basically get

$$v_r * g$$

as a result of the computation and the reason is that it calculates the derivatives of $g$ wrt $n_k$s as exactly zero. Meaning that it doesn't recognise the summation indices as dummy indices.

How can I correct my code such that the dummy summation variables will be a dummy and hence I'll be able to perform the above calculation correctly?

Addedum: If you get the following result when you run the code $$v_r \left(B \sum _k n_k \log (n_k v_k)+\sum _f n_f w_f+\sum _l \left(\sum _j 0.5 n_j n_l Q_{l\, j}\right)+P\right)$$ it means that the code is not computing what is needs to compute because this is a wrong answer. For example, if you put Clear[g] right before the definition of a in the code, you can see that during the calculation of a, all the derivatives of $g$ wrt $\eta_k$ are taken as zero.

  • $\begingroup$ It is working as expected in v12.2. $\endgroup$ Apr 25, 2021 at 20:05
  • $\begingroup$ @AlexTrounev I run the code on the cloud, it didn't. $\endgroup$
    – Our
    Apr 26, 2021 at 5:14
  • $\begingroup$ @AlexTrounev And I've just tried on v12.2; the derivatives of $g$ wrt the indexed variables are still zero. Do note that the answer $v_r g$ is a wrong answer. $\endgroup$
    – Our
    Apr 26, 2021 at 5:45
  • $\begingroup$ Works properly for me in 12.2 on Windows 10 Pro, producing $$ v_r \left(B \sum _k n_k \log (n_k v_k)+\sum _f n_f w_f+\sum _l \left(\sum _j 0.5 n_j n_l Q_{l\, j}\right)+P\right).$$. The cloud machine is running on Linux. $\endgroup$
    – user64494
    Apr 26, 2021 at 7:50
  • $\begingroup$ @user64494 that is not the correct answer, that is what I'm saying $\endgroup$
    – Our
    Apr 26, 2021 at 8:07


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.