Sage交互快速启动¶
这个 Sage 快速入门教程是为MAA准备工作坊“Sage:对本科生使用开源数学软件”开发的(由NSF提供资金,到期日:0817071)。
无价的资源是Sage wiki http://wiki.sagemath.org/interact (在Google中输入“sage interactive”, UTMOST Sage Cell Repository (贡献的交互的集合)。
从一个命令开始¶
如何创建一个交互式单元?首先,让我们集中精力做一件新的事情!也许我们只是想要一个有一些选择的绘图机。
因此,让我们从获取您希望输出的命令开始。这里我们只想要一个简单的情节。
sage: plot(x^2,(x,-3,3))
Graphics object consisting of 1 graphics primitive
然后抽象出你想改变的部分。我们将让用户更改函数,让它成为一个变量 f
.
sage: f=x^3
sage: plot(f,(x,-3,3))
Graphics object consisting of 1 graphics primitive
这一点很重要,因为它能让你退后一步,想想你真正要做什么。
现在是技术部分。我们把这个做成 def
功能-参见 programming tutorial .
sage: def myplot(f=x^2):
....: show(plot(f,(x,-3,3)))
让我们测试一下 def
功能 myplot
只要打个电话。
sage: myplot()
如果我们用不同的值来调用它 f
,我们应该换个情节。
sage: myplot(x^3)
到目前为止,我们只定义了一个新函数,所以这是review。为了使“控件”允许用户交互式地输入函数,我们只需在函数前面加上 @interact
.
sage: @interact
sage: def myplot(f=x^2):
....: show(plot(f,(x,-3,3)))
注解
技术上什么 @interact
does是包装函数,因此上面的内容等价于:
def myplot(..): ...
myplot=interact(myplot)
请注意,我们仍然可以调用函数,即使在使用 @interact
. 这在调试它时通常很有用。
sage: myplot(x^4)
增加复杂性¶
我们可以继续用变量替换表达式的其他部分。请注意 _
现在是函数名。这是一个我们不在乎的废弃名字的公正惯例。
sage: @interact
sage: def _(f=x^2, a=-3, b=3):
....: show(plot(f,(x,a,b)))
如果我们通过 ('label', default_value)
对于控件,则控件在打印时获取标签。在这里,我们为他们三个人都写了一些文字。记住文本必须用引号括起来!否则你会认为它是指某个Sage(Sage)会认为它是一个较低的定义。
sage: @interact
sage: def _(f=('$f$', x^2), a=('lower', -3), b=('upper', 3)):
....: show(plot(f,(x,a,b)))
我们可以显式地指定控件的类型以及选项。看到了吗 below 更多关于可能性的细节。
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]
andzoom[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))
还有一种按钮类型 disable automatic updates .
以前的交互有点难看,因为所有的控件都堆叠在一起。我们可以使用 layout
参数。
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具有以下所有功能:
盒
滑块
范围滑块
复选框
选择器(下拉列表或按钮)
方格
颜色选择器
纯文本
下面我们将进一步说明这些问题。
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()
默认情况下,范围是将范围划分为50步的滑块。
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())
不更新选项¶
最后一个问题是,当控件变得如此复杂,以至于看到interact更新每个想要进行的更改都会适得其反时,会发生什么情况?例如,考虑改变三重积分的端点和积分顺序,或者下面的例子中整个矩阵可能会改变。
在这种情况下,在我们明确表示之前不需要任何更新,我们可以使用 auto_update=False
选项。这将创建一个按钮,使用户能够在他或她准备好后立即进行更新。
sage: @interact
sage: def _(m=('matrix', identity_matrix(2)), auto_update=False):
....: print(m.eigenvalues())