Skip to content

Commit 329f36e

Browse files
add_real_imag (#1557)
* add_real_imag * remove decorator+add test to public CI --------- Co-authored-by: Vladislav Perevezentsev <[email protected]>
1 parent 9746426 commit 329f36e

14 files changed

+426
-84
lines changed

.github/workflows/conda-package.yml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ env:
2727
test_sycl_queue.py
2828
test_umath.py
2929
test_usm_type.py
30+
third_party/cupy/core_tests/test_ndarray_complex_ops.py
3031
third_party/cupy/linalg_tests/test_product.py
3132
third_party/cupy/logic_tests/test_comparison.py
3233
third_party/cupy/logic_tests/test_truth.py

dpnp/dparray.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"""Module DPArray
3131
3232
This module contains Array class represents multi-dimensional array
33-
using USB interface for an Intel GPU device.
33+
using USM interface for an Intel GPU device.
3434
3535
"""
3636

@@ -51,7 +51,7 @@ from dpnp.dpnp_iface import get_dpnp_descriptor as iface_get_dpnp_descriptor
5151
from dpnp.dpnp_iface import prod as iface_prod
5252
from dpnp.dpnp_iface import sum as iface_sum
5353

54-
# It's prohibeted to use 'import *' from 'dpnp.dpnp_iface_arraycreation' module here,
54+
# It's prohibited to use 'import *' from 'dpnp.dpnp_iface_arraycreation' module here,
5555
# because module has 'array' function, but cython has already imported 'array' by default.
5656
# It would cause import collision. Thus instead import each function explicitly.
5757
from dpnp.dpnp_iface_arraycreation import (
@@ -196,7 +196,7 @@ cdef class dparray:
196196
"""Multi-dimensional array using USM interface for an Intel GPU device.
197197

198198
This class implements a subset of methods of :class:`numpy.ndarray`.
199-
The difference is that this class allocates the array content useing
199+
The difference is that this class allocates the array content using
200200
USM interface on the current GPU device.
201201

202202
Args:

dpnp/dpnp_algo/dpnp_elementwise_common.py

+82
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"dpnp_floor_divide",
6464
"dpnp_greater",
6565
"dpnp_greater_equal",
66+
"dpnp_imag",
6667
"dpnp_invert",
6768
"dpnp_isfinite",
6869
"dpnp_isinf",
@@ -80,6 +81,7 @@
8081
"dpnp_not_equal",
8182
"dpnp_power",
8283
"dpnp_proj",
84+
"dpnp_real",
8385
"dpnp_remainder",
8486
"dpnp_right_shift",
8587
"dpnp_round",
@@ -1259,6 +1261,46 @@ def dpnp_greater_equal(x1, x2, out=None, order="K"):
12591261
return dpnp_array._create_from_usm_ndarray(res_usm)
12601262

12611263

1264+
_imag_docstring = """
1265+
imag(x, out=None, order="K")
1266+
1267+
Computes imaginary part of each element `x_i` for input array `x`.
1268+
1269+
Args:
1270+
x (dpnp.ndarray):
1271+
Input array, expected to have numeric data type.
1272+
out ({None, dpnp.ndarray}, optional):
1273+
Output array to populate.
1274+
Array have the correct shape and the expected data type.
1275+
order ("C","F","A","K", optional):
1276+
Memory layout of the newly output array, if parameter `out` is `None`.
1277+
Default: "K".
1278+
Returns:
1279+
dpnp.ndarray:
1280+
An array containing the element-wise imaginary component of input.
1281+
If the input is a real-valued data type, the returned array has
1282+
the same data type. If the input is a complex floating-point
1283+
data type, the returned array has a floating-point data type
1284+
with the same floating-point precision as complex input.
1285+
"""
1286+
1287+
1288+
imag_func = UnaryElementwiseFunc(
1289+
"imag", ti._imag_result_type, ti._imag, _imag_docstring
1290+
)
1291+
1292+
1293+
def dpnp_imag(x, out=None, order="K"):
1294+
"""Invokes imag() from dpctl.tensor implementation for imag() function."""
1295+
1296+
# dpctl.tensor only works with usm_ndarray
1297+
x1_usm = dpnp.get_usm_ndarray(x)
1298+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
1299+
1300+
res_usm = imag_func(x1_usm, out=out_usm, order=order)
1301+
return dpnp_array._create_from_usm_ndarray(res_usm)
1302+
1303+
12621304
_invert_docstring = """
12631305
invert(x, out=None, order='K')
12641306
@@ -2021,6 +2063,46 @@ def dpnp_proj(x, out=None, order="K"):
20212063
return dpnp_array._create_from_usm_ndarray(res_usm)
20222064

20232065

2066+
_real_docstring = """
2067+
real(x, out=None, order="K")
2068+
2069+
Computes real part of each element `x_i` for input array `x`.
2070+
2071+
Args:
2072+
x (dpnp.ndarray):
2073+
Input array, expected to have numeric data type.
2074+
out ({None, dpnp.ndarray}, optional):
2075+
Output array to populate.
2076+
Array have the correct shape and the expected data type.
2077+
order ("C","F","A","K", optional):
2078+
Memory layout of the newly output array, if parameter `out` is `None`.
2079+
Default: "K".
2080+
Returns:
2081+
dpnp.ndarray:
2082+
An array containing the element-wise real component of input.
2083+
If the input is a real-valued data type, the returned array has
2084+
the same data type. If the input is a complex floating-point
2085+
data type, the returned array has a floating-point data type
2086+
with the same floating-point precision as complex input.
2087+
"""
2088+
2089+
2090+
real_func = UnaryElementwiseFunc(
2091+
"real", ti._real_result_type, ti._real, _real_docstring
2092+
)
2093+
2094+
2095+
def dpnp_real(x, out=None, order="K"):
2096+
"""Invokes real() from dpctl.tensor implementation for real() function."""
2097+
2098+
# dpctl.tensor only works with usm_ndarray
2099+
x1_usm = dpnp.get_usm_ndarray(x)
2100+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
2101+
2102+
res_usm = real_func(x1_usm, out=out_usm, order=order)
2103+
return dpnp_array._create_from_usm_ndarray(res_usm)
2104+
2105+
20242106
_remainder_docstring_ = """
20252107
remainder(x1, x2, out=None, order='K')
20262108
Calculates the remainder of division for each element `x1_i` of the input array

dpnp/dpnp_array.py

+75-3
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,44 @@ def flatten(self, order="C"):
819819
return new_arr
820820

821821
# 'getfield',
822-
# 'imag',
822+
823+
@property
824+
def imag(self):
825+
"""
826+
The imaginary part of the array.
827+
828+
For full documentation refer to :obj:`numpy.ndarray.imag`.
829+
830+
Examples
831+
--------
832+
>>> import dpnp as np
833+
>>> x = np.sqrt(np.array([1+0j, 0+1j]))
834+
>>> x.imag
835+
array([0. , 0.70710677])
836+
837+
"""
838+
return dpnp.imag(self)
839+
840+
@imag.setter
841+
def imag(self, value):
842+
"""
843+
Set the imaginary part of the array.
844+
845+
For full documentation refer to :obj:`numpy.ndarray.imag`.
846+
847+
Examples
848+
--------
849+
>>> import dpnp as np
850+
>>> a = np.array([1+2j, 3+4j, 5+6j])
851+
>>> a.imag = 9
852+
>>> a
853+
array([1.+9.j, 3.+9.j, 5.+9.j])
854+
855+
"""
856+
if dpnp.issubsctype(self.dtype, dpnp.complexfloating):
857+
dpnp.copyto(self._array_obj.imag, value)
858+
else:
859+
raise TypeError("array does not have imaginary part to set")
823860

824861
def item(self, id=None):
825862
"""
@@ -975,7 +1012,42 @@ def put(self, indices, vals, /, *, axis=None, mode="wrap"):
9751012
return dpnp.put(self, indices, vals, axis=axis, mode=mode)
9761013

9771014
# 'ravel',
978-
# 'real',
1015+
1016+
@property
1017+
def real(self):
1018+
"""
1019+
The real part of the array.
1020+
1021+
For full documentation refer to :obj:`numpy.ndarray.real`.
1022+
1023+
Examples
1024+
--------
1025+
>>> import dpnp as np
1026+
>>> x = np.sqrt(np.array([1+0j, 0+1j]))
1027+
>>> x.real
1028+
array([1. , 0.70710677])
1029+
1030+
"""
1031+
return dpnp.real(self)
1032+
1033+
@real.setter
1034+
def real(self, value):
1035+
"""
1036+
Set the real part of the array.
1037+
1038+
For full documentation refer to :obj:`numpy.ndarray.real`.
1039+
1040+
Examples
1041+
--------
1042+
>>> import dpnp as np
1043+
>>> a = np.array([1+2j, 3+4j, 5+6j])
1044+
>>> a.real = 9
1045+
>>> a
1046+
array([9.+2.j, 9.+4.j, 9.+6.j])
1047+
1048+
"""
1049+
dpnp.copyto(self._array_obj.real, value)
1050+
9791051
# 'repeat',
9801052

9811053
def reshape(self, *sh, **kwargs):
@@ -1050,7 +1122,7 @@ def shape(self, newshape):
10501122
10511123
"""
10521124

1053-
dpnp.reshape(self, newshape=newshape)
1125+
self._array_obj.shape = newshape
10541126

10551127
@property
10561128
def size(self):

0 commit comments

Comments
 (0)