# Sage交互快速启动¶

## 从一个命令开始¶

sage: plot(x^2,(x,-3,3))
Graphics object consisting of 1 graphics primitive


sage: f=x^3
sage: plot(f,(x,-3,3))
Graphics object consisting of 1 graphics primitive


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


sage: myplot()


sage: myplot(x^3)


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


def myplot(..): ...
myplot=interact(myplot)


sage: myplot(x^4)


## 增加复杂性¶

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


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


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


• range_slider, which passes in two values, zoom[0] and zoom[1]

• True/False 转换为最终用户的复选框

sage: @interact
sage: 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))


sage: @interact(layout=dict(top=[['f', 'color']],
....: left=[['axes'],['fill']],
....: bottom=[['zoom']]))
sage: 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))


## 控件类型¶

• 滑块

• 范围滑块

• 复选框

• 选择器（下拉列表或按钮）

• 方格

• 颜色选择器

• 纯文本

sage: @interact
sage: def _(frame=checkbox(True, label='Use frame')):
....:     show(plot(sin(x), (x,-5,5)), frame=frame)

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

sage: var('x,y')
sage: colormaps=sage.plot.colors.colormaps.keys()
sage: @interact
sage: def _(cmap=selector(['RdBu', 'jet', 'gray','gray_r'],buttons=True),
sage: 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()


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


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


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


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


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


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


## 不更新选项¶

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