pythonsympy

How can I rewrite the complex number z = 5^i into standard form z=cos(log(5)) + i * sin(log(5)) with SymPy?


I would like to write complex numbers z into the standard form z = a + i b with a and b real numbers.

For most of my cases, the sympy construct z.expand(complex=True) does what I am expecting but not in all cases. For instance, I fail to rewrite z = 5**sp.I and SymPy just gives back the input:

In [1]: import sympy as sp

In [2]: c1 = 2 * sp.sqrt(2) * sp.exp(-3 * sp.pi * sp.I / 4)

In [3]: c1.expand(complex=True)  # works as expected
Out[3]: -2 - 2*I

In [4]: c2 = 5**(sp.I)           # SymPy fails here

In [5]: c2.expand(complex=True)
Out[5]: re(5**I) + I*im(5**I)

In [6]: sp.__version__
Out[6]: '1.13.2'

For c2, I would expect the conversion to give me cos(log(5)) + i * sin(log(5)). Is there a way to obtain this result?


Solution

  • First convert it to exponential form, then convert to trig.

    from sympy import I, cos, exp
    expr = 5 ** I
    expr = expr.rewrite(exp).rewrite(cos)
    print( expr )
    

    Output:

    cos(log(5)) + I*sin(log(5))