$
cat pythonComplex01.py
import numpy as np
from cxroots import Circle
def count_zeros(f, df, contour_points):
"""
Counts zeros of f(z) within the region enclosed by contour_points.
f: The analytic function
df: The derivative of the function
contour_points: Array of complex numbers forming a closed loop
"""
# Evaluate f and f' at each point on the contour
fz = f(contour_points)
dfz = df(contour_points)
# Calculate the integrand: f'(z) / f(z)
integrand = dfz / fz
# Calculate dz (the difference between consecutive points)
dz = np.diff(contour_points, append=contour_points[0])
# Compute the contour integral using the trapezoidal rule
integral = np.sum(integrand * dz)
# The result should be an integer
return int(np.round((integral / (2j * np.pi)).real))
t = np.linspace(0, 2 * np.pi, 1000)
circle = 3 * np.exp(1j * t)
f = lambda z: z**4 + 4*z**3 - 8*z - 2
df = lambda z: 4*z**3 + 12*z**2 -8
print("The Argument Principle and the Logarithmic Derivative in Complex Analysis\n")
print("f(z) = z**4 + 4*z**3 - 8*z - 2 ; df/dz(z) = 4*z**3 + 12*z**2 -8\n")
print(f"Number of zeros in circle of radius 3: {count_zeros(f, df, circle)}")
# Define a circle of radius 3 centered at the origin
C = Circle(0, 3)
# Find all roots within the circle
roots = C.roots(f, df)
print(roots,"\n")
circle = 1 * np.exp(1j * t)
print(f"Number of zeros in circle of radius 1: {count_zeros(f, df, circle)}")
# Define a circle of radius 3 centered at the origin
C = Circle(0, 1)
# Find all roots within the circle
roots = C.roots(f, df)
print(roots)
$ python pythonComplex01.py
The Argument Principle and the Logarithmic Derivative in Complex Analysis
f(z) = z**4 + 4*z**3 - 8*z - 2 ; df/dz(z) = 4*z**3 + 12*z**2 -8
Number of zeros in circle of radius 3: 3
Multiplicity | Root
------------------------------------------------
1 | -1.741963784303 +0.000000000000i
1 | -0.258036215697 +0.000000000000i
1 | 1.334414218339 +0.000000000000i
Number of zeros in circle of radius 1: 1
Multiplicity | Root
------------------------------------------------
1 | -0.258036215697 +0.000000000000i
$ cat pythonComplex02.py
import numpy as np
from cxroots import Circle
def count_zeros(f, df, contour_points):
"""
Counts zeros of f(z) within the region enclosed by contour_points.
f: The analytic function
df: The derivative of the function
contour_points: Array of complex numbers forming a closed loop
"""
# Evaluate f and f' at each point on the contour
fz = f(contour_points)
dfz = df(contour_points)
# Calculate the integrand: f'(z) / f(z)
integrand = dfz / fz
# Calculate dz (the difference between consecutive points)
dz = np.diff(contour_points, append=contour_points[0])
# Compute the contour integral using the trapezoidal rule
integral = np.sum(integrand * dz)
# The result should be an integer
return int(np.round((integral / (2j * np.pi)).real))
t = np.linspace(0, 2 * np.pi, 1000)
circle = 3 * np.exp(1j * t)
f = lambda z: z**13 + 5*z + 2
df = lambda z: 13*z**12 + 5
print("The Argument Principle and the Logarithmic Derivative in Complex Analysis\n")
print("f(z) = z**13 +5*z +2 ; df/dz(z) = 13*z**12 + 5\n")
print(f"Number of zeros in circle of radius 3: {count_zeros(f, df, circle)}")
# Define a circle of radius 3 centered at the origin
C = Circle(0, 3)
# Find all roots within the circle
roots = C.roots(f, df)
print(roots)
circle = 1 * np.exp(1j * t)
print(f"Number of zeros in circle of radius 1: {count_zeros(f, df, circle)}")
# Define a circle of radius 3 centered at the origin
C = Circle(0, 1)
# Find all roots within the circle
roots = C.roots(f, df)
print(roots)
~/ComplexAnalysis
❯ python pythonComplex02.py
The Argument Principle and the Logarithmic Derivative in Complex Analysis
f(z) = z**13 +5*z +2 ; df/dz(z) = 13*z**12 + 5
Number of zeros in circle of radius 3: 13
RootError encountered when subdivding Annulus sector: center=0.000+0.000i, r0=1.129, r1=1.221, phi0=7.414, phi1=8.168 into:
Annulus sector: center=0.000+0.000i, r0=1.129, r1=1.156, phi0=7.414, phi1=8.168
Annulus sector: center=0.000+0.000i, r0=1.156, r1=1.221, phi0=7.414, phi1=8.168
Multiplicity | Root
------------------------------------------------
1 | -1.063154526530 -0.299112694677i
1 | -1.063154526530 +0.299112694677i
1 | -0.771405797002 -0.815087403529i
1 | -0.771405797002 +0.815087403529i
1 | -0.399998657881 +0.000000000000i
1 | -0.262722673288 -1.111020328751i
1 | -0.262722673288 +1.111020328751i
1 | 0.326597088580 -1.109580758477i
1 | 0.326597088580 +1.109580758477i
1 | 0.837706346436 -0.811700768559i
1 | 0.837706346436 +0.811700768559i
1 | 1.132978890745 +0.297012150385i
1 | 1.132978890745 -0.297012150385i
Number of zeros in circle of radius 1: 1
Multiplicity | Root
------------------------------------------------
1 | -0.399998657881 +0.000000000000i

