(a, n=1, axis=-1, prepend=None, append=None)
| 573 | |
| 574 | @derived_from(np) |
| 575 | def diff(a, n=1, axis=-1, prepend=None, append=None): |
| 576 | a = asarray(a) |
| 577 | n = int(n) |
| 578 | axis = int(axis) |
| 579 | |
| 580 | if n == 0: |
| 581 | return a |
| 582 | if n < 0: |
| 583 | raise ValueError(f"order must be non-negative but got {n}") |
| 584 | |
| 585 | combined = [] |
| 586 | if prepend is not None: |
| 587 | prepend = asarray_safe(prepend, like=meta_from_array(a)) |
| 588 | if prepend.ndim == 0: |
| 589 | shape = list(a.shape) |
| 590 | shape[axis] = 1 |
| 591 | prepend = broadcast_to(prepend, tuple(shape)) |
| 592 | combined.append(prepend) |
| 593 | |
| 594 | combined.append(a) |
| 595 | |
| 596 | if append is not None: |
| 597 | append = asarray_safe(append, like=meta_from_array(a)) |
| 598 | if append.ndim == 0: |
| 599 | shape = list(a.shape) |
| 600 | shape[axis] = 1 |
| 601 | append = np.broadcast_to(append, tuple(shape)) |
| 602 | combined.append(append) |
| 603 | |
| 604 | if len(combined) > 1: |
| 605 | a = concatenate(combined, axis) |
| 606 | |
| 607 | sl_1 = a.ndim * [slice(None)] |
| 608 | sl_2 = a.ndim * [slice(None)] |
| 609 | |
| 610 | sl_1[axis] = slice(1, None) |
| 611 | sl_2[axis] = slice(None, -1) |
| 612 | |
| 613 | sl_1 = tuple(sl_1) |
| 614 | sl_2 = tuple(sl_2) |
| 615 | |
| 616 | r = a |
| 617 | for _ in range(n): |
| 618 | r = r[sl_1] - r[sl_2] |
| 619 | |
| 620 | return r |
| 621 | |
| 622 | |
| 623 | @derived_from(np) |
nothing calls this directly
no test coverage detected
searching dependent graphs…