Skip to content

BUG: distance arg of np.gradient must be scalar, fix docstring #7618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,9 +1410,10 @@ def gradient(f, *varargs, **kwargs):

Returns
-------
gradient : list of ndarray
Each element of `list` has the same shape as `f` giving the derivative
of `f` with respect to each dimension.
gradient : ndarray or list of ndarray
A set of ndarrays (or a single ndarray if there is only one dimension)
correposnding to the derivatives of f with respect to each dimension.
Each derivative has the same shape as f.

Examples
--------
Expand All @@ -1432,9 +1433,8 @@ def gradient(f, *varargs, **kwargs):
[ 1. , 1. , 1. ]])]

>>> x = np.array([0, 1, 2, 3, 4])
>>> dx = np.gradient(x)
>>> y = x**2
>>> np.gradient(y, dx, edge_order=2)
>>> np.gradient(y, edge_order=2)
array([-0., 2., 4., 6., 8.])

The axis keyword can be used to specify a subset of axes of which the gradient is calculated
Expand Down Expand Up @@ -1472,6 +1472,8 @@ def gradient(f, *varargs, **kwargs):
else:
raise SyntaxError(
"invalid number of arguments")
if any([not np.isscalar(dxi) for dxi in dx]):
raise ValueError("distances must be scalars")

edge_order = kwargs.pop('edge_order', 1)
if kwargs:
Expand Down
3 changes: 3 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ def test_badargs(self):
assert_raises(SyntaxError, gradient, x, np.array([1., 1.]),
np.array([1., 1.]), np.array([1., 1.]))

# disallow arrays as distances, see gh-6847
assert_raises(ValueError, gradient, np.arange(5), np.ones(5))

def test_masked(self):
# Make sure that gradient supports subclasses like masked arrays
x = np.ma.array([[1, 1], [3, 4]],
Expand Down