Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168733
Image: ubuntu2004

Sage Interact Quickstart

This Sage worksheet was developed for the MAA PREP Workshop "Sage: Using Open-Source Mathematics Software with Undergraduates" (funding provided by NSF DUE 0817071).

Invaluable resources are the Sage wiki http://wiki.sagemath.org/interact (type "sage interact" into Google) and the interact documentation.

 

Start by getting the commands for what you want the output to look like.  Here we just want a simple plot.

plot(x^2,(x,-3,3))

Then abstract out the parts you want to change.  We'll be letting the user change the function, so let's make that a variable ff.

f=x^3 plot(f,(x,-3,3))

Now make this a "def" function.  The "show" or "print" is needed since the output is not automatically printed from within a function.  Note also that we give the variable a default value of x^2.  This is what ff is if the user does not specify a value for ff.

def myplot(f=x^2): show(plot(f,(x,-3,3)))

Let's test the def function myplot by just calling it.

myplot()

If we call it with a different value for f, we should get a different plot.

myplot(x^3)

Now to make a control to enter the function, we just preface the function with @interact.

@interact def myplot(f=x^2): show(plot(f,(x,-3,3)))

Tech tip: Technically what the @interact does is wrap the function, so the above is equivalent to:

def myplot(..): ...

myplot=interact(myplot)

Note that we can still call our function, even when we've used @interact.  This is often useful in debugging it.

myplot(x^4)

We can go ahead and replace other parts of the expression with variables.  Note the "_" is the function name now.  That is a convention for throw-away names that we don't care about.

@interact def _(f=x^2,a=-3,b=3): show(plot(f,(x,a,b)))

If we pass ('label', default_value) in for a control, then the control gets the label when printed.

@interact def _(f=('$f$',x^2),a=('lower',-3),b=('upper',3)): show(plot(f,(x,a,b)))

We can specify the type of control explicitly, along with options.

@interact def _(f=input_box(x^2,width=20, label="$f$")): show(plot(f,(x,-3,3)))

Here's another type of control: a color picker.

@interact def _(f=input_box(x^2,width=20), color=color_selector()): show(plot(f,(x,-3,3), color=color))

Here are a bunch of options.  Notice the new controls:

  • range slider, which passes in two values, zoom[0] and zoom[1]
  • True/False get converted to checkboxes
@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(f,(x,zoom[0], zoom[1]), color=color, axes=axes,fill=fill))

That was a bit ugly because all of the controls were stacked on top of each other.  We can layout the controls in a grid in the top, bottom, left, or right using the 'layout' parameter.

@interact(layout=dict(top=[['f', 'color']], left=[['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(f,(x,zoom[0], zoom[1]), color=color, axes=axes,fill=fill))

Control Types

Sage has:

  • boxes
  • sliders
  • range sliders
  • checkboxes
  • selectors (dropdown lists or buttons)
  • grid of boxes
  • color selectors
  • plain text

We illustrate some of these.

@interact def _(frame=checkbox(True, label='Use frame')): show(plot(sin(x), (x,-5,5)), frame=frame)
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()
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()

By default, ranges are sliders that divide the range into 500 steps (I think that's the right number...)

@interact def _(n=(1,20)): print factorial(n)

You can set the step size to get, for example, just integer values.

@interact def _(n=slider(1,20,step_size=1)): print factorial(n)

Or you can explicitly specify the slider values.

@interact def _(n=slider([1..20])): print factorial(n)

And the slider values don't even have to be numbers!

@interact def _(fun=('function', slider([sin,cos,tan,sec,csc,cot]))): print fun(4.39293)

Matrices are automatically converted to a grid of input boxes.

@interact def _(m=('matrix', identity_matrix(2))): print m.eigenvalues()

Here's how to get vectors from a grid of boxes.

@interact def _(v=('vector', input_grid(1, 3, default=[[1,2,3]], to_value=lambda x: vector(flatten(x))))): print v.norm()

Sometimes we don't want any updates until we specifically say so.  We can use the auto_update=False option for that.

@interact def _(m=('matrix', identity_matrix(2)), auto_update=False): print m.eigenvalues()