Skip to content

Commit

Permalink
add_real_imag
Browse files Browse the repository at this point in the history
  • Loading branch information
vtavana committed Sep 12, 2023
1 parent 51057ba commit 304c986
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 45 deletions.
6 changes: 3 additions & 3 deletions dpnp/dparray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"""Module DPArray
This module contains Array class represents multi-dimensional array
using USB interface for an Intel GPU device.
using USM interface for an Intel GPU device.
"""

Expand All @@ -51,7 +51,7 @@ from dpnp.dpnp_iface import get_dpnp_descriptor as iface_get_dpnp_descriptor
from dpnp.dpnp_iface import prod as iface_prod
from dpnp.dpnp_iface import sum as iface_sum

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

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

Args:
Expand Down
82 changes: 82 additions & 0 deletions dpnp/dpnp_algo/dpnp_elementwise_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"dpnp_floor_divide",
"dpnp_greater",
"dpnp_greater_equal",
"dpnp_imag",
"dpnp_invert",
"dpnp_isfinite",
"dpnp_isinf",
Expand All @@ -80,6 +81,7 @@
"dpnp_not_equal",
"dpnp_power",
"dpnp_proj",
"dpnp_real",
"dpnp_remainder",
"dpnp_right_shift",
"dpnp_round",
Expand Down Expand Up @@ -1259,6 +1261,46 @@ def dpnp_greater_equal(x1, x2, out=None, order="K"):
return dpnp_array._create_from_usm_ndarray(res_usm)


_imag_docstring = """
imag(x, out=None, order="K")
Computes imaginary part of each element `x_i` for input array `x`.
Args:
x (dpnp.ndarray):
Input array, expected to have numeric data type.
out ({None, dpnp.ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
dpnp.ndarray:
An array containing the element-wise imaginary component of input.
If the input is a real-valued data type, the returned array has
the same datat type. If the input is a complex floating-point
data type, the returned array has a floating-point data type
with the same floating-point precision as complex input.
"""


imag_func = UnaryElementwiseFunc(
"imag", ti._imag_result_type, ti._imag, _imag_docstring
)


def dpnp_imag(x, out=None, order="K"):
"""Invokes imag() from dpctl.tensor implementation for imag() function."""

# dpctl.tensor only works with usm_ndarray
x1_usm = dpnp.get_usm_ndarray(x)
out_usm = None if out is None else dpnp.get_usm_ndarray(out)

res_usm = imag_func(x1_usm, out=out_usm, order=order)
return dpnp_array._create_from_usm_ndarray(res_usm)


_invert_docstring = """
invert(x, out=None, order='K')
Expand Down Expand Up @@ -2021,6 +2063,46 @@ def dpnp_proj(x, out=None, order="K"):
return dpnp_array._create_from_usm_ndarray(res_usm)


_real_docstring = """
real(x, out=None, order="K")
Computes real part of each element `x_i` for input array `x`.
Args:
x (dpnp.ndarray):
Input array, expected to have numeric data type.
out ({None, dpnp.ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
dpnp.ndarray:
An array containing the element-wise real component of input.
If the input is a real-valued data type, the returned array has
the same datat type. If the input is a complex floating-point
data type, the returned array has a floating-point data type
with the same floating-point precision as complex input.
"""


real_func = UnaryElementwiseFunc(
"real", ti._real_result_type, ti._real, _real_docstring
)


def dpnp_real(x, out=None, order="K"):
"""Invokes real() from dpctl.tensor implementation for real() function."""

# dpctl.tensor only works with usm_ndarray
x1_usm = dpnp.get_usm_ndarray(x)
out_usm = None if out is None else dpnp.get_usm_ndarray(out)

res_usm = real_func(x1_usm, out=out_usm, order=order)
return dpnp_array._create_from_usm_ndarray(res_usm)


_remainder_docstring_ = """
remainder(x1, x2, out=None, order='K')
Calculates the remainder of division for each element `x1_i` of the input array
Expand Down
51 changes: 48 additions & 3 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,29 @@ def flatten(self, order="C"):
return new_arr

# 'getfield',
# 'imag',

@property
def imag(self):
"""
The imaginary part of the array.
For full documentation refer to :obj:`numpy.ndarray.imag`.
Examples
--------
>>> import dpnp as np
>>> x = np.sqrt(np.array([1+0j, 0+1j]))
>>> x.imag
array([ 0. , 0.70710678])
"""
return dpnp.imag(self)

@imag.setter
def imag(self, value):
if dpnp.issubsctype(self.dtype, dpnp.complexfloating):
dpnp.copyto(self._array_obj.imag, value)
else:
raise TypeError("dpnp.ndarray does not have imaginary part to set")

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

# 'ravel',
# 'real',

@property
def real(self):
"""
The real part of the array.
For full documentation refer to :obj:`numpy.ndarray.real`.
Examples
--------
>>> import dpnp as np
>>> x = np.sqrt(np.array([1+0j, 0+1j]))
>>> x.real
array([ 1. , 0.70710678])
"""
if dpnp.issubsctype(self.dtype, dpnp.complexfloating):
return dpnp.real(self)
else:
return self

@real.setter
def real(self, value):
dpnp.copyto(self._array_obj.real, value)

# 'repeat',

def reshape(self, *sh, **kwargs):
Expand Down Expand Up @@ -1050,7 +1095,7 @@ def shape(self, newshape):
"""

dpnp.reshape(self, newshape=newshape)
self._array_obj.shape = newshape

@property
def size(self):
Expand Down
Loading

0 comments on commit 304c986

Please sign in to comment.