3

我正在尝试加快我的代码,目前在 Python / Numpy 中运行需要一个多小时。大部分计算时间发生在下面粘贴的函数中。

我正在尝试对 Z 进行矢量化,但我发现三重 for 循环相当困难。我可以在numpy.diff某处实现该功能吗?看一看:

def MyFESolver(KK,D,r,Z):
    global tdim
    global xdim
    global q1
    global q2
    for k in range(1,tdim):
        for i in range(1,xdim-1):
            for j in range (1,xdim-1):
                Z[k,i,j]=Z[k-1,i,j]+r*q1*Z[k-1,i,j]*(KK-Z[k-1,i,j])+D*q2*(Z[k-1,i-1,j]-4*Z[k-1,i,j]+Z[k-1,i+1,j]+Z[k-1,i,j-1]+Z[k-1,i,j+1])
    return Z

tdim = 75 xdim = 25

4

2 回答 2

4

我同意,这很棘手,因为四个边上的 BC 破坏了刚度矩阵的简单结构。您可以像这样摆脱空间循环:

from pylab import *
from scipy.sparse.lil import lil_matrix
tdim = 3;     xdim = 4;  r = 1.0;  q1, q2 = .05, .05; KK= 1.0; D = .5  #random values
Z = ones((tdim, xdim, xdim))
#Iterate in time
for k in range(1,tdim):
    Z_prev = Z[k-1,:,:] #may need to flatten
    Z_up = Z_prev[1:-1,2:]
    Z_down = Z_prev[1:-1,:-2]

    Z_left = Z_prev[:-2,1:-1]
    Z_right = Z_prev[2:,1:-1]

    centre_term  = (q1*r*(Z_prev[1:-1,1:-1] + KK) - 4*D*q2)* Z_prev[1:-1,1:-1] 

    Z[k,1:-1,1:-1]= Z_prev[1:-1,1:-1]+ centre_term + q2*(Z_up+Z_left+Z_right+Z_down)

但我不认为你可以摆脱时间循环......

我认为表达式:

Z_up = Z_prev[1:-1,2:]

makes a copy in numpy, whereas what you want is a view - if you can figure out how to do this - it should be even faster (how much?)

Finally, I agree with the rest of the answerers - from experience, this kind of loops are better done in C and then wrapped into numpy. But the above should be faster than the original...

于 2012-10-25T12:17:22.750 回答
2

这看起来像是 Cython 的理想案例。我建议在 Cython 中编写该函数,它可能会快数百倍。

于 2012-10-25T11:07:59.450 回答