MCPcopy Create free account
hub / github.com/dask/dask / diff

Function diff

dask/array/routines.py:577–622  ·  view source on GitHub ↗
(a, n=1, axis=-1, prepend=None, append=None)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

asarrayFunction · 0.90
asarray_safeFunction · 0.90
meta_from_arrayFunction · 0.90
broadcast_toFunction · 0.90
concatenateFunction · 0.90

Tested by

no test coverage detected