Ask Your Question
1

function parameters as sum limits

asked 2019-04-09 07:13:11 -0500

suit gravatar image

updated 2019-04-10 02:19:29 -0500

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-10 18:06:10 -0500

kcrisman gravatar image

updated 2019-04-10 18:06:42 -0500

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 08:24:25 -0500 )edit

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

kcrisman gravatar imagekcrisman ( 2019-04-11 21:12:49 -0500 )edit
2

answered 2019-04-09 13:18:43 -0500

FrédéricC gravatar image

updated 2019-04-09 13:19:44 -0500

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 07:13:11 -0500

Seen: 54 times

Last updated: Apr 10