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