Skip to content

Commit 65a7578

Browse files
authored
Merge 5a6824e into 264c6d8
2 parents 264c6d8 + 5a6824e commit 65a7578

File tree

3 files changed

+190
-188
lines changed

3 files changed

+190
-188
lines changed

dpnp/dpnp_iface_manipulation.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,18 +3894,13 @@ def transpose(a, axes=None):
38943894
38953895
"""
38963896

3897-
if isinstance(a, dpnp_array):
3898-
array = a
3899-
elif isinstance(a, dpt.usm_ndarray):
3900-
array = dpnp_array._create_from_usm_ndarray(a)
3901-
else:
3902-
raise TypeError(
3903-
f"An array must be any of supported type, but got {type(a)}"
3904-
)
3897+
dpnp.check_supported_arrays_type(a)
3898+
if isinstance(a, dpt.usm_ndarray):
3899+
a = dpnp_array._create_from_usm_ndarray(a)
39053900

39063901
if axes is None:
3907-
return array.transpose()
3908-
return array.transpose(*axes)
3902+
return a.transpose()
3903+
return a.transpose(*axes)
39093904

39103905

39113906
permute_dims = transpose # permute_dims is an alias for transpose

dpnp/tests/test_arraymanipulation.py

Lines changed: 174 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,38 @@
1515
from .third_party.cupy import testing
1616

1717

18+
class TestAsfarray:
19+
@testing.with_requires("numpy<2.0")
20+
@pytest.mark.parametrize("dtype", get_all_dtypes())
21+
@pytest.mark.parametrize(
22+
"data", [[1, 2, 3], [1.0, 2.0, 3.0]], ids=["[1, 2, 3]", "[1., 2., 3.]"]
23+
)
24+
def test_asfarray1(self, dtype, data):
25+
expected = numpy.asfarray(data, dtype)
26+
result = dpnp.asfarray(data, dtype)
27+
assert_array_equal(result, expected)
28+
29+
@testing.with_requires("numpy<2.0")
30+
@pytest.mark.usefixtures("suppress_complex_warning")
31+
@pytest.mark.parametrize("dtype", get_all_dtypes())
32+
@pytest.mark.parametrize("data", [[1.0, 2.0, 3.0]], ids=["[1., 2., 3.]"])
33+
@pytest.mark.parametrize("data_dtype", get_all_dtypes(no_none=True))
34+
def test_asfarray2(self, dtype, data, data_dtype):
35+
expected = numpy.asfarray(numpy.array(data, dtype=data_dtype), dtype)
36+
result = dpnp.asfarray(dpnp.array(data, dtype=data_dtype), dtype)
37+
assert_array_equal(result, expected)
38+
39+
# This is only for coverage with NumPy 2.0 and above
40+
def test_asfarray_coverage(self):
41+
expected = dpnp.array([1.0, 2.0, 3.0])
42+
result = dpnp.asfarray([1, 2, 3])
43+
assert_array_equal(result, expected)
44+
45+
expected = dpnp.array([1.0, 2.0, 3.0], dtype=dpnp.float32)
46+
result = dpnp.asfarray([1, 2, 3], dtype=dpnp.float32)
47+
assert_array_equal(result, expected)
48+
49+
1850
class TestAtleast1d:
1951
def test_0D_array(self):
2052
a = dpnp.array(1)
@@ -132,6 +164,148 @@ def test_dpnp_dpt_array(self):
132164
assert_array_equal(res, desired)
133165

134166

167+
class TestBroadcastArray:
168+
def assert_broadcast_correct(self, input_shapes):
169+
np_arrays = [numpy.zeros(s, dtype="i1") for s in input_shapes]
170+
out_np_arrays = numpy.broadcast_arrays(*np_arrays)
171+
dpnp_arrays = [dpnp.asarray(Xnp) for Xnp in np_arrays]
172+
out_dpnp_arrays = dpnp.broadcast_arrays(*dpnp_arrays)
173+
for Xnp, X in zip(out_np_arrays, out_dpnp_arrays):
174+
assert_array_equal(
175+
Xnp, dpnp.asnumpy(X), err_msg=f"Failed for {input_shapes})"
176+
)
177+
178+
def assert_broadcast_arrays_raise(self, input_shapes):
179+
dpnp_arrays = [dpnp.asarray(numpy.zeros(s)) for s in input_shapes]
180+
pytest.raises(ValueError, dpnp.broadcast_arrays, *dpnp_arrays)
181+
182+
def test_broadcast_arrays_same(self):
183+
Xnp = numpy.arange(10)
184+
Ynp = numpy.arange(10)
185+
res_Xnp, res_Ynp = numpy.broadcast_arrays(Xnp, Ynp)
186+
X = dpnp.asarray(Xnp)
187+
Y = dpnp.asarray(Ynp)
188+
res_X, res_Y = dpnp.broadcast_arrays(X, Y)
189+
assert_array_equal(res_Xnp, dpnp.asnumpy(res_X))
190+
assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y))
191+
192+
def test_broadcast_arrays_one_off(self):
193+
Xnp = numpy.array([[1, 2, 3]])
194+
Ynp = numpy.array([[1], [2], [3]])
195+
res_Xnp, res_Ynp = numpy.broadcast_arrays(Xnp, Ynp)
196+
X = dpnp.asarray(Xnp)
197+
Y = dpnp.asarray(Ynp)
198+
res_X, res_Y = dpnp.broadcast_arrays(X, Y)
199+
assert_array_equal(res_Xnp, dpnp.asnumpy(res_X))
200+
assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y))
201+
202+
@pytest.mark.parametrize(
203+
"shapes",
204+
[
205+
(),
206+
(1,),
207+
(3,),
208+
(0, 1),
209+
(0, 3),
210+
(1, 0),
211+
(3, 0),
212+
(1, 3),
213+
(3, 1),
214+
(3, 3),
215+
],
216+
)
217+
def test_broadcast_arrays_same_shapes(self, shapes):
218+
for shape in shapes:
219+
single_input_shapes = [shape]
220+
self.assert_broadcast_correct(single_input_shapes)
221+
double_input_shapes = [shape, shape]
222+
self.assert_broadcast_correct(double_input_shapes)
223+
triple_input_shapes = [shape, shape, shape]
224+
self.assert_broadcast_correct(triple_input_shapes)
225+
226+
@pytest.mark.parametrize(
227+
"shapes",
228+
[
229+
[[(1,), (3,)]],
230+
[[(1, 3), (3, 3)]],
231+
[[(3, 1), (3, 3)]],
232+
[[(1, 3), (3, 1)]],
233+
[[(1, 1), (3, 3)]],
234+
[[(1, 1), (1, 3)]],
235+
[[(1, 1), (3, 1)]],
236+
[[(1, 0), (0, 0)]],
237+
[[(0, 1), (0, 0)]],
238+
[[(1, 0), (0, 1)]],
239+
[[(1, 1), (0, 0)]],
240+
[[(1, 1), (1, 0)]],
241+
[[(1, 1), (0, 1)]],
242+
],
243+
)
244+
def test_broadcast_arrays_same_len_shapes(self, shapes):
245+
# Check that two different input shapes of the same length, but some have
246+
# ones, broadcast to the correct shape.
247+
for input_shapes in shapes:
248+
self.assert_broadcast_correct(input_shapes)
249+
self.assert_broadcast_correct(input_shapes[::-1])
250+
251+
@pytest.mark.parametrize(
252+
"shapes",
253+
[
254+
[[(), (3,)]],
255+
[[(3,), (3, 3)]],
256+
[[(3,), (3, 1)]],
257+
[[(1,), (3, 3)]],
258+
[[(), (3, 3)]],
259+
[[(1, 1), (3,)]],
260+
[[(1,), (3, 1)]],
261+
[[(1,), (1, 3)]],
262+
[[(), (1, 3)]],
263+
[[(), (3, 1)]],
264+
[[(), (0,)]],
265+
[[(0,), (0, 0)]],
266+
[[(0,), (0, 1)]],
267+
[[(1,), (0, 0)]],
268+
[[(), (0, 0)]],
269+
[[(1, 1), (0,)]],
270+
[[(1,), (0, 1)]],
271+
[[(1,), (1, 0)]],
272+
[[(), (1, 0)]],
273+
[[(), (0, 1)]],
274+
],
275+
)
276+
def test_broadcast_arrays_different_len_shapes(self, shapes):
277+
# Check that two different input shapes (of different lengths) broadcast
278+
# to the correct shape.
279+
for input_shapes in shapes:
280+
self.assert_broadcast_correct(input_shapes)
281+
self.assert_broadcast_correct(input_shapes[::-1])
282+
283+
@pytest.mark.parametrize(
284+
"shapes",
285+
[
286+
[[(3,), (4,)]],
287+
[[(2, 3), (2,)]],
288+
[[(3,), (3,), (4,)]],
289+
[[(1, 3, 4), (2, 3, 3)]],
290+
],
291+
)
292+
def test_incompatible_shapes_raise_valueerror(self, shapes):
293+
for input_shapes in shapes:
294+
self.assert_broadcast_arrays_raise(input_shapes)
295+
self.assert_broadcast_arrays_raise(input_shapes[::-1])
296+
297+
def test_broadcast_arrays_empty_input(self):
298+
assert dpnp.broadcast_arrays() == []
299+
300+
def test_subok_error(self):
301+
x = dpnp.ones((4))
302+
with pytest.raises(NotImplementedError):
303+
dpnp.broadcast_arrays(x, subok=True)
304+
305+
with pytest.raises(NotImplementedError):
306+
dpnp.broadcast_to(x, (4, 4), subok=True)
307+
308+
135309
class TestColumnStack:
136310
def test_non_iterable(self):
137311
with pytest.raises(TypeError):
@@ -987,180 +1161,6 @@ def test_generator(self):
9871161
dpnp.vstack(map(lambda x: x, dpnp.ones((3, 2))))
9881162

9891163

990-
@testing.with_requires("numpy<2.0")
991-
@pytest.mark.parametrize("dtype", get_all_dtypes())
992-
@pytest.mark.parametrize(
993-
"data", [[1, 2, 3], [1.0, 2.0, 3.0]], ids=["[1, 2, 3]", "[1., 2., 3.]"]
994-
)
995-
def test_asfarray(dtype, data):
996-
expected = numpy.asfarray(data, dtype)
997-
result = dpnp.asfarray(data, dtype)
998-
999-
assert_array_equal(result, expected)
1000-
1001-
1002-
@testing.with_requires("numpy<2.0")
1003-
@pytest.mark.usefixtures("suppress_complex_warning")
1004-
@pytest.mark.parametrize("dtype", get_all_dtypes())
1005-
@pytest.mark.parametrize("data", [[1.0, 2.0, 3.0]], ids=["[1., 2., 3.]"])
1006-
@pytest.mark.parametrize("data_dtype", get_all_dtypes(no_none=True))
1007-
def test_asfarray2(dtype, data, data_dtype):
1008-
expected = numpy.asfarray(numpy.array(data, dtype=data_dtype), dtype)
1009-
result = dpnp.asfarray(dpnp.array(data, dtype=data_dtype), dtype)
1010-
1011-
assert_array_equal(result, expected)
1012-
1013-
1014-
def assert_broadcast_correct(input_shapes):
1015-
np_arrays = [numpy.zeros(s, dtype="i1") for s in input_shapes]
1016-
out_np_arrays = numpy.broadcast_arrays(*np_arrays)
1017-
dpnp_arrays = [dpnp.asarray(Xnp) for Xnp in np_arrays]
1018-
out_dpnp_arrays = dpnp.broadcast_arrays(*dpnp_arrays)
1019-
for Xnp, X in zip(out_np_arrays, out_dpnp_arrays):
1020-
assert_array_equal(
1021-
Xnp, dpnp.asnumpy(X), err_msg=f"Failed for {input_shapes})"
1022-
)
1023-
1024-
1025-
def assert_broadcast_arrays_raise(input_shapes):
1026-
dpnp_arrays = [dpnp.asarray(numpy.zeros(s)) for s in input_shapes]
1027-
pytest.raises(ValueError, dpnp.broadcast_arrays, *dpnp_arrays)
1028-
1029-
1030-
def test_broadcast_arrays_same():
1031-
Xnp = numpy.arange(10)
1032-
Ynp = numpy.arange(10)
1033-
res_Xnp, res_Ynp = numpy.broadcast_arrays(Xnp, Ynp)
1034-
X = dpnp.asarray(Xnp)
1035-
Y = dpnp.asarray(Ynp)
1036-
res_X, res_Y = dpnp.broadcast_arrays(X, Y)
1037-
assert_array_equal(res_Xnp, dpnp.asnumpy(res_X))
1038-
assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y))
1039-
1040-
1041-
def test_broadcast_arrays_one_off():
1042-
Xnp = numpy.array([[1, 2, 3]])
1043-
Ynp = numpy.array([[1], [2], [3]])
1044-
res_Xnp, res_Ynp = numpy.broadcast_arrays(Xnp, Ynp)
1045-
X = dpnp.asarray(Xnp)
1046-
Y = dpnp.asarray(Ynp)
1047-
res_X, res_Y = dpnp.broadcast_arrays(X, Y)
1048-
assert_array_equal(res_Xnp, dpnp.asnumpy(res_X))
1049-
assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y))
1050-
1051-
1052-
@pytest.mark.parametrize(
1053-
"shapes",
1054-
[
1055-
(),
1056-
(1,),
1057-
(3,),
1058-
(0, 1),
1059-
(0, 3),
1060-
(1, 0),
1061-
(3, 0),
1062-
(1, 3),
1063-
(3, 1),
1064-
(3, 3),
1065-
],
1066-
)
1067-
def test_broadcast_arrays_same_shapes(shapes):
1068-
for shape in shapes:
1069-
single_input_shapes = [shape]
1070-
assert_broadcast_correct(single_input_shapes)
1071-
double_input_shapes = [shape, shape]
1072-
assert_broadcast_correct(double_input_shapes)
1073-
triple_input_shapes = [shape, shape, shape]
1074-
assert_broadcast_correct(triple_input_shapes)
1075-
1076-
1077-
@pytest.mark.parametrize(
1078-
"shapes",
1079-
[
1080-
[[(1,), (3,)]],
1081-
[[(1, 3), (3, 3)]],
1082-
[[(3, 1), (3, 3)]],
1083-
[[(1, 3), (3, 1)]],
1084-
[[(1, 1), (3, 3)]],
1085-
[[(1, 1), (1, 3)]],
1086-
[[(1, 1), (3, 1)]],
1087-
[[(1, 0), (0, 0)]],
1088-
[[(0, 1), (0, 0)]],
1089-
[[(1, 0), (0, 1)]],
1090-
[[(1, 1), (0, 0)]],
1091-
[[(1, 1), (1, 0)]],
1092-
[[(1, 1), (0, 1)]],
1093-
],
1094-
)
1095-
def test_broadcast_arrays_same_len_shapes(shapes):
1096-
# Check that two different input shapes of the same length, but some have
1097-
# ones, broadcast to the correct shape.
1098-
1099-
for input_shapes in shapes:
1100-
assert_broadcast_correct(input_shapes)
1101-
assert_broadcast_correct(input_shapes[::-1])
1102-
1103-
1104-
@pytest.mark.parametrize(
1105-
"shapes",
1106-
[
1107-
[[(), (3,)]],
1108-
[[(3,), (3, 3)]],
1109-
[[(3,), (3, 1)]],
1110-
[[(1,), (3, 3)]],
1111-
[[(), (3, 3)]],
1112-
[[(1, 1), (3,)]],
1113-
[[(1,), (3, 1)]],
1114-
[[(1,), (1, 3)]],
1115-
[[(), (1, 3)]],
1116-
[[(), (3, 1)]],
1117-
[[(), (0,)]],
1118-
[[(0,), (0, 0)]],
1119-
[[(0,), (0, 1)]],
1120-
[[(1,), (0, 0)]],
1121-
[[(), (0, 0)]],
1122-
[[(1, 1), (0,)]],
1123-
[[(1,), (0, 1)]],
1124-
[[(1,), (1, 0)]],
1125-
[[(), (1, 0)]],
1126-
[[(), (0, 1)]],
1127-
],
1128-
)
1129-
def test_broadcast_arrays_different_len_shapes(shapes):
1130-
# Check that two different input shapes (of different lengths) broadcast
1131-
# to the correct shape.
1132-
1133-
for input_shapes in shapes:
1134-
assert_broadcast_correct(input_shapes)
1135-
assert_broadcast_correct(input_shapes[::-1])
1136-
1137-
1138-
@pytest.mark.parametrize(
1139-
"shapes",
1140-
[
1141-
[[(3,), (4,)]],
1142-
[[(2, 3), (2,)]],
1143-
[[(3,), (3,), (4,)]],
1144-
[[(1, 3, 4), (2, 3, 3)]],
1145-
],
1146-
)
1147-
def test_incompatible_shapes_raise_valueerror(shapes):
1148-
for input_shapes in shapes:
1149-
assert_broadcast_arrays_raise(input_shapes)
1150-
assert_broadcast_arrays_raise(input_shapes[::-1])
1151-
1152-
1153-
def test_broadcast_arrays_empty_input():
1154-
assert dpnp.broadcast_arrays() == []
1155-
1156-
1157-
def test_subok_error():
1158-
x = dpnp.ones((4))
1159-
with pytest.raises(NotImplementedError):
1160-
dpnp.broadcast_arrays(x, subok=True)
1161-
dpnp.broadcast_to(x, (4, 4), subok=True)
1162-
1163-
11641164
def test_can_cast():
11651165
X = dpnp.ones((2, 2), dtype=dpnp.int64)
11661166
pytest.raises(TypeError, dpnp.can_cast, X, 1)

0 commit comments

Comments
 (0)