def param(xlist, ylist, vw=ViewWindow(), colorlist=None, auto=False, gif=False, add=False, avg=False, filename="param" + str(int(time())), save=True): if callable(xlist): xlist = [xlist] if callable(ylist): ylist = [ylist] if auto: vw.autoparam(xlist, ylist) data = PlotData(vw) images = [] color = 0 if colorlist is None: colorlist = Color.colors(len(xlist)) for i in range(len(xlist)): x = xlist[i] y = ylist[i] t = vw.tmin if colorlist != 'rainbow': color = colorlist[i] while t <= vw.tmax: if colorlist == 'rainbow': color = Color.rbow(vw.tmax - t, 2) u = t + vw.tstep data.line0((x(t), y(t)), (x(u), y(u)), color, add, avg) t = u if gif: images.append(data.save("", False)) if gif: writegif(filename, images, 0.01) return data.save(filename, save)
def gridspanningtree(length, height, d=2, r=0, color=Color(255, 0, 0), g="grid", filename=None, treefunc="dfs", gif=False, save=True): images = [] if not isinstance(g, Graph): (x, y) = (randint(0, length - 1), randint(0, height - 1)) g = getattr(Graph, g)(length, height).treefunc(treefunc, (x, y)) else: (x, y) = list(g.dict.keys)[0] vw = ViewWindow(dimx=d * length + d - 1, dimy=d * height + d - 1) data = PlotData(vw) color2 = 0.8 * color for v1 in g.dict: for i in range(-r, r + 1): for j in range(-r, r + 1): data.putpixel((d * (v1[0] + 1) - 1 + i, d * (v1[1] + 1) - 1 + j), color) s = {(x, y)} q = [(x, y)] while q: v1 = q.pop(0) for v2 in g.dict[v1]: if v2 not in s: for i in range(1 + r, d - r): data.putpixel( (d * (v1[0] + 1) - 1 + (v2[0] - v1[0]) * i, d * (v1[1] + 1) - 1 + (v2[1] - v1[1]) * i), color2) q.append(v2) s.add(v2) if gif: images.append(data.save("", False)) if filename is None: filename = "gst " + str((length, height)) + " " + str(int(time())) ret = data.save(filename, save) if gif: writegif(filename, images, 0.01) return ret
def gridspanningtree2(length, height, d=2, r=0, g="grid", color=Color(255, 0, 0), filename=None, treefunc=None, func="dfs", gif=False, save=True): images = [] if not isinstance(g, Graph): g = getattr(Graph, g)(length, height) if treefunc is not None: (x, y) = (randint(0, length - 1), randint(0, height - 1)) g = g.treefunc(treefunc, (x, y)) (x, y) = (randint(0, length - 1), randint(0, height - 1)) g = g.func(func, (x, y)) vw = ViewWindow(dimx=d * length + d - 1, dimy=d * height + d - 1) data = PlotData(vw) color2 = 0.8 * color for p in g: try: for i in range(-r, r + 1): for j in range(-r, r + 1): data.putpixel((d * (p[0] + 1) - 1 + i, d * (p[1] + 1) - 1 + j), color) except TypeError: for i in range(-r, r + 1): for j in range(-r, r + 1): data.putpixel((d * (p[1][0] + 1) - 1 + i, d * (p[1][1] + 1) - 1 + j), color) for i in range(1 + r, d - r): data.putpixel( (d * (p[0][0] + 1) - 1 + (p[1][0] - p[0][0]) * i, d * (p[0][1] + 1) - 1 + (p[1][1] - p[0][1]) * i), color2) if gif: images.append(data.save("", False)) if filename is None: filename = "gst2 " + str((length, height)) + " " + str(time()) ret = data.save(filename, save) if gif: writegif(filename, images, 0.01) return ret
def funcgraph(funclist, vw=ViewWindow(), colorlist=None, auto=False, filename="plot" + str(int(time())), gif=False, add=False, avg=False, save=True): images = [] if auto: vw.autograph(funclist) if callable(funclist): funclist = [funclist] data = PlotData(vw) if colorlist is None: colorlist = Color.colors(len(funclist)) for j in range(len(funclist)): f = funclist[j] it = vw.xiterator() x0 = it.next() for x1 in it: if colorlist == "rainbow": color = Color.rbow(x1, 2) else: color = colorlist[j] data.line((x0, f(x0)), (x1, f(x1)), color, add, avg) x0 = x1 if gif: images.append(data.save("", False)) if gif: writegif(filename, images, 0.01) return data.save(filename, save)
def zrotate(xlist, ylist, vw=ViewWindow(), segs=4, colorlist=None, auto=False, filename=None, gif=False, add=True, avg=False, save=True): if auto: vw.autoparam(xlist, ylist) if filename is None: filename = "zrotate" + str(int(time())) if callable(xlist): xlist = [xlist] if callable(ylist): ylist = [ylist] data = PlotData(vw, "rainbow" if colorlist == "rainbow" else Color(0, 0, 0)) images = [] if colorlist is None: colorlist = Color.colors(len(xlist)) tlist = [vw.tmin + (vw.tmax - vw.tmin) * j / segs for j in range(segs + 1)] while tlist[0] < vw.tmax: for (x, y, color) in zip(xlist, ylist, colorlist): if colorlist == 'rainbow': color = Color.rbow(tlist[0], segs) for j in range(len(tlist) - 1): data.line0((x(tlist[j]), y(tlist[j])), (x(tlist[j + 1]), y(tlist[j + 1])), color, add, avg) if gif: images.append(data.save("", False)) tlist = [t + vw.tstep for t in tlist] if gif: writegif(filename, images, 0.01) return data.save(filename, save)
def juliazoom(f, vw, z0=0, factor=1.5, frames=20, n=50, duration=0.5, zmax=2, color=Color(255, 0, 0), filename=None): images = [] for i in range(frames): vw = ViewWindow(z0.real - factor ** -i, z0.real + factor ** -i, z0.imag - factor ** -i, z0.imag + factor ** -i, dimx=vw.dimx, dimy=vw.dimy) images.append(julia(f, vw, n, zmax, color, filename, False)) if filename is None: filename = "julia " + str(n) + " " + str(zmax) + " " + str(int(time())) writegif(filename, images, duration)
def gridspanningtree2(length, height, d=2, r=0, g="grid", color=Color(255, 0, 0), filename=None, treefunc=None, func="dfs", gif=False, save=True): images = [] if not isinstance(g, Graph): g = getattr(Graph, g)(length, height) if treefunc is not None: (x, y) = (randint(0, length - 1), randint(0, height - 1)) g = g.treefunc(treefunc, (x, y)) (x, y) = (randint(0, length - 1), randint(0, height - 1)) g = g.func(func, (x, y)) vw = ViewWindow(dimx=d * length + d - 1, dimy=d * height + d - 1) data = PlotData(vw) color2 = 0.8 * color for p in g: try: for i in range(-r, r + 1): for j in range(-r, r + 1): data.putpixel( (d * (p[0] + 1) - 1 + i, d * (p[1] + 1) - 1 + j), color) except TypeError: for i in range(-r, r + 1): for j in range(-r, r + 1): data.putpixel( (d * (p[1][0] + 1) - 1 + i, d * (p[1][1] + 1) - 1 + j), color) for i in range(1 + r, d - r): data.putpixel( (d * (p[0][0] + 1) - 1 + (p[1][0] - p[0][0]) * i, d * (p[0][1] + 1) - 1 + (p[1][1] - p[0][1]) * i), color2) if gif: images.append(data.save("", False)) if filename is None: filename = "gst2 " + str((length, height)) + " " + str(time()) ret = data.save(filename, save) if gif: writegif(filename, images, 0.01) return ret
def graphpict(length, height, graphtype="grid", treefunc="dfs", func="bfs", colorfunc="hilbert", gif=False, gifres=None, v0=None, v1=None, rand=True, save=True, scales=None): images = [] clist = [] if isinstance(graphtype, Graph): area = len(graphtype.dict) else: area = length * height if not colorfunc.startswith("rbow"): clist = colorcube(colorfunc) if func == "hilbert": l = hilbert(int(ceil(log(max(length, height), 2)))) gstr = "None" else: if not isinstance(graphtype, Graph): gstr = graphtype graphtype = getattr(Graph, graphtype)(length, height) else: gstr = "Graph" if v0 is None: v0 = choice(graphtype.dict.keys()) if treefunc is not None: if v1 is None: v1 = choice(graphtype.dict.keys()) graphtype = graphtype.treefunc(treefunc, v1, rand) print "Computed spanning tree" l = graphtype.func(func, v0, rand) if gif and gifres is None: gifres = ceil(area / 150.0) vw = ViewWindow(dimx=length, dimy=height) data = PlotData(vw, scales=scales) i = 0 for p in l: if colorfunc.startswith("rbow"): data.putpixel(p, getattr(Color, colorfunc)(2 * pi * i / area, 1)) else: data.putpixel(p, Color(*[c * 4 for c in clist[min(64 ** 3 * i / area, 64 ** 3 - 1)]])) if gif and (i + 1) % gifres == 0: images.append(data.save("", False)) i += 1 filename = str((length, height)) + " " + gstr + " spanningtree" + str(treefunc) + " " + func + " " + colorfunc + " " + str( rand) + " " + str((v0, v1)) + " " + str(int(time())) ret = data.save(filename, save) if gif: writegif(filename, images, 0.001) return ret
def newtondist(f, vw=ViewWindow(), df=None, n=1, pxdist=1, filename="newtondist" + str(int(time()))): images = [] if df is None: df = diffquo(f, .0001) col = Color.colors(vw.dimx * vw.dimy) for k in range(6): data = PlotData(vw) for i in range(vw.dimx): for j in range(vw.dimy): if i % pxdist == 0 and j % pxdist == 0: z0 = vw.xpxcart(i) + 1j * vw.ypxcart(j) z = newton(f, z0, df, n, 1e-1) z0 = newton(f, z0, df, n - 1, 1e-1) data.putpoint(((k * z.real + (5 - k) * z0.real) / 5, (k * z.imag + (5 - k) * z0.imag) / 5), col[i + j * vw.dimx]) images.append(data.save("", False)) writegif(filename, images, 1) return images[-1]
def gridspanningtree(length, height, d=2, r=0, color=Color(255, 0, 0), g="grid", filename=None, treefunc="dfs", gif=False, save=True): images = [] if not isinstance(g, Graph): (x, y) = (randint(0, length - 1), randint(0, height - 1)) g = getattr(Graph, g)(length, height).treefunc(treefunc, (x, y)) else: (x, y) = list(g.dict.keys)[0] vw = ViewWindow(dimx=d * length + d - 1, dimy=d * height + d - 1) data = PlotData(vw) color2 = 0.8 * color for v1 in g.dict: for i in range(-r, r + 1): for j in range(-r, r + 1): data.putpixel( (d * (v1[0] + 1) - 1 + i, d * (v1[1] + 1) - 1 + j), color) s = {(x, y)} q = [(x, y)] while q: v1 = q.pop(0) for v2 in g.dict[v1]: if v2 not in s: for i in range(1 + r, d - r): data.putpixel( (d * (v1[0] + 1) - 1 + (v2[0] - v1[0]) * i, d * (v1[1] + 1) - 1 + (v2[1] - v1[1]) * i), color2) q.append(v2) s.add(v2) if gif: images.append(data.save("", False)) if filename is None: filename = "gst " + str((length, height)) + " " + str(int(time())) ret = data.save(filename, save) if gif: writegif(filename, images, 0.01) return ret
def juliazoom(f, vw, z0=0, factor=1.5, frames=20, n=50, duration=0.5, zmax=2, color=Color(255, 0, 0), filename=None): images = [] for i in range(frames): vw = ViewWindow(z0.real - factor**-i, z0.real + factor**-i, z0.imag - factor**-i, z0.imag + factor**-i, dimx=vw.dimx, dimy=vw.dimy) images.append(julia(f, vw, n, zmax, color, filename, False)) if filename is None: filename = "julia " + str(n) + " " + str(zmax) + " " + str(int(time())) writegif(filename, images, duration)
def graphpict(length, height, graphtype="grid", treefunc="dfs", func="bfs", colorfunc="hilbert", gif=False, gifres=None, v0=None, v1=None, rand=True, save=True, scales=None): images = [] clist = [] if isinstance(graphtype, Graph): area = len(graphtype.dict) else: area = length * height if not colorfunc.startswith("rbow"): clist = colorcube(colorfunc) if func == "hilbert": l = hilbert(int(ceil(log(max(length, height), 2)))) gstr = "None" else: if not isinstance(graphtype, Graph): gstr = graphtype graphtype = getattr(Graph, graphtype)(length, height) else: gstr = "Graph" if v0 is None: v0 = choice(graphtype.dict.keys()) if treefunc is not None: if v1 is None: v1 = choice(graphtype.dict.keys()) graphtype = graphtype.treefunc(treefunc, v1, rand) print "Computed spanning tree" l = graphtype.func(func, v0, rand) if gif and gifres is None: gifres = ceil(area / 150.0) vw = ViewWindow(dimx=length, dimy=height) data = PlotData(vw, scales=scales) i = 0 for p in l: if colorfunc.startswith("rbow"): data.putpixel(p, getattr(Color, colorfunc)(2 * pi * i / area, 1)) else: data.putpixel( p, Color( *[c * 4 for c in clist[min(64**3 * i / area, 64**3 - 1)]])) if gif and (i + 1) % gifres == 0: images.append(data.save("", False)) i += 1 filename = str((length, height)) + " " + gstr + " spanningtree" + str( treefunc) + " " + func + " " + colorfunc + " " + str(rand) + " " + str( (v0, v1)) + " " + str(int(time())) ret = data.save(filename, save) if gif: writegif(filename, images, 0.001) return ret