How to solve sin (z) = 2 in sympy?

Sympy works with complex numbers, so it’s possible to solve equations of type sin(z)=2. However, I can’t solve it. Anyone have an idea how to solve it in Sympy?

BTW, the solution takes the form of the following:

z=\frac{\pi}{2}+\ln(2\pm\sqrt{3})i

<code> z = \ frac {\ pi} {2} + \ ln (2 \ pm \ sqrt {3}) i </code>

I will add a very specialized method to solve this problem in Sympy, which can hardly be generalized:

from sympy import *
z=symbols('z')
r=re(sin(z)-2)
i=im(sin(z))
x,y=symbols('x,y',real=True)
eq1=r.subs({re(z):x,im(z):y})
eq2=i.subs({re(z):x,im(z):y})
solve((eq1,eq2),(x,y))

Output signal [(pi/2, log(-sqrt(3) + 2)), (pi/2, log(sqrt(3) + 2))]. Does anyone have a better solution?

+4
source share
2 answers

If you prefer a format log, use .rewrite(log)for example

In [4]: asin(2).rewrite(log)
Out[4]:
      ⎛  ___        ⎞
-ⅈ⋅log⎝╲╱ 3 ⋅ⅈ + 2⋅ⅈ⎠

Combining this with Games answer you can get:

In [3]: sols = solve(sin(z) - 2, z)

In [4]: sols
Out[4]: [π - asin(2), asin(2)]

In [5]: [i.rewrite(log) for i in sols]
Out[5]:
⎡         ⎛  ___        ⎞        ⎛  ___        ⎞⎤
⎣π + ⅈ⋅log⎝╲╱ 3 ⋅ⅈ + 2⋅ⅈ⎠, -ⅈ⋅log⎝╲╱ 3 ⋅ⅈ + 2⋅ⅈ⎠⎦

, , , sin 2*pi . SymPy , , sin(z + 2*pi*n) sin(z):

In [8]: n = Symbol('n', integer=True)

In [9]: sols = solve(sin(z + 2*pi*n) - 2, z)

In [10]: sols
Out[10]: [-2⋅π⋅n + asin(2), -2⋅π⋅n + π - asin(2)]

In [11]: [i.rewrite(log) for i in sols]
Out[11]:
⎡              ⎛  ___        ⎞                    ⎛  ___        ⎞⎤-2⋅π⋅n - ⅈ⋅log⎝╲╱ 3 ⋅ⅈ + 2⋅ⅈ⎠, -2⋅π⋅n + π + ⅈ⋅log⎝╲╱ 3 ⋅ⅈ + 2⋅ⅈ⎠⎦

n - .

+4

, :

sin(z) - 2 = 0

:

>>> from sympy.solvers import solve
>>> from sympy import *
>>> z = Symbol('z')
>>> solve(sin(z) - 2, z)
[pi - asin(2), asin(2)]
>>> asin(2).evalf()
1.5707963267949 - 1.31695789692482*I
+4

All Articles