Skip to content

Commit cac6399

Browse files
committed
Add python implementation of divmod
1 parent b99ff9c commit cac6399

File tree

1 file changed

+101
-3
lines changed

1 file changed

+101
-3
lines changed

dpnp/dpnp_iface_mathematical.py

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
DPNPAngle,
6767
DPNPBinaryFunc,
6868
DPNPBinaryFuncOutKw,
69+
DPNPBinaryTwoOutputsFunc,
6970
DPNPFix,
7071
DPNPImag,
7172
DPNPReal,
@@ -102,6 +103,7 @@
102103
"cumulative_prod",
103104
"cumulative_sum",
104105
"diff",
106+
"divmod",
105107
"divide",
106108
"ediff1d",
107109
"fabs",
@@ -1630,6 +1632,100 @@ def diff(a, n=1, axis=-1, prepend=None, append=None):
16301632
)
16311633

16321634

1635+
_DIVMOD_DOCSTRING = r"""
1636+
Calculates the quotient and the remainder for each element :math:`x1_i` of the
1637+
input array `x1` with the respective element :math:`x2_i` of the input array
1638+
`x2`.
1639+
1640+
For full documentation refer to :obj:`numpy.divmod`.
1641+
1642+
Parameters
1643+
----------
1644+
x1 : {dpnp.ndarray, usm_ndarray}
1645+
Dividend input array, expected to have a real-valued floating-point data
1646+
type.
1647+
x2 : {dpnp.ndarray, usm_ndarray}
1648+
Divisor input array, expected to have a real-valued floating-point data
1649+
type.
1650+
out1 : {None, dpnp.ndarray, usm_ndarray}, optional
1651+
Output array for the quotient to populate. Array must have the same shape
1652+
as `x` and the expected data type.
1653+
1654+
Default: ``None``.
1655+
out2 : {None, dpnp.ndarray, usm_ndarray}, optional
1656+
Output array for the remainder to populate. Array must have the same shape
1657+
as `x` and the expected data type.
1658+
1659+
Default: ``None``.
1660+
out : tuple of None, dpnp.ndarray, or usm_ndarray, optional
1661+
A location into which the result is stored. If provided, it must be a tuple
1662+
and have length equal to the number of outputs. Each provided array must
1663+
have the same shape as `x` and the expected data type.
1664+
It is prohibited to pass output arrays through `out` keyword when either
1665+
`out1` or `out2` is passed.
1666+
1667+
Default: ``(None, None)``.
1668+
order : {None, "C", "F", "A", "K"}, optional
1669+
Memory layout of the newly output array, if parameter `out` is ``None``.
1670+
1671+
Default: ``"K"``.
1672+
1673+
Returns
1674+
-------
1675+
quotient : dpnp.ndarray
1676+
Element-wise quotient resulting from floor division.
1677+
remainder : dpnp.ndarray
1678+
Element-wise remainder from floor division.
1679+
1680+
Limitations
1681+
-----------
1682+
Parameters `where`, `dtype` and `subok` are supported with their default values.
1683+
Keyword argument `kwargs` is currently unsupported.
1684+
Otherwise ``NotImplementedError`` exception will be raised.
1685+
1686+
Notes
1687+
-----
1688+
At least one of `x1` or `x2` must be an array.
1689+
1690+
If ``x1.shape != x2.shape``, they must be broadcastable to a common shape
1691+
(which becomes the shape of the output).
1692+
1693+
Equivalent to :math:`(x1 // x2, x1 \% x2)`, but faster because it avoids
1694+
redundant work. It is used to implement the Python built-in function ``divmod``
1695+
on :class:`dpnp.ndarray`.
1696+
1697+
Complex dtypes are not supported, they will raise a ``TypeError``.
1698+
1699+
See Also
1700+
--------
1701+
:obj:`dpnp.floor_divide` : Equivalent to Python's :math:`//` operator.
1702+
:obj:`dpnp.remainder` : Equivalent to Python's :math:`\%` operator.
1703+
:obj:`dpnp.modf` : Equivalent to ``divmod(x, 1)`` for positive `x` with the
1704+
return values switched.
1705+
1706+
Examples
1707+
--------
1708+
>>> import dpnp as np
1709+
>>> np.divmod(np.arange(5), 3)
1710+
(array([0, 0, 0, 1, 1]), array([0, 1, 2, 0, 1]))
1711+
1712+
The Python built-in function ``divmod`` function can be used as a shorthand for
1713+
``np.divmod`` on :class:`dpnp.ndarray`.
1714+
1715+
>>> x = np.arange(5)
1716+
>>> divmod(x, 3)
1717+
(array([0, 0, 0, 1, 1]), array([0, 1, 2, 0, 1]))
1718+
1719+
"""
1720+
1721+
divmod = DPNPBinaryTwoOutputsFunc(
1722+
"divmod",
1723+
ufi._divmod_result_type,
1724+
ufi._divmod,
1725+
_DIVMOD_DOCSTRING,
1726+
)
1727+
1728+
16331729
def ediff1d(ary, to_end=None, to_begin=None):
16341730
"""
16351731
The differences between consecutive elements of an array.
@@ -2065,6 +2161,7 @@ def ediff1d(ary, to_end=None, to_begin=None):
20652161
See Also
20662162
--------
20672163
:obj:`dpnp.remainder` : Remainder complementary to floor_divide.
2164+
:obj:`dpnp.divmod` : Simultaneous floor division and remainder.
20682165
:obj:`dpnp.divide` : Standard division.
20692166
:obj:`dpnp.floor` : Round a number to the nearest integer toward minus infinity.
20702167
:obj:`dpnp.ceil` : Round a number to the nearest integer toward infinity.
@@ -2445,7 +2542,7 @@ def ediff1d(ary, to_end=None, to_begin=None):
24452542
"""
24462543

24472544
frexp = DPNPUnaryTwoOutputsFunc(
2448-
"_frexp",
2545+
"frexp",
24492546
ufi._frexp_result_type,
24502547
ufi._frexp,
24512548
_FREXP_DOCSTRING,
@@ -3207,7 +3304,7 @@ def interp(x, xp, fp, left=None, right=None, period=None):
32073304
"""
32083305

32093306
ldexp = DPNPBinaryFunc(
3210-
"_ldexp",
3307+
"ldexp",
32113308
ufi._ldexp_result_type,
32123309
ufi._ldexp,
32133310
_LDEXP_DOCSTRING,
@@ -3487,7 +3584,7 @@ def interp(x, xp, fp, left=None, right=None, period=None):
34873584
"""
34883585

34893586
modf = DPNPUnaryTwoOutputsFunc(
3490-
"_modf",
3587+
"modf",
34913588
ufi._modf_result_type,
34923589
ufi._modf,
34933590
_MODF_DOCSTRING,
@@ -4344,6 +4441,7 @@ def real_if_close(a, tol=100):
43444441
See Also
43454442
--------
43464443
:obj:`dpnp.fmod` : Calculate the element-wise remainder of division.
4444+
:obj:`dpnp.divmod` : Simultaneous floor division and remainder.
43474445
:obj:`dpnp.divide` : Standard division.
43484446
:obj:`dpnp.floor` : Round a number to the nearest integer toward minus infinity.
43494447
:obj:`dpnp.floor_divide` : Compute the largest integer smaller or equal to the

0 commit comments

Comments
 (0)