sympycomplex-numbers

Solving equations involving complex conjugates


I am trying to solve a system of equations using the Sympy solve() method, and my equations involve complex conjugates of the variables. This results in solve() sometimes not finding a solution which I know should be there, and throwing errors at other times.

I think my issue comes down to the following situation.

from sympy import symbols, conjugate, solve, pprint
x = symbols('x')
x_conjugate = conjugate(x)

solution_1 = solve(x+x_conjugate,x)
pprint(solution_1)

solution_2 = solve(x-x_conjugate,x)
pprint(solution_2)

I am not exactly sure what I expect the format of the output to be for these, but certainly the equation x+conjugate(x)=0 has solutions (the whole i-axis in the complex plane), and the equation x-conjugate(x)=0 has the whole real line as solutions.

How can I use Sympy to solve equations like these, or systems that involve equations like these?

I see this question, which gives a wonky work-around, but this seems unsatisfactory somewhat impractical when dealing with the kinds of things I want to do. How to solve complex equations in python?


Solution

  • I defined x as complex and proceeded as:

    x = symbols('x', complex=True)
    >>> solve((x-conjugate(x)).as_real_imag(),x)
    [{x: re(x), im(x): 0}]
    

    The as_real_imag() method splits the expression into real and imaginary portions which must both be zero. If you define a and b as real symbols and construct x = a+I*b you may have better luck with the "direct" solving approach:

    enter image description here