![]() | 1 | initial version |
The problem lies with diff
. Its second (and further) argument(s) can be either a symbolic variable or an integer, giving the order of differentiation wrt the first (or preceding) argument.
The crux of the matter is that diff(x, n)
is interpreted by Sage as $\frac{\partial}{dn}\frac{\partial{d}\sin x}{dx}\,=\,\frac{\partial\cos x}{dn}\,=\,0$. BTW :
sage: diff(sin(x), x, n)
0
For further enlightenment, meditate (deeply !) diff?
Ifn is not an integer, it is interpreted as a symbolic variable, and is not replaced by its value in the loop.
Yes, it is a pain in the asterix... Yes, it's a design flaw (probably going back to Maxima, née Macsyma, which doesn't make us any younger...). And no, it can't be fixed without invalidating (mega?)tons of code resting on this basis.
Complain fiercely...
BTW, Mathematica has a very similar problem :
sage: mathematica.interact()
--> Switching to Mathematica <--
mathematica: Sum[D[Sin[x], x, i], {i, 1, 2}]
General::ivar: 1 is not a valid variable.
General::ivar: 1 is not a valid variable.
General::ivar: 2 is not a valid variable.
General::stop: Further output of General::ivar
will be suppressed during this calculation.
D[Cos[x], 1] + D[Cos[x], 2]
but :
mathematica: Sum[D[Sin[x], {x, i}], {i, 1, 2}]
Cos[x] - Sin[x]
Mathematica allows to express the order of differentiation by including it in a list [differentiation variable, order of differentiation]
. BTW :
mathematica: D[D[F[x], {x, p}],{x, q}]
(p + q)
F [x]
Sage does not (yet ?) have this. The workaround given by @FredericC and imbut won't accept infinite lists....)plemented by @achrzesz is currently the only way (but won't accept infinite sums, which are often the point of such summations...).
HTH,