'Scipy.optimize.newton for complex valued function

I'm trying to find a root using scipy.optimize.newton for a complex valued function. The function works great when I pass the derivative of the function as one of the arguments thus the Newton-Raphson method is used, however when I do not pass the derivative and try to use the secant method with only one inital guess (it works if a second inital guess is passed thus the problematic line is skipped), it generates the following error:

# I understand the error I was just wondering if there's a workaround.
x, r = optimize.newton(f_complex, x0=complex(5, -2), tol=1e-9, full_output=True)

# Error
File "C:\...\Python\Python310\lib\site-packages\scipy\optimize\zeros.py", line 325, in newton
p1 += (eps if p1 >= 0 else -eps)
TypeError: '>=' not supported between instances of 'complex' and 'int'

This is what I've tried:

from scipy import optimize

def f(z):
    return z**2 + 1 

def fp(z):
    return 2 * z

# works
x, r = x, r = optimize.newton(f, x0=complex(5, -2), fprime=fp, tol=1e-9, full_output=True)
print(r)

# does not work
x, r = x, r = optimize.newton(f, x0=complex(5, -2), tol=1e-9, full_output=True)
print(r)

#  works if I pass a second initial guess thus line #325 is skipped
x, r = x, r = optimize.newton(f, x0=complex(5, -2), tol=1e-9, full_output=True, x1=complex(4, -1.5))
print(r)

My question is:

Does the internal secant method work for complex valued functions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source