Skip to content

Commit a22300e

Browse files
committed
Add python implementation of divmod
1 parent 8e10440 commit a22300e

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

dpnp/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@
319319
cumulative_prod,
320320
cumulative_sum,
321321
diff,
322+
divmod,
322323
divide,
323324
ediff1d,
324325
fabs,
@@ -767,6 +768,7 @@
767768
"deg2rad",
768769
"degrees",
769770
"diff",
771+
"divmod",
770772
"divide",
771773
"ediff1d",
772774
"exp",

dpnp/dpnp_iface_mathematical.py

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
DPNPAngle,
6666
DPNPBinaryFunc,
6767
DPNPBinaryFuncOutKw,
68+
DPNPBinaryTwoOutputsFunc,
6869
DPNPFix,
6970
DPNPImag,
7071
DPNPReal,
@@ -1564,6 +1565,100 @@ def diff(a, n=1, axis=-1, prepend=None, append=None):
15641565
)
15651566

15661567

1568+
_DIVMOD_DOCSTRING = r"""
1569+
Calculates the quotient and the remainder for each element :math:`x1_i` of the
1570+
input array `x1` with the respective element :math:`x2_i` of the input array
1571+
`x2`.
1572+
1573+
For full documentation refer to :obj:`numpy.divmod`.
1574+
1575+
Parameters
1576+
----------
1577+
x1 : {dpnp.ndarray, usm_ndarray}
1578+
Dividend input array, expected to have a real-valued floating-point data
1579+
type.
1580+
x2 : {dpnp.ndarray, usm_ndarray}
1581+
Divisor input array, expected to have a real-valued floating-point data
1582+
type.
1583+
out1 : {None, dpnp.ndarray, usm_ndarray}, optional
1584+
Output array for the quotient to populate. Array must have the same shape
1585+
as `x` and the expected data type.
1586+
1587+
Default: ``None``.
1588+
out2 : {None, dpnp.ndarray, usm_ndarray}, optional
1589+
Output array for the remainder to populate. Array must have the same shape
1590+
as `x` and the expected data type.
1591+
1592+
Default: ``None``.
1593+
out : tuple of None, dpnp.ndarray, or usm_ndarray, optional
1594+
A location into which the result is stored. If provided, it must be a tuple
1595+
and have length equal to the number of outputs. Each provided array must
1596+
have the same shape as `x` and the expected data type.
1597+
It is prohibited to pass output arrays through `out` keyword when either
1598+
`out1` or `out2` is passed.
1599+
1600+
Default: ``(None, None)``.
1601+
order : {None, "C", "F", "A", "K"}, optional
1602+
Memory layout of the newly output array, if parameter `out` is ``None``.
1603+
1604+
Default: ``"K"``.
1605+
1606+
Returns
1607+
-------
1608+
quotient : dpnp.ndarray
1609+
Element-wise quotient resulting from floor division.
1610+
remainder : dpnp.ndarray
1611+
Element-wise remainder from floor division.
1612+
1613+
Limitations
1614+
-----------
1615+
Parameters `where`, `dtype` and `subok` are supported with their default values.
1616+
Keyword argument `kwargs` is currently unsupported.
1617+
Otherwise ``NotImplementedError`` exception will be raised.
1618+
1619+
Notes
1620+
-----
1621+
At least one of `x1` or `x2` must be an array.
1622+
1623+
If ``x1.shape != x2.shape``, they must be broadcastable to a common shape
1624+
(which becomes the shape of the output).
1625+
1626+
Equivalent to :math:`(x1 // x2, x1 \% x2)`, but faster because it avoids
1627+
redundant work. It is used to implement the Python built-in function ``divmod``
1628+
on :class:`dpnp.ndarray`.
1629+
1630+
Complex dtypes are not supported, they will raise a ``TypeError``.
1631+
1632+
See Also
1633+
--------
1634+
:obj:`dpnp.floor_divide` : Equivalent to Python's :math:`//` operator.
1635+
:obj:`dpnp.remainder` : Equivalent to Python's :math:`\%` operator.
1636+
:obj:`dpnp.modf` : Equivalent to ``divmod(x, 1)`` for positive `x` with the
1637+
return values switched.
1638+
1639+
Examples
1640+
--------
1641+
>>> import dpnp as np
1642+
>>> np.divmod(np.arange(5), 3)
1643+
(array([0, 0, 0, 1, 1]), array([0, 1, 2, 0, 1]))
1644+
1645+
The Python built-in function ``divmod`` function can be used as a shorthand for
1646+
``np.divmod`` on :class:`dpnp.ndarray`.
1647+
1648+
>>> x = np.arange(5)
1649+
>>> divmod(x, 3)
1650+
(array([0, 0, 0, 1, 1]), array([0, 1, 2, 0, 1]))
1651+
1652+
"""
1653+
1654+
divmod = DPNPBinaryTwoOutputsFunc(
1655+
"divmod",
1656+
ufi._divmod_result_type,
1657+
ufi._divmod,
1658+
_DIVMOD_DOCSTRING,
1659+
)
1660+
1661+
15671662
def ediff1d(ary, to_end=None, to_begin=None):
15681663
"""
15691664
The differences between consecutive elements of an array.
@@ -1999,6 +2094,7 @@ def ediff1d(ary, to_end=None, to_begin=None):
19992094
See Also
20002095
--------
20012096
:obj:`dpnp.remainder` : Remainder complementary to floor_divide.
2097+
:obj:`dpnp.divmod` : Simultaneous floor division and remainder.
20022098
:obj:`dpnp.divide` : Standard division.
20032099
:obj:`dpnp.floor` : Round a number to the nearest integer toward minus infinity.
20042100
:obj:`dpnp.ceil` : Round a number to the nearest integer toward infinity.
@@ -2379,7 +2475,7 @@ def ediff1d(ary, to_end=None, to_begin=None):
23792475
"""
23802476

23812477
frexp = DPNPUnaryTwoOutputsFunc(
2382-
"_frexp",
2478+
"frexp",
23832479
ufi._frexp_result_type,
23842480
ufi._frexp,
23852481
_FREXP_DOCSTRING,
@@ -3141,7 +3237,7 @@ def interp(x, xp, fp, left=None, right=None, period=None):
31413237
"""
31423238

31433239
ldexp = DPNPBinaryFunc(
3144-
"_ldexp",
3240+
"ldexp",
31453241
ufi._ldexp_result_type,
31463242
ufi._ldexp,
31473243
_LDEXP_DOCSTRING,
@@ -3421,7 +3517,7 @@ def interp(x, xp, fp, left=None, right=None, period=None):
34213517
"""
34223518

34233519
modf = DPNPUnaryTwoOutputsFunc(
3424-
"_modf",
3520+
"modf",
34253521
ufi._modf_result_type,
34263522
ufi._modf,
34273523
_MODF_DOCSTRING,
@@ -4278,6 +4374,7 @@ def real_if_close(a, tol=100):
42784374
See Also
42794375
--------
42804376
:obj:`dpnp.fmod` : Calculate the element-wise remainder of division.
4377+
:obj:`dpnp.divmod` : Simultaneous floor division and remainder.
42814378
:obj:`dpnp.divide` : Standard division.
42824379
:obj:`dpnp.floor` : Round a number to the nearest integer toward minus infinity.
42834380
:obj:`dpnp.floor_divide` : Compute the largest integer smaller or equal to the

0 commit comments

Comments
 (0)