[Numpy-discussion] Changed behavior of np.gradient

Derek Homeier derek at astro.physik.uni-goettingen.de
Sat Oct 4 16:14:18 EDT 2014


Hi Ariel,

> I think that the docstring in 1.9 is fine (has the 1.9 result). The docs online (for all of numpy) are still on version 1.8, though. 
> 
> I think that enabling the old behavior might be useful, if only so that I can write code that behaves consistently across these two versions of numpy. For now, I might just copy over the 1.8 code into my project. 
> 
Hmm, I got this with 1.9.0:

Examples
    --------
    >>> x = np.array([1, 2, 4, 7, 11, 16], dtype=np.float)
    >>> np.gradient(x)
    array([ 1. ,  1.5,  2.5,  3.5,  4.5,  5. ])
    >>> np.gradient(x, 2)
    array([ 0.5 ,  0.75,  1.25,  1.75,  2.25,  2.5 ])
    
    >>> np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float))
    [array([[ 2.,  2., -1.],
           [ 2.,  2., -1.]]),
    array([[ 1. ,  2.5,  4. ],
           [ 1. ,  1. ,  1. ]])]

In [5]: x =np.array([1, 2, 4, 7, 11, 16], dtype=np.float)

In [6]: print(np.gradient(x))
[ 0.5  1.5  2.5  3.5  4.5  5.5]

In [7]: print(np.gradient(x, 2))
[ 0.25  0.75  1.25  1.75  2.25  2.75]
…

I think there is a point for supporting the old behaviour besides backwards-compatibility or any sort of Matlab-compliance
as I’d probably like to be able to restrict a function to linear/1st order differences in cases where I know the input to be
not well-behaved.

+1 for an order=2 or maxorder=2 flag

Cheers,
						Derek




More information about the NumPy-Discussion mailing list