The search for the square root of z coincides logically with the solution of the equation (x + i * y) ** 2 = z. So you can do just that:
from sympy import * z = -24-70*I x, y = symbols('x y', real=True) result = solve((x+I*y)**2 - z, (x, y))
Result [(-5, 7), (5, -7)]
For convenience, this can be wrapped as a function:
def my_sqrt(z): x, y = symbols('x y', real=True) sol = solve((x+I*y)**2 - z, (x, y)) return sol[0][0] + sol[0][1]*I
Now you can use my_sqrt(-24-70*I) and get -5 + 7*I
The same strategy helps in your quadratic equation example:
x, y = symbols('x y', real=True) z = x + I*y solve(z ** 2 + (1 + I) * z + (6 + 18 * I), (x, y))
Output: [(-3, 3), (2, -4)]
source share