|
15 | 15 | from .third_party.cupy import testing
|
16 | 16 |
|
17 | 17 |
|
| 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 | + |
18 | 50 | class TestAtleast1d:
|
19 | 51 | def test_0D_array(self):
|
20 | 52 | a = dpnp.array(1)
|
@@ -132,6 +164,148 @@ def test_dpnp_dpt_array(self):
|
132 | 164 | assert_array_equal(res, desired)
|
133 | 165 |
|
134 | 166 |
|
| 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 | + |
135 | 309 | class TestColumnStack:
|
136 | 310 | def test_non_iterable(self):
|
137 | 311 | with pytest.raises(TypeError):
|
@@ -987,180 +1161,6 @@ def test_generator(self):
|
987 | 1161 | dpnp.vstack(map(lambda x: x, dpnp.ones((3, 2))))
|
988 | 1162 |
|
989 | 1163 |
|
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 |
| - |
1164 | 1164 | def test_can_cast():
|
1165 | 1165 | X = dpnp.ones((2, 2), dtype=dpnp.int64)
|
1166 | 1166 | pytest.raises(TypeError, dpnp.can_cast, X, 1)
|
|
0 commit comments