Skip to content

Commit 39fe5d7

Browse files
committed
implement dpnp.reciprocal and dpnp.angle
1 parent 20513fb commit 39fe5d7

23 files changed

+390
-105
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ enum class DPNPFuncName : size_t
237237
parameters */
238238
DPNP_FN_REMAINDER, /**< Used in numpy.remainder() impl */
239239
DPNP_FN_RECIP, /**< Used in numpy.recip() impl */
240-
DPNP_FN_RECIP_EXT, /**< Used in numpy.recip() impl, requires extra
241-
parameters */
242240
DPNP_FN_REPEAT, /**< Used in numpy.repeat() impl */
243241
DPNP_FN_RIGHT_SHIFT, /**< Used in numpy.right_shift() impl */
244242
DPNP_FN_RNG_BETA, /**< Used in numpy.random.beta() impl */

dpnp/backend/kernels/dpnp_krnl_elemwise.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -928,15 +928,6 @@ static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
928928
fmap[DPNPFuncName::DPNP_FN_RECIP][eft_DBL][eft_DBL] = {
929929
eft_DBL, (void *)dpnp_recip_c_default<double>};
930930

931-
fmap[DPNPFuncName::DPNP_FN_RECIP_EXT][eft_INT][eft_INT] = {
932-
eft_INT, (void *)dpnp_recip_c_ext<int32_t>};
933-
fmap[DPNPFuncName::DPNP_FN_RECIP_EXT][eft_LNG][eft_LNG] = {
934-
eft_LNG, (void *)dpnp_recip_c_ext<int64_t>};
935-
fmap[DPNPFuncName::DPNP_FN_RECIP_EXT][eft_FLT][eft_FLT] = {
936-
eft_FLT, (void *)dpnp_recip_c_ext<float>};
937-
fmap[DPNPFuncName::DPNP_FN_RECIP_EXT][eft_DBL][eft_DBL] = {
938-
eft_DBL, (void *)dpnp_recip_c_ext<double>};
939-
940931
fmap[DPNPFuncName::DPNP_FN_SIGN][eft_INT][eft_INT] = {
941932
eft_INT, (void *)dpnp_sign_c_default<int32_t>};
942933
fmap[DPNPFuncName::DPNP_FN_SIGN][eft_LNG][eft_LNG] = {

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
110110
DPNP_FN_QR_EXT
111111
DPNP_FN_RADIANS
112112
DPNP_FN_RADIANS_EXT
113-
DPNP_FN_RECIP
114-
DPNP_FN_RECIP_EXT
115113
DPNP_FN_RNG_BETA
116114
DPNP_FN_RNG_BETA_EXT
117115
DPNP_FN_RNG_BINOMIAL
@@ -342,4 +340,3 @@ Trigonometric functions
342340
"""
343341
cpdef dpnp_descriptor dpnp_degrees(dpnp_descriptor array1)
344342
cpdef dpnp_descriptor dpnp_radians(dpnp_descriptor array1)
345-
cpdef dpnp_descriptor dpnp_recip(dpnp_descriptor array1)

dpnp/dpnp_algo/dpnp_algo_trigonometric.pxi

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ and the rest of the library
3838
__all__ += [
3939
'dpnp_degrees',
4040
'dpnp_radians',
41-
'dpnp_recip',
4241
'dpnp_unwrap'
4342
]
4443

@@ -47,10 +46,6 @@ cpdef utils.dpnp_descriptor dpnp_degrees(utils.dpnp_descriptor x1):
4746
return call_fptr_1in_1out_strides(DPNP_FN_DEGREES_EXT, x1)
4847

4948

50-
cpdef utils.dpnp_descriptor dpnp_recip(utils.dpnp_descriptor x1):
51-
return call_fptr_1in_1out_strides(DPNP_FN_RECIP_EXT, x1)
52-
53-
5449
cpdef utils.dpnp_descriptor dpnp_radians(utils.dpnp_descriptor x1):
5550
return call_fptr_1in_1out_strides(DPNP_FN_RADIANS_EXT, x1)
5651

dpnp/dpnp_algo/dpnp_elementwise_common.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dpnp_acos",
3939
"dpnp_acosh",
4040
"dpnp_add",
41+
"dpnp_angle",
4142
"dpnp_asin",
4243
"dpnp_asinh",
4344
"dpnp_atan",
@@ -88,6 +89,7 @@
8889
"dpnp_power",
8990
"dpnp_proj",
9091
"dpnp_real",
92+
"dpnp_reciprocal",
9193
"dpnp_remainder",
9294
"dpnp_right_shift",
9395
"dpnp_round",
@@ -182,6 +184,7 @@ def _make_unary_func(
182184
):
183185
impl_fn = dpt_unary_fn.get_implementation_function()
184186
type_resolver_fn = dpt_unary_fn.get_type_result_resolver_function()
187+
acceptance_fn = dpt_unary_fn.get_type_promotion_path_acceptance_function()
185188

186189
def _call_func(src, dst, sycl_queue, depends=None):
187190
"""A callback to register in UnaryElementwiseFunc class of dpctl.tensor"""
@@ -195,7 +198,7 @@ def _call_func(src, dst, sycl_queue, depends=None):
195198
return impl_fn(src, dst, sycl_queue, depends)
196199

197200
func = dpt_unary_fn.__class__(
198-
name, type_resolver_fn, _call_func, fn_docstring
201+
name, type_resolver_fn, _call_func, fn_docstring, acceptance_fn
199202
)
200203
return func
201204

@@ -404,6 +407,45 @@ def dpnp_add(x1, x2, out=None, order="K"):
404407
return dpnp_array._create_from_usm_ndarray(res_usm)
405408

406409

410+
_angle_docstring = """
411+
angle(x, out=None, order="K")
412+
413+
Computes the phase angle (also called the argument) of each element `x_i` for
414+
input array `x`.
415+
416+
Args:
417+
x (dpnp.ndarray):
418+
Input array, expected to have a complex-valued floating-point data type.
419+
out ({None, dpnp.ndarray}, optional):
420+
Output array to populate.
421+
Array must have the correct shape and the expected data type.
422+
order ("C", "F", "A", "K", optional):
423+
Memory layout of the newly output array, if parameter `out` is ``None``.
424+
Default: "K".
425+
Returns:
426+
dpnp.ndarray:
427+
An array containing the element-wise phase angles.
428+
The returned array has a floating-point data type determined
429+
by the Type Promotion Rules.
430+
"""
431+
432+
angle_func = _make_unary_func("angle", dpt.angle, _angle_docstring)
433+
434+
435+
def dpnp_angle(x, out=None, order="K"):
436+
"""Invokes angle() from dpctl.tensor implementation for angle() function."""
437+
438+
# dpctl.tensor only works with usm_ndarray
439+
x1_usm = dpnp.get_usm_ndarray(x)
440+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
441+
442+
res_usm = angle_func(x1_usm, out=out_usm, order=order)
443+
if out is None:
444+
return dpnp_array._create_from_usm_ndarray(res_usm)
445+
else:
446+
return out
447+
448+
407449
_asin_docstring = """
408450
asin(x, out=None, order='K')
409451
@@ -2452,6 +2494,46 @@ def dpnp_real(x, out=None, order="K"):
24522494
return dpnp_array._create_from_usm_ndarray(res_usm)
24532495

24542496

2497+
_reciprocal_docstring = """
2498+
reciprocal(x, out=None, order="K")
2499+
2500+
Computes the reciprocal of each element `x_i` for input array `x`.
2501+
2502+
Args:
2503+
x (dpnp.ndarray):
2504+
Input array, expected to have a real-valued floating-point data type.
2505+
out ({None, dpnp.ndarray}, optional):
2506+
Output array to populate.
2507+
Array must have the correct shape and the expected data type.
2508+
order ("C", "F", "A", "K", optional):
2509+
Memory layout of the newly output array, if parameter `out` is ``None``.
2510+
Default: "K".
2511+
Returns:
2512+
dpnp.ndarray:
2513+
An array containing the element-wise reciprocals.
2514+
The returned array has a floating-point data type determined
2515+
by the Type Promotion Rules.
2516+
"""
2517+
2518+
reciprocal_func = _make_unary_func(
2519+
"reciprocal", dpt.reciprocal, _reciprocal_docstring
2520+
)
2521+
2522+
2523+
def dpnp_reciprocal(x, out=None, order="K"):
2524+
"""Invokes reciprocal() from dpctl.tensor implementation for reciprocal() function."""
2525+
2526+
# dpctl.tensor only works with usm_ndarray
2527+
x1_usm = dpnp.get_usm_ndarray(x)
2528+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
2529+
2530+
res_usm = reciprocal_func(x1_usm, out=out_usm, order=order)
2531+
if out is None:
2532+
return dpnp_array._create_from_usm_ndarray(res_usm)
2533+
else:
2534+
return out
2535+
2536+
24552537
_remainder_docstring = """
24562538
remainder(x1, x2, out=None, order='K')
24572539

dpnp/dpnp_iface_bitwise.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# cython: language_level=3
2-
# distutils: language = c++
31
# -*- coding: utf-8 -*-
42
# *****************************************************************************
53
# Copyright (c) 2016-2023, Intel Corporation

dpnp/dpnp_iface_manipulation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ def ravel(a, order="C"):
11741174
11751175
Parameters
11761176
----------
1177-
x : {dpnp_array, usm_ndarray}
1177+
x : {dpnp.array, usm_ndarray}
11781178
Input array. The elements in `a` are read in the order specified by order,
11791179
and packed as a 1-D array.
11801180
order : {'C', 'F'}, optional
@@ -1187,7 +1187,7 @@ def ravel(a, order="C"):
11871187
11881188
Returns
11891189
-------
1190-
out : dpnp_array
1190+
out : dpnp.array
11911191
A contiguous 1-D array of the same subtype as `a`, with shape (a.size,).
11921192
11931193
See Also
@@ -1220,7 +1220,7 @@ def repeat(a, repeats, axis=None):
12201220
12211221
Parameters
12221222
----------
1223-
x : {dpnp_array, usm_ndarray}
1223+
x : {dpnp.array, usm_ndarray}
12241224
Input array.
12251225
repeat : int or array of int
12261226
The number of repetitions for each element. `repeats` is broadcasted to fit
@@ -1231,7 +1231,7 @@ def repeat(a, repeats, axis=None):
12311231
12321232
Returns
12331233
-------
1234-
out : dpnp_array
1234+
out : dpnp.array
12351235
Output array which has the same shape as `a`, except along the given axis.
12361236
12371237
See Also

dpnp/dpnp_iface_mathematical.py

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
check_nd_call_func,
5555
dpnp_abs,
5656
dpnp_add,
57+
dpnp_angle,
5758
dpnp_ceil,
5859
dpnp_conj,
5960
dpnp_copysign,
@@ -82,6 +83,7 @@
8283
"abs",
8384
"absolute",
8485
"add",
86+
"angle",
8587
"around",
8688
"ceil",
8789
"clip",
@@ -291,6 +293,62 @@ def add(
291293
)
292294

293295

296+
def angle(z, deg=False):
297+
"""
298+
Return the angle of the complex argument.
299+
300+
For full documentation refer to :obj:`numpy.angle`.
301+
302+
Parameters
303+
----------
304+
x : {dpnp.array, usm_ndarray}
305+
Input array, expected to have a complex-valued floating-point data type.
306+
deg : bool, optional
307+
Return angle in degrees if True, radians if False (default).
308+
309+
Returns
310+
-------
311+
out : dpnp.ndarray
312+
The counterclockwise angle from the positive real axis on
313+
the complex plane in the range `(-pi, pi]`.
314+
The returned array has a floating-point data type determined
315+
by the Type Promotion Rules.
316+
317+
Notes
318+
-----
319+
Although the angle of the complex number 0 is undefined, `dpnp.angle(0)` returns the value 0.
320+
321+
See Also
322+
--------
323+
:obj:`dpnp.arctan2` : Element-wise arc tangent of `x1/x2` choosing the quadrant correctly.
324+
:obj:`dpnp.absolute` : Calculate the absolute value element-wise.
325+
326+
Examples
327+
--------
328+
>>> import dpnp as np
329+
>>> a = np.array([1.0, 1.0j, 1+1j])
330+
>>> np.angle(a) # in radians
331+
array([0. , 1.57079633, 0.78539816]) # may vary
332+
333+
>>> np.angle(a, deg=True) # in degrees
334+
array([ 0., 90., 45.])
335+
336+
"""
337+
338+
if not dpnp.isscalar(z):
339+
dpnp.check_supported_arrays_type(z)
340+
res = dpnp_angle(z)
341+
if deg is True:
342+
res = res * (180 / dpnp.pi)
343+
return res
344+
else:
345+
return call_origin(
346+
numpy.angle,
347+
z,
348+
deg=deg,
349+
)
350+
351+
294352
def around(x, /, decimals=0, out=None):
295353
"""
296354
Round an array to the given number of decimals.
@@ -390,12 +448,12 @@ def clip(a, a_min, a_max, *, out=None, order="K", **kwargs):
390448
391449
Parameters
392450
----------
393-
a : {dpnp_array, usm_ndarray}
451+
a : {dpnp.array, usm_ndarray}
394452
Array containing elements to clip.
395-
a_min, a_max : {dpnp_array, usm_ndarray, None}
453+
a_min, a_max : {dpnp.array, usm_ndarray, None}
396454
Minimum and maximum value. If ``None``, clipping is not performed on the corresponding edge.
397455
Only one of `a_min` and `a_max` may be ``None``. Both are broadcast against `a`.
398-
out : {dpnp_array, usm_ndarray}, optional
456+
out : {dpnp.array, usm_ndarray}, optional
399457
The results will be placed in this array. It may be the input array for in-place clipping.
400458
`out` must be of the right shape to hold the output. Its type is preserved.
401459
order : {"C", "F", "A", "K", None}, optional
@@ -404,7 +462,7 @@ def clip(a, a_min, a_max, *, out=None, order="K", **kwargs):
404462
405463
Returns
406464
-------
407-
out : dpnp_array
465+
out : dpnp.array
408466
An array with the elements of `a`, but where values < `a_min` are replaced with `a_min`,
409467
and those > `a_max` with `a_max`.
410468

0 commit comments

Comments
 (0)