Skip to content

Commit 758532b

Browse files
authored
Merge branch 'master' into diag_vander_ptp_trace
2 parents 5b5439d + c31557c commit 758532b

File tree

1 file changed

+222
-38
lines changed

1 file changed

+222
-38
lines changed

tests/test_mathematical.py

+222-38
Original file line numberDiff line numberDiff line change
@@ -1070,9 +1070,9 @@ def test_invalid_out(self, out):
10701070
assert_raises(TypeError, numpy.add, a.asnumpy(), 2, out)
10711071

10721072

1073-
class TestHypot:
1074-
@pytest.mark.parametrize("dtype", get_float_dtypes())
1075-
def test_hypot(self, dtype):
1073+
class TestDivide:
1074+
@pytest.mark.parametrize("dtype", get_float_complex_dtypes())
1075+
def test_divide(self, dtype):
10761076
array1_data = numpy.arange(10)
10771077
array2_data = numpy.arange(5, 15)
10781078
out = numpy.empty(10, dtype=dtype)
@@ -1081,55 +1081,169 @@ def test_hypot(self, dtype):
10811081
dp_array1 = dpnp.array(array1_data, dtype=dtype)
10821082
dp_array2 = dpnp.array(array2_data, dtype=dtype)
10831083
dp_out = dpnp.array(out, dtype=dtype)
1084-
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1084+
result = dpnp.divide(dp_array1, dp_array2, out=dp_out)
10851085

10861086
# original
10871087
np_array1 = numpy.array(array1_data, dtype=dtype)
10881088
np_array2 = numpy.array(array2_data, dtype=dtype)
1089-
expected = numpy.hypot(np_array1, np_array2, out=out)
1089+
expected = numpy.divide(np_array1, np_array2, out=out)
10901090

1091-
assert_allclose(expected, result)
1092-
assert_allclose(out, dp_out)
1091+
assert_dtype_allclose(result, expected)
1092+
assert_dtype_allclose(dp_out, out)
10931093

1094-
@pytest.mark.parametrize("dtype", get_float_dtypes())
1094+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1095+
@pytest.mark.parametrize("dtype", get_float_complex_dtypes())
10951096
def test_out_dtypes(self, dtype):
10961097
size = 10
10971098

10981099
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
10991100
np_array2 = numpy.arange(size, dtype=dtype)
1100-
np_out = numpy.empty(size, dtype=numpy.float32)
1101-
expected = numpy.hypot(np_array1, np_array2, out=np_out)
1101+
np_out = numpy.empty(size, dtype=numpy.complex64)
1102+
expected = numpy.divide(np_array1, np_array2, out=np_out)
11021103

11031104
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
11041105
dp_array2 = dpnp.arange(size, dtype=dtype)
11051106

1106-
dp_out = dpnp.empty(size, dtype=dpnp.float32)
1107-
if dtype != dpnp.float32:
1107+
dp_out = dpnp.empty(size, dtype=dpnp.complex64)
1108+
if dtype != dpnp.complex64:
11081109
# dtype of out mismatches types of input arrays
11091110
with pytest.raises(TypeError):
1110-
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1111+
dpnp.divide(dp_array1, dp_array2, out=dp_out)
11111112

11121113
# allocate new out with expected type
11131114
dp_out = dpnp.empty(size, dtype=dtype)
11141115

1115-
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1116+
result = dpnp.divide(dp_array1, dp_array2, out=dp_out)
1117+
assert_dtype_allclose(result, expected)
11161118

1117-
tol = numpy.finfo(numpy.float32).resolution
1118-
assert_allclose(expected, result, rtol=tol, atol=tol)
1119+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1120+
@pytest.mark.parametrize("dtype", get_float_complex_dtypes())
1121+
def test_out_overlap(self, dtype):
1122+
size = 15
1123+
# DPNP
1124+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1125+
dpnp.divide(dp_a[size::], dp_a[::2], out=dp_a[:size:])
11191126

1120-
@pytest.mark.parametrize("dtype", get_float_dtypes())
1127+
# original
1128+
np_a = numpy.arange(2 * size, dtype=dtype)
1129+
numpy.divide(np_a[size::], np_a[::2], out=np_a[:size:])
1130+
1131+
assert_dtype_allclose(dp_a, np_a)
1132+
1133+
@pytest.mark.parametrize("dtype", get_float_complex_dtypes())
1134+
def test_inplace_strided_out(self, dtype):
1135+
size = 21
1136+
1137+
np_a = numpy.arange(size, dtype=dtype)
1138+
np_a[::3] /= 4
1139+
1140+
dp_a = dpnp.arange(size, dtype=dtype)
1141+
dp_a[::3] /= 4
1142+
1143+
assert_allclose(dp_a, np_a)
1144+
1145+
@pytest.mark.parametrize(
1146+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1147+
)
1148+
def test_invalid_shape(self, shape):
1149+
dp_array1 = dpnp.arange(10)
1150+
dp_array2 = dpnp.arange(5, 15)
1151+
dp_out = dpnp.empty(shape)
1152+
1153+
with pytest.raises(ValueError):
1154+
dpnp.divide(dp_array1, dp_array2, out=dp_out)
1155+
1156+
@pytest.mark.parametrize(
1157+
"out",
1158+
[4, (), [], (3, 7), [2, 4]],
1159+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1160+
)
1161+
def test_invalid_out(self, out):
1162+
a = dpnp.arange(10)
1163+
1164+
assert_raises(TypeError, dpnp.divide, a, 2, out)
1165+
assert_raises(TypeError, numpy.divide, a.asnumpy(), 2, out)
1166+
1167+
1168+
class TestFloorDivide:
1169+
@pytest.mark.parametrize(
1170+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1171+
)
1172+
def test_floor_divide(self, dtype):
1173+
array1_data = numpy.arange(10)
1174+
array2_data = numpy.arange(5, 15)
1175+
out = numpy.empty(10, dtype=dtype)
1176+
1177+
# DPNP
1178+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1179+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1180+
dp_out = dpnp.array(out, dtype=dtype)
1181+
result = dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1182+
1183+
# original
1184+
np_array1 = numpy.array(array1_data, dtype=dtype)
1185+
np_array2 = numpy.array(array2_data, dtype=dtype)
1186+
expected = numpy.floor_divide(np_array1, np_array2, out=out)
1187+
1188+
assert_allclose(result, expected)
1189+
assert_allclose(dp_out, out)
1190+
1191+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1192+
@pytest.mark.parametrize(
1193+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1194+
)
1195+
def test_out_dtypes(self, dtype):
1196+
size = 10
1197+
1198+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1199+
np_array2 = numpy.arange(size, dtype=dtype)
1200+
np_out = numpy.empty(size, dtype=numpy.complex64)
1201+
expected = numpy.floor_divide(np_array1, np_array2, out=np_out)
1202+
1203+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1204+
dp_array2 = dpnp.arange(size, dtype=dtype)
1205+
1206+
dp_out = dpnp.empty(size, dtype=dpnp.complex64)
1207+
if dtype != dpnp.complex64:
1208+
# dtype of out mismatches types of input arrays
1209+
with pytest.raises(TypeError):
1210+
dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1211+
1212+
# allocate new out with expected type
1213+
dp_out = dpnp.empty(size, dtype=dtype)
1214+
1215+
result = dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1216+
assert_allclose(result, expected)
1217+
1218+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1219+
@pytest.mark.parametrize(
1220+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1221+
)
11211222
def test_out_overlap(self, dtype):
11221223
size = 15
11231224
# DPNP
11241225
dp_a = dpnp.arange(2 * size, dtype=dtype)
1125-
dpnp.hypot(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1226+
dpnp.floor_divide(dp_a[size::], dp_a[::2], out=dp_a[:size:])
11261227

11271228
# original
11281229
np_a = numpy.arange(2 * size, dtype=dtype)
1129-
numpy.hypot(np_a[size::], np_a[::2], out=np_a[:size:])
1230+
numpy.floor_divide(np_a[size::], np_a[::2], out=np_a[:size:])
11301231

1131-
tol = numpy.finfo(numpy.float32).resolution
1132-
assert_allclose(np_a, dp_a, rtol=tol, atol=tol)
1232+
assert_allclose(dp_a, np_a)
1233+
1234+
@pytest.mark.parametrize(
1235+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1236+
)
1237+
def test_inplace_strided_out(self, dtype):
1238+
size = 21
1239+
1240+
np_a = numpy.arange(size, dtype=dtype)
1241+
np_a[::3] //= 4
1242+
1243+
dp_a = dpnp.arange(size, dtype=dtype)
1244+
dp_a[::3] //= 4
1245+
1246+
assert_allclose(dp_a, np_a)
11331247

11341248
@pytest.mark.parametrize(
11351249
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
@@ -1140,7 +1254,7 @@ def test_invalid_shape(self, shape):
11401254
dp_out = dpnp.empty(shape)
11411255

11421256
with pytest.raises(ValueError):
1143-
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1257+
dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
11441258

11451259
@pytest.mark.parametrize(
11461260
"out",
@@ -1150,8 +1264,8 @@ def test_invalid_shape(self, shape):
11501264
def test_invalid_out(self, out):
11511265
a = dpnp.arange(10)
11521266

1153-
assert_raises(TypeError, dpnp.hypot, a, 2, out)
1154-
assert_raises(TypeError, numpy.hypot, a.asnumpy(), 2, out)
1267+
assert_raises(TypeError, dpnp.floor_divide, a, 2, out)
1268+
assert_raises(TypeError, numpy.floor_divide, a.asnumpy(), 2, out)
11551269

11561270

11571271
class TestFmax:
@@ -1316,6 +1430,90 @@ def test_invalid_out(self, out):
13161430
assert_raises(TypeError, numpy.fmin, a.asnumpy(), 2, out)
13171431

13181432

1433+
class TestHypot:
1434+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1435+
def test_hypot(self, dtype):
1436+
array1_data = numpy.arange(10)
1437+
array2_data = numpy.arange(5, 15)
1438+
out = numpy.empty(10, dtype=dtype)
1439+
1440+
# DPNP
1441+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1442+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1443+
dp_out = dpnp.array(out, dtype=dtype)
1444+
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1445+
1446+
# original
1447+
np_array1 = numpy.array(array1_data, dtype=dtype)
1448+
np_array2 = numpy.array(array2_data, dtype=dtype)
1449+
expected = numpy.hypot(np_array1, np_array2, out=out)
1450+
1451+
assert_allclose(expected, result)
1452+
assert_allclose(out, dp_out)
1453+
1454+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1455+
def test_out_dtypes(self, dtype):
1456+
size = 10
1457+
1458+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1459+
np_array2 = numpy.arange(size, dtype=dtype)
1460+
np_out = numpy.empty(size, dtype=numpy.float32)
1461+
expected = numpy.hypot(np_array1, np_array2, out=np_out)
1462+
1463+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1464+
dp_array2 = dpnp.arange(size, dtype=dtype)
1465+
1466+
dp_out = dpnp.empty(size, dtype=dpnp.float32)
1467+
if dtype != dpnp.float32:
1468+
# dtype of out mismatches types of input arrays
1469+
with pytest.raises(TypeError):
1470+
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1471+
1472+
# allocate new out with expected type
1473+
dp_out = dpnp.empty(size, dtype=dtype)
1474+
1475+
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1476+
1477+
tol = numpy.finfo(numpy.float32).resolution
1478+
assert_allclose(expected, result, rtol=tol, atol=tol)
1479+
1480+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1481+
def test_out_overlap(self, dtype):
1482+
size = 15
1483+
# DPNP
1484+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1485+
dpnp.hypot(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1486+
1487+
# original
1488+
np_a = numpy.arange(2 * size, dtype=dtype)
1489+
numpy.hypot(np_a[size::], np_a[::2], out=np_a[:size:])
1490+
1491+
tol = numpy.finfo(numpy.float32).resolution
1492+
assert_allclose(np_a, dp_a, rtol=tol, atol=tol)
1493+
1494+
@pytest.mark.parametrize(
1495+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1496+
)
1497+
def test_invalid_shape(self, shape):
1498+
dp_array1 = dpnp.arange(10)
1499+
dp_array2 = dpnp.arange(5, 15)
1500+
dp_out = dpnp.empty(shape)
1501+
1502+
with pytest.raises(ValueError):
1503+
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1504+
1505+
@pytest.mark.parametrize(
1506+
"out",
1507+
[4, (), [], (3, 7), [2, 4]],
1508+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1509+
)
1510+
def test_invalid_out(self, out):
1511+
a = dpnp.arange(10)
1512+
1513+
assert_raises(TypeError, dpnp.hypot, a, 2, out)
1514+
assert_raises(TypeError, numpy.hypot, a.asnumpy(), 2, out)
1515+
1516+
13191517
class TestMaximum:
13201518
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
13211519
def test_maximum(self, dtype):
@@ -1846,17 +2044,3 @@ def test_inplace_remainder(dtype):
18462044
dp_a %= 4
18472045

18482046
assert_allclose(dp_a, np_a)
1849-
1850-
1851-
@pytest.mark.parametrize(
1852-
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1853-
)
1854-
def test_inplace_floor_divide(dtype):
1855-
size = 21
1856-
np_a = numpy.arange(size, dtype=dtype)
1857-
dp_a = dpnp.arange(size, dtype=dtype)
1858-
1859-
np_a //= 4
1860-
dp_a //= 4
1861-
1862-
assert_allclose(dp_a, np_a)

0 commit comments

Comments
 (0)