Ask Your Question

Noob question about lists in sum()

asked 2012-08-26 05:48:00 -0500

physmathfun gravatar image

updated 2012-08-26 07:02:33 -0500

Hi there, I have a (presumably) very easy question:

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 "", line 10, in <module>
    exec compile(u'open("","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZGluZ3MgPSBbMSAsIDIsIDMsIDQsIDVdCmxlbmd0aCA9IGxlbihkaW5ncykKc3VtKDEvZGluZ3Nba10sIGssIDAsIGxlbmd0aCAtMSAp"),globals())+"\\n"); execfile(os.path.abspath(""))
  File "", line 1, in <module>

  File "/tmp/tmpDDv20Z/", 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!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-08-26 07:06:47 -0500

You can always do

sage: dings = [1 , 2, 3, 4, 5]
sage: length = len(dings)
sage: sum([1/dings[k] for k in range(length)])


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)
edit flag offensive delete link more

answered 2012-08-26 07:50:47 -0500

physmathfun gravatar image

updated 2012-08-26 11:14:02 -0500

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

edit flag offensive delete link more



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

DSM gravatar imageDSM ( 2012-08-26 08:23:34 -0500 )edit

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

physmathfun gravatar imagephysmathfun ( 2012-08-26 11:16:16 -0500 )edit

Hey, happens to all of us. :^)

DSM gravatar imageDSM ( 2012-08-26 11:51:52 -0500 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2012-08-26 05:48:00 -0500

Seen: 2,463 times

Last updated: Aug 26 '12