#NEWLINE# def textplot( expr , a , b , W = 55 , H = 18 ) : #NEWLINE# #INDENT# "\n    Print a crude ASCII art plot of the SymPy expression 'expr' (which\n    should contain a single symbol, e.g. x or something else) over the\n    interval [a, b].\n\n    Examples\n    ========\n\n    textplot(sin(t)*t, 0, 15)\n    " #NEWLINE# free = expr . free_symbols #NEWLINE# if ( len ( free ) > 1 ) : #NEWLINE# #INDENT# raise ValueError ( 'length can not be greater than 1' ) #NEWLINE# #UNINDENT# x = ( free . pop ( ) if free else Dummy ( ) ) #NEWLINE# f = lambdify ( [ x ] , expr ) #NEWLINE# a = float ( a ) #NEWLINE# b = float ( b ) #NEWLINE# y = ( [ 0 ] * W ) #NEWLINE# for x in range ( W ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# y [ x ] = f ( ( a + ( ( ( b - a ) / float ( W ) ) * x ) ) ) #NEWLINE# #UNINDENT# except ( TypeError , ValueError ) : #NEWLINE# #INDENT# y [ x ] = 0 #NEWLINE# #UNINDENT# #UNINDENT# ma = max ( y ) #NEWLINE# mi = min ( y ) #NEWLINE# if ( ma == mi ) : #NEWLINE# #INDENT# if ma : #NEWLINE# #INDENT# ( mi , ma ) = sorted ( [ 0 , ( 2 * ma ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( mi , ma ) = ( ( - 1 ) , 1 ) #NEWLINE# #UNINDENT# #UNINDENT# for x in range ( W ) : #NEWLINE# #INDENT# y [ x ] = int ( ( ( float ( H ) * ( y [ x ] - mi ) ) / ( ma - mi ) ) ) #NEWLINE# #UNINDENT# margin = 7 #NEWLINE# print #NEWLINE# for h in range ( ( H - 1 ) , ( - 1 ) , ( - 1 ) ) : #NEWLINE# #INDENT# s = ( [ ' ' ] * W ) #NEWLINE# for x in range ( W ) : #NEWLINE# #INDENT# if ( y [ x ] == h ) : #NEWLINE# #INDENT# if ( ( ( x == 0 ) or ( y [ ( x - 1 ) ] == ( h - 1 ) ) ) and ( ( x == ( W - 1 ) ) or ( y [ ( x + 1 ) ] == ( h + 1 ) ) ) ) : #NEWLINE# #INDENT# s [ x ] = '/' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( ( x == 0 ) or ( y [ ( x - 1 ) ] == ( h + 1 ) ) ) and ( ( x == ( W - 1 ) ) or ( y [ ( x + 1 ) ] == ( h - 1 ) ) ) ) : #NEWLINE# #INDENT# s [ x ] = '\\' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# s [ x ] = '.' #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( h == ( H - 1 ) ) : #NEWLINE# #INDENT# prefix = ( '%g' % ma ) . rjust ( margin ) [ : margin ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( h == ( H // 2 ) ) : #NEWLINE# #INDENT# prefix = ( '%g' % ( ( mi + ma ) / 2 ) ) . rjust ( margin ) [ : margin ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( h == 0 ) : #NEWLINE# #INDENT# prefix = ( '%g' % mi ) . rjust ( margin ) [ : margin ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# prefix = ( ' ' * margin ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# s = '' . join ( s ) #NEWLINE# if ( h == ( H // 2 ) ) : #NEWLINE# #INDENT# s = s . replace ( ' ' , '-' ) #NEWLINE# #UNINDENT# print ( ( ( prefix + ' | ' ) + s ) ) #NEWLINE# #UNINDENT# bottom = ( ' ' * ( margin + 3 ) ) #NEWLINE# bottom += ( '%g' % a ) . ljust ( ( ( W // 2 ) - 4 ) ) #NEWLINE# bottom += ( '%g' % ( ( a + b ) / 2 ) ) . ljust ( ( W // 2 ) ) #NEWLINE# bottom += ( '%g' % b ) #NEWLINE# print ( bottom )