递归函数返回none类型,尽管计算类型是元组

时间:2015-02-06 23:01:00

标签: python-2.7 recursion nonetype

我尝试实施de Boor算法的Python版本:

cvx=lambda a, b, t: (1-t) * a + t * b 
cvxP=lambda (P, Q, t): (cvx(P[0], Q[0], t), cvx(P[1], Q[1], t))

def omega(u, k, t):
    return map(lambda j: (t-u[j])/(u[j+k]-u[j]), xrange(1,k+1))

def cvxList( d, alpha):
    return map(cvxP, zip(d[:-1], d[1:], alpha))


def DeBoor(d, u, k, t):
    #evaluates a point c(t) on an arc of B-spline curve
    # of degree k, defined by:
    # u_1<=... u_k<u_{k+1}<=...u_{2k}  the sequence of knots
    # d_0, d_1, ... d_k   de Boor points 
    # t in [u_k, u_{k+1})

    if (k>0):
        #print d, u, k
        DeBoor(cvxList(d, omega(u, k,t)), u[1:-1], k-1,t )  
    else:   
        #print d[0], type(d[0])
        return d[0]

函数DeBoor将点计算为tuple。 但是当我检查cpt=DeBoor(args)的类型时 它显示为NoneType

d=[(0.16, 0.18), (0.22, 0.84), (0.83, 0.74), (0.80, 0.16)]
u=[j for j in range(7)]
k=3
cpt=DeBoor(d, u, k, 3.5)
print cpt, type(cpt)

>>>(0.5231250000000001, 0.7641666666666667) <type 'tuple'>
>>>None <type 'NoneType'>

我无法弄清楚为什么没有将正确的类型传递给cpt

1 个答案:

答案 0 :(得分:1)

您的递归通话不会返回任何内容:

if (k>0):
    return DeBoor(...)