Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

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

asked 2 years ago

c.p. gravatar image

updated 2 years ago

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

 var('x,n')
 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))?

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
1

answered 2 years ago

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 dndsinxdx=cosxdn=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,

Preview: (hide)
link

Comments

Sympy gives some infinite sum functionality

from sympy import *
x=symbols('x')
n=symbols('n',integer=True)
Sum(x**n,(n,0,oo))
achrzesz gravatar imageachrzesz ( 2 years ago )

Or:

x=symbols('x',real=True)
fps(1/(1-x))
achrzesz gravatar imageachrzesz ( 2 years ago )

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 ( 2 years ago )
2

answered 2 years ago

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.

Preview: (hide)
link
1

answered 2 years ago

achrzesz gravatar image
sum([diff(sin(x),x,n) for n in [1,2]])
Preview: (hide)
link

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

Stats

Asked: 2 years ago

Seen: 282 times

Last updated: Nov 18 '22