Skip to content

Add operation __index__ and __complex__ #1285

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

Merged
merged 3 commits into from
Feb 9, 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
10 changes: 8 additions & 2 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ def __bool__(self):
return self._array_obj.__bool__()

# '__class__',
# '__complex__',

def __complex__(self):
return self._array_obj.__complex__()

# '__contains__',
# '__copy__',
# '__deepcopy__',
Expand Down Expand Up @@ -187,7 +190,10 @@ def __gt__(self, other):
# '__imatmul__',
# '__imod__',
# '__imul__',
# '__index__',

def __index__(self):
return self._array_obj.__index__()

# '__init__',
# '__init_subclass__',

Expand Down
52 changes: 39 additions & 13 deletions tests/test_dparray.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import dpnp
import numpy
import pytest
from .helper import get_all_dtypes

import dpnp
import dpctl.tensor as dpt

import numpy
from numpy.testing import (
assert_array_equal
)


@pytest.mark.parametrize("res_dtype",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32, numpy.bool_, numpy.complex_],
ids=['float64', 'float32', 'int64', 'int32', 'bool', 'complex'])
@pytest.mark.parametrize("arr_dtype",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32, numpy.bool_, numpy.complex_],
ids=['float64', 'float32', 'int64', 'int32', 'bool', 'complex'])
@pytest.mark.parametrize("res_dtype", get_all_dtypes())
@pytest.mark.parametrize("arr_dtype", get_all_dtypes())
@pytest.mark.parametrize("arr",
[[-2, -1, 0, 1, 2], [[-2, -1], [1, 2]], []],
ids=['[-2, -1, 0, 1, 2]', '[[-2, -1], [1, 2]]', '[]'])
Expand All @@ -18,12 +20,10 @@ def test_astype(arr, arr_dtype, res_dtype):
dpnp_array = dpnp.array(numpy_array)
expected = numpy_array.astype(res_dtype)
result = dpnp_array.astype(res_dtype)
numpy.testing.assert_array_equal(expected, result)
assert_array_equal(expected, result)


@pytest.mark.parametrize("arr_dtype",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32, numpy.bool_, numpy.complex_],
ids=['float64', 'float32', 'int64', 'int32', 'bool', 'complex'])
@pytest.mark.parametrize("arr_dtype", get_all_dtypes())
@pytest.mark.parametrize("arr",
[[-2, -1, 0, 1, 2], [[-2, -1], [1, 2]], []],
ids=['[-2, -1, 0, 1, 2]', '[[-2, -1], [1, 2]]', '[]'])
Expand All @@ -32,7 +32,7 @@ def test_flatten(arr, arr_dtype):
dpnp_array = dpnp.array(arr, dtype=arr_dtype)
expected = numpy_array.flatten()
result = dpnp_array.flatten()
numpy.testing.assert_array_equal(expected, result)
assert_array_equal(expected, result)


@pytest.mark.parametrize("shape",
Expand Down Expand Up @@ -68,3 +68,29 @@ def test_flags_strides(dtype, order, strides):
assert usm_array.flags == dpnp_array.flags
assert numpy_array.flags.c_contiguous == dpnp_array.flags.c_contiguous
assert numpy_array.flags.f_contiguous == dpnp_array.flags.f_contiguous


@pytest.mark.parametrize("func", [bool, float, int, complex])
@pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)])
@pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False, no_complex=True))
def test_scalar_type_casting(func, shape, dtype):
numpy_array = numpy.full(shape, 5, dtype=dtype)
dpnp_array = dpnp.full(shape, 5, dtype=dtype)
assert func(numpy_array) == func(dpnp_array)


@pytest.mark.parametrize("method", ["__bool__", "__float__", "__int__", "__complex__"])
@pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)])
@pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False, no_complex=True, no_none=True))
def test_scalar_type_casting_by_method(method, shape, dtype):
numpy_array = numpy.full(shape, 4.7, dtype=dtype)
dpnp_array = dpnp.full(shape, 4.7, dtype=dtype)
assert getattr(numpy_array, method)() == getattr(dpnp_array, method)()


@pytest.mark.parametrize("shape", [(1,), (1, 1), (1, 1, 1)])
@pytest.mark.parametrize("index_dtype", [dpnp.int32, dpnp.int64])
def test_array_as_index(shape, index_dtype):
ind_arr = dpnp.ones(shape, dtype=index_dtype)
a = numpy.arange(ind_arr.size + 1)
assert a[tuple(ind_arr)] == a[1]