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.Sun, 26 Aug 2012 18:51:52 +0200Noob question about lists in sum()https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/Hi there, I have a (presumably) very easy question:
var('k')
sum(1/k, k, 1, 5)
works, but
dings = [1 , 2, 3, 4, 5]
length = len(dings)
sum(1/dings[k], k, 0, length - 1)
does not.
Error Message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_6.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZGluZ3MgPSBbMSAsIDIsIDMsIDQsIDVdCmxlbmd0aCA9IGxlbihkaW5ncykKc3VtKDEvZGluZ3Nba10sIGssIDAsIGxlbmd0aCAtMSAp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpDDv20Z/___code___.py", line 5, in <module>
exec compile(u'sum(_sage_const_1 /dings[k], k, _sage_const_0 , length -_sage_const_1 )
File "", line 1, in <module>
File "expression.pyx", line 4163, in sage.symbolic.expression.Expression.__index__ (sage/symbolic/expression.cpp:20198)
File "expression.pyx", line 745, in sage.symbolic.expression.Expression._integer_ (sage/symbolic/expression.cpp:5152)
TypeError: unable to convert x (=k) to an integer
So I tried writing it with dings[int(k)], didn't work either.
Why? Thanks!
Sun, 26 Aug 2012 12:48:00 +0200https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/Answer by physmathfun for <p>Hi there, I have a (presumably) very easy question:</p>
<pre><code>var('k')
sum(1/k, k, 1, 5)
</code></pre>
<p>works, but</p>
<pre><code>dings = [1 , 2, 3, 4, 5]
length = len(dings)
sum(1/dings[k], k, 0, length - 1)
</code></pre>
<p>does not.</p>
<p>Error Message:</p>
<pre><code>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_6.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZGluZ3MgPSBbMSAsIDIsIDMsIDQsIDVdCmxlbmd0aCA9IGxlbihkaW5ncykKc3VtKDEvZGluZ3Nba10sIGssIDAsIGxlbmd0aCAtMSAp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpDDv20Z/___code___.py", line 5, in <module>
exec compile(u'sum(_sage_const_1 /dings[k], k, _sage_const_0 , length -_sage_const_1 )
File "", line 1, in <module>
File "expression.pyx", line 4163, in sage.symbolic.expression.Expression.__index__ (sage/symbolic/expression.cpp:20198)
File "expression.pyx", line 745, in sage.symbolic.expression.Expression._integer_ (sage/symbolic/expression.cpp:5152)
TypeError: unable to convert x (=k) to an integer
</code></pre>
<p>So I tried writing it with dings[int(k)], didn't work either.</p>
<p>Why? Thanks!</p>
https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?answer=13963#post-id-13963[edit]My following answer is dumb due to not properly reading the first answer[/edit]
Thanks for your answer!
The thing is that I actually want to use a list of measured values, so dings would not be a list of following integers (like range(1,5)), but some (real) numbers, for example
dings = [1.2438, 1.2473, 1.2398] just with more than 3 numbers. So I do need to use a list in a sum context.
I could write it with a for loop (and did, which worked), but I really want to use a sum.Sun, 26 Aug 2012 14:50:47 +0200https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?answer=13963#post-id-13963Comment by DSM for <p>[edit]My following answer is dumb due to not properly reading the first answer[/edit]</p>
<p>Thanks for your answer!</p>
<p>The thing is that I actually want to use a list of measured values, so dings would not be a list of following integers (like range(1,5)), but some (real) numbers, for example
dings = [1.2438, 1.2473, 1.2398] just with more than 3 numbers. So I do need to use a list in a sum context.
I could write it with a for loop (and did, which worked), but I really want to use a sum.</p>
https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?comment=19175#post-id-19175Hey, happens to all of us. :^)Sun, 26 Aug 2012 18:51:52 +0200https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?comment=19175#post-id-19175Comment by DSM for <p>[edit]My following answer is dumb due to not properly reading the first answer[/edit]</p>
<p>Thanks for your answer!</p>
<p>The thing is that I actually want to use a list of measured values, so dings would not be a list of following integers (like range(1,5)), but some (real) numbers, for example
dings = [1.2438, 1.2473, 1.2398] just with more than 3 numbers. So I do need to use a list in a sum context.
I could write it with a for loop (and did, which worked), but I really want to use a sum.</p>
https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?comment=19177#post-id-19177Did you try what was recommended? John's first suggestion will fit your use case. I would write it as `sum(1/d for d in dings)`, myself.Sun, 26 Aug 2012 15:23:34 +0200https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?comment=19177#post-id-19177Comment by physmathfun for <p>[edit]My following answer is dumb due to not properly reading the first answer[/edit]</p>
<p>Thanks for your answer!</p>
<p>The thing is that I actually want to use a list of measured values, so dings would not be a list of following integers (like range(1,5)), but some (real) numbers, for example
dings = [1.2438, 1.2473, 1.2398] just with more than 3 numbers. So I do need to use a list in a sum context.
I could write it with a for loop (and did, which worked), but I really want to use a sum.</p>
https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?comment=19176#post-id-19176Me idiot! It sure does! I just somehow ... didn't recognize that on my first read.
So thx you two and sry for wasting your time by being temporarily illiterate.Sun, 26 Aug 2012 18:16:16 +0200https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?comment=19176#post-id-19176Answer by John Palmieri for <p>Hi there, I have a (presumably) very easy question:</p>
<pre><code>var('k')
sum(1/k, k, 1, 5)
</code></pre>
<p>works, but</p>
<pre><code>dings = [1 , 2, 3, 4, 5]
length = len(dings)
sum(1/dings[k], k, 0, length - 1)
</code></pre>
<p>does not.</p>
<p>Error Message:</p>
<pre><code>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_6.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZGluZ3MgPSBbMSAsIDIsIDMsIDQsIDVdCmxlbmd0aCA9IGxlbihkaW5ncykKc3VtKDEvZGluZ3Nba10sIGssIDAsIGxlbmd0aCAtMSAp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpDDv20Z/___code___.py", line 5, in <module>
exec compile(u'sum(_sage_const_1 /dings[k], k, _sage_const_0 , length -_sage_const_1 )
File "", line 1, in <module>
File "expression.pyx", line 4163, in sage.symbolic.expression.Expression.__index__ (sage/symbolic/expression.cpp:20198)
File "expression.pyx", line 745, in sage.symbolic.expression.Expression._integer_ (sage/symbolic/expression.cpp:5152)
TypeError: unable to convert x (=k) to an integer
</code></pre>
<p>So I tried writing it with dings[int(k)], didn't work either.</p>
<p>Why? Thanks!</p>
https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?answer=13962#post-id-13962You can always do
sage: dings = [1 , 2, 3, 4, 5]
sage: length = len(dings)
sage: sum([1/dings[k] for k in range(length)])
or
sage: var('k')
sage: d(k) = 1/k # defines d to be a (symbolic) function
sage: sum(d(k), k, 1, 5)
The second version works more generally:
sage: var('k')
sage: d(k) = 1/k^2
sage: sum(d(k), k, 1, infinity)
1/6*pi^2Sun, 26 Aug 2012 14:06:47 +0200https://ask.sagemath.org/question/9266/noob-question-about-lists-in-sum/?answer=13962#post-id-13962