Finding complex roots from set of non-linear equations in python
When I encounter this type of problem I try to rewrite my function as an array of real and imaginary parts. For example, if f
is your function which takes complex input array x
(say x
has size 2, for simplicity)
from numpy import *
def f(x):
# Takes a complex-valued vector of size 2 and outputs a complex-valued vector of size 2
return [x[0]-3*x[1]+1j+2, x[0]+x[1]] # <-- for example
def real_f(x1):
# converts a real-valued vector of size 4 to a complex-valued vector of size 2
# outputs a real-valued vector of size 4
x = [x1[0]+1j*x1[1],x1[2]+1j*x1[3]]
actual_f = f(x)
return [real(actual_f[0]),imag(actual_f[0]),real(actual_f[1]),imag(actual_f[1])]
The new function, real_f
can be used in fsolve
: the real and imaginary parts of the function are simultaneously solved for, treating the real and imaginary parts of the input argument as independent.