Ask Your Question

physmathfun's profile - activity

2014-07-31 11:43:08 +0200 received badge  Famous Question (source)
2013-11-07 10:59:04 +0200 received badge  Notable Question (source)
2013-08-08 10:03:12 +0200 received badge  Popular Question (source)
2012-08-26 18:31:51 +0200 received badge  Scholar (source)
2012-08-26 18:31:51 +0200 marked best answer Noob question about lists in sum()

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
2012-08-26 18:30:58 +0200 received badge  Supporter (source)
2012-08-26 18:16:16 +0200 commented answer Noob question about lists in sum()

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.

2012-08-26 14:53:52 +0200 received badge  Editor (source)
2012-08-26 14:50:47 +0200 answered a question Noob question about lists in sum()

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

2012-08-26 12:48:00 +0200 asked a question 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!