Advertisement
xyzdragon

Plot roots

Aug 23rd, 2017
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. import numpy as np
  2. import itertools
  3.  
  4. def squareRoots( x ):
  5.     return np.roots( [1,0,-x] )
  6. def cubeRoots( x ):
  7.     return np.roots( [1,0,0,-x] )
  8.  
  9. xs = squareRoots(50)
  10. ys = [ y for x in xs for y in cubeRoots(7+x) ]
  11. solutions = [ a[0]+a[1] for a in itertools.product( ys, ys ) ]
  12. uniqueSolutions = np.unique( np.array( solutions ) )
  13. print "Found",len( solutions ),"out of which",len( uniqueSolutions ),"are unique:"
  14. for sol in uniqueSolutions:
  15.     print "    "+str( sol )
  16.  
  17. import matplotlib.pyplot as plt
  18. fig = plt.figure( figsize = (12,12) )
  19. ax = fig.add_subplot( 111, xlim=[-6,6], ylim=[-6,6] )
  20. ax.set_aspect( 'equal', 'datalim' )
  21.  
  22. ax.scatter( [ x.real for x in uniqueSolutions ], [ x.imag for x in uniqueSolutions ], s = 100, color = 'r', zorder = 5, label = r"$(7 \pm \sqrt{50})^{\frac{1}{3}} + (7 \pm \sqrt{50})^{\frac{1}{3}}$" )
  23. ax.scatter( [ x.real for x in np.unique( np.array(ys) ) ], [ x.imag for x in np.unique( np.array(ys) ) ], s = 50, color = '0.5', zorder = 4, label = r"$(7 \pm \sqrt{50})^{\frac{1}{3}}$" )
  24.  
  25. # round to make np.unique work with rounding errors
  26. rs  = np.unique( np.array( [ abs(x) for x in uniqueSolutions ] ).round( decimals = 12 ) )
  27. rys = np.unique( np.array( [ abs(x) for x in ys              ] ).round( decimals = 12 ) )
  28. print "rs  =",rs
  29. print "rys =",rys
  30. for r in rs:
  31.     ax.add_artist( plt.Circle( (0,0), r, edgecolor = 'r', alpha = 0.5, linestyle = '--', fill = False, linewidth = 2 ) )
  32. for y in ys:
  33.   for ry in rys:
  34.     ax.add_artist( plt.Circle( ( y.real, y.imag ), ry, edgecolor = '0.5', fill = False, linewidth = 2 ) )
  35. ax.legend()
  36. fig.tight_layout()
  37. fig.savefig( "cuberoot-solutions.png" )
  38.  
  39. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement