scipy.weave.inline在使用数学库时未按预期工作

2024-09-27 18:07:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用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()

Tags: 函数版本returndefinlineabsfloatwidth

热门问题