ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 18 May 2011 17:23:11 +0200How-to: sumhttps://ask.sagemath.org/question/8027/how-to-sum/Suppose I have defined a function
f(x)=x^2
and a list
x=[0,1,2,3,4]
Now I can calculate the product
x[2]*f(x[2])
But: If I use the command
k=var('k')
sum(x[k]*f(x[k]),k,0,4)
I will get the message:
TypeError: unable to convert x (=k) to an integer
How can I realize such a summation?Sat, 26 Mar 2011 18:01:24 +0100https://ask.sagemath.org/question/8027/how-to-sum/Answer by parzan for <p>Suppose I have defined a function </p>
<pre><code>f(x)=x^2
</code></pre>
<p>and a list</p>
<pre><code>x=[0,1,2,3,4]
</code></pre>
<p>Now I can calculate the product </p>
<pre><code>x[2]*f(x[2])
</code></pre>
<p>But: If I use the command</p>
<pre><code>k=var('k')
sum(x[k]*f(x[k]),k,0,4)
</code></pre>
<p>I will get the message: </p>
<pre><code>TypeError: unable to convert x (=k) to an integer
</code></pre>
<p>How can I realize such a summation?</p>
https://ask.sagemath.org/question/8027/how-to-sum/?answer=12250#post-id-12250Here are some ways to sum the squares of the numbers from 0 to 1000, with timings:
sage: var('x');f = x^2;
sage: timeit('sum(f,x,0,1000)')
5 loops, best of 3: 271 ms per loop
sage: timeit('sum([f(x=t) for t in [0..1000]])')
5 loops, best of 3: 39 ms per loop
sage: timeit('sum([t^2 for t in [0..1000]])')
625 loops, best of 3: 1.21 ms per loop
sage: timeit('reduce(lambda x,y:x+y^2,[0..1000],0)')
625 loops, best of 3: 1.13 ms per loop
sage: timeit('reduce(lambda x,y:x+y^2,xrange(0,1001),0)')
625 loops, best of 3: 945 µs per loop
So if speed matters to you, you should learn a little python!
For example, the syntax [EXPR for VAR in LIST] is very handy - it evaluates EXPR with VAR ranging over the list (or any iterable object) LIST.
You can even use it with a filtering:
sum([x^2 for x in [0..1000] if is_prime(x)])
will sum the squares of primes in this range.
To understand the last two summation examples above you should read about lambda, reduce, and xrange in a python tutorial. It will pay off!
Sat, 02 Apr 2011 14:55:23 +0200https://ask.sagemath.org/question/8027/how-to-sum/?answer=12250#post-id-12250Answer by cswiercz for <p>Suppose I have defined a function </p>
<pre><code>f(x)=x^2
</code></pre>
<p>and a list</p>
<pre><code>x=[0,1,2,3,4]
</code></pre>
<p>Now I can calculate the product </p>
<pre><code>x[2]*f(x[2])
</code></pre>
<p>But: If I use the command</p>
<pre><code>k=var('k')
sum(x[k]*f(x[k]),k,0,4)
</code></pre>
<p>I will get the message: </p>
<pre><code>TypeError: unable to convert x (=k) to an integer
</code></pre>
<p>How can I realize such a summation?</p>
https://ask.sagemath.org/question/8027/how-to-sum/?answer=12229#post-id-12229Given the syntax of the `sum()` function, which can be read by executing "`sum?`" (with the question mark) in the Sage notebook, you don't need to define a list to compute the sum. For example,
sage: var('x')
sage: f = x^2 # note that you don't write f(x) = x^2
sage: sum(f,x,0,4)
30
Also, be careful defining a variable, `x`, as well as a list with the same name. (You wrote `f = x^2` as well as `x=[0,1,2,3,4]`.) Hope this helps.Sat, 26 Mar 2011 19:52:32 +0100https://ask.sagemath.org/question/8027/how-to-sum/?answer=12229#post-id-12229Answer by Volker Braun for <p>Suppose I have defined a function </p>
<pre><code>f(x)=x^2
</code></pre>
<p>and a list</p>
<pre><code>x=[0,1,2,3,4]
</code></pre>
<p>Now I can calculate the product </p>
<pre><code>x[2]*f(x[2])
</code></pre>
<p>But: If I use the command</p>
<pre><code>k=var('k')
sum(x[k]*f(x[k]),k,0,4)
</code></pre>
<p>I will get the message: </p>
<pre><code>TypeError: unable to convert x (=k) to an integer
</code></pre>
<p>How can I realize such a summation?</p>
https://ask.sagemath.org/question/8027/how-to-sum/?answer=12352#post-id-12352The first syntax is symbolic summation:
sage: var('a,b,i')
sage: sum(2^i, i, a, b)
-2^a + 2^(b + 1)
It will always try to evaluate the sum symbolically. If you use something that doesn't understand symbolic variables (like GF(8), which is implemented by polybori) then you get the "unable to convert to integer" error. Because polybori can only exponentiate by integers, and not by formal variables.
The second syntax is Python's list creation and summation of lists:
sage: [ 2^j for j in range(0,3) ]
[1, 2, 4]
sage: sum(_)
7
Note that the variable inside the list comprehension (which I called j here) is automatically created and will overwrite other variables. In your second example, calling the inner variable i overwrites the previously-defined symbolic variable var('i')
Mon, 16 May 2011 10:54:01 +0200https://ask.sagemath.org/question/8027/how-to-sum/?answer=12352#post-id-12352Comment by kcrisman for <p>The first syntax is symbolic summation:</p>
<pre><code>sage: var('a,b,i')
sage: sum(2^i, i, a, b)
-2^a + 2^(b + 1)
</code></pre>
<p>It will always try to evaluate the sum symbolically. If you use something that doesn't understand symbolic variables (like GF(8), which is implemented by polybori) then you get the "unable to convert to integer" error. Because polybori can only exponentiate by integers, and not by formal variables.</p>
<p>The second syntax is Python's list creation and summation of lists:</p>
<pre><code>sage: [ 2^j for j in range(0,3) ]
[1, 2, 4]
sage: sum(_)
7
</code></pre>
<p>Note that the variable inside the list comprehension (which I called j here) is automatically created and will overwrite other variables. In your second example, calling the inner variable i overwrites the previously-defined symbolic variable var('i')</p>
https://ask.sagemath.org/question/8027/how-to-sum/?comment=21738#post-id-21738This is a great thread, if one can call this a thread. Many different points of view and followups!Tue, 17 May 2011 10:00:07 +0200https://ask.sagemath.org/question/8027/how-to-sum/?comment=21738#post-id-21738Answer by Vincent for <p>Suppose I have defined a function </p>
<pre><code>f(x)=x^2
</code></pre>
<p>and a list</p>
<pre><code>x=[0,1,2,3,4]
</code></pre>
<p>Now I can calculate the product </p>
<pre><code>x[2]*f(x[2])
</code></pre>
<p>But: If I use the command</p>
<pre><code>k=var('k')
sum(x[k]*f(x[k]),k,0,4)
</code></pre>
<p>I will get the message: </p>
<pre><code>TypeError: unable to convert x (=k) to an integer
</code></pre>
<p>How can I realize such a summation?</p>
https://ask.sagemath.org/question/8027/how-to-sum/?answer=12351#post-id-12351I met a similar problem. Take the following example.
i=var('i')
K.<a> = GF(8, 'a')
for k in K:
print sum(k^(2^i),i,0,3)
which prints:
Traceback (click to the left of this block for traceback)
...
TypeError: unable to convert x (=i) to an integer
Let us try another syntax.
i=var('i')
K.<a> = GF(8, 'a')
for k in K:
sum([k^(2^i) for i in [0..3]])
Now it is okay thanks to the previous answer. I wonder if it's possible to make it work with the first syntax.
Sun, 15 May 2011 11:55:37 +0200https://ask.sagemath.org/question/8027/how-to-sum/?answer=12351#post-id-12351Answer by Jason Bandlow for <p>Suppose I have defined a function </p>
<pre><code>f(x)=x^2
</code></pre>
<p>and a list</p>
<pre><code>x=[0,1,2,3,4]
</code></pre>
<p>Now I can calculate the product </p>
<pre><code>x[2]*f(x[2])
</code></pre>
<p>But: If I use the command</p>
<pre><code>k=var('k')
sum(x[k]*f(x[k]),k,0,4)
</code></pre>
<p>I will get the message: </p>
<pre><code>TypeError: unable to convert x (=k) to an integer
</code></pre>
<p>How can I realize such a summation?</p>
https://ask.sagemath.org/question/8027/how-to-sum/?answer=12360#post-id-12360To add to the diversity of answers here, another valid approach is to add a single line to what you already have:
f(x)=x^2
g(x)=x*f(x)
k=var('k')
sum(g(k),k,0,4)
This will return 100. The point is that you can use this syntax to sum the values of a proper function, but not arbitrary Python expressions.Wed, 18 May 2011 17:23:11 +0200https://ask.sagemath.org/question/8027/how-to-sum/?answer=12360#post-id-12360