|
65 | 65 | "iscomplexobj",
|
66 | 66 | "isfinite",
|
67 | 67 | "isfortran",
|
| 68 | + "isin", |
68 | 69 | "isinf",
|
69 | 70 | "isnan",
|
70 | 71 | "isneginf",
|
@@ -1115,6 +1116,93 @@ def isfortran(a):
|
1115 | 1116 | return a.flags.fnc
|
1116 | 1117 |
|
1117 | 1118 |
|
| 1119 | +def isin(element, test_elements, assume_unique=False, invert=False): |
| 1120 | + """ |
| 1121 | + Calculates ``element in test_elements``, broadcasting over `element` only. |
| 1122 | + Returns a boolean array of the same shape as `element` that is True |
| 1123 | + where an element of `element` is in `test_elements` and False otherwise. |
| 1124 | +
|
| 1125 | + Parameters |
| 1126 | + ---------- |
| 1127 | + element : {array_like, dpnp.ndarray, usm_ndarray} |
| 1128 | + Input array. |
| 1129 | + test_elements : {array_like, dpnp.ndarray, usm_ndarray} |
| 1130 | + The values against which to test each value of `element`. |
| 1131 | + This argument is flattened if it is an array or array_like. |
| 1132 | + See notes for behavior with non-array-like parameters. |
| 1133 | + assume_unique : bool, optional |
| 1134 | + Ignored |
| 1135 | + invert : bool, optional |
| 1136 | + If True, the values in the returned array are inverted, as if |
| 1137 | + calculating `element not in test_elements`. Default is False. |
| 1138 | + ``dpnp.isin(a, b, invert=True)`` is equivalent to (but faster |
| 1139 | + than) ``dpnp.invert(dpnp.isin(a, b))``. |
| 1140 | +
|
| 1141 | +
|
| 1142 | + Returns |
| 1143 | + ------- |
| 1144 | + isin : dpnp.ndarray of bool dtype |
| 1145 | + Has the same shape as `element`. The values `element[isin]` |
| 1146 | + are in `test_elements`. |
| 1147 | +
|
| 1148 | +
|
| 1149 | + Examples |
| 1150 | + -------- |
| 1151 | + >>> import dpnp as np |
| 1152 | + >>> element = 2*np.arange(4).reshape((2, 2)) |
| 1153 | + >>> element |
| 1154 | + array([[0, 2], |
| 1155 | + [4, 6]]) |
| 1156 | + >>> test_elements = [1, 2, 4, 8] |
| 1157 | + >>> mask = np.isin(element, test_elements) |
| 1158 | + >>> mask |
| 1159 | + array([[False, True], |
| 1160 | + [ True, False]]) |
| 1161 | + >>> element[mask] |
| 1162 | + array([2, 4]) |
| 1163 | +
|
| 1164 | + The indices of the matched values can be obtained with `nonzero`: |
| 1165 | +
|
| 1166 | + >>> np.nonzero(mask) |
| 1167 | + (array([0, 1]), array([1, 0])) |
| 1168 | +
|
| 1169 | + The test can also be inverted: |
| 1170 | +
|
| 1171 | + >>> mask = np.isin(element, test_elements, invert=True) |
| 1172 | + >>> mask |
| 1173 | + array([[ True, False], |
| 1174 | + [False, True]]) |
| 1175 | + >>> element[mask] |
| 1176 | + array([0, 6]) |
| 1177 | +
|
| 1178 | + """ |
| 1179 | + |
| 1180 | + dpnp.check_supported_arrays_type(element, test_elements, scalar_type=True) |
| 1181 | + if dpnp.isscalar(element): |
| 1182 | + usm_element = dpt.asarray( |
| 1183 | + element, |
| 1184 | + sycl_queue=test_elements.sycl_queue, |
| 1185 | + usm_type=test_elements.usm_type, |
| 1186 | + ) |
| 1187 | + elif dpnp.isscalar(test_elements): |
| 1188 | + usm_test = dpt.asarray( |
| 1189 | + test_elements, |
| 1190 | + sycl_queue=element.sycl_queue, |
| 1191 | + usm_type=element.usm_type, |
| 1192 | + ) |
| 1193 | + else: |
| 1194 | + usm_element = dpnp.get_usm_ndarray(element) |
| 1195 | + usm_test = dpnp.get_usm_ndarray(test_elements) |
| 1196 | + return dpnp.get_result_array( |
| 1197 | + dpt.isin( |
| 1198 | + usm_element, |
| 1199 | + usm_test, |
| 1200 | + assume_unique=assume_unique, |
| 1201 | + invert=invert, |
| 1202 | + ) |
| 1203 | + ) |
| 1204 | + |
| 1205 | + |
1118 | 1206 | _ISINF_DOCSTRING = """
|
1119 | 1207 | Tests each element :math:`x_i` of the input array `x` to determine if equal to
|
1120 | 1208 | positive or negative infinity.
|
|
0 commit comments