2016-03-08 21 views
1

我寫了下面兩種功能:如何在python中使用矢量化方法?

def iterate_list(f, x0, n_iterations): 

    # initialise the list consisting of iterates of f 
    list = [x0] 

    # initialise x 
    x = x0 

    # iterate using for loop 
    for n in xrange(n_iterations): 
     x = f(x) 
     list.append(x) 

    # return the list consisting of iterates of f 
    return list 

def lyapunov_exponent(f, df, x0, n_iterations): 

    from iterate_list import iterate_list 
    import numpy as np 

    # vectorise the list of iterates x0 to xn of f 
    vec = np.array(iterate_list(f, x0, n_iterations)) 

    # calculate the sum of all the values of df for x0 to xn 
    s = sum(np.log(abs(df(vec)))) 

    # calculate the mean as an estimate of the lyapunov exponent 
    return s/(n_iterations + 1) 

f = k * x * (1-x) 
df = k * (1 - 2*x) 

但是現在我想改變的k值的兩種功能,並設置了k = np.linspace(3.0, 4.0, 11)。我如何修改函數,使其返回每個k值的Lyapunov指數列表?我現在遇到的困難是我想使用矢量化方法而不是循環。

+11

請粘貼代碼** **直接進入你的問題。 – MarkyPython

+0

@MarkyPython現在可以工作嗎? – Febday

+0

@AimeeHe:不,你發佈了你的代碼的照片。我們無法將代碼圖片剪切並粘貼到代碼編輯器中運行。刪除圖片,並將代碼作爲文本發佈。 – Gerrat

回答

0

你的功能,在這種背景下運行:

f = lambda x,k: k * x * (1-x) 
df = lambda x,k: k * (1 - 2*x) 

# test k iteratively 
for k in [1,2]: 
    print(iterate_list(f, .25, k, 5)) 
    print(lyapunov_exponent(f, df, .25, k, 5)) 

# test k as array; x0 is matching size 
k = np.array([1,2]) 
x0 = np.array([.25,.25]) 
ll = iterate_list(f, x0, k, 5) 
print(ll) 
la = np.array(ll) # turn list array as done in lyapunov 
print(la.shape) 
print(la[:,0]) 
print(la[:,1]) 
print(lyapunov_exponent(f, df, x0, k, 5)) 

產生

2243:~/mypy$ python2.7 stack35879467.py 
[0.25, 0.1875, 0.15234375, 0.1291351318359375, 0.11245924956165254, 0.09981216674968249] 
-0.383796035676 
[0.25, 0.375, 0.46875, 0.498046875, 0.49999237060546875, 0.4999999998835847] 
-6.58489821532 
[array([ 0.25, 0.25]), array([ 0.1875, 0.375 ]), array([ 0.15234375, 0.46875 ]), array([ 0.12913513, 0.49804688]), array([ 0.11245925, 0.49999237]), array([ 0.09981217, 0.5  ])] 
(6, 2) 
[ 0.25  0.1875  0.15234375 0.12913513 0.11245925 0.09981217] 
[ 0.25  0.375  0.46875  0.49804688 0.49999237 0.5  ] 
[-0.38379604 -6.58489822] 

所以用向量k運行問題(和匹配x0)產生相同的結果反覆做。實際上,kx0可以具有幾乎任何維度(0d,1d,2d等),只要它們具有相同的形狀。

它可能可以清理東西,但你的功能可以'矢量化'在k就好了。

擺脫iterate_list中的迭代將更加棘手,因爲每個階段的值取決於前一個值。有可能編寫f(x,k)以使用cumprod


下面是功能從我的行書:

def iterate_list(f, x0, k, n_iterations): 

    # initialise the list consisting of iterates of f 
    alist = [x0] 

    # initialise x 
    x = x0 

    # iterate using for loop 
    for n in xrange(n_iterations): 
     x = f(x,k) 
     alist.append(x) 

    # return the list consisting of iterates of f 
    return alist 

def lyapunov_exponent(f, df, x0, k, n_iterations): 

    # vectorise the list of iterates x0 to xn of f 
    vec = np.array(iterate_list(f, x0, k, n_iterations)) 

    # calculate the sum of all the values of df for x0 to xn 
    s = sum(np.log(abs(df(vec,k)))) 

    # calculate the mean as an estimate of the lyapunov exponent 
    return s/(n_iterations + 1) 
+0

非常感謝您的幫助!我理解用向量k運行並匹配x0的基本原理。然而,我試圖在python中運行你的上下文,它一直告訴我「iterate_list只需要3個參數(4給出)」,當我試圖取消參數k時,它說「lambda()只需要2個參數(1給定)」。如何修改我以前的函數iterate_list和lyapunov_exponent以消除此問題? – Febday

+0

將'f'和'df'的調用改爲包含'k';也爲你的函數添加'k'參數。 'f(x,k)' – hpaulj

+0

當我發佈我的答案時,我試圖記住我對你的函數做了什麼改變,想不出任何。但我忘記了我已經在多個層面上添加了'k'作爲參數。 – hpaulj