【问题标题】:A recursive function returns a none type although the computed type is tuple尽管计算类型是元组,但递归函数返回无类型
【发布时间】:2015-02-06 23:00:59
【问题描述】:

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

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

【问题讨论】:

    标签: python-2.7 recursion nonetype


    【解决方案1】:

    您的递归调用没有返回任何内容:

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

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 2016-04-05
      • 1970-01-01
      相关资源
      最近更新 更多