MCPcopy Create free account
hub / github.com/numpy/numpy / ediff1d

Function ediff1d

numpy/lib/arraysetops.py:38–122  ·  view source on GitHub ↗

The differences between consecutive elements of an array. Parameters ---------- ary : array_like If necessary, will be flattened before the differences are taken. to_end : array_like, optional Number(s) to append at the end of the returned differences. to_be

(ary, to_end=None, to_begin=None)

Source from the content-addressed store, hash-verified

36
37@array_function_dispatch(_ediff1d_dispatcher)
38def ediff1d(ary, to_end=None, to_begin=None):
39 """
40 The differences between consecutive elements of an array.
41
42 Parameters
43 ----------
44 ary : array_like
45 If necessary, will be flattened before the differences are taken.
46 to_end : array_like, optional
47 Number(s) to append at the end of the returned differences.
48 to_begin : array_like, optional
49 Number(s) to prepend at the beginning of the returned differences.
50
51 Returns
52 -------
53 ediff1d : ndarray
54 The differences. Loosely, this is ``ary.flat[1:] - ary.flat[:-1]``.
55
56 See Also
57 --------
58 diff, gradient
59
60 Notes
61 -----
62 When applied to masked arrays, this function drops the mask information
63 if the `to_begin` and/or `to_end` parameters are used.
64
65 Examples
66 --------
67 >>> x = np.array([1, 2, 4, 7, 0])
68 >>> np.ediff1d(x)
69 array([ 1, 2, 3, -7])
70
71 >>> np.ediff1d(x, to_begin=-99, to_end=np.array([88, 99]))
72 array([-99, 1, 2, ..., -7, 88, 99])
73
74 The returned array is always 1D.
75
76 >>> y = [[1, 2, 4], [1, 6, 24]]
77 >>> np.ediff1d(y)
78 array([ 1, 2, -3, 5, 18])
79
80 """
81 # force a 1d array
82 ary = np.asanyarray(ary).ravel()
83
84 # enforce that the dtype of `ary` is used for the output
85 dtype_req = ary.dtype
86
87 # fast track default case
88 if to_begin is None and to_end is None:
89 return ary[1:] - ary[:-1]
90
91 if to_begin is None:
92 l_begin = 0
93 else:
94 to_begin = np.asanyarray(to_begin)
95 if not np.can_cast(to_begin, dtype_req, casting="same_kind"):

Callers 2

test_ediff1dMethod · 0.90

Calls 3

maxFunction · 0.50
ravelMethod · 0.45
__array_wrap__Method · 0.45

Tested by 2

test_ediff1dMethod · 0.72