Changeset 7903:439e75ca1c61


Ignore:
Timestamp:
01/01/08 02:24:35 (5 years ago)
Author:
William Stein <wstein@…>
Branch:
default
Message:

3d plotting -- more work on parametric plotting.

Location:
sage
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sage/plot/plot3d/parametric_plot3d.py

    r7902 r7903  
    4545    We demonstrate each of the four ways to call this function. 
    4646 
    47     1. A space curve defined by three functions of 1 variable: 
    48         sage: show(parametric_plot3d( (sin, cos, lambda u: u/10), (0, 20))) 
    49  
    50     Note above the lambda function, which creates a callable Python function 
    51     that sends u to u/10. 
    52  
    53     2. Next we draw the same plot as above, but using symbolic functions: 
    54         sage: var('u') 
    55         sage: show(parametric_plot3d( (sin(u), cos(u), u/10), (u, 0, 20))) 
    56  
    57     3. We draw a parametric surface using 3 Python functions (defined using 
    58        lambda): 
     47        1. A space curve defined by three functions of 1 variable: 
     48            sage: show(parametric_plot3d( (sin, cos, lambda u: u/10), (0, 20))) 
     49 
     50        Note above the lambda function, which creates a callable Python function 
     51        that sends u to u/10. 
     52 
     53        2. Next we draw the same plot as above, but using symbolic functions: 
     54            sage: var('u') 
     55            sage: show(parametric_plot3d( (sin(u), cos(u), u/10), (u, 0, 20))) 
     56 
     57        3. We draw a parametric surface using 3 Python functions (defined using 
     58           lambda): 
     59            sage: f = (lambda u,v: cos(u), lambda u,v: sin(u)+cos(v), lambda u,v: sin(v)) 
     60            sage: show(parametric_plot3d(f, (0, 2*pi), (-pi, pi)))                  
     61 
     62        4. The same surface, but where the defining functions are symbolic: 
     63            sage: var('u,v') 
     64            sage: show(parametric_plot3d((cos(u), sin(u) + cos(v), sin(v)), (u, 0, 2*pi), (v, -pi, pi))) 
     65 
     66    We call the space curve function but with polynomials instead of 
     67    symbolic variables. 
     68 
     69        sage: R.<t> = RDF[] 
     70        sage: show(parametric_plot3d( (t, t^2, t^3), (t, 0, 3) ) ) 
    5971         
    60      
    61     4. The same surface, but where the defining functions are symbolic: 
    62         sage: var('u,v') 
    63         sage: show(parametric_plot3d((cos(u), sin(u) + cos(v), sin(v)), (u, 0, 2*pi), (v, -pi, pi))) 
     72    Next we plot the same curve, but because we use (0, 3) instead of (t, 0, 3), 
     73    each polynomial is viewed as a callable function of one variable: 
     74 
     75        sage: show(parametric_plot3d( (t, t^2, t^3), (0, 3) ) ) 
     76 
     77    We do a plot but mix a symbolic input, and an integer: 
     78        sage: var('t') 
     79        sage: show(parametric_plot3d( (1, sin(t), cos(t)), (t, 0, 3) ) ) 
     80 
     81    We plot two interlinked tori: 
     82        sage: f1 = (4+(3+cos(v))*sin(u), 4+(3+cos(v))*cos(u), 4+sin(v)) 
     83        sage: f2 = (8+(3+cos(v))*cos(u), 3+sin(v), 4+(3+cos(v))*sin(u)) 
     84        sage: p1 = parametric_plot3d(f1, (u,0,2*pi), (v,0,2*pi), texture="red") 
     85        sage: p2 = parametric_plot3d(f2, (u,0,2*pi), (v,0,2*pi), texture="blue") 
     86        sage: show(p1 + p2)     
    6487    """ 
    6588    # TODO: 
     
    102125    fail = 0 
    103126     
    104     f_x, f_y, f_z = f 
    105127    if u is None: 
     128        f_x, f_y, f_z = f 
    106129        for t in vals: 
    107130            try: 
     
    110133                fail += 1 
    111134    else: 
     135        f_x, f_y, f_z = [ensure_subs(m) for m in f] 
    112136        for t in vals: 
    113137            try: 
    114                 w.append((float(f_x.substitute({u:t})), float(f_y.substitute({u:t})), 
    115                           float(f_z.substitute({u:t})))) 
     138                w.append((float(f_x.subs({u:t})), float(f_y.subs({u:t})), 
     139                          float(f_z.subs({u:t})))) 
    116140            except TypeError: 
    117141                fail += 1 
     142                 
    118143    if fail > 0: 
    119144        print "WARNING: Failed to evaluate parametric plot at %s points"%fail 
     
    136161        if v is None: 
    137162            raise ValueError, "both ranges must specify a variable or neither must" 
    138         f0, f1, f2 = f 
     163        f0, f1, f2 = [ensure_subs(w) for w in f] 
    139164        def f_x(uu,vv): 
    140             return float(f0.substitute({u:uu, v:vv})) 
     165            return float(f0.subs({u:uu, v:vv})) 
    141166        def f_y(uu,vv): 
    142             return float(f1.substitute({u:uu, v:vv})) 
     167            return float(f1.subs({u:uu, v:vv})) 
    143168        def f_z(uu,vv): 
    144             return float(f2.substitute({u:uu, v:vv})) 
     169            return float(f2.subs({u:uu, v:vv})) 
    145170 
    146171    def g(x,y): 
     
    191216        v.append(b) 
    192217    return v 
     218 
     219 
     220def ensure_subs(f): 
     221    if not hasattr(f, 'subs'): 
     222        from sage.calculus.all import SR 
     223        return SR(f) 
     224    return f 
  • sage/rings/polynomial/polynomial_element.pyx

    r7618 r7903  
    260260            sage: f.subs(5) 
    261261            127 
    262         """ 
     262            sage: f.subs({x:2}) 
     263            7 
     264            sage: f.subs({}) 
     265            x^3 + x - 3             
     266            sage: f.subs({'x':2}) 
     267            Traceback (most recent call last): 
     268            ... 
     269            TypeError: keys do not match self's parent 
     270        """ 
     271        if len(x) == 1 and isinstance(x[0], dict): 
     272            g = self.parent().gen() 
     273            if x[0].has_key(g): 
     274                return self(x[0][g]) 
     275            elif len(x[0]) > 0: 
     276                raise TypeError, "keys do not match self's parent" 
     277            return self 
    263278        return self.__call__(*x, **kwds) 
    264279         
  • sage/server/notebook/cell.py

    r7902 r7903  
    601601                else: 
    602602                    size = 200 
    603                 popup  = """<br><a href="javascript:jmol_popup('%s');">Enlarge 3d View</a>"""%url 
     603                popup  = """<br><a href="javascript:jmol_popup('%s');">Enlarge</a>"""%url 
    604604                script = '<script>jmol_applet(%s, "%s");</script>%s' % (size, url, popup) 
    605605                images.append(script) 
  • sage/server/notebook/js.py

    r7902 r7903  
    4646 
    4747function jmol_popup(url) { 
    48     win = window.open ("", url, "width=400,height=400,resizable=1"); 
     48    win = window.open ("", "jmol viewer", "width=400,height=400,resizable=1,statusbar=0"); 
     49    win.document.body.innerHTML = ""; 
     50    win.document.title = "Sage 3d Viewer"; 
     51    win.document.writeln("<h1 align=center>Sage 3d Viewer</h1>"); 
    4952    jmolSetDocument(win.document); 
    5053    jmolApplet("100%", "script" + url); 
     54    win.focus();  
    5155} 
    5256    """ 
     
    821825    window.open ("/history",  
    822826      "", "menubar=1,scrollbars=1,width=800,height=600, toolbar=1,resizable=1"); 
    823  
    824827} 
    825828 
Note: See TracChangeset for help on using the changeset viewer.