<s> """<STR_LIT>""" <EOL> from colorsys import hsv_to_rgb , hls_to_rgb <EOL> from . libmp import NoConvergence <EOL> from . libmp . backend import xrange <EOL> class VisualizationMethods ( object ) : <EOL> plot_ignore = ( ValueError , ArithmeticError , ZeroDivisionError , NoConvergence ) <EOL> def plot ( ctx , f , xlim = [ - <NUM_LIT:5> , <NUM_LIT:5> ] , ylim = None , points = <NUM_LIT:200> , file = None , dpi = None , <EOL> singularities = [ ] , axes = None ) : <EOL> r"""<STR_LIT>""" <EOL> if file : <EOL> axes = None <EOL> fig = None <EOL> if not axes : <EOL> import pylab <EOL> fig = pylab . figure ( ) <EOL> axes = fig . add_subplot ( <NUM_LIT> ) <EOL> if not isinstance ( f , ( tuple , list ) ) : <EOL> f = [ f ] <EOL> a , b = xlim <EOL> colors = [ '<STR_LIT:b>' , '<STR_LIT:r>' , '<STR_LIT:g>' , '<STR_LIT:m>' , '<STR_LIT:k>' ] <EOL> for n , func in enumerate ( f ) : <EOL> x = ctx . arange ( a , b , ( b - a ) / float ( points ) ) <EOL> segments = [ ] <EOL> segment = [ ] <EOL> in_complex = False <EOL> for i in xrange ( len ( x ) ) : <EOL> try : <EOL> if i != <NUM_LIT:0> : <EOL> for sing in singularities : <EOL> if x [ i - <NUM_LIT:1> ] <= sing and x [ i ] >= sing : <EOL> raise ValueError <EOL> v = func ( x [ i ] ) <EOL> if ctx . isnan ( v ) or abs ( v ) > <NUM_LIT> : <EOL> raise ValueError <EOL> if hasattr ( v , "<STR_LIT>" ) and v . imag : <EOL> re = float ( v . real ) <EOL> im = float ( v . imag ) <EOL> if not in_complex : <EOL> in_complex = True <EOL> segments . append ( segment ) <EOL> segment = [ ] <EOL> segment . append ( ( float ( x [ i ] ) , re , im ) ) <EOL> else : <EOL> if in_complex : <EOL> in_complex = False <EOL> segments . append ( segment ) <EOL> segment = [ ] <EOL> if hasattr ( v , "<STR_LIT>" ) : <EOL> v = v . real <EOL> segment . append ( ( float ( x [ i ] ) , v ) ) <EOL> except ctx . plot_ignore : <EOL> if segment : <EOL> segments . append ( segment ) <EOL> segment = [ ] <EOL> if segment : <EOL> segments . append ( segment ) <EOL> for segment in segments : <EOL> x = [ s [ <NUM_LIT:0> ] for s in segment ] <EOL> y = [ s [ <NUM_LIT:1> ] for s in segment ] <EOL> if not x : <EOL> continue <EOL> c = colors [ n % len ( colors ) ] <EOL> if len ( segment [ <NUM_LIT:0> ] ) == <NUM_LIT:3> : <EOL> z = [ s [ <NUM_LIT:2> ] for s in segment ] <EOL> axes . plot ( x , y , '<STR_LIT>' + c , linewidth = <NUM_LIT:3> ) <EOL> axes . plot ( x , z , '<STR_LIT::>' + c , linewidth = <NUM_LIT:3> ) <EOL> else : <EOL> axes . plot ( x , y , c , linewidth = <NUM_LIT:3> ) <EOL> axes . set_xlim ( [ float ( _ ) for _ in xlim ] ) <EOL> if ylim : <EOL> axes . set_ylim ( [ float ( _ ) for _ in ylim ] ) <EOL> axes . set_xlabel ( '<STR_LIT:x>' ) <EOL> axes . set_ylabel ( '<STR_LIT>' ) <EOL> axes . grid ( True ) <EOL> if fig : <EOL> if file : <EOL> pylab . savefig ( file , dpi = dpi ) <EOL> else : <EOL> pylab . show ( ) <EOL> def default_color_function ( ctx , z ) : <EOL> if ctx . isinf ( z ) : <EOL> return ( <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ) <EOL> if ctx . isnan ( z ) : <EOL> return ( <NUM_LIT:0.5> , <NUM_LIT:0.5> , <NUM_LIT:0.5> ) <EOL> pi = <NUM_LIT> <EOL> a = ( float ( ctx . arg ( z ) ) + ctx . pi ) / ( <NUM_LIT:2> * ctx . pi ) <EOL> a = ( a + <NUM_LIT:0.5> ) % <NUM_LIT:1.0> <EOL> b = <NUM_LIT:1.0> - float ( <NUM_LIT:1> / ( <NUM_LIT:1.0> + abs ( z ) ** <NUM_LIT> ) ) <EOL> return hls_to_rgb ( a , b , <NUM_LIT> ) <EOL> blue_orange_colors = [ <EOL> ( - <NUM_LIT:1.0> , ( <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ) ) , <EOL> ( - <NUM_LIT> , ( <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.5> ) ) , <EOL> ( - <NUM_LIT:0.5> , ( <NUM_LIT:0.0> , <NUM_LIT:0.5> , <NUM_LIT:1.0> ) ) , <EOL> ( - <NUM_LIT> , ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) , <EOL> ( <NUM_LIT:0.0> , ( <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ) ) , <EOL> ( <NUM_LIT> , ( <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ) ) , <EOL> ( <NUM_LIT:0.5> , ( <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT:0.0> ) ) , <EOL> ( <NUM_LIT> , ( <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT:0.0> ) ) , <EOL> ( <NUM_LIT:1.0> , ( <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ) ) , <EOL> ( <NUM_LIT> , ( <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ) ) , <EOL> ] <EOL> def phase_color_function ( ctx , z ) : <EOL> if ctx . isinf ( z ) : <EOL> return ( <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ) <EOL> if ctx . isnan ( z ) : <EOL> return ( <NUM_LIT:0.5> , <NUM_LIT:0.5> , <NUM_LIT:0.5> ) <EOL> pi = <NUM_LIT> <EOL> w = float ( ctx . arg ( z ) ) / pi <EOL> w = max ( min ( w , <NUM_LIT:1.0> ) , - <NUM_LIT:1.0> ) <EOL> for i in range ( <NUM_LIT:1> , len ( blue_orange_colors ) ) : <EOL> if blue_orange_colors [ i ] [ <NUM_LIT:0> ] > w : <EOL> a , ( ra , ga , ba ) = blue_orange_colors [ i - <NUM_LIT:1> ] <EOL> b , ( rb , gb , bb ) = blue_orange_colors [ i ] <EOL> s = ( w - a ) / ( b - a ) <EOL> return ra + ( rb - ra ) * s , ga + ( gb - ga ) * s , ba + ( bb - ba ) * s <EOL> def cplot ( ctx , f , re = [ - <NUM_LIT:5> , <NUM_LIT:5> ] , im = [ - <NUM_LIT:5> , <NUM_LIT:5> ] , points = <NUM_LIT> , color = None , <EOL> verbose = False , file = None , dpi = None , axes = None ) : <EOL> """<STR_LIT>""" <EOL> if color is None or color == "<STR_LIT:default>" : <EOL> color = ctx . default_color_function <EOL> if color == "<STR_LIT>" : <EOL> color = ctx . phase_color_function <EOL> import pylab <EOL> if file : <EOL> axes = None <EOL> fig = None <EOL> if not axes : <EOL> fig = pylab . figure ( ) <EOL> axes = fig . add_subplot ( <NUM_LIT> ) <EOL> rea , reb = re <EOL> ima , imb = im <EOL> dre = reb - rea <EOL> dim = imb - ima <EOL> M = int ( ctx . sqrt ( points * dre / dim ) + <NUM_LIT:1> ) <EOL> N = int ( ctx . sqrt ( points * dim / dre ) + <NUM_LIT:1> ) <EOL> x = pylab . linspace ( rea , reb , M ) <EOL> y = pylab . linspace ( ima , imb , N ) <EOL> w = pylab . zeros ( ( N , M , <NUM_LIT:3> ) ) <EOL> for n in xrange ( N ) : <EOL> for m in xrange ( M ) : <EOL> z = ctx . mpc ( x [ m ] , y [ n ] ) <EOL> try : <EOL> v = color ( f ( z ) ) <EOL> except ctx . plot_ignore : <EOL> v = ( <NUM_LIT:0.5> , <NUM_LIT:0.5> , <NUM_LIT:0.5> ) <EOL> w [ n , m ] = v <EOL> if verbose : <EOL> print ( str ( n ) + '<STR_LIT>' + str ( N ) ) <EOL> rea , reb , ima , imb = [ float ( _ ) for _ in [ rea , reb , ima , imb ] ] <EOL> axes . imshow ( w , extent = ( rea , reb , ima , imb ) , origin = '<STR_LIT>' ) <EOL> axes . set_xlabel ( '<STR_LIT>' ) <EOL> axes . set_ylabel ( '<STR_LIT>' ) <EOL> if fig : <EOL> if file : <EOL> pylab . savefig ( file , dpi = dpi ) <EOL> else : <EOL> pylab . show ( ) <EOL> def splot ( ctx , f , u = [ - <NUM_LIT:5> , <NUM_LIT:5> ] , v = [ - <NUM_LIT:5> , <NUM_LIT:5> ] , points = <NUM_LIT:100> , keep_aspect = True , wireframe = False , file = None , dpi = None , axes = None ) : <EOL> """<STR_LIT>""" <EOL> import pylab <EOL> import mpl_toolkits . mplot3d as mplot3d <EOL> if file : <EOL> axes = None <EOL> fig = None <EOL> if not axes : <EOL> fig = pylab . figure ( ) <EOL> axes = mplot3d . axes3d . Axes3D ( fig ) <EOL> ua , ub = u <EOL> va , vb = v <EOL> du = ub - ua <EOL> dv = vb - va <EOL> if not isinstance ( points , ( list , tuple ) ) : <EOL> points = [ points , points ] <EOL> M , N = points <EOL> u = pylab . linspace ( ua , ub , M ) <EOL> v = pylab . linspace ( va , vb , N ) <EOL> x , y , z = [ pylab . zeros ( ( M , N ) ) for i in xrange ( <NUM_LIT:3> ) ] <EOL> xab , yab , zab = [ [ <NUM_LIT:0> , <NUM_LIT:0> ] for i in xrange ( <NUM_LIT:3> ) ] <EOL> for n in xrange ( N ) : <EOL> for m in xrange ( M ) : <EOL> fdata = f ( ctx . convert ( u [ m ] ) , ctx . convert ( v [ n ] ) ) <EOL> try : <EOL> x [ m , n ] , y [ m , n ] , z [ m , n ] = fdata <EOL> except TypeError : <EOL> x [ m , n ] , y [ m , n ] , z [ m , n ] = u [ m ] , v [ n ] , fdata <EOL> for c , cab in [ ( x [ m , n ] , xab ) , ( y [ m , n ] , yab ) , ( z [ m , n ] , zab ) ] : <EOL> if c < cab [ <NUM_LIT:0> ] : <EOL> cab [ <NUM_LIT:0> ] = c <EOL> if c > cab [ <NUM_LIT:1> ] : <EOL> cab [ <NUM_LIT:1> ] = c <EOL> if wireframe : <EOL> axes . plot_wireframe ( x , y , z , rstride = <NUM_LIT:4> , cstride = <NUM_LIT:4> ) <EOL> else : <EOL> axes . plot_surface ( x , y , z , rstride = <NUM_LIT:4> , cstride = <NUM_LIT:4> ) <EOL> axes . set_xlabel ( '<STR_LIT:x>' ) <EOL> axes . set_ylabel ( '<STR_LIT:y>' ) <EOL> axes . set_zlabel ( '<STR_LIT:z>' ) <EOL> if keep_aspect : <EOL> dx , dy , dz = [ cab [ <NUM_LIT:1> ] - cab [ <NUM_LIT:0> ] for cab in [ xab , yab , zab ] ] <EOL> maxd = max ( dx , dy , dz ) <EOL> if dx < maxd : <EOL> delta = maxd - dx <EOL> axes . set_xlim3d ( xab [ <NUM_LIT:0> ] - delta / <NUM_LIT> , xab [ <NUM_LIT:1> ] + delta / <NUM_LIT> ) <EOL> if dy < maxd : <EOL> delta = maxd - dy <EOL> axes . set_ylim3d ( yab [ <NUM_LIT:0> ] - delta / <NUM_LIT> , yab [ <NUM_LIT:1> ] + delta / <NUM_LIT> ) <EOL> if dz < maxd : <EOL> delta = maxd - dz <EOL> axes . set_zlim3d ( zab [ <NUM_LIT:0> ] - delta / <NUM_LIT> , zab [ <NUM_LIT:1> ] + delta / <NUM_LIT> ) <EOL> if fig : <EOL> if file : <EOL> pylab . savefig ( file , dpi = dpi ) <EOL> else : <EOL> pylab . show ( ) <EOL> VisualizationMethods . plot = plot <EOL> VisualizationMethods . default_color_function = default_color_function <EOL> VisualizationMethods . phase_color_function = phase_color_function <EOL> VisualizationMethods . cplot = cplot <EOL> VisualizationMethods . splot = splot </s>
