MCPcopy Index your code
hub / github.com/numpy/numpy / diff

Function diff

numpy/lib/_function_base_impl.py:1414–1542  ·  view source on GitHub ↗

Calculate the n-th discrete difference along the given axis. The first difference is given by ``out[i] = a[i+1] - a[i]`` along the given axis, higher differences are calculated by using `diff` recursively. Parameters ---------- a : array_like Input array n

(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue)

Source from the content-addressed store, hash-verified

1412
1413@array_function_dispatch(_diff_dispatcher)
1414def diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue):
1415 """
1416 Calculate the n-th discrete difference along the given axis.
1417
1418 The first difference is given by ``out[i] = a[i+1] - a[i]`` along
1419 the given axis, higher differences are calculated by using `diff`
1420 recursively.
1421
1422 Parameters
1423 ----------
1424 a : array_like
1425 Input array
1426 n : int, optional
1427 The number of times values are differenced. If zero, the input
1428 is returned as-is.
1429 axis : int, optional
1430 The axis along which the difference is taken, default is the
1431 last axis.
1432 prepend, append : array_like, optional
1433 Values to prepend or append to `a` along axis prior to
1434 performing the difference. Scalar values are expanded to
1435 arrays with length 1 in the direction of axis and the shape
1436 of the input array in along all other axes. Otherwise the
1437 dimension and shape must match `a` except along axis.
1438
1439 Returns
1440 -------
1441 diff : ndarray
1442 The n-th differences. The shape of the output is the same as `a`
1443 except along `axis` where the dimension is smaller by `n`. The
1444 type of the output is the same as the type of the difference
1445 between any two elements of `a`. This is the same as the type of
1446 `a` in most cases. A notable exception is `datetime64`, which
1447 results in a `timedelta64` output array.
1448
1449 See Also
1450 --------
1451 gradient, ediff1d, cumsum
1452
1453 Notes
1454 -----
1455 Type is preserved for boolean arrays, so the result will contain
1456 `False` when consecutive elements are the same and `True` when they
1457 differ.
1458
1459 For unsigned integer arrays, the results will also be unsigned. This
1460 should not be surprising, as the result is consistent with
1461 calculating the difference directly:
1462
1463 >>> u8_arr = np.array([1, 0], dtype=np.uint8)
1464 >>> np.diff(u8_arr)
1465 array([255], dtype=uint8)
1466 >>> u8_arr[1,...] - u8_arr[0,...]
1467 np.uint8(255)
1468
1469 If this is not desirable, then the array should be cast to a larger
1470 integer type first:
1471

Callers 14

fill_diagonalFunction · 0.90
diag_indices_fromFunction · 0.90
test_basicMethod · 0.90
test_axisMethod · 0.90
test_ndMethod · 0.90
test_nMethod · 0.90
test_timesMethod · 0.90
test_subclassMethod · 0.90
test_prependMethod · 0.90
test_appendMethod · 0.90
test_simpleMethod · 0.90
test_periodMethod · 0.90

Calls 2

asanyarrayFunction · 0.85
sliceFunction · 0.85

Tested by 10

test_basicMethod · 0.72
test_axisMethod · 0.72
test_ndMethod · 0.72
test_nMethod · 0.72
test_timesMethod · 0.72
test_subclassMethod · 0.72
test_prependMethod · 0.72
test_appendMethod · 0.72
test_simpleMethod · 0.72
test_periodMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…