numpy gradient() behaves differently between versions

女生的网名这么多〃 提交于 2019-12-11 16:47:54

问题


I noticed this when searching around numpy.gradient() usages. It seems that since numpy 1.13, the function treats it input argument differently than previous versions. Here is a simple example:

import numpy as np

x=np.linspace(0,10,100)
y=np.sin(x)

dx=np.gradient(x)

grad1=np.gradient(y,dx)
print grad1

grad2=np.gradient(y,x)
print grad2

I tested in numpy 1.13 and 1.11.3.

In 1.13, gradient(array1, array2) treats array2 as coordinates (x) for array1. But in 1.11.3, it treats array2 as the differentiation of coordinates (dx). So grad1 works in 1.11.3, and grad2 works for 1.13.

I think this is a rather dangeous trap, people may get wrong results if the same code is run using different versions of numpy. Or am I missing something obvious that clears this ambiguity?

来源:https://stackoverflow.com/questions/46701906/numpy-gradient-behaves-differently-between-versions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!