Currently,
[ldexp](https://numpy.org/doc/stable/reference/generated/numpy.ldexp.html)
throws a TypeError on a complex input:
```python3
import numpy as np
def naive_ldexp(x, n):
return x * 2**n
def new_ldexp(x, n):
if np.iscomplex(x):
y = np.empty_like(x)
y.real = np.ldexp(x.real, n)
y.imag = np.ldexp(x.imag, n)
return y
else:
return np.ldexp(x, n)
def main():
x = 2.2 + 3.3j
n = 3
print(naive_ldexp(x, n))
print(new_ldexp(x, n))
print(np.ldexp(x, n))
if __name__ == "__main__":
main()
```
```
(17.6+26.4j)
(17.6+26.4j)
Traceback (most recent call last):
File "test.py", line 34, in <module>
main()
File "test.py", line 30, in main
print(np.ldexp(x, n))
^^^^^^^^^^^^^^
TypeError: ufunc 'ldexp' not supported for the input types, and the inputs
could not be safely coerced to any supported types according to the casting
rule ''safe''
```
This can easily be solved by applying ldexp to the real and imaginary parts of
the input, as shown in the function new_ldexp above.
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: [email protected]