Ask Your Question
1

function parameters as sum limits

asked 2019-04-09 14:13:11 +0200

suit gravatar image

updated 2019-04-10 09:19:29 +0200

FrédéricC gravatar image

I am completely new to sage, so I am afraid that this should be a very standard well known issue. Sorry for that.

I need to define a function one of whose parameters is a limit of a sum. I tried:

d,n,i = var('d,n,i')
def N(d,n):
 if n==1:
        return 1
 else:
        return sum(N(d,i),i,1,n-1)

But sage complains with a RuntimeError. Why is that? I suppose that for some reason the parameter n from the function is not assigned to the variable limit n-1 in the sum. I this correct? How can I fix that?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2019-04-11 01:06:10 +0200

kcrisman gravatar image

updated 2019-04-11 01:06:42 +0200

I will elaborate on the other answer, which is correct; you could use

sage: def N(d,n):
....:  if n==1:
....:         return 1
....:  else:
....:         return sum(N(d,i) for i in range(1,n))

Ordinarily, this should have just returned some sort of evident error that you couldn't do it (such as below). But because of a subtlety in how you defined your function, instead you never actually discovered this in your sum. Your sum instead kept returning itself when n=2, so you got a RuntimeError about infinite recursion.

If you do sum? in the terminal or notebook, you should get documentation that includes this:

   Warning: This function only works with symbolic expressions. To
     sum any other objects like list elements or function return
     values, please use python summation, see
     http://docs.python.org/library/functions.html#sumIn particular,
     this does not work:

        sage: n = var('n')
        sage: mylist = [1,2,3,4,5]
        sage: sum(mylist[n], n, 0, 3)
        Traceback (most recent call last):
        ...
        TypeError: unable to convert n to an integer

     Use python "sum()" instead:

        sage: sum(mylist[n] for n in range(4))
        10

Also, where does d occur in your function? It seems to be a dummy variable in the truest sense of the word - completely silent, that is.

edit flag offensive delete link more

Comments

1

Thank you for the clarification; and yes d is just a remainder from the original (more complicated) function I needed to compute. Should I remove it from the question?

suit gravatar imagesuit ( 2019-04-11 15:24:25 +0200 )edit

Probably not at this point, since then we would have to update all the answers.

kcrisman gravatar imagekcrisman ( 2019-04-12 04:12:49 +0200 )edit
2

answered 2019-04-09 20:18:43 +0200

FrédéricC gravatar image

updated 2019-04-09 20:19:44 +0200

Use this:

return sum(N(d,i) for i in range(1,n))

Your syntax is for symbolic sums only. And remove your first line.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-04-09 14:13:11 +0200

Seen: 564 times

Last updated: Apr 11 '19