0

I am writing a code to find roots of a polynomial for different values of a parameters k and l. This code works fine apart from when the parameter equals zero.

My approach has been to offset the zero to a small number (as shown below), but this has disadvantages.

xlist = np.linspace(-n_steps*step_size,near_0,n_steps+1)
xlist=np.append(xlist,np.linspace(step_size,n_steps*step_size,n_steps))
ylist = np.linspace(-n_steps*step_size,near_0,n_steps+1)
ylist=np.append(ylist,np.linspace(step_size,n_steps*step_size,n_steps))

for k_i,k in enumerate(xlist):
    for l_i,l in enumerate(ylist):
        p=[1,-1j*w*k,l**2/(k**2+l**2)*(1-1/R),-l**2/(k**2+l**2)*w*1j*k]
        roots=np.roots(p)

What are the pros and cons of different ways of avoiding calculating when the parameters are zero? The answer at k=0 or l=0 is not important. I suppose the best way would be to not run the calculation when k or l is 0.

Thanks for any help you can give.

Chogg
  • 389
  • 2
  • 19

1 Answers1

0

You could just do:

xlist = np.linspace(-n_steps*step_size,near_0,n_steps+1)
xlist=np.append(xlist,np.linspace(step_size,n_steps*step_size,n_steps))
ylist = np.linspace(-n_steps*step_size,near_0,n_steps+1)
ylist=np.append(ylist,np.linspace(step_size,n_steps*step_size,n_steps))

for k_i,k in enumerate(xlist):
    for l_i,l in enumerate(ylist):
        if k == 0 or l == 0:  # Just continue!
            continue

        p=[1,-1j*w*k,l**2/(k**2+l**2)*(1-1/R),-l**2/(k**2+l**2)*w*1j*k]
        roots=np.roots(p)
jsmiao
  • 433
  • 1
  • 5
  • 13