Storing roots of a complex function in an array in SciPy

PeteyCoco

I have the complex function below:

 import numpy as np
 import scipy as sp
 from scipy.special import jv, hankel1, jvp, h1vp, h2vp, gamma

 nr = 2
 m = 20

 def Dm(x):
     return nr * jvp(m,nr*x,1) * hankel1(m,x) - jv(m,nr*x) * h1vp(m,x,1)

I'm looking to find as many complex roots of Dm(x) that lie in the 4th quadrant of the complex plane as I can using newton() from scipy.optimize and then store them into a one dimensional array. The best method I can come up with is to brute force it by using newton() at regularly spaced intervals across a finite portion of the 4th quadrant, check if the root is a duplicate of a previous root, check if the root is indeed a root, and then store it into an array. Once that algorithm finishes I want to sort the array by increasing real components. My questions are:

(i) Can I create an array of undefined length that I can keep adding values to as I find them?

(ii) Can I plot the function in such a way that I can visualize the roots? The math says that they are all on one sheet of the complex plane.

(iii) Is there a better method for finding the roots? I feel that I'll miss a lot of roots in the domain with my method.

DrV

Some answers:

(i) Use a list. Arrays have fixed size. Appending to a list is a very cheap option. When you append a new root to the list, check that the previous root is not in the list by, e.g., calculating np.amin(np.abs(np.array(a)-b)) where a is the list of existing roots and b is the new root. If this value is very small, you have arrived at an existing root. (How small depends on the function. It cannot be 0.0, as then you are usually not recognizing the same root due to floating point and iteration inaccuracies.)

If you have a very large number of roots (thousands), you may want to sort them as soon as you receive them. This makes searching the matching roots faster. On the other hand, most probably >90% of the time is spent in iterating the roots, and you do not need to worry about other performance issues. Then you just compile the list, sort it (list sorting is easy and fast) and convert into an array, if you need.

(ii) Yes. Two examples below: (For countour stuff, thank you belongs to Warren Weckesser and his very good answer!)

import numpy as np
from scipy.special import jv, hankel1, jvp, h1vp
import matplotlib.pyplot as plt


nr = 2
m = 20

# create a 2000 x 2000 sample complex plane between -2-2i .. +2+2i
x = np.linspace(-2, 2, 2000)
y = np.linspace(-2, 2, 2000)
X, Y = np.meshgrid(x, y)
C = X + 1j * Y 
z = 1-C**2

# draw a contour image of the imaginary part (red) and real part (blue)
csr = plt.contour(x, y, z.real, 5, colors='b')
plt.clabel(csr)
csi = plt.contour(x, y, z.imag, 5, colors='r')
plt.clabel(csi)
plt.axis('equal')
plt.savefig('contours.png')

# draw an image of the absolute value of the function, black representing zeros
plt.figure()
plt.imshow(abs(z), extent=[-2,2,-2,2], cmap=plt.cm.gray)
plt.axis('equal')
plt.savefig('absval.png')

This gives countours.png:

Countours of 1-C^2

and absval.png:

Absolute value of 1-C^2

Note that if you want to zoom the images, it is usually necessary to change the limits and recalculate the complex values z to avoid missing details. The images can of course be plotted on top of each other, the image colour palette can be changed, countour has a million options. If you only want to draw the zeros, replace the number 5 (number of contours) with [0] (plot only the listed contour) in the countour calls.

Of course, you will replace my (1-C^2) by your own function. The only thing to take care is that if the function receives an array of complex numbers, it returns an array of results in the same shape calculated pointwise. Imshow needs to receive an array of scalars. For more information, please see the imshow documentation.

(iii) There may be, but there are no general methods for finding all minima/maxima/zeros of arbitrary functions. (The function may even have an infinite number of roots.) Your idea of first plotting the function is a good one. Then you'll understand its behaviour easier.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

function fzero, matlab , internal rate of return , complex roots

From Dev

Best way to find roots of a multidimensional, scalar function with SciPy

From Dev

Storing complex objects passed by servlet into an array in javascript

From Dev

Storing objects in an array in javascript is storing the function

From Dev

Finding zeros of a complex function in SciPy/NumPy

From Dev

Factor to complex roots using sympy

From Dev

Hidden field not storing value from partial view into an array of complex object

From Dev

Finding the roots of a function on the stack

From Dev

Finding roots of function in Python

From Dev

Finding roots but not asymptotes of a function

From Dev

Finding roots of a function

From Dev

Finding roots of function in Python

From Dev

Roots of digamma function in R

From Dev

Storing a function inside a bash array element

From Dev

Storing SYSCALL functions in array of function pointers

From Dev

Storing a string array in a class member function and returning it

From Dev

Storing multiple function values in a string/array (Python)

From Dev

Storing a string array in a class member function and returning it

From Dev

Storing multiple function values in a string/array (Python)

From Dev

Storing complex object in ConcurrentHashMap

From Dev

Storing complex data

From Dev

Storing complex data in redis

From Dev

Plot the multiplication of complex roots of fractional polynomials

From Dev

Plot complex roots of unity as arrow-vectors on complex plane

From Dev

Roots of integral of Airy function (matlab)

From Dev

Find all roots of a function in python

From Dev

Storing a Function

From Dev

Storing a Complex Object in Session State

From Dev

Storing and retrieving a complex type in UWP

Related Related

  1. 1

    function fzero, matlab , internal rate of return , complex roots

  2. 2

    Best way to find roots of a multidimensional, scalar function with SciPy

  3. 3

    Storing complex objects passed by servlet into an array in javascript

  4. 4

    Storing objects in an array in javascript is storing the function

  5. 5

    Finding zeros of a complex function in SciPy/NumPy

  6. 6

    Factor to complex roots using sympy

  7. 7

    Hidden field not storing value from partial view into an array of complex object

  8. 8

    Finding the roots of a function on the stack

  9. 9

    Finding roots of function in Python

  10. 10

    Finding roots but not asymptotes of a function

  11. 11

    Finding roots of a function

  12. 12

    Finding roots of function in Python

  13. 13

    Roots of digamma function in R

  14. 14

    Storing a function inside a bash array element

  15. 15

    Storing SYSCALL functions in array of function pointers

  16. 16

    Storing a string array in a class member function and returning it

  17. 17

    Storing multiple function values in a string/array (Python)

  18. 18

    Storing a string array in a class member function and returning it

  19. 19

    Storing multiple function values in a string/array (Python)

  20. 20

    Storing complex object in ConcurrentHashMap

  21. 21

    Storing complex data

  22. 22

    Storing complex data in redis

  23. 23

    Plot the multiplication of complex roots of fractional polynomials

  24. 24

    Plot complex roots of unity as arrow-vectors on complex plane

  25. 25

    Roots of integral of Airy function (matlab)

  26. 26

    Find all roots of a function in python

  27. 27

    Storing a Function

  28. 28

    Storing a Complex Object in Session State

  29. 29

    Storing and retrieving a complex type in UWP

HotTag

Archive