# Ticket #1833: trac-1833.patch

File trac-1833.patch, 7.9 KB (added by was, 15 years ago)
• ## sage/plot/plot3d/all.py

# HG changeset patch
# User William Stein <wstein@gmail.com>
# Date 1200673284 28800
# Node ID b9a91aae0cef44760155acd7580732ed07174650
# Parent  ed92b4e4e4ea6f35cd7fa529c5a242df3cf4b373
trac #1833 and #1737 -- 3d plot optimizations

diff -r ed92b4e4e4ea -r b9a91aae0cef sage/plot/plot3d/all.py
 a from plot3d            import plot3d, plot3d_adaptive from plot3d            import plot3d from parametric_plot3d import parametric_plot3d from list_plot3d       import list_plot3d
• ## sage/plot/plot3d/parametric_plot3d.py

diff -r ed92b4e4e4ea -r b9a91aae0cef sage/plot/plot3d/parametric_plot3d.py
 a def parametric_plot3d(f, urange, vrange= INPUT: f -- a 3-tuple of functions or expressions urange -- a 2-tuple (u_min, u_max) or a 3-tuple (u, u_min, u_max) vrange -- (optional -- only used for surfaces) a 2-tuple (u_min, u_max) or a 3-tuple (u, u_min, u_max) vrange -- (optional -- only used for surfaces) a 2-tuple (v_min, v_max) or a 3-tuple (v, v_min, v_max) plot_points -- (default: "automatic", which is 75 for curves and [15,15] for surfaces) initial number of sample points in each parameter; an integer for a curve, def parametric_plot3d_curve(f, urange, p u, vals = var_and_list_of_values(urange, plot_points) w = [] fail = 0 if u is None: try: f, u, v = adapt_if_symbolic(f) except TypeError: pass if u is None: f_x, f_y, f_z = f for t in vals: def parametric_plot3d_surface(f, urange, u, u_vals = var_and_list_of_values(urange, int(points0)) v, v_vals = var_and_list_of_values(vrange, int(points1)) if u is None and v is None: try: f, u, v = adapt_if_symbolic(f) except TypeError: pass if u is None: if not v is None: raise ValueError, "both ranges must specify a variable or neither must" def parametric_plot3d_surface(f, urange, return (float(f_x(x,y)), float(f_y(x,y)), float(f_z(x,y))) return ParametricSurface(g, (u_vals, v_vals), **kwds) def adapt_if_symbolic(f): """ If f is symbolic find the variables u, v to substitute into f. Otherwise raise a TypeError. This function is used internally by the plot commands for efficiency reasons only. """ from sage.calculus.calculus import is_SymbolicExpression, SR if sum([is_SymbolicExpression(a) for a in f]) > 0: g = [SR(a) for a in f] vars = list(set(sum([list(a.variables()) for a in g], []))) vars.sort() if len(vars) > 0: u = vars[0] if len(vars) > 1: v = vars[1] else: v = None return g, u, v else: g = [lambda x: float(a) for a in g] return g, None, None
• ## sage/plot/plot3d/plot3d.py

diff -r ed92b4e4e4ea -r b9a91aae0cef sage/plot/plot3d/plot3d.py
 a r""" r""" Plotting Functions. EXAMPLES: EXAMPLES: sage: def f(x,y): ...       return math.sin(y*y+x*x)/math.sqrt(x*x+y*y+.0001) ... sage: P = plot3d_adaptive(f,(-3,3),(-3,3), color=rainbow(60, 'rgbtuple'), max_bend=.1, max_depth=15) sage: P = plot3d(f,(-3,3),(-3,3), adaptive=True, color=rainbow(60, 'rgbtuple'), max_bend=.1, max_depth=15) sage: P.show() sage: def f(x,y): ...       return math.exp(x/5)*math.sin(y) ... sage: P = plot3d_adaptive(f,(-5,5),(-5,5), color=['red','yellow']) sage: P = plot3d(f,(-5,5),(-5,5), adaptive=True, color=['red','yellow']) sage: from sage.plot.plot3d.plot3d import axes sage: S = P + axes(6, color='black') sage: S.show() We plot "cape man": sage: S += sphere((.45, .1,.15),size=.1, color='white') + sphere((.51, .1,.17), size=.05, color='black') sage: S += sphere((.5,0,-.2),size=.1, color='yellow') sage: def f(x,y): return math.exp(x/5)*math.cos(y) sage: P = plot3d_adaptive(f,(-5,5),(-5,5), ['red','yellow'], max_depth=10) sage: P = plot3d(f,(-5,5),(-5,5), adaptive=True, color=['red','yellow'], max_depth=10) sage: cape_man = P.scale(.2) + S.translate(1,0,0) sage: cape_man sage: cape_man.show(aspect_ratio=[1,1,1]) AUTHOR: -- Tom Boothby: adaptive refinement triangles class TrivialTriangleFactory: return [a,b,c] import parametric_plot3d def plot3d(f, urange, vrange, **kwds): def plot3d(f, urange, vrange, adaptive=False, **kwds): """ INPUT: f -- a symbolic expression or function of 2 variables urange -- a 2-tuple (u_min, u_max) or a 3-tuple (u, u_min, u_max) vrange -- a 2-tuple (v_min, v_max) or a 3-tuple (v, v_min, v_max) adaptive -- (default: False) whether to use adaptive refinement to draw the plot (slower, but may look better) EXAMPLES: We plot a 3d function defined as a Python function: sage: plot3d(lambda x, y: x^2 + y^2, (-2,2), (-2,2)) We plot the same 3d function but using adaptive refinement: sage: plot3d(lambda x, y: x^2 + y^2, (-2,2), (-2,2), adaptive=True) Adaptive refinement but with more points: sage: plot3d(lambda x, y: x^2 + y^2, (-2,2), (-2,2), adaptive=True, initial_depth=5) We plot some 3d symbolic functions: sage: x, y = var('x,y') def plot3d(f, urange, vrange, **kwds): Two wobby translucent planes: sage: x,y = var('x,y') sage: P = plot3d(x+y+sin(x*y), (x,-10,10),(y,-10,10), opacity=0.87) sage: P = plot3d(x+y+sin(x*y), (x,-10,10),(y,-10,10), opacity=0.87, color='blue') sage: Q = plot3d(x-2*y-cos(x*y),(x,-10,10),(y,-10,10),opacity=0.3,color='red') sage: P + Q def plot3d(f, urange, vrange, **kwds): sage: L + P + Q """ if len(urange) == 2: w = (lambda u,v: u, lambda u,v: v, f) from parametric_plot3d import adapt_if_symbolic try: f, u,v = adapt_if_symbolic((f,1,1)) f = f[0] w = (u, v, f) urange = (u, urange[0], urange[1]) vrange = (v, vrange[0], vrange[1]) except TypeError: w = (lambda u,v: u, lambda u,v: v, f) else: u = urange[0] v = vrange[0] w = (u, v, f) P = parametric_plot3d.parametric_plot3d(w, urange, vrange, **kwds) if adaptive: P = plot3d_adaptive(f, urange, vrange, **kwds) else: P = parametric_plot3d.parametric_plot3d(w, urange, vrange, **kwds) P.frame_aspect_ratio([1.0,1.0,0.5]) return P def plot3d_adaptive(f, x_range, y_range, color="automatic", grad_f=None, max_bend=.5, max_depth=5, initial_depth=4, num_colors=128, **kwds): """ r""" Adaptive 3d plotting of a function of two variables. This is used internally by the plot3d command when the option \code{adaptive=True} is given. INPUT: f -- a symbolic function or a Python function of 3 variables. def plot3d_adaptive(f, x_range, y_range, **kwds -- standard graphics parameters EXAMPLES: We plot sin(xy): We plot $\sin(xy)$: sage: from sage.plot.plot3d.plot3d import plot3d_adaptive sage: x,y=var('x,y'); plot3d_adaptive(sin(x*y), (x,-pi,pi), (y,-pi,pi), initial_depth=5) """