pythonmathcomplex-numbers

Euler's identity in python


I am trying to calculate the Euler's in python with the following code:

import numpy as np
import cmath
x = 0
y = 1
z=complex(x,y)

out=complex(np.e**(np.pi*z.imag))
print(out)

But what I am getting is the following (and I should get -1 or something close to it with floating point errors)

$ python code.py
(23.140692632779263+0j)

Solution

  • import numpy as np
    
    x = 0
    y = 1
    z = complex(x, y)
    
    out = np.exp(np.pi * z)
    print(out)
    

    The issue might be because z.imag is being used directly. Since z is a complex number, it has both real and imaginary parts. To get the imaginary part, you can directly access z.imag, but it might not give you the desired result. Instead, you should use z directly in the exponentiation.

    This gives the desired output