我在使用scipy.weave.inline. 我想编程一个以lcenter为中心的unitstep函数,它的宽度为。我有两个版本:python版本,称为pm和优化函数pm_weave,但看起来abs工作不正常。请参阅下面的代码。如果你运行它,你会得到一个大小为1的窗口,不管输入是什么,所以看起来abs不起作用。例如,如果你去掉腹肌,它的工作原理和你所期望的完全一样
我该怎么解决这个问题?在
def pm_weave(w,lcenter,width_nm):
""" Return a unitstep function that is centered around lcenter with height 1.0 and width width_nm """
lcenter = float(lcenter)
w = float(w)
width_nm = float(width_nm)
code = """
#include <math.h>
float wl = 1.88495559215387594307758602997E3/w;
if(abs(lcenter-wl) < width_nm) {
return_val = 1.0;
}
else {
return_val = 0.0;
}
"""
res = weave.inline(code,['w','lcenter','width_nm'],
type_converters = weave.converters.blitz,
compiler = "gcc", headers=["<math.h>"]
)
return res
def pm(w,lcenter,width_nm):
"""
Return a unitstep function centered around lcenter [nm] with width width_nm. w
should be a radial frequency.
"""
return abs(600*np.pi/w - lcenter) < width_nm/2. and 1. or 0.
plot(wavelength_list,map(lambda w:pm(toRadialFrequency(w),778,1),wavelength_list),label="Desired behaviour")
plot(wavelength_list,map(lambda w:pm_weave(toRadialFrequency(w),778,1),wavelength_list),'^',label="weave.inline behaviour")
ylim(0,1.5)
show()
我认为您可能需要在C代码中使用}。}将用于浮点运算。在
fabs()
,而不是{abs()
将截断结果,而{相关问题 更多 >
编程相关推荐