ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 11 Apr 2019 21:12:49 -0500function parameters as sum limitshttps://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/ 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?
Tue, 09 Apr 2019 07:13:11 -0500https://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/Answer by kcrisman for <p>I am completely new to sage, so I am afraid that this should be a very standard well known issue. Sorry for that.</p>
<p>I need to define a function one of whose parameters is a limit of a sum.
I tried:</p>
<pre><code>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)
</code></pre>
<p>But sage complains with a RuntimeError. Why is that? I suppose that for some reason the parameter <code>n</code> from the function is not assigned to the variable limit <code>n-1</code> in the sum. I this correct? How can I fix that?</p>
https://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/?answer=46083#post-id-46083I 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.Wed, 10 Apr 2019 18:06:10 -0500https://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/?answer=46083#post-id-46083Comment by kcrisman for <p>I will elaborate on the other answer, which is correct; you could use</p>
<pre><code>sage: def N(d,n):
....: if n==1:
....: return 1
....: else:
....: return sum(N(d,i) for i in range(1,n))
</code></pre>
<p>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 <code>n=2</code>, so you got a <code>RuntimeError</code> about infinite recursion.</p>
<p>If you do <code>sum?</code> in the terminal or notebook, you should get documentation that includes this:</p>
<pre><code> 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
</code></pre>
<p>Also, where does <code>d</code> occur in your function? It seems to be a dummy variable in the truest sense of the word - completely silent, that is.</p>
https://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/?comment=46105#post-id-46105Probably not at this point, since then we would have to update all the answers.Thu, 11 Apr 2019 21:12:49 -0500https://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/?comment=46105#post-id-46105Comment by suit for <p>I will elaborate on the other answer, which is correct; you could use</p>
<pre><code>sage: def N(d,n):
....: if n==1:
....: return 1
....: else:
....: return sum(N(d,i) for i in range(1,n))
</code></pre>
<p>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 <code>n=2</code>, so you got a <code>RuntimeError</code> about infinite recursion.</p>
<p>If you do <code>sum?</code> in the terminal or notebook, you should get documentation that includes this:</p>
<pre><code> 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
</code></pre>
<p>Also, where does <code>d</code> occur in your function? It seems to be a dummy variable in the truest sense of the word - completely silent, that is.</p>
https://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/?comment=46094#post-id-46094Thank 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?Thu, 11 Apr 2019 08:24:25 -0500https://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/?comment=46094#post-id-46094Answer by FrédéricC for <p>I am completely new to sage, so I am afraid that this should be a very standard well known issue. Sorry for that.</p>
<p>I need to define a function one of whose parameters is a limit of a sum.
I tried:</p>
<pre><code>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)
</code></pre>
<p>But sage complains with a RuntimeError. Why is that? I suppose that for some reason the parameter <code>n</code> from the function is not assigned to the variable limit <code>n-1</code> in the sum. I this correct? How can I fix that?</p>
https://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/?answer=46067#post-id-46067Use this:
return sum(N(d,i) for i in range(1,n))
Your syntax is for symbolic sums only. And remove your first line.Tue, 09 Apr 2019 13:18:43 -0500https://ask.sagemath.org/question/46060/function-parameters-as-sum-limits/?answer=46067#post-id-46067