Ask Your Question
0

Noob question about lists in sum()

asked 2012-08-26 12:48:00 +0100

physmathfun gravatar image

updated 2012-08-26 14:02:33 +0100

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!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2012-08-26 14:06:47 +0100

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)])

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^2
edit flag offensive delete link more
0

answered 2012-08-26 14:50:47 +0100

physmathfun gravatar image

updated 2012-08-26 18:14:02 +0100

[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

Comments

1

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 15:23:34 +0100 )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 18:16:16 +0100 )edit

Hey, happens to all of us. :^)

DSM gravatar imageDSM ( 2012-08-26 18:51:52 +0100 )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

Stats

Asked: 2012-08-26 12:48:00 +0100

Seen: 5,269 times

Last updated: Aug 26 '12