def plot(self,a,b): """ Enables easy plotting of all the Bessel functions directly from the Bessel class. TESTS:: sage: plot(Bessel(2),3,4) sage: Bessel(2).plot(3,4) sage: P = Bessel(2,'I').plot(1,5) sage: P += Bessel(2,'J').plot(1,5) sage: P += Bessel(2,'K').plot(1,5) sage: P += Bessel(2,'Y').plot(1,5) sage: show(P) """ nu = self.order() s = self.system() t = self.type() if t == "I": f = lambda z: bessel_I(nu,z,s) P = plot(f,a,b) if t == "J": f = lambda z: bessel_J(nu,z,s) P = plot(f,a,b) if t == "K": f = lambda z: bessel_K(nu,z,s) P = plot(f,a,b) if t == "Y": f = lambda z: bessel_Y(nu,z,s) P = plot(f,a,b) return P
def plot(self, a, b): """ Enables easy plotting of all the Bessel functions directly from the Bessel class. TESTS:: sage: plot(Bessel(2),3,4) sage: Bessel(2).plot(3,4) sage: P = Bessel(2,'I').plot(1,5) sage: P += Bessel(2,'J').plot(1,5) sage: P += Bessel(2,'K').plot(1,5) sage: P += Bessel(2,'Y').plot(1,5) sage: show(P) """ nu = self.order() s = self.system() t = self.type() if t == "I": f = lambda z: bessel_I(nu, z, s) P = plot(f, a, b) if t == "J": f = lambda z: bessel_J(nu, z, s) P = plot(f, a, b) if t == "K": f = lambda z: bessel_K(nu, z, s) P = plot(f, a, b) if t == "Y": f = lambda z: bessel_Y(nu, z, s) P = plot(f, a, b) return P
def return_plot(self, interval=(0,1), adaptive_recursion=4, plot_points=4, adaptive_tolerance=0.10): r""" Return a plot of percolation probability using basic sage plot settings. INPUT: - ``interval``, default=(0,1) - ``adaptive_recursion``, default=0 - ``plot_points``, default=10 - ``adaptive_tolerance`` default=0.10 EXAMPLES:: sage: from slabbe import PercolationProbability sage: T = PercolationProbability(d=2, n=10, stop=100) sage: T.return_plot() # optional long Graphics object consisting of 1 graphics primitive """ P = plot(self, interval, adaptive_recursion=adaptive_recursion, plot_points=plot_points, adaptive_tolerance=adaptive_tolerance) P += text(repr(self), (0.8,0.2)) return P
def plot_error_function(model_filename, N, x0, Tfrac=0.8): """ Plot the estimated error of the linearized ODE as a function of time. INPUT: - ``model_filename`` -- string containing the model in text format - ``N`` -- truncation order - ``x0`` -- initial point, a list - ``Tfrac`` -- (optional, default: `0.8`): fraction of the convergence radius, to specify the plotting range in the time axis NOTE: This function calls ``error_function`` for the error computations. """ from sage.plot.graphics import Graphics from sage.plot.plot import plot from sage.plot.line import line [Ts, eps] = error_function(model_filename, N, x0) P = Graphics() P = plot(eps, 0, Ts * Tfrac, axes_labels=["$t$", r"$\mathcal{E}(t)$"]) P += line([[Ts, 0], [Ts, eps(t=Ts * Tfrac)]], linestyle='dotted', color='black') return P
def return_plot(self, interval=(0, 1), adaptive_recursion=4, plot_points=4, adaptive_tolerance=0.10): r""" Return a plot of percolation probability using basic sage plot settings. INPUT: - ``interval``, default=(0,1) - ``adaptive_recursion``, default=0 - ``plot_points``, default=10 - ``adaptive_tolerance`` default=0.10 EXAMPLES:: sage: from slabbe import PercolationProbability sage: T = PercolationProbability(d=2, n=10, stop=100) sage: T.return_plot() # optional long Graphics object consisting of 1 graphics primitive """ P = plot(self, interval, adaptive_recursion=adaptive_recursion, plot_points=plot_points, adaptive_tolerance=adaptive_tolerance) P += text(repr(self), (0.8, 0.2)) return P
def plot(self, show_box=False, colors=["white","lightgray","darkgray"]): r""" Return a plot of ``self``. INPUT: - ``show_box`` -- boolean (default: ``False``); if ``True``, also shows the visible tiles on the `xy`-, `yz`-, `zx`-planes - ``colors`` -- (default: ``["white", "lightgray", "darkgray"]``) list ``[A, B, C]`` of 3 strings representing colors EXAMPLES:: sage: PP = PlanePartition([[4,3,3,1],[2,1,1],[1,1]]) sage: PP.plot() Graphics object consisting of 27 graphics primitives """ x = self._max_x y = self._max_y z = self._max_z from sage.functions.trig import cos, sin from sage.plot.polygon import polygon from sage.symbolic.constants import pi from sage.plot.plot import plot Uside = [[0,0], [cos(-pi/6),sin(-pi/6)], [0,-1], [cos(7*pi/6),sin(7*pi/6)]] Lside = [[0,0], [cos(-pi/6),sin(-pi/6)], [cos(pi/6),sin(pi/6)], [0,1]] Rside = [[0,0], [0,1], [cos(5*pi/6),sin(5*pi/6)], [cos(7*pi/6),sin(7*pi/6)]] Xdir = [cos(7*pi/6), sin(7*pi/6)] Ydir = [cos(-pi/6), sin(-pi/6)] Zdir = [0, 1] def move(side, i, j, k): return [[P[0]+i*Xdir[0]+j*Ydir[0]+k*Zdir[0], P[1]+i*Xdir[1]+j*Ydir[1]+k*Zdir[1]] for P in side] def add_topside(i, j, k): return polygon(move(Uside,i,j,k), edgecolor="black", color=colors[0]) def add_leftside(i, j, k): return polygon(move(Lside,i,j,k), edgecolor="black", color=colors[1]) def add_rightside(i, j, k): return polygon(move(Rside,i,j,k), edgecolor="black", color=colors[2]) TP = plot([]) for r in range(len(self.z_tableau())): for c in range(len(self.z_tableau()[r])): if self.z_tableau()[r][c] > 0 or show_box: TP += add_topside(r, c, self.z_tableau()[r][c]) for r in range(len(self.y_tableau())): for c in range(len(self.y_tableau()[r])): if self.y_tableau()[r][c] > 0 or show_box: TP += add_rightside(c, self.y_tableau()[r][c], r) for r in range(len(self.x_tableau())): for c in range(len(self.x_tableau()[r])): if self.x_tableau()[r][c] > 0 or show_box: TP += add_leftside(self.x_tableau()[r][c], r, c) TP.axes(show=False) return TP
def plot_vector_field3d(functions, xrange, yrange, zrange, plot_points=5, colors='jet', center_arrows=False, **kwds): r""" Plot a 3d vector field INPUT: - ``functions`` - a list of three functions, representing the x-, y-, and z-coordinates of a vector - ``xrange``, ``yrange``, and ``zrange`` - three tuples of the form (var, start, stop), giving the variables and ranges for each axis - ``plot_points`` (default 5) - either a number or list of three numbers, specifying how many points to plot for each axis - ``colors`` (default 'jet') - a color, list of colors (which are interpolated between), or matplotlib colormap name, giving the coloring of the arrows. If a list of colors or a colormap is given, coloring is done as a function of length of the vector - ``center_arrows`` (default False) - If True, draw the arrows centered on the points; otherwise, draw the arrows with the tail at the point - any other keywords are passed on to the plot command for each arrow EXAMPLES:: sage: x,y,z=var('x y z') sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi)) Graphics3d Object sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),colors=['red','green','blue']) Graphics3d Object sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),colors='red') Graphics3d Object sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),plot_points=4) Graphics3d Object sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),plot_points=[3,5,7]) Graphics3d Object sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),center_arrows=True) Graphics3d Object TESTS: This tests that :trac:`2100` is fixed in a way compatible with this command:: sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),center_arrows=True,aspect_ratio=(1,2,1)) Graphics3d Object """ (ff,gg,hh), ranges = setup_for_eval_on_grid(functions, [xrange, yrange, zrange], plot_points) xpoints, ypoints, zpoints = [srange(*r, include_endpoint=True) for r in ranges] points = [vector((i,j,k)) for i in xpoints for j in ypoints for k in zpoints] vectors = [vector((ff(*point), gg(*point), hh(*point))) for point in points] try: from matplotlib.cm import get_cmap cm = get_cmap(colors) except (TypeError, ValueError): cm = None if cm is None: if isinstance(colors, (list, tuple)): from matplotlib.colors import LinearSegmentedColormap cm = LinearSegmentedColormap.from_list('mymap',colors) else: cm = lambda x: colors max_len = max(v.norm() for v in vectors) scaled_vectors = [v/max_len for v in vectors] if center_arrows: G = sum([plot(v,color=cm(v.norm()),**kwds).translate(p-v/2) for v,p in zip(scaled_vectors, points)]) G._set_extra_kwds(kwds) return G else: G = sum([plot(v,color=cm(v.norm()),**kwds).translate(p) for v,p in zip(scaled_vectors, points)]) G._set_extra_kwds(kwds) return G
def plot_holes(self): myplot = plot([]) for h in self._holes: myplot += h.plot() return myplot
def plot(self,color = 'red'): r = 2*self._radius a = self._center[0] b = self._center[1] Pts = [(a,b+r),(a+r,b),(a,b-r),(a-r,b)] return plot(polygon(Pts), color=color)
def plot_center(self, color = 'red'): return plot(point(self._center, color=color))
def plot(self,color='blue'): return plot(polygon(self.corners()))
def plot_vector_field3d(functions, xrange, yrange, zrange, plot_points=5, colors="jet", center_arrows=False, **kwds): r""" Plot a 3d vector field INPUT: - ``functions`` - a list of three functions, representing the x-, y-, and z-coordinates of a vector - ``xrange``, ``yrange``, and ``zrange`` - three tuples of the form (var, start, stop), giving the variables and ranges for each axis - ``plot_points`` (default 5) - either a number or list of three numbers, specifying how many points to plot for each axis - ``colors`` (default 'jet') - a color, list of colors (which are interpolated between), or matplotlib colormap name, giving the coloring of the arrows. If a list of colors or a colormap is given, coloring is done as a function of length of the vector - ``center_arrows`` (default False) - If True, draw the arrows centered on the points; otherwise, draw the arrows with the tail at the point - any other keywords are passed on to the plot command for each arrow EXAMPLES:: sage: x,y,z=var('x y z') sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi)) sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),colors=['red','green','blue']) sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),colors='red') sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),plot_points=4) sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),plot_points=[3,5,7]) sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),center_arrows=True) TESTS: This tests that :trac:`2100` is fixed in a way compatible with this command:: sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),center_arrows=True,aspect_ratio=(1,2,1)) """ (ff, gg, hh), ranges = setup_for_eval_on_grid(functions, [xrange, yrange, zrange], plot_points) xpoints, ypoints, zpoints = [srange(*r, include_endpoint=True) for r in ranges] points = [vector((i, j, k)) for i in xpoints for j in ypoints for k in zpoints] vectors = [vector((ff(*point), gg(*point), hh(*point))) for point in points] try: from matplotlib.cm import get_cmap cm = get_cmap(colors) except (TypeError, ValueError): cm = None if cm is None: if isinstance(colors, (list, tuple)): from matplotlib.colors import LinearSegmentedColormap cm = LinearSegmentedColormap.from_list("mymap", colors) else: cm = lambda x: colors max_len = max(v.norm() for v in vectors) scaled_vectors = [v / max_len for v in vectors] if center_arrows: return sum([plot(v, color=cm(v.norm()), **kwds).translate(p - v / 2) for v, p in zip(scaled_vectors, points)]) else: return sum([plot(v, color=cm(v.norm()), **kwds).translate(p) for v, p in zip(scaled_vectors, points)])
def plot_time_triggered(model, N, x0, tini, T, NRESETS, NPOINTS, j, **kwargs): r""" Solve and plot the time-triggered algorithm for Carleman linarization. INPUT: - ``model`` -- PolynomialODE - ``N`` -- integer, truncation order - ``x0`` -- list, initial condition - ``tini`` -- initial time - ``T`` -- final time - ``NRESETS`` -- integer, fixed number of resets - ``NPOINTS`` -- integer, number of computation points - ``j`` -- integer in `0\ldots n-1`, variable to plot against time OUTPUT: Solution as a collection of lists, each list corresponding to the solution of a given chunk. NOTES: Other optional keyword argument include: - ``color`` -- color to be plotted """ G = Graphics() if 'color' in kwargs: color=kwargs['color'] else: color='blue' Fj = get_Fj_from_model(model.funcs(), model.dim(), model.degree()) AN = truncated_matrix(N, *Fj, input_format="Fj_matrices") solution = solve_time_triggered(AN, N, x0, tini, T, NRESETS, NPOINTS) # number of samples in each chunk CHUNK_SIZE = int(NPOINTS/(NRESETS+1)) tdom = srange(tini, T, (T-tini)/(NPOINTS-1)*1., include_endpoint=True) tdom_k = tdom[0:CHUNK_SIZE] G += list_plot(zip(tdom_k, solution[0][:, j]), plotjoined=True, linestyle="dashed", color=color, legend_label="$N="+str(N)+", r="+str(NRESETS)+"$") for i in range(1, NRESETS+1): # add point at switching time? G += point([tdom_k[0], x0_k[1]], size=25, marker='x', color=color) # add solution for this chunk G += list_plot(zip(tdom_k, solution[i][:, j]), plotjoined=True, linestyle="dashed", color=color) # add numerical solution of the nonlinear ODE # solution of the nonlinear ODE S = model.solve(x0=x0, tini=tini, T=T, NPOINTS=NPOINTS) x_t = lambda i : S.interpolate_solution(i) G += plot(x_t(j), tini, T, axes_labels = ["$t$", "$x_{"+str(j)+"}$"], gridlines=True, color="black") return G