我在使用 scipy.weave.inline 时遇到问题。我想编写一个以 lcenter 为中心的 unitstep 函数,其宽度为 nm。我有两个版本: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()