Returns a bool array, where True if input element is real. If element has complex type with zero complex part, the return value for that element is True. Parameters ---------- x : array_like Input array. Returns ------- out : ndarray, bool Bool
(x)
| 245 | |
| 246 | @array_function_dispatch(_is_type_dispatcher) |
| 247 | def isreal(x): |
| 248 | """ |
| 249 | Returns a bool array, where True if input element is real. |
| 250 | |
| 251 | If element has complex type with zero complex part, the return value |
| 252 | for that element is True. |
| 253 | |
| 254 | Parameters |
| 255 | ---------- |
| 256 | x : array_like |
| 257 | Input array. |
| 258 | |
| 259 | Returns |
| 260 | ------- |
| 261 | out : ndarray, bool |
| 262 | Boolean array of same shape as `x`. |
| 263 | |
| 264 | Notes |
| 265 | ----- |
| 266 | `isreal` may behave unexpectedly for string or object arrays (see examples) |
| 267 | |
| 268 | See Also |
| 269 | -------- |
| 270 | iscomplex |
| 271 | isrealobj : Return True if x is not a complex type. |
| 272 | |
| 273 | Examples |
| 274 | -------- |
| 275 | >>> a = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j], dtype=complex) |
| 276 | >>> np.isreal(a) |
| 277 | array([False, True, True, True, True, False]) |
| 278 | |
| 279 | The function does not work on string arrays. |
| 280 | |
| 281 | >>> a = np.array([2j, "a"], dtype="U") |
| 282 | >>> np.isreal(a) # Warns about non-elementwise comparison |
| 283 | False |
| 284 | |
| 285 | Returns True for all elements in input array of ``dtype=object`` even if |
| 286 | any of the elements is complex. |
| 287 | |
| 288 | >>> a = np.array([1, "2", 3+4j], dtype=object) |
| 289 | >>> np.isreal(a) |
| 290 | array([ True, True, True]) |
| 291 | |
| 292 | isreal should not be used with object arrays |
| 293 | |
| 294 | >>> a = np.array([1+2j, 2+1j], dtype=object) |
| 295 | >>> np.isreal(a) |
| 296 | array([ True, True]) |
| 297 | |
| 298 | """ |
| 299 | return imag(x) == 0 |
| 300 | |
| 301 | |
| 302 | @array_function_dispatch(_is_type_dispatcher) |