Skip to content

Commit dcea437

Browse files
committed
in place divide and floor_divide
1 parent 79cb518 commit dcea437

File tree

1 file changed

+121
-22
lines changed

1 file changed

+121
-22
lines changed

tests/test_mathematical.py

Lines changed: 121 additions & 22 deletions
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_dtypes() + get_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,56 +1081,71 @@ 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+
tol = 1e-07
1092+
assert_allclose(expected, result, rtol=tol, atol=tol)
1093+
assert_allclose(out, dp_out, rtol=tol, atol=tol)
10931094

1094-
@pytest.mark.parametrize("dtype", get_float_dtypes())
1095+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1096+
@pytest.mark.parametrize("dtype", get_float_dtypes() + get_complex_dtypes())
10951097
def test_out_dtypes(self, dtype):
10961098
size = 10
10971099

10981100
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
10991101
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)
1102+
np_out = numpy.empty(size, dtype=numpy.complex64)
1103+
expected = numpy.divide(np_array1, np_array2, out=np_out)
11021104

11031105
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
11041106
dp_array2 = dpnp.arange(size, dtype=dtype)
11051107

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

11121114
# allocate new out with expected type
11131115
dp_out = dpnp.empty(size, dtype=dtype)
11141116

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

1117-
tol = numpy.finfo(numpy.float32).resolution
1119+
tol = 1e-07
11181120
assert_allclose(expected, result, rtol=tol, atol=tol)
11191121

1120-
@pytest.mark.parametrize("dtype", get_float_dtypes())
1122+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1123+
@pytest.mark.parametrize("dtype", get_float_dtypes() + get_complex_dtypes())
11211124
def test_out_overlap(self, dtype):
11221125
size = 15
11231126
# DPNP
11241127
dp_a = dpnp.arange(2 * size, dtype=dtype)
1125-
dpnp.hypot(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1128+
dpnp.divide(dp_a[size::], dp_a[::2], out=dp_a[:size:])
11261129

11271130
# original
11281131
np_a = numpy.arange(2 * size, dtype=dtype)
1129-
numpy.hypot(np_a[size::], np_a[::2], out=np_a[:size:])
1132+
numpy.divide(np_a[size::], np_a[::2], out=np_a[:size:])
11301133

1131-
tol = numpy.finfo(numpy.float32).resolution
1134+
tol = 1e-07
11321135
assert_allclose(np_a, dp_a, rtol=tol, atol=tol)
11331136

1137+
@pytest.mark.parametrize("dtype", get_float_dtypes() + get_complex_dtypes())
1138+
def test_inplace_strided_out(self, dtype):
1139+
size = 21
1140+
1141+
np_a = numpy.arange(size, dtype=dtype)
1142+
np_a[::3] /= 4
1143+
1144+
dp_a = dpnp.arange(size, dtype=dtype)
1145+
dp_a[::3] /= 4
1146+
1147+
assert_allclose(dp_a, np_a)
1148+
11341149
@pytest.mark.parametrize(
11351150
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
11361151
)
@@ -1140,7 +1155,7 @@ def test_invalid_shape(self, shape):
11401155
dp_out = dpnp.empty(shape)
11411156

11421157
with pytest.raises(ValueError):
1143-
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1158+
dpnp.divide(dp_array1, dp_array2, out=dp_out)
11441159

11451160
@pytest.mark.parametrize(
11461161
"out",
@@ -1150,8 +1165,8 @@ def test_invalid_shape(self, shape):
11501165
def test_invalid_out(self, out):
11511166
a = dpnp.arange(10)
11521167

1153-
assert_raises(TypeError, dpnp.hypot, a, 2, out)
1154-
assert_raises(TypeError, numpy.hypot, a.asnumpy(), 2, out)
1168+
assert_raises(TypeError, dpnp.divide, a, 2, out)
1169+
assert_raises(TypeError, numpy.divide, a.asnumpy(), 2, out)
11551170

11561171

11571172
class TestFmax:
@@ -1316,6 +1331,90 @@ def test_invalid_out(self, out):
13161331
assert_raises(TypeError, numpy.fmin, a.asnumpy(), 2, out)
13171332

13181333

1334+
class TestHypot:
1335+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1336+
def test_hypot(self, dtype):
1337+
array1_data = numpy.arange(10)
1338+
array2_data = numpy.arange(5, 15)
1339+
out = numpy.empty(10, dtype=dtype)
1340+
1341+
# DPNP
1342+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1343+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1344+
dp_out = dpnp.array(out, dtype=dtype)
1345+
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1346+
1347+
# original
1348+
np_array1 = numpy.array(array1_data, dtype=dtype)
1349+
np_array2 = numpy.array(array2_data, dtype=dtype)
1350+
expected = numpy.hypot(np_array1, np_array2, out=out)
1351+
1352+
assert_allclose(expected, result)
1353+
assert_allclose(out, dp_out)
1354+
1355+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1356+
def test_out_dtypes(self, dtype):
1357+
size = 10
1358+
1359+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1360+
np_array2 = numpy.arange(size, dtype=dtype)
1361+
np_out = numpy.empty(size, dtype=numpy.float32)
1362+
expected = numpy.hypot(np_array1, np_array2, out=np_out)
1363+
1364+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1365+
dp_array2 = dpnp.arange(size, dtype=dtype)
1366+
1367+
dp_out = dpnp.empty(size, dtype=dpnp.float32)
1368+
if dtype != dpnp.float32:
1369+
# dtype of out mismatches types of input arrays
1370+
with pytest.raises(TypeError):
1371+
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1372+
1373+
# allocate new out with expected type
1374+
dp_out = dpnp.empty(size, dtype=dtype)
1375+
1376+
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1377+
1378+
tol = numpy.finfo(numpy.float32).resolution
1379+
assert_allclose(expected, result, rtol=tol, atol=tol)
1380+
1381+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1382+
def test_out_overlap(self, dtype):
1383+
size = 15
1384+
# DPNP
1385+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1386+
dpnp.hypot(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1387+
1388+
# original
1389+
np_a = numpy.arange(2 * size, dtype=dtype)
1390+
numpy.hypot(np_a[size::], np_a[::2], out=np_a[:size:])
1391+
1392+
tol = numpy.finfo(numpy.float32).resolution
1393+
assert_allclose(np_a, dp_a, rtol=tol, atol=tol)
1394+
1395+
@pytest.mark.parametrize(
1396+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1397+
)
1398+
def test_invalid_shape(self, shape):
1399+
dp_array1 = dpnp.arange(10)
1400+
dp_array2 = dpnp.arange(5, 15)
1401+
dp_out = dpnp.empty(shape)
1402+
1403+
with pytest.raises(ValueError):
1404+
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1405+
1406+
@pytest.mark.parametrize(
1407+
"out",
1408+
[4, (), [], (3, 7), [2, 4]],
1409+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1410+
)
1411+
def test_invalid_out(self, out):
1412+
a = dpnp.arange(10)
1413+
1414+
assert_raises(TypeError, dpnp.hypot, a, 2, out)
1415+
assert_raises(TypeError, numpy.hypot, a.asnumpy(), 2, out)
1416+
1417+
13191418
class TestMaximum:
13201419
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
13211420
def test_maximum(self, dtype):

0 commit comments

Comments
 (0)