Chris_M_Thomasson

Complex Roots Function in Python 3

Jul 12th, 2016
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. import math;
  2.  
  3.  
  4. # Complex Roots
  5. def croots(z, p):
  6.     l = math.sqrt(z.real**2 + z.imag**2);
  7.     s = l**(1.0 / p);
  8.     a = math.atan2(z.imag, z.real) / p;
  9.  
  10.     n = math.ceil(math.fabs(p));
  11.     astep = (math.pi * 2.0) / p;
  12.  
  13.     result = [];
  14.  
  15.     for i in range(n):
  16.         r = complex(math.cos(a + astep * i) * s,
  17.                     math.sin(a + astep * i) * s);
  18.         # print(r);
  19.         result.append(r);
  20.  
  21.     return result;
  22.  
  23.  
  24. # The secret key (c), and origin point (z)
  25. c = (-.75+.09j);
  26. z = (0+0j);
  27. p = 7.628;
  28.  
  29. print("c = " + str(c));
  30. print("z = " + str(z));
  31. print("p = " + str(p));
  32.  
  33. # display the complex roots
  34. print("complex roots for z^" + str(p) + " + c");
  35. print("_____________________________________");
  36. cr = croots(z - c, p);
  37. for b in cr:
  38.     print(b);
  39.  
  40. print("_____________________________________");
Advertisement
Add Comment
Please, Sign In to add comment