Friday 15 March 2013

python - numpy.gradient() seems to produce erroneous boundary values (using first differences) -



python - numpy.gradient() seems to produce erroneous boundary values (using first differences) -

there seems problem function numpy.gradient() (numpy 1.9.0) regarding how computes boundary (start , end) values (which know using first differences, while central values computed using central differences). consider instance next example:

import numpy np arr = np.array([1, 2, 4]) arrgrad = np.gradient(arr)

i here expect arrgrad obtain values

[ 1. 1.5 2. ]

i.e.

arrgrad[0] = (2-1)/1 = 1 arrgrad[1] = (4-1)/2 = 1.5 arrgrad[2] = (4-2)/1 = 2

but result

[ 0.5 1.5 2.5]

the behaviour of numpy.gradient() version 1.8.1 (obtained https://github.com/numpy/numpy/blob/v1.8.1/numpy/lib/function_base.py) seems produce right result, however.

is erroneous behaviour described above result of bug? (i'm using python 3.4.2, 64 bit.)

apparently way gradients calculated changed between 1.8.1 , 1.9.0 332d628, boundary elements calculated using second-order accurate approximation well, whereas first-order accurate.

however, documentation on numpy website not include 1.9.0 docs, 1.8.1, see proper documentation can utilize np.source(np.gradient) or print(np.gradient.__doc__).

python numpy

No comments:

Post a Comment