Instantly share code, notes, and snippets.

# catchmrbharath/plot.py

Created May 14, 2012 17:02
Show Gist options
• Save catchmrbharath/2695079 to your computer and use it in GitHub Desktop.
interval arithmetic plotting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import sympy.mpmath as mp from sympy import * from sympy.utilities import lambdify import matplotlib.pyplot as plt import math import fractions import time #supports the following functions. MPI = {'sin': mp.iv.sin, 'cos': mp.iv.cos, 'exp': mp.iv.exp, 'log': mp.iv.log, 'sqrt': mp.iv.sqrt, 'log10': mp.iv.log10} x,y = symbols('x y') func = lambdify((x, y),(y > 1 / x),MPI) def plotImplicit(func, left, right, top, bottom): W = 1024 # No. of horizontal pixels H = 1024 # No. of vertical pixels k = fractions.gcd(W, H) k = int(math.log(fractions.gcd(k, 2**int(math.log(k, 2))))) IntervalList = [] PlotList = [] a = 0 while a*2**k < W: b = 0 while b*2**k < H: xInter = mp.mpi(left + a * 2**k * (right - left)/W, left + (a + 1) * 2**k * (right - left) / W) yInter = mp.mpi(bottom + b * 2**k * (top - bottom) / H, bottom + (b + 1) * 2**k * (top - bottom) / H) IntervalList.append([xInter,yInter]) b += 1 a += 1 while k > 0 and len(IntervalList): IntervalList, plot = refinePixels(func, IntervalList) PlotList.extend(plot) k = k - 1 print k ylist2 = [] plt.figure() plt.axis([left, right, bottom, top]) PlotList.extend(IntervalList) plots = PlotList[0] xlist = [] ylist = [] for plots in PlotList: s, t = plots[0]._mpi_ s = mp.libmp.to_float(s) t = mp.libmp.to_float(t) u, v = plots[1]._mpi_ u = mp.libmp.to_float(u) v = mp.libmp.to_float(v) xlist.extend([s, s, t, t, None]) ylist.extend([u, v, v, u, None]) plt.fill(xlist,ylist, facecolor = 'b', alpha = 0.3, edgecolor = 'None') def refinePixels(func, IntervalList): tempIntervalList = [] plotList = [] for intervalList in IntervalList: if func(intervalList[0], intervalList[1]) == True: plotList.append([intervalList[0], intervalList[1]]) elif func(intervalList[0], intervalList[1]) == None: #divide into 4 sub regions s, t = intervalList[0]._mpi_ s = mp.libmp.to_float(s) t = mp.libmp.to_float(t) u, v = intervalList[1]._mpi_ u = mp.libmp.to_float(u) v = mp.libmp.to_float(v) avgx = (s + t) / 2.0 avgy = (u + v) / 2.0 #create intervals a = mp.mpi(s, avgx) b = mp.mpi(avgx, t) c = mp.mpi(u, avgy) d = mp.mpi(avgy, v) tempIntervalList.append([a,c]) tempIntervalList.append([a,d]) tempIntervalList.append([b,c]) tempIntervalList.append([b,d]) return tempIntervalList, plotList plotImplicit(func, -1.0, 1.0, 5.0, -5.0 ) plt.show()

### Krastanov commented May 18, 2012

Just a quick warning about something that will create problems during the review. You are using camelCase naming conventions when most of sympy is following the pythonic conventions (checkout PEP8). For instance: CamelCase for names of classes, underlines for names of functions and variables.

### catchmrbharath commented May 18, 2012

Thanks. Will change it.