作图¶
Sage可以使用matplotlib、openmath、gnuplot或surf绘图,但标准发行版中Sage只包含matplotlib和openmath。有关冲浪示例,请参见 打印曲面 .
Sage中的绘图可以用许多不同的方式完成。您可以通过gnuplot绘制函数(二维或三维)或一组点(仅二维),也可以通过Maxima绘制微分方程的解(Maxima又称为gnuplot或openmath),也可以使用Singular与绘图包surf(Sage不附带)的接口。 gnuplot
没有隐式绘图命令,因此如果要使用隐式绘图绘制曲线或曲面,则最好使用Singular的界面进行浏览,如第ch:AG章“代数几何”中所述。
二维绘图功能¶
默认的绘图方法在使用优秀的 matplotlib
包裹。
要查看其中任何一个,请键入 P.save("<path>/myplot.png")
然后在图形查看器(如gimp)中打开它。
可以绘制分段定义的函数:
sage: f1 = 1
sage: f2 = 1-x
sage: f3 = exp(x)
sage: f4 = sin(2*x)
sage: f = piecewise([((0,1),f1), ((1,2),f2), ((2,3),f3), ((3,10),f4)])
sage: f.plot(x,0,10)
Graphics object consisting of 1 graphics primitive
也可以生成其他函数:
Jacobi椭圆函数的一个红色图 \(\text{{sn}}(x,2)\) , \(-3<x<3\) (不要键入 ....:
:
sage: L = [(i/100.0, maxima.eval('jacobi_sn (%s/100.0,2.0)'%i))
....: for i in range(-300,300)]
sage: show(line(L, rgbcolor=(3/4,1/4,1/8)))
一块红色的土地 \(J\) -贝塞尔函数 \(J_2(x)\) , \(0<x<10\) :
sage: L = [(i/10.0, maxima.eval('bessel_j (2,%s/10.0)'%i)) for i in range(100)]
sage: show(line(L, rgbcolor=(3/4,1/4,5/8)))
Riemann-zeta函数的紫色图 \(\zeta(1/2 + it)\) , \(0<t<30\) :
sage: I = CDF.0
sage: show(line([zeta(1/2 + k*I/6) for k in range(180)], rgbcolor=(3/4,1/2,5/8)))
绘制曲线¶
要用Sage绘制曲线,可以使用Singular和surf(http://surf.sourceforge.net/,也可以作为实验包提供)或使用matplotlib(与Sage一起提供)。
Mat普特利布¶
这里有几个例子。要查看它们,请键入 p.save("<path>/my_plot.png")
(何处) <path>
是一个目录路径,您对该路径具有写入权限,可以保存绘图,并在查看器(如GIMP)中查看它。
尼科米德的蓝色贝壳:
sage: L = [[1+5*cos(pi/2+pi*i/100), tan(pi/2+pi*i/100)*
....: (1+5*cos(pi/2+pi*i/100))] for i in range(1,100)]
sage: line(L, rgbcolor=(1/4,1/8,3/4))
Graphics object consisting of 1 graphics primitive
蓝色的粗隆部(3片叶子):
sage: n = 4; h = 3; b = 2
sage: L = [[n*cos(pi*i/100)+h*cos((n/b)*pi*i/100),
....: n*sin(pi*i/100)-h*sin((n/b)*pi*i/100)] for i in range(200)]
sage: line(L, rgbcolor=(1/4,1/4,3/4))
Graphics object consisting of 1 graphics primitive
蓝色的粗隆部(4片叶子):
sage: n = 6; h = 5; b = 2
sage: L = [[n*cos(pi*i/100)+h*cos((n/b)*pi*i/100),
....: n*sin(pi*i/100)-h*sin((n/b)*pi*i/100)] for i in range(200)]
sage: line(L, rgbcolor=(1/4,1/4,3/4))
Graphics object consisting of 1 graphics primitive
帕斯卡的红色利马:
sage: L = [[sin(pi*i/100)+sin(pi*i/50),-(1+cos(pi*i/100)+cos(pi*i/50))]
....: for i in range(-100,101)]
sage: line(L, rgbcolor=(1,1/4,1/2))
Graphics object consisting of 1 graphics primitive
一条浅绿色的麦克劳林三分线:
sage: L = [[2*(1-4*cos(-pi/2+pi*i/100)^2),10*tan(-pi/2+pi*i/100)*
....: (1-4*cos(-pi/2+pi*i/100)^2)] for i in range(1,100)]
sage: line(L, rgbcolor=(1/4,1,1/8))
Graphics object consisting of 1 graphics primitive
Bernoulli的一个绿色柠檬酸盐(我们省略了i==100,因为这会产生0除法错误):
sage: v = [(1/cos(-pi/2+pi*i/100), tan(-pi/2+pi*i/100)) for i in range(1,200) if i!=100 ]
sage: L = [(a/(a^2+b^2), b/(a^2+b^2)) for a,b in v]
sage: line(L, rgbcolor=(1/4,3/4,1/8))
Graphics object consisting of 1 graphics primitive
冲浪¶
尤其是因为 surf
仅在UNIX类型的操作系统上可用(并且不包含在Sage中),使用Sage中的以下命令进行打印仅在此类操作系统上可用。顺便说一句,surf包含在几个流行的Linux发行版中。
sage: s = singular.eval
sage: s('LIB "surf.lib";')
...
sage: s("ring rr0 = 0,(x1,x2),dp;")
''
sage: s("ideal I = x1^3 - x2^2;")
''
sage: s("plot(I);")
...
出版社 q
当“冲浪”窗口处于活动状态时,退出“冲浪”并返回“萨奇”。
可以将此绘图另存为冲浪脚本。在弹出的冲浪窗口中,只需选择 file
, save as
等等。。(类型 q
或选择 file
, quit
,关闭窗口。)
文雅的情节是鼓励读者去尝试的。
开放路径¶
Openmath是由W.Schelter编写的TCL/tkgui绘图程序。
下面的命令将绘制函数 \(\cos(2x)+2e^{{-x}}\)
sage: maxima.plot2d('cos(2*x) + 2*exp(-x)','[x,0,1]', # not tested (pops up a window)
....: '[plot_format,openmath]')
(MacOSX用户:请注意 openmath
命令是在xterm shell中启动的会话中运行的,而不是使用标准的Mac终端应用程序。)
sage: maxima.eval('load("plotdf");')
'".../share/maxima/.../share/dynamics/plotdf.lisp"'
sage: maxima.eval('plotdf(x+y,[trajectory_at,2,-0.1]); ') # not tested
这将绘制一个方向字段(Maxima包的plotdf也是由W.Schelter编写的)
几个功能的二维图:
sage: maxima.plot2d('[x,x^2,x^3]','[x,-1,1]','[plot_format,openmath]') # not tested
Openmath还可以对窗体的表面进行三维打印 \(z=f(x,y)\) 作为 \(x\) 和 \(y\) 矩形上的范围。例如,这里有一个“实时”3D绘图,您可以用鼠标移动它:
sage: maxima.plot3d ("sin(x^2 + y^2)", "[x, -3, 3]", "[y, -3, 3]", # not tested
....: '[plot_format, openmath]')
通过适当旋转,可以查看等高线图。
超光速三维绘图¶
射线追踪包超光速子与Sage一起分发。三维绘图看起来很好,但往往需要更多的设置。以下是参数化空间曲线的示例:
sage: f = lambda t: (t,t^2,t^3)
sage: t = Tachyon(camera_center=(5,0,4))
sage: t.texture('t')
sage: t.light((-20,-20,40), 0.2, (1,1,1))
sage: t.parametric_plot(f,-5,5,'t',min_depth=6)
类型 t.show()
来看看这个。
其他示例见参考手册。
gnuplot¶
你一定有 gnuplot
安装以运行这些命令。
首先,下面是绘制函数的方法:{plot!函数}
sage: maxima.plot2d('sin(x)','[x,-5,5]')
sage: opts = '[gnuplot_term, ps], [gnuplot_out_file, "sin-plot.eps"]'
sage: maxima.plot2d('sin(x)','[x,-5,5]',opts)
sage: opts = '[gnuplot_term, ps], [gnuplot_out_file, "/tmp/sin-plot.eps"]'
sage: maxima.plot2d('sin(x)','[x,-5,5]',opts)
eps文件默认保存到当前目录,但如果愿意,可以指定路径。
以下是平面中参数曲线的打印示例:
sage: maxima.plot2d_parametric(["sin(t)","cos(t)"], "t",[-3.1,3.1])
sage: opts = '[gnuplot_preamble, "set nokey"], [gnuplot_term, ps],
....: [gnuplot_out_file, "circle-plot.eps"]'
sage: maxima.plot2d_parametric(["sin(t)","cos(t)"], "t", [-3.1,3.1], options=opts)
下面是一个3空间参数曲面的绘图示例:{plot!参数曲面}
sage: maxima.plot3d_parametric(["v*sin(u)","v*cos(u)","v"], ["u","v"],
....: [-3.2,3.2],[0,3]) # optional -- pops up a window.
sage: opts = '[gnuplot_term, ps], [gnuplot_out_file, "sin-cos-plot.eps"]'
sage: maxima.plot3d_parametric(["v*sin(u)","v*cos(u)","v"], ["u","v"],
....: [-3.2,3.2],[0,3],opts) # optional -- pops up a window.
To illustrate how to pass gnuplot options in , here is an example of a plot of a set of points involving the Riemann zeta function \(\zeta(s)\) (computed using Pari but plotted using Maxima and Gnuplot): {plot!points} {Riemann zeta function}
sage: zeta_ptsx = [ (pari(1/2 + i*I/10).zeta().real()).precision(1)
....: for i in range (70,150)]
sage: zeta_ptsy = [ (pari(1/2 + i*I/10).zeta().imag()).precision(1)
....: for i in range (70,150)]
sage: maxima.plot_list(zeta_ptsx, zeta_ptsy) # optional -- pops up a window.
sage: opts='[gnuplot_preamble, "set nokey"], [gnuplot_term, ps],
....: [gnuplot_out_file, "zeta.eps"]'
sage: maxima.plot_list(zeta_ptsx, zeta_ptsy, opts) # optional -- pops up a window.
打印曲面¶
在中绘制曲面与绘制曲线没有区别,尽管语法略有不同。尤其是,你需要 surf
加载。 {plot!surface using surf}
sage: singular.eval('ring rr1 = 0,(x,y,z),dp;')
''
sage: singular.eval('ideal I(1) = 2x2-1/2x3 +1-y+1;')
''
sage: singular.eval('plot(I(1));')
...