Skip to main content
 首页 » 编程设计

python之代码优化三次插值

2025年05月04日1052php

我阅读 Stack 问题和答案已经有一段时间了,发现了很多非常有用的优化。 我在优化以下代码时遇到了瓶颈,这些代码“仅”用于将笛卡尔 map 转换为极坐标 map 。但是随着半径的增加角点的密度的特殊性意味着更大的半径用更多的点来描述。

有什么办法可以加快这个过程吗?

Rbin=440 
Angbin=440 
def cart2pol_alt(data,center,Rfact): 
 
""" 
Cubic interpolation 
Fastest implementation of the cubic interpolation so far 
Faster than max(0,(x+1)**3) by 20% or any of the factorization by bool (x+2>0) by 60-65% 
""" 
def cubic(x): 
    p0=lambda y: (y+2)**3 if (y+2>0) else 0 
    p1=lambda y: (y+1)**3 if (y+1>0) else 0 
    p2=lambda y: (y)**3 if (y>0) else 0 
    p3=lambda y: (y-1)**3 if (y-1>0) else 0 
    return (p0(x)-4*p1(x)+6*p2(x)-4*p3(x))/6. 
 
""" 
Adapt the  selected area size to polar basis size 
""" 
 
rad=Rfact*np.concatenate([r*np.ones(2*r+1) for r in np.arange(Rbin)]) 
theta=np.concatenate([np.pi*np.arange(t)/t for t in 2*np.arange(Rbin)+1]) 
x=rad*np.cos(theta) + center[1] 
y=-rad*np.sin(theta) + center[0] 
#Cubic interpolation 
ix=x.astype('int') 
iy=y.astype('int') 
dx=x-ix 
dy=y-iy 
polar=np.zeros_like(rad) 
 
nX,nY=data.shape 
for index in np.arange(Rbin*Angbin): 
    for i in np.arange(-1,3): 
        for j in np.arange(-1,3): 
            condx=np.logical_and((ix[index]+i)<nX,(ix[index]+i)>=0) 
            condy=np.logical_and((iy[index]+j)<nY,(iy[index]+j)>=0) 
            if np.logical_and(condx,condy): 
                cub=cubic(i-dx[index])*cubic(dy[index]-j) 
                polar[index]+=data.ravel()[(ix[index]+i)*nY+(iy[index]+j)]*cub     
 
return polar 

编辑。感谢你的帮助。矢量化并不容易,但我设法将执行时间从 4 分钟减少到 2 秒,但使用了以下代码。我希望它可以帮助一些人。

Rbin=440 
Angbin=440 
def cart2pol(data,scale,center,Rfact): 
 
""" 
    Cubic interpolation 
    Fastest implementation of the cubic interpolation so far for an array 
""" 
def cubic(y): 
    p0=(y+2)**3 
        p0[p0<0]=0 
        p1 = (y+1)**3 
        p1[p1<0]=0 
        p2 = y**3 
        p2[p2<0]=0 
        p3 =(y-1)**3 
        p3[p3<0]=0 
        return (p0-4.*p1+6.*p2-4.*p3)/6. 
 
rad=Rfact*np.concatenate([r*np.ones(2*r+1) for r in np.arange(Rbin)]) 
theta=np.concatenate([np.pi*np.arange(t)/t for t in 2*np.arange(Rbin)+1]) 
x=rad*np.cos(theta) + center[1] 
y=-rad*np.sin(theta) + center[0] 
#Cubic interpolation 
ix=x.astype('int') 
iy=y.astype('int') 
dx=x-ix 
dy=y-iy 
polar=np.zeros_like(rad) 
 
i,j=np.meshgrid(np.arange(-1,3),np.arange(-1,3)) 
i=i.ravel() 
j=j.ravel() 
I,IX=np.meshgrid(i,ix) 
J,IY=np.meshgrid(i,iy) 
IXI=IX+I 
IYJ=IY+J 
    nX,nY=data.shape 
rule1=(IXI<nX) & (IXI>=0) 
rule2=(IYJ<nY) & (IYJ>=0) 
cub=cubic(I-dx.reshape((dx.shape[0],1)))*cubic(dy.reshape((dy.shape[0],1))-J) 
dat=data.ravel()[(IXI*nY+IYJ).ravel()].reshape(IYJ.shape) 
polar+=(dat*cub).sum(axis=1) 
return polar 

如果有人看到其他优化,请告诉我!

请您参考如下方法:

您可以将 for 循环矢量化为如下内容:

nX,nY=data.shape 
for i in np.arange(-1,3): 
    for j in np.arange(-1,3): 
        condx = np.logical_and((ix + i) < nX, (ix + i) >=0) 
        condx = np.logical_and((iy + j) < nY, (iy + j) >=0) 
        cub = cubic(i-dx) * cubic(dy - i) 
        polar += data.ravel()[(ix + i) * nY + (iy + j)] * cub * \ 
                                     np.logical_and(condx, condy) 

您还可以从三次函数中删除 if 语句

def cubic(x): 
    p0 = (y+2)**3 
    p0[p0 < 0] = 0 
    p1 = (y+1)**3 
    p1[p1 < 0] = 0 
    p2 = (y)**3 
    p2[p2 < 0] = 0 
    p3 = (y-1)**3 
    p3[p3 < 0] = 0 
    return (p0 - 4*p1 + 6*p2 - 4*p3) /6.