Ask Your Question

Revision history [back]

A (beginner) problem with Cython, II

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