Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_real_imag #1557

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ env:
test_sycl_queue.py
test_umath.py
test_usm_type.py
third_party/cupy/core_tests/test_ndarray_complex_ops.py
third_party/cupy/linalg_tests/test_product.py
third_party/cupy/logic_tests/test_comparison.py
third_party/cupy/logic_tests/test_truth.py
Expand Down
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 data 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 data 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
78 changes: 75 additions & 3 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,44 @@ 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.70710677])

"""
return dpnp.imag(self)

@imag.setter
def imag(self, value):
"""
Set the imaginary part of the array.

For full documentation refer to :obj:`numpy.ndarray.imag`.

Examples
--------
>>> import dpnp as np
>>> a = np.array([1+2j, 3+4j, 5+6j])
>>> a.imag = 9
>>> a
array([1.+9.j, 3.+9.j, 5.+9.j])

"""
if dpnp.issubsctype(self.dtype, dpnp.complexfloating):
vtavana marked this conversation as resolved.
Show resolved Hide resolved
dpnp.copyto(self._array_obj.imag, value)
else:
raise TypeError("array does not have imaginary part to set")

def item(self, id=None):
"""
Expand Down Expand Up @@ -975,7 +1012,42 @@ 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.70710677])

"""
return dpnp.real(self)

@real.setter
def real(self, value):
"""
Set the real part of the array.

For full documentation refer to :obj:`numpy.ndarray.real`.

Examples
--------
>>> import dpnp as np
>>> a = np.array([1+2j, 3+4j, 5+6j])
>>> a.real = 9
>>> a
array([9.+2.j, 9.+4.j, 9.+6.j])

"""
dpnp.copyto(self._array_obj.real, value)

# 'repeat',

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

"""

dpnp.reshape(self, newshape=newshape)
self._array_obj.shape = newshape
vtavana marked this conversation as resolved.
Show resolved Hide resolved

@property
def size(self):
Expand Down
Loading