0

嗨,我有以下代码:

import numpy as np
from scipy import pi,sin,cos
import matplotlib.pyplot as plt
import math as m

image = np.zeros(shape=(201,201))

a=4
b=3
pa = 45

def dE(i,j,a,b,PA,x0,y0):
    the = -PA+90.0
    x=i - x0
    y=j - y0
    cos_a,sin_a=cos(the*pi/180.0),sin(the*pi/180.0)
    Xs = (x*cos_a - y*sin_a)**2.0 
    Ys = (x*sin_a + y*cos_a)**2.0 
    r = np.sqrt((Xs/a**2) + (Ys/b**2)) 
    return r

for i in range(0,len(image)):
    for j in range(0,len(image)):
        image[j,i] = dE(i,j,a,b,pa,100.0,100.0)

plt.imshow(image)
plt.show()

如何并行化底部的两个 for 循环?提前致谢!

我已经添加了上面的完整代码。干杯克里斯

4

1 回答 1

0

好的,谢谢您的意见,这真的很有帮助!

import numpy as np
from scipy import pi,sin,cos
import matplotlib.pyplot as plt
import math as m

image = np.zeros(shape=(201,201))

a=4
b=3
pa = 240

def dEllipse(x,y,a,b,PA):
    the = -PA+90.0
    cos_a,sin_a=cos(the*pi/180.0),sin(the*pi/180.0)
    Xs = (x*cos_a - y*sin_a)**2.0 
    Ys = (x*sin_a + y*cos_a)**2.0 
    r = np.sqrt((Xs/a**2) + (Ys/b**2)) 
    return r

x = np.arange(0-100.0, 201-100.0)
y = np.arange(0-100.0, 201-100.0)
xx, yy = np.meshgrid(x, y)

new_image= dEllipse(xx,yy,a,b,pa)

plt.imshow(new_image)
plt.show()
于 2013-03-16T20:08:40.097 回答