Skip to content

Commit 3324da4

Browse files
authored
return a view for dpnp.ndarray.real and dpnp.ndarray.imag (#1719)
* return a view for dpnp.ndarray.real/imag * return self for real numbers * update real/imag/conj test * fix dtype in test_conj_out
1 parent f33ef19 commit 3324da4

File tree

3 files changed

+152
-57
lines changed

3 files changed

+152
-57
lines changed

dpnp/dpnp_array.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,9 @@ def imag(self):
831831
array([0. , 0.70710677])
832832
833833
"""
834-
return dpnp.imag(self)
834+
return dpnp_array._create_from_usm_ndarray(
835+
dpnp.get_usm_ndarray(self).imag
836+
)
835837

836838
@imag.setter
837839
def imag(self, value):
@@ -1042,7 +1044,12 @@ def real(self):
10421044
array([1. , 0.70710677])
10431045
10441046
"""
1045-
return dpnp.real(self)
1047+
if dpnp.issubsctype(self.dtype, dpnp.complexfloating):
1048+
return dpnp_array._create_from_usm_ndarray(
1049+
dpnp.get_usm_ndarray(self).real
1050+
)
1051+
else:
1052+
return self
10461053

10471054
@real.setter
10481055
def real(self, value):

tests/test_dparray.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ def test_flags_strides(dtype, order, strides):
8686
assert numpy_array.flags.f_contiguous == dpnp_array.flags.f_contiguous
8787

8888

89+
def test_flags_writable():
90+
a = dpnp.arange(10, dtype="f4")
91+
a.flags["W"] = False
92+
93+
a.shape = (5, 2)
94+
assert not a.flags.writable
95+
assert not a.T.flags.writable
96+
assert not a.real.flags.writable
97+
assert not a[0:3].flags.writable
98+
99+
a = dpnp.arange(10, dtype="c8")
100+
a.flags["W"] = False
101+
102+
assert not a.real.flags.writable
103+
assert not a.imag.flags.writable
104+
105+
89106
def test_print_dpnp_int():
90107
result = repr(dpnp.array([1, 0, 2, -3, -1, 2, 21, -9], dtype="i4"))
91108
expected = "array([ 1, 0, 2, -3, -1, 2, 21, -9], dtype=int32)"

tests/test_mathematical.py

Lines changed: 126 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -906,70 +906,141 @@ def test_signbit(data, dtype):
906906
assert_dtype_allclose(result, expected)
907907

908908

909-
@pytest.mark.parametrize(
910-
"func",
911-
["real", "imag", "conj"],
912-
ids=["real", "imag", "conj"],
913-
)
914-
@pytest.mark.parametrize(
915-
"data",
916-
[complex(-1, -4), complex(-1, 2), complex(3, -7), complex(4, 12)],
917-
ids=[
918-
"complex(-1, -4)",
919-
"complex(-1, 2)",
920-
"complex(3, -7)",
921-
"complex(4, 12)",
922-
],
923-
)
924-
@pytest.mark.parametrize("dtype", get_complex_dtypes())
925-
def test_complex_funcs(func, data, dtype):
926-
np_a = numpy.array(data, dtype=dtype)
927-
dpnp_a = dpnp.array(data, dtype=dtype)
909+
class TestConj:
910+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
911+
def test_conj(self, dtype):
912+
a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype)
913+
ia = dpnp.array(a)
928914

929-
result = getattr(dpnp, func)(dpnp_a)
930-
expected = getattr(numpy, func)(np_a)
931-
assert_dtype_allclose(result, expected)
915+
result = dpnp.conj(ia)
916+
expected = numpy.conj(a)
917+
assert_dtype_allclose(result, expected)
932918

933-
# out keyword
934-
if func == "conj":
935-
dp_out = dpnp.empty(expected.shape, dtype=expected.dtype)
936-
result = getattr(dpnp, func)(dpnp_a, out=dp_out)
919+
@pytest.mark.parametrize("dtype", get_complex_dtypes())
920+
def test_conj_complex(self, dtype):
921+
x1 = numpy.random.uniform(-5, 5, 20)
922+
x2 = numpy.random.uniform(-5, 5, 20)
923+
a = numpy.array(x1 + 1j * x2, dtype=dtype)
924+
ia = dpnp.array(a)
925+
926+
result = dpnp.conj(ia)
927+
expected = numpy.conj(a)
928+
assert_dtype_allclose(result, expected)
929+
930+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
931+
def test_conj_ndarray(self, dtype):
932+
a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype)
933+
ia = dpnp.array(a)
934+
935+
result = ia.conj()
936+
assert result is ia
937+
assert_dtype_allclose(result, a.conj())
938+
939+
@pytest.mark.parametrize("dtype", get_complex_dtypes())
940+
def test_conj_complex_ndarray(self, dtype):
941+
x1 = numpy.random.uniform(-5, 5, 20)
942+
x2 = numpy.random.uniform(-5, 5, 20)
943+
a = numpy.array(x1 + 1j * x2, dtype=dtype)
944+
ia = dpnp.array(a)
945+
946+
assert_dtype_allclose(ia.conj(), a.conj())
947+
948+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
949+
def test_conj_out(self, dtype):
950+
a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype)
951+
ia = dpnp.array(a)
952+
953+
expected = numpy.conj(a)
954+
dp_out = dpnp.empty(ia.shape, dtype=dtype)
955+
result = dpnp.conj(ia, out=dp_out)
937956
assert dp_out is result
938957
assert_dtype_allclose(result, expected)
939958

940959

941-
@pytest.mark.parametrize("dtype", get_complex_dtypes())
942-
def test_projection_infinity(dtype):
943-
X = [
944-
complex(1, 2),
945-
complex(dpnp.inf, -1),
946-
complex(0, -dpnp.inf),
947-
complex(-dpnp.inf, dpnp.nan),
948-
]
949-
Y = [
950-
complex(1, 2),
951-
complex(dpnp.inf, -0.0),
952-
complex(dpnp.inf, -0.0),
953-
complex(dpnp.inf, 0.0),
954-
]
955-
956-
a = dpnp.array(X, dtype=dtype)
957-
result = dpnp.proj(a)
958-
expected = dpnp.array(Y, dtype=dtype)
959-
assert_dtype_allclose(result, expected)
960+
class TestRealImag:
961+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
962+
def test_real_imag(self, dtype):
963+
a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype)
964+
ia = dpnp.array(a)
960965

961-
# out keyword
962-
dp_out = dpnp.empty(expected.shape, dtype=expected.dtype)
963-
result = dpnp.proj(a, out=dp_out)
964-
assert dp_out is result
965-
assert_dtype_allclose(result, expected)
966+
result = dpnp.real(ia)
967+
assert result is ia
968+
expected = numpy.real(a)
969+
assert expected is a
970+
assert_dtype_allclose(result, expected)
966971

972+
result = dpnp.imag(ia)
973+
expected = numpy.imag(a)
974+
assert_dtype_allclose(result, expected)
967975

968-
@pytest.mark.parametrize("dtype", get_all_dtypes())
969-
def test_projection(dtype):
970-
result = dpnp.proj(dpnp.array(1, dtype=dtype))
971-
expected = dpnp.array(complex(1, 0))
972-
assert_allclose(result, expected)
976+
@pytest.mark.parametrize("dtype", get_complex_dtypes())
977+
def test_real_imag_complex(self, dtype):
978+
x1 = numpy.random.uniform(-5, 5, 20)
979+
x2 = numpy.random.uniform(-5, 5, 20)
980+
a = numpy.array(x1 + 1j * x2, dtype=dtype)
981+
ia = dpnp.array(a)
982+
983+
result = dpnp.real(ia)
984+
expected = numpy.real(a)
985+
assert_dtype_allclose(result, expected)
986+
987+
result = dpnp.imag(ia)
988+
expected = numpy.imag(a)
989+
assert_dtype_allclose(result, expected)
990+
991+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
992+
def test_real_imag_ndarray(self, dtype):
993+
a = numpy.array(numpy.random.uniform(-5, 5, 20), dtype=dtype)
994+
ia = dpnp.array(a)
995+
996+
result = ia.real
997+
assert result is ia
998+
assert_dtype_allclose(result, a.real)
999+
assert_dtype_allclose(ia.imag, a.imag)
1000+
1001+
@pytest.mark.parametrize("dtype", get_complex_dtypes())
1002+
def test_real_imag_complex_ndarray(self, dtype):
1003+
x1 = numpy.random.uniform(-5, 5, 20)
1004+
x2 = numpy.random.uniform(-5, 5, 20)
1005+
a = numpy.array(x1 + 1j * x2, dtype=dtype)
1006+
ia = dpnp.array(a)
1007+
1008+
assert_dtype_allclose(ia.real, a.real)
1009+
assert_dtype_allclose(ia.imag, a.imag)
1010+
1011+
1012+
class TestProjection:
1013+
@pytest.mark.parametrize("dtype", get_complex_dtypes())
1014+
def test_projection_infinity(self, dtype):
1015+
X = [
1016+
complex(1, 2),
1017+
complex(dpnp.inf, -1),
1018+
complex(0, -dpnp.inf),
1019+
complex(-dpnp.inf, dpnp.nan),
1020+
]
1021+
Y = [
1022+
complex(1, 2),
1023+
complex(dpnp.inf, -0.0),
1024+
complex(dpnp.inf, -0.0),
1025+
complex(dpnp.inf, 0.0),
1026+
]
1027+
1028+
a = dpnp.array(X, dtype=dtype)
1029+
result = dpnp.proj(a)
1030+
expected = dpnp.array(Y, dtype=dtype)
1031+
assert_dtype_allclose(result, expected)
1032+
1033+
# out keyword
1034+
dp_out = dpnp.empty(expected.shape, dtype=expected.dtype)
1035+
result = dpnp.proj(a, out=dp_out)
1036+
assert dp_out is result
1037+
assert_dtype_allclose(result, expected)
1038+
1039+
@pytest.mark.parametrize("dtype", get_all_dtypes())
1040+
def test_projection(self, dtype):
1041+
result = dpnp.proj(dpnp.array(1, dtype=dtype))
1042+
expected = dpnp.array(complex(1, 0))
1043+
assert_allclose(result, expected)
9731044

9741045

9751046
@pytest.mark.parametrize("val_type", get_all_dtypes(no_none=True))

0 commit comments

Comments
 (0)