Ask Your Question

Why is 'sum( diff(sin(x),x,n), n,1,2)' zero?

asked 2022-11-17 08:34:37 +0100

c.p. gravatar image

updated 2022-11-17 08:36:56 +0100

I was trying to define Taylor like series of functions, when I found out that

 sum( diff(sin(x),x,n), n,1,2)

yields 0. I have no idea what the correct syntax is. Why doesn't it give `diff(sin(x),x) +diff(sin(x),x,2)' (i.e. $\cos(x)-\sin(x)$)?

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2022-11-18 15:42:04 +0100

Emmanuel Charpentier gravatar image

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)

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...).


edit flag offensive delete link more


Sympy gives some infinite sum functionality

from sympy import *
achrzesz gravatar imageachrzesz ( 2022-11-18 17:16:38 +0100 )edit


achrzesz gravatar imageachrzesz ( 2022-11-18 17:27:09 +0100 )edit

Sage's sumhas some infinite sums abilities :

sage: p=var("p", domain="integer")
sage: with assuming(abs(x)-1<0): sum(x^p, p, 0, oo)
-1/(x - 1)

What I meant was that @FredericC 's workaround can't be applied to infinite sums...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-11-18 18:29:45 +0100 )edit

answered 2022-11-17 08:59:17 +0100

FrédéricC gravatar image

The command sum has two different meanings, depending on the syntax that you use.

On the one hand

sum(f(k), k, a, b)

means the symbolic sum, trying to find formulas and where k is a symbolic variable in the symbolic ring SR.

On the other hand,

sum(f(k) for k in range(a, b+1))

is just performing the summation, one value after another, as Python does. Here you need not (and should rather not) declare k as a symbolic variable.

You should rather use the python syntax, until you understand better what sage is doing.

edit flag offensive delete link more

answered 2022-11-17 12:34:18 +0100

achrzesz gravatar image
sum([diff(sin(x),x,n) for n in [1,2]])
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-11-17 08:34:37 +0100

Seen: 52 times

Last updated: Nov 18