ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 06 Sep 2013 12:02:37 +0200A (beginner) problem with Cythonhttps://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/Why the following small code generates a problem in Sage-Cython ?
from sage.all import *
cpdef acceptablenumbers(int n, int m):
cdef int i,a,s
cdef list S,l
S=[]
for i in range(n,m+1):
l=list(factor(i))
a=len(l)
s=sum(l[j][1] for j in range(a))
if ((a==3 and s>3) or a>3):
S.append(i)
return S
while the following code runs very well in Sage-Python :
def acceptablenumbers(n,m):
S=[]
for i in range(n,m+1):
l=list(factor(i))
a=len(l)
s=sum(l[j][1] for j in range(a))
if ((a==3 and s>3) or a>3):
S.append(i)
return SWed, 04 Sep 2013 17:48:34 +0200https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/Answer by Luca for <p>Why the following small code generates a problem in Sage-Cython ? </p>
<pre><code>from sage.all import *
cpdef acceptablenumbers(int n, int m):
cdef int i,a,s
cdef list S,l
S=[]
for i in range(n,m+1):
l=list(factor(i))
a=len(l)
s=sum(l[j][1] for j in range(a))
if ((a==3 and s>3) or a>3):
S.append(i)
return S
</code></pre>
<p>while the following code runs very well in Sage-Python : </p>
<pre><code>def acceptablenumbers(n,m):
S=[]
for i in range(n,m+1):
l=list(factor(i))
a=len(l)
s=sum(l[j][1] for j in range(a))
if ((a==3 and s>3) or a>3):
S.append(i)
return S
</code></pre>
https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?answer=15414#post-id-15414I don't think Cython supports generators yet. In any case, changing
s=sum(l[j][1] for j in range(a))
to
s=sum([l[j][1] for j in range(a)])
solves the issue.
Wed, 04 Sep 2013 18:22:36 +0200https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?answer=15414#post-id-15414Comment by Nathann for <p>I don't think Cython supports generators yet. In any case, changing </p>
<pre><code>s=sum(l[j][1] for j in range(a))
</code></pre>
<p>to</p>
<pre><code>s=sum([l[j][1] for j in range(a)])
</code></pre>
<p>solves the issue.</p>
https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17047#post-id-17047Well, I just meant that it works in Cython the same way that it works in Python, which is still somehow recent.Thu, 05 Sep 2013 10:41:34 +0200https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17047#post-id-17047Comment by Sébastien Palcoux for <p>I don't think Cython supports generators yet. In any case, changing </p>
<pre><code>s=sum(l[j][1] for j in range(a))
</code></pre>
<p>to</p>
<pre><code>s=sum([l[j][1] for j in range(a)])
</code></pre>
<p>solves the issue.</p>
https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17056#post-id-17056@Nathann : It works for what ? What do you mean ?Thu, 05 Sep 2013 06:16:12 +0200https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17056#post-id-17056Comment by Nathann for <p>I don't think Cython supports generators yet. In any case, changing </p>
<pre><code>s=sum(l[j][1] for j in range(a))
</code></pre>
<p>to</p>
<pre><code>s=sum([l[j][1] for j in range(a)])
</code></pre>
<p>solves the issue.</p>
https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17057#post-id-17057The yield keyword is available and works, though :-)Thu, 05 Sep 2013 05:53:25 +0200https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17057#post-id-17057Comment by Nathann for <p>I don't think Cython supports generators yet. In any case, changing </p>
<pre><code>s=sum(l[j][1] for j in range(a))
</code></pre>
<p>to</p>
<pre><code>s=sum([l[j][1] for j in range(a)])
</code></pre>
<p>solves the issue.</p>
https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17030#post-id-17030The 'yield' keyword.Fri, 06 Sep 2013 12:02:37 +0200https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17030#post-id-17030Comment by Sébastien Palcoux for <p>I don't think Cython supports generators yet. In any case, changing </p>
<pre><code>s=sum(l[j][1] for j in range(a))
</code></pre>
<p>to</p>
<pre><code>s=sum([l[j][1] for j in range(a)])
</code></pre>
<p>solves the issue.</p>
https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17059#post-id-17059Thank you Luca, you solved my problem.Wed, 04 Sep 2013 18:29:44 +0200https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17059#post-id-17059Comment by Sébastien Palcoux for <p>I don't think Cython supports generators yet. In any case, changing </p>
<pre><code>s=sum(l[j][1] for j in range(a))
</code></pre>
<p>to</p>
<pre><code>s=sum([l[j][1] for j in range(a)])
</code></pre>
<p>solves the issue.</p>
https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17046#post-id-17046@Nathann : I'm sorry for my misunderstanding, but what is your "it" in "it works" ?Thu, 05 Sep 2013 11:02:55 +0200https://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/?comment=17046#post-id-17046