|
8 | 8 |
|
9 | 9 | import dpnp
|
10 | 10 |
|
11 |
| -from .helper import get_all_dtypes |
| 11 | +from .helper import assert_dtype_allclose, get_all_dtypes |
12 | 12 |
|
13 | 13 |
|
14 | 14 | @pytest.mark.parametrize(
|
@@ -88,6 +88,73 @@ def test_max_min_NotImplemented(func):
|
88 | 88 | getattr(dpnp, func)(ia, initial=6)
|
89 | 89 |
|
90 | 90 |
|
| 91 | +class TestMean: |
| 92 | + @pytest.mark.parametrize("dtype", get_all_dtypes()) |
| 93 | + def test_mean_axis_tuple(self, dtype): |
| 94 | + dp_array = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) |
| 95 | + np_array = dpnp.asnumpy(dp_array) |
| 96 | + |
| 97 | + result = dpnp.mean(dp_array, axis=(0, 1)) |
| 98 | + expected = numpy.mean(np_array, axis=(0, 1)) |
| 99 | + assert_allclose(expected, result) |
| 100 | + |
| 101 | + @pytest.mark.parametrize("dtype", get_all_dtypes()) |
| 102 | + @pytest.mark.parametrize("axis", [0, 1, (0, 1)]) |
| 103 | + def test_mean_out(self, dtype, axis): |
| 104 | + dp_array = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype=dtype) |
| 105 | + np_array = dpnp.asnumpy(dp_array) |
| 106 | + |
| 107 | + expected = numpy.mean(np_array, axis=axis) |
| 108 | + result = dpnp.empty_like(dpnp.asarray(expected)) |
| 109 | + dpnp.mean(dp_array, axis=axis, out=result) |
| 110 | + assert_dtype_allclose(result, expected) |
| 111 | + |
| 112 | + @pytest.mark.parametrize("dtype", get_all_dtypes()) |
| 113 | + def test_mean_dtype(self, dtype): |
| 114 | + dp_array = dpnp.array([[0, 1, 2], [3, 4, 0]], dtype="i4") |
| 115 | + np_array = dpnp.asnumpy(dp_array) |
| 116 | + |
| 117 | + expected = numpy.mean(np_array, dtype=dtype) |
| 118 | + result = dpnp.mean(dp_array, dtype=dtype) |
| 119 | + assert_allclose(expected, result) |
| 120 | + |
| 121 | + @pytest.mark.usefixtures("suppress_invalid_numpy_warnings") |
| 122 | + @pytest.mark.parametrize("axis", [0, 1, (0, 1)]) |
| 123 | + @pytest.mark.parametrize("shape", [(2, 3), (2, 0), (0, 3)]) |
| 124 | + def test_mean_empty(self, axis, shape): |
| 125 | + dp_array = dpnp.empty(shape, dtype=dpnp.int64) |
| 126 | + np_array = dpnp.asnumpy(dp_array) |
| 127 | + |
| 128 | + result = dpnp.mean(dp_array, axis=axis) |
| 129 | + expected = numpy.mean(np_array, axis=axis) |
| 130 | + assert_allclose(expected, result) |
| 131 | + |
| 132 | + def test_mean_strided(self): |
| 133 | + dp_array = dpnp.array([-2, -1, 0, 1, 0, 2], dtype="f4") |
| 134 | + np_array = dpnp.asnumpy(dp_array) |
| 135 | + |
| 136 | + result = dpnp.mean(dp_array[::-1]) |
| 137 | + expected = numpy.mean(np_array[::-1]) |
| 138 | + assert_allclose(expected, result) |
| 139 | + |
| 140 | + result = dpnp.mean(dp_array[::2]) |
| 141 | + expected = numpy.mean(np_array[::2]) |
| 142 | + assert_allclose(expected, result) |
| 143 | + |
| 144 | + def test_mean_scalar(self): |
| 145 | + dp_array = dpnp.array(5) |
| 146 | + np_array = dpnp.asnumpy(dp_array) |
| 147 | + |
| 148 | + result = dp_array.mean() |
| 149 | + expected = np_array.mean() |
| 150 | + assert_allclose(expected, result) |
| 151 | + |
| 152 | + def test_mean_NotImplemented(func): |
| 153 | + ia = dpnp.arange(5) |
| 154 | + with pytest.raises(NotImplementedError): |
| 155 | + dpnp.mean(ia, where=False) |
| 156 | + |
| 157 | + |
91 | 158 | @pytest.mark.usefixtures("allow_fall_back_on_numpy")
|
92 | 159 | @pytest.mark.parametrize(
|
93 | 160 | "array",
|
|
0 commit comments