PREP Quickstart Interact
system:sage


<h1>Sage Interact Quickstart</h1>
<p><span id="cell_outer_0">This&nbsp;<a href="http://www.sagemath.org/" target="_blank">Sage</a>&nbsp;worksheet was developed for the MAA PREP  Workshop "Sage: Using Open-Source Mathematics Software with  Undergraduates" (funding provided by NSF DUE 0817071).</span></p>
<p>Invaluable resources are the Sage wiki <a href="http://wiki.sagemath.org/interact" target="_blank">http://wiki.sagemath.org/interact</a> (type "sage interact" into Google) and the <a href="http://sagemath.org/doc/reference/sagenb/notebook/interact.html#sagenb.notebook.interact.interact" target="_blank">interact documentation</a>.</p>
<p>&nbsp;</p>

{{{id=27|
plot(x^2,(x,-3,3))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=28|
f=x^2
plot(f,(x,-3,3))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Note the "show" that is needed.</p>

{{{id=29|
def myplot(f=x^2):
    show(plot(f,(x,-3,3)))
///
}}}

{{{id=30|
myplot()
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=32|
myplot(x^3)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=35|
@interact
def myplot(f=x^2):
    show(plot(f,(x,-3,3)))
///
}}}

{{{id=15|
@interact
def _(f=x^2):
    show(plot(f,(x,-3,3)))
///
}}}

{{{id=21|
@interact
def _(f=input_box(x^2,width=20)):
    show(plot(x^2,(x,-3,3)))
///
}}}

{{{id=19|
@interact
def _(f=input_box(x^2,width=20), color=color_selector(widget='colorpicker')):
    show(plot(x^2,(x,-3,3), color=color))
///
}}}

{{{id=37|

///
}}}

{{{id=38|

///
}}}

{{{id=18|
@interact
def _(f=input_box(x^2,width=20), 
color=color_selector(widget='colorpicker', label=""),
axes=True,
fill=True,
zoom=range_slider(-3,3,default=(-3,3))):
    show(plot(x^2,(x,zoom[0], zoom[1]), color=color, axes=axes,fill=fill))
///
}}}

{{{id=24|
@interact(layout=dict(top=[['f', 'color']], 
right=[['axes'],['fill']], 
bottom=[['zoom']]))
def _(f=input_box(x^2,width=20), 
color=color_selector(widget='colorpicker', label=""),
axes=True,
fill=True,
zoom=range_slider(-3,3, default=(-3,3))):
    show(plot(x^2,(x,zoom[0], zoom[1]), color=color, axes=axes,fill=fill))
///
}}}

<h1>Control Types</h1>

<p>Sage has:</p>
<ul>
<li>boxes</li>
<li>sliders</li>
<li>range sliders</li>
<li>checkboxes</li>
<li>selectors (dropdown lists or buttons)</li>
<li>grid of boxes</li>
<li>color selectors</li>
<li>plain text</li>
</ul>
<p>We illustrate some of these.</p>

{{{id=9|
@interact
def _(frame=checkbox(True, label='Use frame')):
    show(plot(sin(x), (x,-5,5)), frame=frame)
///
}}}

{{{id=10|
var('x,y')
colormaps=sage.plot.colors.colormaps.keys()
@interact
def _(cmap=selector(colormaps)):
    contour_plot(x^2-y^2,(x,-2,2),(y,-2,2),cmap=cmap).show()
///
}}}

{{{id=13|
var('x,y')
colormaps=sage.plot.colors.colormaps.keys()
@interact
def _(cmap=selector(['RdBu', 'jet', 'gray','gray_r'],buttons=True),
type=['density','contour']):
    if type=='contour':
        contour_plot(x^2-y^2,(x,-2,2),(y,-2,2),cmap=cmap, aspect_ratio=1).show()
    else:
        density_plot(x^2-y^2,(x,-2,2),(y,-2,2),cmap=cmap, frame=True,axes=False,aspect_ratio=1).show()
///
}}}

{{{id=33|
@interact
def _(n=(1,20)):
    print factorial(n)
///
}}}

{{{id=42|
@interact
def _(n=slider(1,20,step_size=1)):
    print factorial(n)
///
}}}

{{{id=40|
@interact
def _(n=slider([1..20])):
    print factorial(n)
///
}}}

{{{id=41|

///
}}}