miklis

algebraic_graph v2.0

Jun 4th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. from math import*
  2. from turtle import*
  3. from time import*
  4.  
  5. def algebraic_graph(functions_list,N=25000, h=[-20,20], v=[-20.,20], h_interval=None, v_interval=None, hwidth_of_cell=1, vwidth_of_cell=1, hscale=1, vscale=1, axes=('x','y')):
  6. #graph of multiple algebraic functions
  7. #h and v sets up domain and range for functions
  8. #h_interval and v_interval sets up territory of gridliness
  9. #IMPORTANT: if range (a,b) of function is small, make sure (a+0.5, b-0.5) doesn't cut off important parts of a function graph
  10. #to change the width of cell change hwidth_of_cell and vwidth_of_cell
  11. #to change the scaling change hscale and vscale
  12. def graph(curves, c1=-575.,c2=575.,d1=-333.,d2=340.,scalingXY=(15,10,'x','y'),rounding=0, background='white', split_h=(True,15,"red"), split_v=(True,10,"red"), lower=None, upper=None, go=goto, message=True):
  13. #curves=list of curves to graph - form of each: [(color, width),*set of points]
  14. #scalingXY[0] - (units to scale x; units to scale y; name of Ox axis; name of Oy axis)
  15. #rounding - precision of scaling
  16. #background - color of background
  17. #split_h[0] - (is horizontal gridliness used; split_h[1] - units per board to grid; color of grid)
  18. #lower - position of left lower corner bounding graph
  19. #upper - position of right upper corner bounding graph
  20. #message - are we printing time graph function takes
  21. '''update: if we have a curve with a stream of data (not (x1,y1), (x2,y2), (x3,y3)... but x1, x2, x3) we assume x1,x2,x3,...=1,2,3,... and y1,y2,y3,... is that stream'''
  22. colormode(255)
  23. if message:
  24. print '2d preview...',
  25. t=time()
  26. reset()
  27. tracer(0)
  28. C,D=c2-c1,d2-d1
  29. for j in range(len(curves)):
  30. if str(type(curves[j][1]))<>"<type 'tuple'>":
  31. for i in range(len(curves[j])-1): curves[j][i+1]=(i,curves[j][i+1])
  32. if lower==None: x1,y1=float(min([min([el[0] for el in n[1:]]) for n in curves])),float(min([min([el[1] for el in n[1:]]) for n in curves]))
  33. else: x1,y1=lower[0],lower[1]
  34. if upper==None: x2,y2=float(max([max([el[0] for el in n[1:]]) for n in curves])),float(max([max([el[1] for el in n[1:]]) for n in curves]))
  35. else: x2,y2=upper[0],upper[1]
  36. #if y1==y2: y1,y2=y1-0.5,y2+0.5
  37. difx,dify=x2-x1,y2-y1
  38. bgcolor(background)
  39. #responsible for drawing a grid
  40. def improved_int(a):
  41. if a>0: return(int(a))
  42. else: return int(a)-1
  43. if split_h[0]:
  44. pencolor(split_h[2])
  45. k=C/difx*(-x1-difx/split_h[1]*improved_int(-x1*split_h[1]/difx)) #a coordinate to begin gridliness
  46. special_line=improved_int(-x1*split_h[1]/difx)/float(split_h[1])
  47. for n in [float(i)/split_h[1] for i in range(split_h[1]+1)]: #each vertical strip
  48. up()
  49. go(c1+k+C*n,d1)
  50. down()
  51. '''draws a special line and labels axis on it'''
  52. if n==special_line:
  53. pensize(3)
  54. for N in [float(i)/scalingXY[1] for i in range(scalingXY[1])]:
  55. go(c1+k+C*n,d1+k+D*N)
  56. write(str(int(round(y1+k*dify/D+N*dify,rounding))), align='right', font=("System", 12, "normal"))
  57. dot(5)
  58. go(c1+k+C*n,d2-D*(1./scalingXY[1])/3.) #let's write y
  59. up()
  60. left(90)
  61. shape('classic')
  62. stamp()
  63. go(c1+k+C*n+4,d2-D*(1./scalingXY[1])/3.-4)
  64. write(str(scalingXY[3]), align='left', font=("Arial", 14, "normal"))
  65. pensize(1)
  66. else: go(c1+k+C*n,d2)
  67. if split_v[0]:
  68. pencolor(split_v[2])
  69. k=D/dify*(-y1-dify/split_v[1]*improved_int(-y1*split_v[1]/dify)) #a coordinate to begin gridliness
  70. special_line=improved_int(-y1*split_v[1]/dify)/float(split_v[1])
  71. for n in [float(i)/split_v[1] for i in range(split_v[1]+1)]: #each vertical strip
  72. up()
  73. go(c1,d1+k+D*n)
  74. down()
  75. if n==special_line:
  76. pensize(3)
  77. for N in [float(i)/scalingXY[0] for i in range(scalingXY[0])]:
  78. go(c1+k+C*N,d1+k+D*n)
  79. write(str(int(round(x1+k*difx/C+N*difx,rounding))), align='right', font=("System", 12, "normal"))
  80. dot(5)
  81. go(c2-C*(1./scalingXY[0])/3.,d1+k+D*n) #let's write x
  82. up()
  83. right(90)
  84. shape('classic')
  85. stamp()
  86. write(str(scalingXY[2]), align='left', font=("Arial", 14, "normal"))
  87. pensize(1)
  88. else: go(c2,d1+k+D*n)
  89. #responsible for drawing a curves
  90. update()
  91. for m in curves:
  92. tracer(0)
  93. pencolor(m[0][0])
  94. pensize(m[0][1])
  95. up()
  96. go(c1+C*(m[1][0]-x1)/difx,d1+D*(m[1][1]-y1)/dify)
  97. down()
  98. for n in m[2:]: go(c1+C*(n[0]-x1)/difx,d1+D*(n[1]-y1)/dify)
  99. update()
  100. pencolor("black")
  101. pensize(1)
  102. up()
  103. if message: print time()-t
  104. hideturtle()
  105. update()
  106. exitonclick()
  107.  
  108. pot=[]
  109. c=[("black",4),("brown",4),("purple",4),("darkgreen", 4), ("grey",4), ("red",4)]
  110. if h_interval==None: h_interval=(h[0]+0.5,h[1]-0.5)
  111. if v_interval==None: v_interval=(v[0]+0.5,v[1]-0.5)
  112. for j in range(len(functions_list)):
  113. dx=(h_interval[1]-h_interval[0])/float(N) #vienos dalies ilgis
  114. line=[c[j]]
  115. for i in range(N+1):
  116. try:
  117. value=functions_list[j](h_interval[0]+i*dx)
  118. if v_interval[0]<=value<=v_interval[1]:
  119. line.append((h_interval[0]+i*dx, functions_list[j](h_interval[0]+i*dx))) #dalos tasku kordinates
  120. else:
  121. if len(line)>2: pot.append(line)
  122. line=[c[j]]
  123. except (ZeroDivisionError, ValueError,TypeError):
  124. if len(line)>2: pot.append(line)
  125. line=[c[j]]
  126. if len(line)>2: pot.append(line)
  127. cell_h=int(h[1]-h[0])
  128. cell_v=int(v[1]-v[0])
  129. K=reduce(lambda x,y: x+y, [[pot[i]] for i in range(len(pot))])
  130. print len(K)
  131. try:
  132. graph(reduce(lambda x,y: x+y, [[pot[i]] for i in range(len(pot))]),c1=-300.,c2=300.,d1=-300.,d2=300.,\
  133. scalingXY=(cell_h/hscale,cell_v/vscale,axes[0],axes[1]), split_h=(True,cell_h/hwidth_of_cell,"blue"), split_v=(True,cell_v/vwidth_of_cell,"blue"),rounding=3,lower=(int(h[0]),int(v[0])),upper=(int(h[1]),int(v[1])))
  134. except ZeroDivisionError: print "We've got ZeroDivisionError, the most probably scale unit exceeds domain"
  135.  
  136. algebraic_graph([lambda x: sin(x)], h=[-500,-450],v=[-2,2],hwidth_of_cell=10,hscale=10)
  137. algebraic_graph([lambda x: sin(10*x)+sin(10.1*x)+sin(10.2*x)], h=[-500,500],v=[-4,4],hwidth_of_cell=50,hscale=100)
  138. algebraic_graph([lambda x: (9-(x-2)*(x-2))**0.5], h=[-10,10],v=[-10,10],axes=('x','f(x)'))
  139. algebraic_graph([lambda x: (x+1)/(1-x)+(x-1)/x-2],h=[-10,10],v=[-1000,1000],vwidth_of_cell=100,vscale=100)
  140. algebraic_graph([lambda x: 3**x, lambda x: 2**x, lambda x: 1.2**x, lambda x: 1**x, lambda x: 0.9**x, lambda x: 0.7**x],h=[-10,10],v=[-10,10])
  141. algebraic_graph([lambda x: x*x, lambda x: (x-2)*(x-2), lambda x: 2(x-2)*(x-2),lambda x: 2(x-2)*(x-2)+3, lambda x: (3*x-2)*(3*x-2), lambda x: (3*x-2)*(3*x-2)], h=[-15,15],v=[-15,15])
Add Comment
Please, Sign In to add comment