[Numpy-discussion] Changed behavior of np.gradient

Ariel Rokem arokem at gmail.com
Sat Oct 4 15:53:26 EDT 2014


On Sat, Oct 4, 2014 at 12:29 PM, Derek Homeier <
derek at astro.physik.uni-goettingen.de> wrote:

> On 4 Oct 2014, at 08:37 pm, Ariel Rokem <arokem at gmail.com> wrote:
>
> > >>> import numpy as np
> > >>> np.__version__
> > '1.9.0'
> > >>> np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float))
> > [array([[ 2.,  2., -1.],
> >        [ 2.,  2., -1.]]), array([[-0.5,  2.5,  5.5],
> >        [ 1. ,  1. ,  1. ]])]
> >
> > On the other hand:
> > >>> import numpy as np
> > >>> np.__version__
> > '1.8.2'
> > >>> 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. ]])]
> >
> > For what it's worth, the 1.8 version of this function seems to be in
> agreement with the Matlab equivalent function ('gradient'):
> > >> gradient([[1, 2, 6]; [3, 4, 5]])
> > ans =
> >     1.0000    2.5000    4.0000
> >     1.0000    1.0000    1.0000
> >
> > This seems like a regression to me, but maybe it's an improvement?
> >
> Technically yes, the function has been changed to use 2nd-order
> differences where possible,
> as is described in the docstring. Someone missed to update the example
> though, which still
> quotes the 1.8 results.
> And if the loss of Matlab-compliance is seen as a disadvantage, maybe
> there is a case for
> re-enabling the old behaviour via keyword argument?
>
>

Thanks for clarifying - I see that now in the docstring as well. It went
from: "The gradient is computed using central differences in the interior
and first differences at the boundaries." to "The gradient is computed
using second order accurate central differences in the interior and second
order accurate one-sides (forward or backwards) differences at the
boundaries.".

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.

Cheers,

Ariel




> Cheers,
>                                         Derek
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141004/010c4cab/attachment.html>


More information about the NumPy-Discussion mailing list