41
41
import dpctl .tensor as dpt
42
42
import dpctl .utils as du
43
43
import numpy
44
- from numpy .core .numeric import normalize_axis_tuple
44
+ from numpy .core .numeric import (
45
+ normalize_axis_index ,
46
+ normalize_axis_tuple ,
47
+ )
45
48
46
49
import dpnp
47
50
from dpnp .dpnp_array import dpnp_array
123
126
]
124
127
125
128
129
+ def _append_to_diff_array (a , axis , combined , values ):
130
+ """
131
+ Append `values` to `combined` list based on data of array `a`.
132
+
133
+ Scalar value (including case with 0d array) is expanded to an array
134
+ with length=1 in the direction of axis and the shape of the input array `a`
135
+ in along all other axes.
136
+ Note, if `values` is a scalar. then it is converted to 0d array allocating
137
+ on the same SYCL queue as the input array `a` and with the same USM type.
138
+
139
+ """
140
+
141
+ dpnp .check_supported_arrays_type (values , scalar_type = True )
142
+ if dpnp .isscalar (values ):
143
+ values = dpnp .asarray (
144
+ values , sycl_queue = a .sycl_queue , usm_type = a .usm_type
145
+ )
146
+
147
+ if values .ndim == 0 :
148
+ shape = list (a .shape )
149
+ shape [axis ] = 1
150
+ values = dpnp .broadcast_to (values , tuple (shape ))
151
+ combined .append (values )
152
+
153
+
126
154
def absolute (
127
155
x ,
128
156
/ ,
@@ -603,6 +631,10 @@ def cumsum(x1, **kwargs):
603
631
Otherwise the function will be executed sequentially on CPU.
604
632
Input array data types are limited by supported DPNP :ref:`Data types`.
605
633
634
+ See Also
635
+ --------
636
+ :obj:`dpnp.diff` : Calculate the n-th discrete difference along the given axis.
637
+
606
638
Examples
607
639
--------
608
640
>>> import dpnp as np
@@ -624,39 +656,95 @@ def cumsum(x1, **kwargs):
624
656
return call_origin (numpy .cumsum , x1 , ** kwargs )
625
657
626
658
627
- def diff (x1 , n = 1 , axis = - 1 , prepend = numpy . _NoValue , append = numpy . _NoValue ):
659
+ def diff (a , n = 1 , axis = - 1 , prepend = None , append = None ):
628
660
"""
629
661
Calculate the n-th discrete difference along the given axis.
630
662
631
663
For full documentation refer to :obj:`numpy.diff`.
632
664
633
- Limitations
634
- -----------
635
- Input array is supported as :obj:`dpnp.ndarray`.
636
- Parameters `axis`, `prepend` and `append` are supported only with default values.
637
- Otherwise the function will be executed sequentially on CPU.
665
+ Parameters
666
+ ----------
667
+ a : {dpnp.ndarray, usm_ndarray}
668
+ Input array
669
+ n : int, optional
670
+ The number of times values are differenced. If zero, the input
671
+ is returned as-is.
672
+ axis : int, optional
673
+ The axis along which the difference is taken, default is the
674
+ last axis.
675
+ prepend, append : {scalar, dpnp.ndarray, usm_ndarray}, optional
676
+ Values to prepend or append to `a` along axis prior to
677
+ performing the difference. Scalar values are expanded to
678
+ arrays with length 1 in the direction of axis and the shape
679
+ of the input array in along all other axes. Otherwise the
680
+ dimension and shape must match `a` except along axis.
681
+
682
+ Returns
683
+ -------
684
+ out : dpnp.ndarray
685
+ The n-th differences. The shape of the output is the same as `a`
686
+ except along `axis` where the dimension is smaller by `n`. The
687
+ type of the output is the same as the type of the difference
688
+ between any two elements of `a`. This is the same as the type of
689
+ `a` in most cases.
690
+
691
+ See Also
692
+ --------
693
+ :obj:`dpnp.gradient` : Return the gradient of an N-dimensional array.
694
+ :obj:`dpnp.ediff1d` : Compute the differences between consecutive elements of an array.
695
+ :obj:`dpnp.cumsum` : Return the cumulative sum of the elements along a given axis.
696
+
697
+ Examples
698
+ --------
699
+ >>> import dpnp as np
700
+ >>> x = np.array([1, 2, 4, 7, 0])
701
+ >>> np.diff(x)
702
+ array([ 1, 2, 3, -7])
703
+ >>> np.diff(x, n=2)
704
+ array([ 1, 1, -10])
705
+
706
+ >>> x = np.array([[1, 3, 6, 10], [0, 5, 6, 8]])
707
+ >>> np.diff(x)
708
+ array([[2, 3, 4],
709
+ [5, 1, 2]])
710
+ >>> np.diff(x, axis=0)
711
+ array([[-1, 2, 0, -2]])
712
+
638
713
"""
639
714
640
- x1_desc = dpnp .get_dpnp_descriptor (x1 , copy_when_nondefault_queue = False )
641
- if x1_desc :
642
- if not isinstance (n , int ):
643
- pass
644
- elif n < 1 :
645
- pass
646
- elif x1_desc .ndim != 1 :
647
- pass
648
- elif axis != - 1 :
649
- pass
650
- elif prepend is not numpy ._NoValue :
651
- pass
652
- elif append is not numpy ._NoValue :
653
- pass
654
- else :
655
- return dpnp_diff (x1_desc , n ).get_pyobj ()
715
+ dpnp .check_supported_arrays_type (a )
716
+ if n == 0 :
717
+ return a
718
+ if n < 0 :
719
+ raise ValueError (f"order must be non-negative but got { n } " )
656
720
657
- return call_origin (
658
- numpy .diff , x1 , n = n , axis = axis , prepend = prepend , append = append
659
- )
721
+ nd = a .ndim
722
+ if nd == 0 :
723
+ raise ValueError ("diff requires input that is at least one dimensional" )
724
+ axis = normalize_axis_index (axis , nd )
725
+
726
+ combined = []
727
+ if prepend is not None :
728
+ _append_to_diff_array (a , axis , combined , prepend )
729
+
730
+ combined .append (a )
731
+ if append is not None :
732
+ _append_to_diff_array (a , axis , combined , append )
733
+
734
+ if len (combined ) > 1 :
735
+ a = dpnp .concatenate (combined , axis = axis )
736
+
737
+ slice1 = [slice (None )] * nd
738
+ slice2 = [slice (None )] * nd
739
+ slice1 [axis ] = slice (1 , None )
740
+ slice2 [axis ] = slice (None , - 1 )
741
+ slice1 = tuple (slice1 )
742
+ slice2 = tuple (slice2 )
743
+
744
+ op = dpnp .not_equal if a .dtype == numpy .bool_ else dpnp .subtract
745
+ for _ in range (n ):
746
+ a = op (a [slice1 ], a [slice2 ])
747
+ return a
660
748
661
749
662
750
def divide (
@@ -1270,6 +1358,10 @@ def gradient(x1, *varargs, **kwargs):
1270
1358
Otherwise the function will be executed sequentially on CPU.
1271
1359
Input array data types are limited by supported DPNP :ref:`Data types`.
1272
1360
1361
+ See Also
1362
+ --------
1363
+ :obj:`dpnp.diff` : Calculate the n-th discrete difference along the given axis.
1364
+
1273
1365
Examples
1274
1366
--------
1275
1367
>>> import dpnp as np
@@ -1379,11 +1471,11 @@ def maximum(
1379
1471
--------
1380
1472
:obj:`dpnp.minimum` : Element-wise minimum of two arrays, propagates NaNs.
1381
1473
:obj:`dpnp.fmax` : Element-wise maximum of two arrays, ignores NaNs.
1382
- :obj:`dpnp.amax` : The maximum value of an array along a given axis, propagates NaNs.
1383
- :obj:`dpnp.nanmax` : The maximum value of an array along a given axis, ignores NaNs.
1384
- :obj:`dpnp.fmax` : Element-wise maximum of two arrays, ignores NaNs.
1385
- :obj:`dpnp.amax` : The maximum value of an array along a given axis, propagates NaNs.
1474
+ :obj:`dpnp.max` : The maximum value of an array along a given axis, propagates NaNs.
1386
1475
:obj:`dpnp.nanmax` : The maximum value of an array along a given axis, ignores NaNs.
1476
+ :obj:`dpnp.fmin` : Element-wise minimum of two arrays, ignores NaNs.
1477
+ :obj:`dpnp.min` : The minimum value of an array along a given axis, propagates NaNs.
1478
+ :obj:`dpnp.nanmin` : The minimum value of an array along a given axis, ignores NaNs.
1387
1479
1388
1480
Examples
1389
1481
--------
@@ -1458,11 +1550,11 @@ def minimum(
1458
1550
--------
1459
1551
:obj:`dpnp.maximum` : Element-wise maximum of two arrays, propagates NaNs.
1460
1552
:obj:`dpnp.fmin` : Element-wise minimum of two arrays, ignores NaNs.
1461
- :obj:`dpnp.amin` : The minimum value of an array along a given axis, propagates NaNs.
1462
- :obj:`dpnp.nanmin` : The minimum value of an array along a given axis, ignores NaNs.
1463
- :obj:`dpnp.fmin` : Element-wise minimum of two arrays, ignores NaNs.
1464
- :obj:`dpnp.amin` : The minimum value of an array along a given axis, propagates NaNs.
1553
+ :obj:`dpnp.min` : The minimum value of an array along a given axis, propagates NaNs.
1465
1554
:obj:`dpnp.nanmin` : The minimum value of an array along a given axis, ignores NaNs.
1555
+ :obj:`dpnp.fmax` : Element-wise maximum of two arrays, ignores NaNs.
1556
+ :obj:`dpnp.max` : The maximum value of an array along a given axis, propagates NaNs.
1557
+ :obj:`dpnp.nanmax` : The maximum value of an array along a given axis, ignores NaNs.
1466
1558
1467
1559
Examples
1468
1560
--------
0 commit comments