︠91ecbf1b-f16c-4e9a-a47c-77ea6459f75ai︠ %hide %html

Cobweb

Sorry, the GeoGebra Applet could not be started. Please make sure that Java 1.4.2 (or later) is installed and active in your browser (Click here to install Java now)

Criado com GeoGebra

Cobweb

\n\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\nSorry, the GeoGebra Applet could not be started. Please make sure that Java 1.4.2 (or later) is installed and active in your browser (Click here to install Java now)\n\n\n\n

Criado com GeoGebra

\n\n"}︡{"stdout": ""}︡ ︠74687bd1-39e9-497b-a98a-51044179d861︠ interact? ︡e8dc9721-c9e5-4f52-bcdd-fc501e6ea963︡{"html": "\n
\n \n

File: /usr/local/sage2/local/lib/python2.6/site-packages/sagenb-0.7.5.1-py2.6.egg/sagenb/notebook/interact.py

\n

Type: <type ‘function’>

\n

Definition: interact(f)

\n

Docstring:

\n
\n

Use interact as a decorator to create interactive Sage notebook\ncells with sliders, text boxes, radio buttons, check boxes, and\ncolor selectors. Simply put @interact on the line before a\nfunction definition in a cell by itself, and choose appropriate\ndefaults for the variable names to determine the types of\ncontrols (see tables below).

\n

INPUT:

\n\n

EXAMPLES:

\n

In each example below we use a single underscore for the function\nname. You can use any name you want; it does not have to\nbe an underscore.

\n

We create an interact control with two inputs, a text input for\nthe variable a and a y slider that runs through the range of\nintegers from 0 to 19.

\n
sage: @interact\n... def _(a=5, y=(0..20)): print a + y\n...\n<html>...\n
\n
\n

Draw a plot interacting with the “continuous” variable a. By\ndefault continuous variables have exactly 50 possibilities.

\n
sage: @interact\n... def _(a=(0,2)):\n...     show(plot(sin(x*(1+a*x)), (x,0,6)), figsize=4)\n<html>...\n
\n
\n

Interact a variable in steps of 1 (we also use an unnamed\nfunction):

\n
sage: @interact\n... def _(n=(10,100,1)):\n...     show(factor(x^n - 1))\n<html>...\n
\n
\n

Interact two variables:

\n
sage: @interact\n... def _(a=(1,4), b=(0,10)):\n...     show(plot(sin(a*x+b), (x,0,6)), figsize=3)\n<html>...\n
\n
\n

Place a block of text among the controls:

\n
sage: @interact\n... def _(t1=text_control("Factors an integer."), n="1"):\n...     print factor(Integer(n))\n<html>...\n
\n
\n

You do not have to use interact as a decorators; you can also\nsimply write interact(f) where f is any Python function\nthat you have defined, though this is frowned upon. E.g., f\ncan also be a library function as long as it is written in\nPython:

\n
sage: interact(matrix)   # put ZZ, 2,2,[1..4] in boxes...\n<html>...\n
\n
\n

If your the time to evaluate your function takes awhile, you may\nnot want to have it reevaluated every time the inputs change. In\norder to prevent this, you can add a keyword auto_update=False to\nyour function to prevent it from updating whenever the values are\nchanged. This will cause a button labeled ‘Update’ to appear\nwhich you can click on to re-evaluate your function.

\n
sage: @interact\n... def _(n=(10,100,1), auto_update=False):\n...     show(factor(x^n - 1))\n<html>...\n
\n
\n

DEFAULTS:

\n

Defaults for the variables of the input function determine\ninteractive controls. The standard controls are input_box,\nslider, range_slider, checkbox, selector,\ninput_grid, and color_selector. There is also a text\ncontrol (see the defaults below).

\n\n

You can also create a color selector by setting the default value for\nan input_box to Color(...).

\n

There are also some convenient defaults that allow you to make\ncontrols automatically without having to explicitly specify them.\nE.g., you can make x a continuous slider of values between u\nand v by just writing x=(u,v) in the argument list of\nyour function. These are all just convenient shortcuts for\ncreating the controls listed above.

\n\n
\n

Note

\n

Suppose you would like to make an interactive with a default\nRGB color of (1,0,0), so the function would have signature\nf(color=(1,0,0)). Unfortunately, the above shortcuts\nreinterpret the (1,0,0) as a discrete slider with step\nsize 0 between 1 and 0. Instead you should do the\nfollowing:

\n
sage: @interact\n... def _(v = input_box((1,0,0))):\n...       show(plot(sin,color=v))\n<html>...\n
\n
\n

An alternative:

\n
sage: @interact\n... def _(c = color_selector((1, 0, 0))):\n...       show(plot(sin, color = c))\n<html>...\n
\n
\n
\n

MORE EXAMPLES:

\n

We give an input box that allows one to enter completely arbitrary\nstrings:

\n
sage: @interact\n... def _(a=input_box('sage', label="Enter your name", type=str)):\n...        print "Hello there %s"%a.capitalize()\n<html>...\n
\n
\n

The scope of variables that you control via interact() are local\nto the scope of the function being interacted with. However, by\nusing the global Python keyword, you can still modify global\nvariables as follows:

\n
sage: xyz = 10\nsage: @interact\n... def _(a=('xyz',5)):\n...       global xyz\n...       xyz = a\n<html>...\n
\n
\n

If you enter the above you obtain an interact() canvas.\nEntering values in the box changes the global variable xyz.\nHere’s a example with several controls:

\n
sage: @interact\n... def _(title=["A Plot Demo", "Something silly", "something tricky"], a=input_box(sin(x*sin(x*sin(x))), 'function'),\n...     clr = Color('red'), thickness=[1..30], zoom=(1,0.95,..,0.1), plot_points=(200..2000)):\n...     html('<h1 align=center>%s</h1>'%title)\n...     print plot_points\n...     show(plot(a, -zoom*pi,zoom*pi, color=clr, thickness=thickness, plot_points=plot_points))\n<html>...\n
\n
\n

For a more compact color control, use an empty label, a different\nwidget ('colorpicker' or 'jpicker'), and hide the input\nbox:

\n
sage: @interact\n... def _(color=color_selector((1,0,1), label='', widget='colorpicker', hide_box=True)):\n...     show(plot(x/(8/7+sin(x)), (x,-50,50), fill=True, fillcolor=color))\n<html>...\n
\n
\n

We give defaults and name the variables:

\n
sage: @interact\n... def _(a=('first', (1,4)), b=(0,10)):\n...       show(plot(sin(a*x+sin(b*x)), (x,0,6)), figsize=3)\n<html>...\n
\n
\n

Another example involving labels, defaults, and the slider\ncommand:

\n
sage: @interact\n... def _(a = slider(1, 4, default=2, label='Multiplier'),\n...       b = slider(0, 10, default=0, label='Phase Variable')):\n...     show(plot(sin(a*x+b), (x,0,6)), figsize=4)\n<html>...\n
\n
\n

An example where the range slider control is useful:

\n
sage: @interact\n... def _(b = range_slider(-20, 20, 1, default=(-19,3), label='Range')):\n...     plot(sin(x)/x, b[0], b[1]).show(xmin=b[0],xmax=b[1])\n<html>...\n
\n
\n

An example using checkboxes, obtained by making the default values\nbools:

\n
sage: @interact\n... def _(axes=('Show axes', True), square=False):\n...       show(plot(sin, -5,5), axes=axes, aspect_ratio = (1 if square else None))\n<html>...\n
\n
\n

An example generating a random walk that uses a checkbox control\nto determine whether points are placed at each step:

\n
sage: @interact\n... def foo(pts = checkbox(True, "points"), n = (50,(10..100))):\n...       s = 0; v = [(0,0)]\n...       for i in range(n):\n...            s += random() - 0.5\n...            v.append((i, s))\n...       L = line(v, rgbcolor='#4a8de2')\n...       if pts: L += points(v, pointsize=20, rgbcolor='black')\n...       show(L)\n<html>...\n
\n
\n

You can rotate and zoom into 3-D graphics while interacting with a\nvariable:

\n
sage: @interact\n... def _(a=(0,1)):\n...     x,y = var('x,y')\n...     show(plot3d(sin(x*cos(y*a)), (x,0,5), (y,0,5)), figsize=4)\n<html>...\n
\n
\n

A random polygon:

\n
sage: pts = [(random(), random()) for _ in xrange(20)]\nsage: @interact\n... def _(n = (4..len(pts)), c=Color('purple') ):\n...     G = points(pts[:n],pointsize=60) + polygon(pts[:n], rgbcolor=c)\n...     show(G, figsize=5, xmin=0, ymin=0)\n<html>...\n
\n
\n

Two “sinks” displayed simultaneously via a contour plot and a 3-D\ninteractive plot:

\n
sage: @interact\n... def _(q1=(-1,(-3,3)), q2=(-2,(-3,3))):\n...     x,y = var('x,y')\n...     f = q1/sqrt((x+1)^2 + y^2) + q2/sqrt((x-1)^2+(y+0.5)^2)\n...     C = contour_plot(f, (-2,2), (-2,2), plot_points=30, contours=15, cmap='cool')\n...     show(C, figsize=3, aspect_ratio=1)\n...     show(plot3d(f, (x,-2,2), (y,-2,2)), figsize=4)\n<html>...\n
\n
\n

This is similar to above, but you can select the color map from a\ndropdown menu:

\n
sage: @interact\n... def _(q1=(-1,(-3,3)), q2=(-2,(-3,3)),\n...    cmap=['autumn', 'bone', 'cool', 'copper', 'gray', 'hot', 'hsv',\n...          'jet', 'pink', 'prism', 'spring', 'summer', 'winter']):\n...     x,y = var('x,y')\n...     f = q1/sqrt((x+1)^2 + y^2) + q2/sqrt((x-1)^2+(y+0.5)^2)\n...     C = contour_plot(f, (x,-2,2), (y,-2,2), plot_points=30, contours=15, cmap=cmap)\n...     show(C, figsize=3, aspect_ratio=1)\n<html>...\n
\n
\n

A quadratic roots etch-a-sketch:

\n
sage: v = []\nsage: html('<h2>Quadratic Root Etch-a-sketch</h2>')\n<html>...<h2>Quadratic Root Etch-a-sketch</h2>...</html>\nsage: @interact\n... def _(a=[-10..10], b=[-10..10], c=[-10..10]):\n...       f = a*x^2 + b*x + c == 0; show(f)\n...       soln = solve(a*x^2 + b*x + c == 0, x)[0].rhs()\n...       show(soln)\n...       P = tuple(CDF(soln))\n...       v.append(P)\n...       show(line(v, rgbcolor='purple') + point(P, pointsize=200))\n<html>...\n
\n
\n

In the following example, we only generate data for a given n\nonce, so that as one varies p the data does not randomly change.\nWe do this by simply caching the results for each n in a\ndictionary.:

\n
sage: data = {}\nsage: @interact\n... def _(n=(500,(100,5000,1)), p=(1,(0.1,10))):\n...     n = int(n)\n...     if not data.has_key(n):\n...         data[n] = [(random(), random()) for _ in xrange(n)]\n...     show(points([(x^p,y^p) for x,y in data[n]], rgbcolor='black'), xmin=0, ymin=0, axes=False)\n<html>...\n
\n
\n

A conchoid:

\n
sage: @interact\n... def _(k=(1.2,(1.1,2)), k_2=(1.2,(1.1,2)), a=(1.5,(1.1,2))):\n...     u, v = var('u,v')\n...     f = (k^u*(1+cos(v))*cos(u), k^u*(1+cos(v))*sin(u), k^u*sin(v)-a*k_2^u)\n...     show(parametric_plot3d(f, (u,0,6*pi), (v,0,2*pi), plot_points=[40,40], texture=(0,0.5,0)))\n<html>...\n
\n
\n

An input grid:

\n
sage: @interact\n... def _(A=matrix(QQ,3,3,range(9)), v=matrix(QQ,3,1,range(3))):\n...     try:\n...         x = A\\v\n...         html('$$%s %s = %s$$'%(latex(A), latex(x), latex(v)))\n...     except:\n...         html('There is no solution to $$%s x=%s$$'%(latex(A), latex(v)))\n<html>...\n
\n
\n
\n\n\n
\n"}︡ ︠9b28223c-f3dc-468c-ae0f-a85e3b847d1a︠ @interact def _(A=matrix(QQ,3,3,range(9)), v=matrix(QQ,3,1,range(3))): try: x = A\v html('$$%s %s = %s$$'%(latex(A), latex(x), latex(v))) except: html('There is no solution to $$%s x=%s$$'%(latex(A), latex(v))) ︡a60cddc4-3b69-48d4-a3d3-b4a98e5034e9︡︡