2011-07-26 24 views
1

私は自分のプロジェクトのためにPythonプログラミングを行っています。これは別の簡単な質問かもしれません。私はxである関数poly_root()で計算された値を使う必要があるこのコードを持っています。その値は、bezier()関数のuとして使用する必要があります。 poly_root()関数の後、計算された値でbezier()関数に移動する必要があります。私が正しい方法でそれをやっているかどうかはわかりません。エラーはありませんが、bezier()関数からは出力されません。どうもありがとうございました。コードのこの部分である関数から計算された値を別の関数に渡す

import copy 

import math 

poly = [[-0.8,3], [0.75,2], [-0.75,1], [0.1,0]] 

def poly_diff(poly): 
    """ Differentiate a polynomial. """ 
    newlist = copy.deepcopy(poly) 

    for term in newlist: 
     term[0] *= term[1] 
     term[1] -= 1 

    return newlist 

def poly_apply(poly, x): 
    """ Apply values to the polynomial. """ 

    sum = 0.0 # force float 

    for term in poly: 
     sum += term[0] * (x ** term[1]) 

    return sum 

def poly_root(poly, start, n, r_i): 
    """ Returns a root of the polynomial, with a starting value.""" 

    poly_d = poly_diff(poly) 
    x = start # starting guess value 
    counter = 0 

    while True: 
     if (n >= 0) and (n < 1): 
      break 

     x_n = x - (float(poly_apply(poly, x))/poly_apply(poly_d, x)) 

     if x_n == x: 
      break 

     x = x_n # this is the u value corresponding to the given time which will be used in bezier equation 

     n -= 1 
     counter += 1 

    if r_i: 
     #print [x, counter]) 
     return [x, counter] 
    else: 
     #print x 
     return x 
    bezier(x) 

def bezier(value) : 
    """ Calculates control points using rational bezier curve equation""" 

    u = value 
    w = 5 

    t = math.pow(1-u,3) * points[0][0] + 3 * u * math.pow(1-u,2) * points[1][0] \ 
     + 3 * (1-u) * math.pow(u,2) * points[2][0] + math.pow(u,3) * points[3][0] 

    t = t * w 

    d = math.pow(1-u,3) * w + 3 * u * w * math.pow(1-u,2) + 3 * (1-u) * w \ 
     * math.pow(u,2) + math.pow(u,3) * w 


    t = t/d 
    print t 

if __name__ == "__main__" : 
    poly_root(poly, 0.42, 1, 0) 

答えて

3

if r_i: 
    #print [x, counter]) 
    return [x, counter] 
else: 
    #print x 
    return x 
bezier(x) 

bezier(x)が到達不能です。あなたはそれを書き直す必要があります。

if r_i: 
    return [x, counter] 
else: 
    return [x, None] 

が続いて下部に、あなたが持つことができる
+0

私はそれを得ました。もちろん、私は何を考えていたのですか?今は正しく動作しています。ありがとうございました。 – zingy

1

poly_rootは、両方の状況にあるものと同じタイプ(2つの要素を持つ、すなわちリスト)を返すことが良いだろう... ...

if __name__ == "__main__" : 
    x, counter = poly_root(poly, 0.42, 1, 0) 
    if counter is None: # I don't know if this is what you intended with your code. 
     bezier(x) 
関連する問題