質問

I tried to make a summation list in Sage. The commands were:

sage: var('n')
sage: var('x')
sage: f = (2/n)*(sin(n*x)*(-1)^(n+1)) 
sage: funclist = [sum(f,n,1,20) for n in range(1,3)]

but it was error:

TypeError: need a summation variable

but when i tried some similar things on python shell. There was no any problem.

>>> x=1
>>> [pow(x,2) for x in range(1,9)]
[1, 4, 9, 16, 25, 36, 49, 64]

and return to Sage, there was no problem if i run program on Sage like this:

sage: var('n')
sage: var('x')
sage: sum(f,n,1,20)
-1/2*sin(4*x) + 2/3*sin(3*x) - sin(2*x) + 2*sin(x)

I don't know how Sage combine a 'sum' function into its program. And don't know how to solve this problem.

役に立ちましたか?

解決

Sage shell is different from the Pytyhon shell, and the function sum is different too. In Sage, it tries to find a symbolic sum, that's why the second argument needs to be a variable. In your first code block, you are essentially trying to evaluate

[sum(f, 1, 1, 20), sum(f, 2, 1, 20)]

From the mathematical point of view, how do you sum over 1? That's why Sage gives you an error. Notice that in the last code block, when you use the variable n, Sage is able to calculate the sum.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top