问题描述
我想检查来自laplace
的{{1}}过滤器的计算,并将其与自己的方法(如果有区别)进行比较。下面有一段我运行的代码
scipy.ndimage
我希望变量import scipy.ndimage.filters
n = 100
x_range = y_range = np.linspace(-1,1,n)
X,Y = np.meshgrid(x_range,y_range)
f_fun = X ** 2 + Y ** 2
f_fun_laplace = 4 * np.ones(f_fun.shape)
res_laplace = scipy.ndimage.filters.laplace(f_fun,mode='constant')
在整个域中具有不变的值res_laplace
(为简单起见,不包括边界),因为这是通过将laplace运算符应用于我的函数得到的函数4
。
但是,在这种情况下,f(x,y) = x^2 + y^2
产生的值为res_laplace
。所以我的问题是..为什么不等于4?
解决方法
在这种特定情况下,答案是您需要将scipy.ndimage.filters.laplace
的输出乘以(1/delta_x) ** 2
的系数。 delta_x = np.diff(x_range)[0]
我只是假设过滤器会解决这个问题,但是事后看来,它当然不知道值delta_x
。
而且由于我们要进行两次微分,所以我们需要将此delta_x
的倒数平方。