Example #1
0
def test_guess_symmetry_1(symmetry):
	C = Circle(0, 3)
	f = lambda z: z**4 + z**3 + z**2 + z

	roots = [0,-1,1j,-1j]
	multiplicities = [1,1,1,1]

	roots_approx_equal(C.roots(f, verbose=True, guessRootSymmetry=symmetry), (roots, multiplicities))
Example #2
0
def test_reevaluation_of_N():
    from cxroots import Circle
    C = Circle(0, 2)
    f = lambda z: (z - 1) * (z - 0.2)**2

    roots = [1, 0.2]
    multiplicities = [1, 2]
    roots_approx_equal(
        C.roots(f, NIntAbsTol=10, intMethod='romb', verbose=True),
        (roots, multiplicities))
Example #3
0
def test_guess_root(guesses):
	C = Circle(0, 3)
	f = lambda z: (z-2.5)**2 * (exp(-z)*sin(z/2.) - 1.2*cos(z))

	roots = [2.5,
			 1.44025113016670301345110737, 
			 -0.974651035111059787741822566 - 1.381047768247156339633038236j,
			 -0.974651035111059787741822566 + 1.381047768247156339633038236j]
	multiplicities = [2,1,1,1]

	roots_approx_equal(C.roots(f, guessRoots=[2.5], verbose=True), (roots, multiplicities))
Example #4
0
    def setUp(self):
        self.roots = roots = [0, -1.234, 1 + 1j, 1 - 1j, 2.345]
        self.multiplicities = [1, 1, 1, 1, 1]
        self.f = lambda z: (z - roots[0]) * (z - roots[1]) * (z - roots[2]) * (
            z - roots[3]) * (z - roots[4])
        self.df = lambda z: (z - roots[1]) * (z - roots[2]) * (z - roots[
            3]) * (z - roots[4]) + (z - roots[0]) * (z - roots[2]) * (
                z - roots[3]) * (z - roots[4]) + (z - roots[0]) * (z - roots[
                    1]) * (z - roots[3]) * (z - roots[4]) + (z - roots[0]) * (
                        z - roots[1]) * (z - roots[2]) * (z - roots[4]) + (
                            z - roots[0]) * (z - roots[1]) * (z - roots[2]) * (
                                z - roots[3])

        self.Circle = Circle(0, 3)
        self.Rectangle = Rectangle([-2, 2], [-2, 2])
        self.halfAnnulus = AnnulusSector(0, [0.5, 3], [-pi / 2, pi / 2])
        self.Annulus = Annulus(0, [1, 2])
Example #5
0
    def setUp(self):
        # Ex 1.4.1 from [KB]
        self.C = Circle(0, 3)
        e = 1e-2
        self.f = lambda z: (z - e) * (1 + (z - sqrt(3))**2)
        self.df = lambda z: (1 +
                             (z - sqrt(3))**2) + (z - e) * 2 * (z - sqrt(3))

        self.roots = [e, sqrt(3) + 1j, sqrt(3) - 1j]
        self.multiplicities = [1, 1, 1]
def calcInt():
    plaTrue = ε > -1.0

    if plaTrue:
        Int = open(f"eps_{ε}_int", "w")
        Pla = open(f"eps_{ε}_pla", "w")
    else:
        Int = open(f"eps_{ε}_int", "w")

    for m in range(65):
        print(f"m = {m}")

        f0 = lambda k: ivp(m, η * k) * hankel1(m, k) / η + iv(m, η * k) * h1vp(
            m, k)
        f1 = (lambda k: ivp(m, η * k, 2) * hankel1(m, k) + c * ivp(m, η * k) *
              h1vp(m, k) + iv(m, η * k) * h1vp(m, k, 2))

        t = np.linspace(0.2, 65.0, num=1024)
        k = 1j * t
        rf = np.real(f0(k))

        ind = np.where(rf[1:] * rf[:-1] < 0.0)[0]
        roots = np.zeros(np.shape(ind), dtype=complex)
        for a, i in enumerate(ind):
            C = Circle(center=1j * (t[i] + t[i + 1]) / 2.0,
                       radius=(t[i + 1] - t[i]))
            z = C.roots(f0, df=f1)
            roots[a] = z.roots[0]

        if plaTrue:
            if m:
                writeFile(Int, m, roots[1:])
                writeFile(Pla, m, roots[[0]])
            else:
                writeFile(Int, m, roots)
        else:
            writeFile(Int, m, roots)

    if plaTrue:
        Int.close()
        Pla.close()
    else:
        Int.close()
Example #7
0
    def setUp(self):
        # Ex 1.4.2 from [KB]
        self.C = Circle(0, 2)
        self.f = lambda z: exp(3 * z) + 2 * z * cos(z) - 1
        self.df = lambda z: 3 * exp(3 * z) + 2 * cos(z) - 2 * z * sin(z)

        self.roots = [
            0, -1.844233953262213, 0.5308949302929305 + 1.33179187675112098j,
            0.5308949302929305 - 1.33179187675112098j
        ]
        self.multiplicities = [1, 1, 1, 1]
Example #8
0
    def setUp(self):
        # Ex 1.4.5 from [KB]
        self.C = Circle(0, 11)
        self.f = lambda z: np.prod([z - k for k in range(1, 11)], axis=0)
        self.df = lambda z: np.sum([
            np.prod([z - k for k in range(1, 11) if k != m], axis=0)
            for m in range(1, 11)
        ],
                                   axis=0)

        self.roots = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        self.multiplicities = np.ones(10)
Example #9
0
    def setUp(self):
        # Ex 1.4.3 from [KB]
        self.C = Circle(0, 5)
        self.f = lambda z: z**2 * (z - 1) * (z - 2) * (z - 3) * (z - 4
                                                                 ) + z * sin(z)
        self.df = lambda z: 2 * z * (3 * z**4 - 25 * z**3 + 70 * z**2 - 75 * z
                                     + 24) + sin(z) + z * cos(z)

        self.roots = [
            0, 1.18906588973011365517521756, 1.72843498616506284043592924,
            3.01990732809571222812005354, 4.03038191606046844562845941
        ]
        self.multiplicities = [2, 1, 1, 1, 1]
Example #10
0
def test_guess_symmetry_2(usedf):
	C = Circle(0, 1.5)
	f = lambda z: z**27-2*z**11+0.5*z**6-1
	df = (lambda z: 27*z**26-22*z**10+3*z**5) if usedf else None

	symmetry = lambda z: [z.conjugate()]

	roots = [-1.03509521179240, 
		  	 -0.920332541459108, 
		  	 1.05026721944263, 
		  	 -0.983563736801535 - 0.382365167035741j, 
		  	 -0.983563736801535 + 0.382365167035741j, 
		  	 -0.792214346729517 - 0.520708613101932j, 
		  	 -0.792214346729517 + 0.520708613101932j, 
		  	 -0.732229626596468 - 0.757345327222341j, 
		  	 -0.732229626596468 + 0.757345327222341j, 
		  	 -0.40289002582335 - 0.825650446354661j, 
		  	 -0.40289002582335 + 0.825650446354661j, 
		  	 -0.383382611408318 - 0.967939747947639j, 
		  	 -0.383382611408318 + 0.967939747947639j, 
		  	 -0.02594227096144 - 1.05524415820652j, 
		  	 -0.02594227096144 + 1.05524415820652j, 
		  	 0.160356899544475 - 0.927983420797727j, 
		  	 0.160356899544475 + 0.927983420797727j, 
		  	 0.41133738621461 - 0.967444751898913j, 
		  	 0.41133738621461 + 0.967444751898913j, 
		  	 0.576737152896681 - 0.719511178392941j, 
		  	 0.576737152896681 + 0.719511178392941j, 
		  	 0.758074415348703 - 0.724716122470435j, 
		  	 0.758074415348703 + 0.724716122470435j, 
		  	 0.903278407433416 - 0.22751872334709j, 
		  	 0.903278407433416 + 0.22751872334709j, 
		  	 0.963018623787179 - 0.427294816877434j, 
		  	 0.963018623787179 + 0.427294816877434j]
	
	multiplicities = np.ones_like(roots)

	roots_approx_equal(C.roots(f, df, verbose=True, guessRootSymmetry=symmetry), (roots, multiplicities))
Example #11
0
    def setUp(self):
        self.C = Circle(0, 3)
        self.f = lambda z: (z * (z + 2))**2 * (exp(2 * z) * cos(z) - 1 - sin(z)
                                               + z**5)
        self.df = lambda z: 2 * (exp(2 * z) * cos(z) - 1 - sin(z) + z**5) * (
            z**2 * (z + 2) + (z + 2)**2 * z) + (z * (z + 2))**2 * (2 * exp(
                2 * z) * cos(z) - exp(2 * z) * sin(z) - cos(z) + 5 * z**4)

        self.roots = [
            0, -2, 2.23755778246706002284084684,
            -0.6511140702635986824274097994 - 0.3904257190882864369857773146j,
            -0.6511140702635986824274097994 + 0.3904257190882864369857773146j,
            0.64857808095387581293067569277 - 1.35662268398824203963215495605j,
            0.64857808095387581293067569277 + 1.35662268398824203963215495605j
        ]
        self.multiplicities = [3, 2, 1, 1, 1, 1, 1]
Example #12
0
    def setUp(self):
        # Ex 1.4.4 from [KB]
        self.C = Circle(0, 3)
        self.f = lambda z: (z * (z - 2))**2 * (exp(2 * z) * cos(z) + z**3 - 1 -
                                               sin(z))
        self.df = lambda z: 2 * z * (z - 2)**2 * (exp(2 * z) * cos(
            z) + z**3 - 1 - sin(z)) + 2 * (z - 2) * z**2 * (exp(2 * z) * cos(
                z) + z**3 - 1 - sin(z)) + (z * (z - 2))**2 * (2 * exp(
                    2 * z) * cos(z) - exp(2 * z) * sin(z) + 3 * z**2 - cos(z))

        self.roots = [
            -0.4607141197289707542294459477 - 0.6254277693477682516688207854j,
            -0.4607141197289707542294459477 + 0.6254277693477682516688207854j,
            0, 2, 1.66468286974551654134568653
        ]
        self.multiplicities = [1, 1, 3, 2, 1]
Example #13
0
F = (Z**3 - 2 * Z**2 + 0.5 * Z + 1) / (1)

# Taking the derivative
FP = sym.diff(F, Z)


def fp(z):
    return complex(FP.evalf(subs={Z: z, C: c}))


print(FP)
#print(complex(FP.evalf(subs={Z: 4+1j, C: 1})))

# Finding the roots
roots = []
cont = Circle(0, 3)
zeroes = cont.roots(f)
zeroes.show()
print(zeroes)
numroots = len(zeroes[0])
for j in range(numroots):
    roots.append(complex(zeroes[0][j].real, zeroes[0][j].imag))
print(roots)

# Assign hues for each root
colours = [(90, 190, 155), (0, 190, 155), (150, 255, 255), (90, 190, 155),
           (90, 190, 155), (90, 190, 155)]

# Converting to latex
# Write the latex expression to file
exp = sym.Eq(FZ, F)
Example #14
0
from numpy import pi

from cxroots import Circle, Annulus, AnnulusSector, Rectangle

# make the contours for the tutorial
Circle(0, 2).show('circle.png')
Rectangle([-2,2],[-1,1]).show('rectangle.png')
Annulus(0, [1,2]).show('annulus.png')
AnnulusSector(0, [1,2], [0,pi]).show('annulussefig.png')
Example #15
0
from numpy import exp, cos, sin
f = lambda z: (exp(2 * z) * cos(z) - 1 - sin(z) + z**5) * (z * (z + 2))**2

from matplotlib import pyplot as plt
plt.figure(figsize=(4.8, 4.8), dpi=80)

from cxroots import Circle
C = Circle(0, 3)
roots = C.roots(f)
roots.show('readmeEx.png')
print(roots)
Example #16
0
from numpy import exp, sin, cos
from cxroots import Circle
C = Circle(0, 3)
f = lambda z: (exp(-z) * sin(z / 2) - 1.2 * cos(z)) * (z + 1.2) * (z - 2.5)**2
C.demo_roots(f,
             guessRoots=[2.5, -1.2],
             saveFile='guessRoots.gif',
             writer='imagemagick')
Example #17
0
from cxroots import Circle
circle = Circle(center=1, radius=0.5)
circle.show()
Example #18
0
from cxroots import Circle
C = Circle(0, 2)
f = lambda z: z**6 + z**3
df = lambda z: 6 * z**5 + 3 * z**2
r = C.roots(f, df)
r.show()
Example #19
0
import pytest
import numpy as np
from scipy import cos, sin

from cxroots import Circle, Rectangle
from cxroots import CxDerivative


@pytest.mark.parametrize('C', [
    pytest.param(Circle(0, 2), id='circle'),
    pytest.param(Rectangle([-1.5, 1.5], [-2, 2]), id='rect'),
    pytest.param(None, id='default')
])
def test_CxDerivative(C):
    f = lambda z: z**10 - 2 * z**5 + sin(z) * cos(z / 2)
    df = lambda z: 10 * (z**9 - z**4) + cos(z) * cos(z / 2) - 0.5 * sin(
        z) * sin(z / 2)

    z = np.array([-1.234, 0.3 + 1j, 0.1j, -0.9 - 0.5j])

    assert CxDerivative(f, z, n=1, contour=C) == pytest.approx(df(z))
Example #20
0
    pytest.param([3, 3.0001, 3.0002, 8, 8.0002, 8 + 0.0001j],
                 [1, 1, 1, 1, 1, 1],
                 id='cluster_10^-4',
                 marks=pytest.mark.slow),
    pytest.param([3, 3.00001, 3.00002, 8, 8.00002, 8 + 0.00001j],
                 [1, 1, 1, 1, 1, 1],
                 id='cluster_10^-5',
                 marks=[
                     pytest.mark.slow,
                     pytest.mark.xfail(reason='Cluster of roots too tight')
                 ])
]

contours = [
    pytest.param(Rectangle([2, 9], [-1, 1]), id='rect'),
    pytest.param(Circle(0, 8.5), id='circle')
]


@pytest.mark.parametrize('C', contours)
@pytest.mark.parametrize('roots,multiplicities', funcs)
def test_rootfinding_df(C, roots, multiplicities):
    f = lambda z: np.prod([z - r for r in roots], axis=0)
    df = lambda z: np.sum([
        np.prod([z - r for r in np.delete(roots, i)], axis=0)
        for i in range(len(roots))
    ],
                          axis=0)
    roots_approx_equal(C.roots(f, df, verbose=True), (roots, multiplicities))

Example #21
0
class TestRootfindingContours(unittest.TestCase):
    def setUp(self):
        self.roots = roots = [0, -1.234, 1 + 1j, 1 - 1j, 2.345]
        self.multiplicities = [1, 1, 1, 1, 1]
        self.f = lambda z: (z - roots[0]) * (z - roots[1]) * (z - roots[2]) * (
            z - roots[3]) * (z - roots[4])
        self.df = lambda z: (z - roots[1]) * (z - roots[2]) * (z - roots[
            3]) * (z - roots[4]) + (z - roots[0]) * (z - roots[2]) * (
                z - roots[3]) * (z - roots[4]) + (z - roots[0]) * (z - roots[
                    1]) * (z - roots[3]) * (z - roots[4]) + (z - roots[0]) * (
                        z - roots[1]) * (z - roots[2]) * (z - roots[4]) + (
                            z - roots[0]) * (z - roots[1]) * (z - roots[2]) * (
                                z - roots[3])

        self.Circle = Circle(0, 3)
        self.Rectangle = Rectangle([-2, 2], [-2, 2])
        self.halfAnnulus = AnnulusSector(0, [0.5, 3], [-pi / 2, pi / 2])
        self.Annulus = Annulus(0, [1, 2])

    def test_rootfinding_circle_fdf(self):
        roots_approx_equal(self.Circle.roots(self.f, self.df, verbose=True),
                           (self.roots, self.multiplicities),
                           decimal=7)

    def test_rootfinding_circle_f(self):
        roots_approx_equal(self.Circle.roots(self.f, self.df, verbose=True),
                           (self.roots, self.multiplicities),
                           decimal=7)

    def test_rootfinding_rectangle_fdf(self):
        roots_approx_equal(self.Rectangle.roots(self.f, self.df, verbose=True),
                           (self.roots[:-1], self.multiplicities[:-1]),
                           decimal=7)

    def test_rootfinding_rectangle_f(self):
        roots_approx_equal(self.Rectangle.roots(self.f, self.df, verbose=True),
                           (self.roots[:-1], self.multiplicities[:-1]),
                           decimal=7)

    def test_rootfinding_halfAnnulus_fdf(self):
        roots_approx_equal(self.halfAnnulus.roots(self.f,
                                                  self.df,
                                                  verbose=True),
                           (self.roots[2:], self.multiplicities[2:]),
                           decimal=7)

    def test_rootfinding_halfAnnulus_f(self):
        roots_approx_equal(self.halfAnnulus.roots(self.f,
                                                  self.df,
                                                  verbose=True),
                           (self.roots[2:], self.multiplicities[2:]),
                           decimal=7)

    def test_rootfinding_Annulus_fdf(self):
        roots_approx_equal(self.Annulus.roots(self.f, self.df, verbose=True),
                           (self.roots[1:-1], self.multiplicities[1:-1]),
                           decimal=7)

    def test_rootfinding_Annulus_f(self):
        roots_approx_equal(self.Annulus.roots(self.f, self.df, verbose=True),
                           (self.roots[1:-1], self.multiplicities[1:-1]),
                           decimal=7)
Example #22
0
from numpy import sin, cos 			
f  = lambda z: 1j*z**5 + z*sin(z)          	# Define f(z)
df = lambda z: 5j*z**4 + z*cos(z) + sin(z) 	# Define f'(z)

from cxroots import Circle 			
C = Circle(0, 2)		# Define a circle, centered at 0 and with radius 2
r = C.roots(f, df)		# Find the roots of f(z) within the circle 

r.show('tutorial_roots.png')
Example #23
0
from cxroots import Circle
from numpy import exp, cos, sin
f = lambda z: (z * (z + 2))**2 * (exp(2 * z) * cos(z) - 1 - sin(z) + z**5)
C = Circle(0, 3)

C.demo_roots(f, saveFile='rootsDemo.gif', writer='imagemagick')