Skip to content

Commit 304c986

Browse files
committed
add_real_imag
1 parent 51057ba commit 304c986

10 files changed

+339
-45
lines changed

dpnp/dparray.pyx

Lines changed: 3 additions & 3 deletions
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

Lines changed: 82 additions & 0 deletions
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 datat 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 datat 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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,29 @@ 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.70710678])
836+
"""
837+
return dpnp.imag(self)
838+
839+
@imag.setter
840+
def imag(self, value):
841+
if dpnp.issubsctype(self.dtype, dpnp.complexfloating):
842+
dpnp.copyto(self._array_obj.imag, value)
843+
else:
844+
raise TypeError("dpnp.ndarray does not have imaginary part to set")
823845

824846
def item(self, id=None):
825847
"""
@@ -975,7 +997,30 @@ def put(self, indices, vals, /, *, axis=None, mode="wrap"):
975997
return dpnp.put(self, indices, vals, axis=axis, mode=mode)
976998

977999
# 'ravel',
978-
# 'real',
1000+
1001+
@property
1002+
def real(self):
1003+
"""
1004+
The real part of the array.
1005+
1006+
For full documentation refer to :obj:`numpy.ndarray.real`.
1007+
1008+
Examples
1009+
--------
1010+
>>> import dpnp as np
1011+
>>> x = np.sqrt(np.array([1+0j, 0+1j]))
1012+
>>> x.real
1013+
array([ 1. , 0.70710678])
1014+
"""
1015+
if dpnp.issubsctype(self.dtype, dpnp.complexfloating):
1016+
return dpnp.real(self)
1017+
else:
1018+
return self
1019+
1020+
@real.setter
1021+
def real(self, value):
1022+
dpnp.copyto(self._array_obj.real, value)
1023+
9791024
# 'repeat',
9801025

9811026
def reshape(self, *sh, **kwargs):
@@ -1050,7 +1095,7 @@ def shape(self, newshape):
10501095
10511096
"""
10521097

1053-
dpnp.reshape(self, newshape=newshape)
1098+
self._array_obj.shape = newshape
10541099

10551100
@property
10561101
def size(self):

0 commit comments

Comments
 (0)