Ask Your Question
0

A (beginner) problem with Cython, II

asked 2013-09-05 00:07:44 -0500

Why the following code generates a problem in Sage-Cython ?
(note the recursion and the global variable bb)

cpdef lintsolve(list T, list A, int m, int ep):
    global bb;
    cdef int t0,t,n,i,m1
    cdef list A1,T1
    if not A:
        bb=[]
    t0=T[0]
    t=len(T)
    if t==1 and m%t0==0:
        A1=[ep]
        A1.extend(A)
        A1.append(m//t0)
        bb.extend([A1])
    if t>1:
        n=m//t0
        for i in range(n+1):
            T1=T[1:]
            m1=m-i*t0
            A1=A[:]
                A1.append(i)
            lintsolve(T1,A1,m1,ep)
    return bb

while the following code runs very well in Sage-Python :

def lintsolve(T,A,m,ep):
    global bb;
    if not A:
        bb=[]
    t0=T[0]
    t=len(T)
    if t==1 and m%t0==0:
        A1=[ep]
        A1.extend(A)
        A1.append(m//t0)
        bb.extend([A1])
    if t>1:
        n=m//t0
        for i in range(n+1):
            T1=T[1:]
            m1=m-i*t0
            A1=A[:]
                A1.append(i)
            lintsolve(T1,A1,m1,ep)
    return bb
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2013-09-05 01:23:03 -0500

Luca gravatar image

Neither work for me, but if I correct the indentation error at

        A1=A[:]
            A1.append(i)

both work for me.

edit flag offensive delete link more

Comments

Thanks @Luca, but this is quite strange, this indentation error does not appear in my text, it appears only when I copy-past on the post, after pushing on the button "preformatted text"...!?

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-09-05 01:37:54 -0500 )edit

Try by yourself, if you push on "edit", select the first code, and push on "preformatted text", then the first line and the line with "A1.append(i)" shift, whereas the other lines not... do you know why ?

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-09-05 01:54:04 -0500 )edit

Anyway, by removing the indentation of my code and indenting again (the same), it works miraculously...

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-09-05 01:57:26 -0500 )edit

I can't edit your question. The problem is maybe due to a mixture of tabs and spaces. What editor do you use?

Luca gravatar imageLuca ( 2013-09-05 02:49:16 -0500 )edit

Yes, it seems to be caused such a mixture, is it a bug ? I use gedit on Ubuntu.

Sébastien Palcoux gravatar imageSébastien Palcoux ( 2013-09-05 02:53:32 -0500 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2013-09-05 00:07:44 -0500

Seen: 130 times

Last updated: Sep 05 '13