Category Archives: sage

让Sage Notebook中的LaTeX环境支持中文

只需要修改$SAGE/devel/sage-main/build/sage/misc/latex.py,就可以让notebook中的latex环境支持中文。
测试环境:Archlinux + Sage 4.7 + TeXLive 2011

  1. COMMON_HEADER字符串的最后添加”\\usepackage{CJKutf8}\n“
  2. 文件中有三处包含”\\begin{document}\n“的字符串(注释块中的不算),在其后添加”\\begin{CJK}{UTF8}{gbsn}\n\n“
  3. 文件中有三处包含”\n\\end{document}”的字符串(注释块中的不算),在其前面添加”\n\n\\end{CJK}\n“

这样,就能在notebook的%latex环境中使用中文了。

Sage-多项式插值

http://www.sagenb.org/home/pub/2736/
下载:Interpolation

使用Sage求最小二乘拟合曲线

1
2
3
4
R=[[-3,3],[0,1],[2,1],[4,3]]
var('a,b,c')
model(x)= a*x^2+b*x+c
find_fit(R,model)

Sage Notebook服务器地址大全

赞一下西安工业大学!

以下信息来自:Sage Math
Sage官方地址:

其他服务器

西安工业大学SAGE校内服务器

https://sagenb.xuepx.cn:8000

============================

3月26日补充:
https://clemix.clemson.edu:34567/

4月13日补充:
http://nb.hssagemath.org/

8月20日补充:
http://www.shumo.com:8000

11月30日补充:
https://sage.openopt.org:8000/

SAGE作图实例:Runge现象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
f(x)=1/(1+25*x^2)
R=PolynomialRing(QQ, 'x')
x4=[-1+2*i/4 for i in range(5)]
pts4=[(i,f(i)) for i in x4]
l4=R.lagrange_polynomial(pts4)
fig=plot(f(x))+plot(l4, color='black', linestyle='--')
x5=[-1+2*i/5 for i in range(6)]
pts5=[(i, f(i)) for i in x5]
l5=R.lagrange_polynomial(pts5)
fig=fig+plot(l5, color='green', linestyle=':')
x10=[-1+2*i/10 for i in range(11)]
pts10=[(i, f(i)) for i in x10]
l10=R.lagrange_polynomial(pts10)
fig=fig+plot(l10, color='red', linestyle='-.')
fig=fig+text('$O$',(-0.1,-0.1),color='black')
fig=fig+line([(1.21,0),(1.21,0)],marker='>',markersize=7,color='black')+line([(0,1.97),(0,1.97)],marker='^',markersize=7,color='black')
fig.show(dpi=150,axes_labels=['$x$', '$y$'],axes_pad=0.01)

Sage总体来说非常不错,但还有不少小毛病:
1、命令的命名有问题。名为lagrange_polynomial,但返回的是已经展开的标准形式。插值多项式是唯一的,不如直接叫interpolation_polynomial。
2、没有显示坐标轴箭头的命令。
3、坐标轴标注的位置不符合习惯。

[转]SAGE tip: Fourier Series Approximation with Interactive mode

sws文件下载:Interactive mode of Fourier Series Approx

via Doxdrum’s Blog by doxdrum on 2/1/11


After my last post, about Fouries Series, I decided to give a try to the interactive mode of SAGE.

:-)

What did I do?

First I check out the examples worked at the Wiki page. In the section of graphics I got inspired from the post Interactive 2D Plotting by Timothy Clemans and Newton’s Method by William Stein post in the Calculus section. So… there it is!

The code is too long to be posted here… but here it goes anyway

1
var('x,n,i')
1
2
def error_msg(msg):
.. print '<p style="font-family: Arial, sans-serif; color: #000;"><span     style="color: red; font-weight: bold;">Error</span>: %s</p>' % msg
1
...
1
2
3
4
5
def a(f, n, init, final):
.. x = f.variables()[0]
.. L = (final - init)/2
.. coeff = integral(f(x)*cos(n*pi*x/L), (x,init,final))/L
.. return coeff

def b(f, n, init, final):
.. x = f.variables()[0]
.. L = (final – init)/2
.. coeff = integral(f(x)*sin(n*pi*x/L), (x,init,final))/L
.. return coeff

def FS(f, n, init, final):
.. x = f.variables()[0]
.. L = (final – init)/2
.. return a(f, 0, init, final)/2 + sum(a(f, i, init, final)*cos(i*x*pi/L)+b(f, i, init, final)*sin(i*x*pi/L), i, 1, n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@interact
def interactive_2d_plotter(clr=Color('green'), expression=input_box('x^2', 'Expression', str), x_range=range_slider(-6,6,1,(-4,4), label='X Range'), order=slider([0..100],None,None,3), square=checkbox(True, 'Square'), axes=checkbox(False, 'Show Axes')):
.. if expression:
..   try:
..      expression = SR(expression) # turn string into a Sage expression
..   except TypeError:
..      print error_msg('This is not an expression.')
..      return
..   try:
..      xmin, xmax = x_range
..      n = order
..      if square or not axes:
..         print "var('%s')nplot(%s).show(%s%s%s)" % (expression.variables()[0], repr(expression), 'aspect_ratio=1' if square else '', ', ' if square and not axes else '', 'axes=False' if not axes else '')
..         if square:
..            P = plot(FS(expression, n, xmin, xmax), xmin, xmax, color=clr) + plot(expression, xmin, xmax)
..            P.show(aspect_ratio=1, axes=axes)
..         else:
..            P = plot(FS(expression, n, xmin, xmax), xmin, xmax, color=clr) + plot(expression, xmin, xmax)
..            P.show(axes=axes)
..      else:
..         print "var('%s')nplot(%s)" % (expression.variables()[0], repr(expression))
..         P = plot(FS(expression, n, xmin, xmax), xmin, xmax, color=clr) + plot(expression, xmin, xmax)
..         P.show(axes=axes)
..   except ValueError:
..      print error_msg('This expression has more than one variable.')
..      return
..   except TypeError:
..      print error_msg("This expression contains an unknown function.")
..      return

because it’s almost in-understandable :-P

Some pictures!!!

The code is set to give the Fourier approximation of the f(x)=x^2 with aspect_ratio = (1,1).

Initial Plot. Approx. of x^2Initial Plot. Approx. of x^2

One can set a non one-to-one aspect_ratio, by un-checking the box,

Initial Plot with-no-squareInitial Plot with-no-square

And we can view the frame as well by checking the other box,

Initial plot no-square and frameInitial plot no-square and frame

The first box allows us to change the colour, by using a palette,

Initial plot with color-paletteInitial plot with color-palette

for example, set the colour to red,

Initial plot in redInitial plot in red

We can also change the function, to an exponential, f(x)=e^x,

Fourier approximation of the exponentialFourier approximation of the exponential

and change the domain of the function,

Changing the intervalChanging the interval

or the order of the approximation,

Exponential changing the order of the approximationExponential changing the order of the approximation

I published my sage file on my page (at the end of the page in the attached files)… for the sake of clarity (and completeness).

Enjoy,

Dox

[转]SAGE tip: Graphs of Elementary Numerical Integrals

原文在墙外,转发一下。对应的sws文件下载:Graphs of Elementary Numerical Integrals

via Doxdrum’s Blog by doxdrum on 1/30/11


Looking on the notebook worksheets publish on sagenb I found a very interesting application of the interactive mode, Interactive.

Since one can not look the result of the published notebooks, I use its code to playing around a little bit, so here are the plots.

The default function to be integrated is sin(x^2)+2, the interval of integration is set [-1,3], the number of partitions of the interval is 4, and the method is the mid-point,

Integration by mid-point method. Partition = 4Integration by mid-point method. Partition = 4

We can see the visual integration, the analitic result, the numerical one, and the error. :-) Nice, Isn’t it?

Let’s change the number of partitions, say to 10,

Integration by mid-point method. Partition = 10Integration by mid-point method. Partition = 10

Much better! But now, let’s try the other methods… left-point,

Integration by left-point method. Partition = 10Integration by left-point method. Partition = 10

The right-point,

Integration by right-point method. Partition = 10Integration by right-point method. Partition = 10

The upper-point,

Integration by upper-point method. Partition = 10Integration by upper-point method. Partition = 10

The lower-point,

Integration by lower-point method. Partition = 10Integration by lower-point method. Partition = 10

The trapezoid method,

Integration by Trapezoid method. Partition = 10Integration by Trapezoid method. Partition = 10

which is one of the best.

But finally the Simpson method,

Integration by Simpson method. Partition = 10Integration by Simpson method. Partition = 10

Impressive!!!

So, I think it’s a good idea to give a try to the interactive mode in SAGE, and if you go thru the code you’ll find out it’s not as difficult as it seems.

Enjoy!

Dox

Sage绘制摆线动画

在Sage中绘制动画,实际上是先画出每一帧,再使用imagemagick将多幅图像转化为gif动画。先来看一个简单例子:

t,u=var(‘t,u’)
cc=[point((cos(t),sin(t)),pointsize=30,rgbcolor='red')+parametric_plot((cos(u),sin(u)),(0,t)) for t in srange(0.01,2*pi,0.2)]
myan=animate(cc,xmin=-1.5, xmax=1.5,ymin=-1.5, ymax=1.5,aspect_ratio=1)
show(myan)

第2行中每帧图像由两部分组成,一是动点,二是圆弧。这里,srange函数中的初值不能取零,否则会出错。第3行中将绘图的坐标范围固定,如果不固定,Sage会将当前图像主体放置在图像中央,自动调整坐标轴的显示范围。这一特性在绘制静态图像时,非常有用,但这样得到的动画往往不是我们想要的。

下面来看圆摆线x=a*(u-sin(u)), y=a*(1-cos(u)),  0<u<2*pi的绘制:

u=var(‘u’)
a=2
pic=[parametric_plot((a*(u-sin(u)),a*(1-cos(u))),(u,0,t),rgbcolor='red')\
+circle((a*t,a),a,rgbcolor='blue')\
+point((a*(t-sin(t)),a*(1-cos(t))),pointsize=20,rgbcolor='red')\
+line([(a*t,a),(a*(t-sin(t)),a*(1-cos(t)))],rgbcolor=’blue’) for t in srange(0.01,2*pi+0.1,0.2)]
bbb=animate(pic,xmin=-0.5,xmax=(2*pi+1)*a,ymin=-1, ymax=2*a+1,aspect_ratio=1)
show(bbb)

注:其他一些数学软件绘图时,如果不手工清除绘图区域,则上一个图像会保留下来,这与Sage的工作方式不同。Sage中的每一帧都是“全新”绘制的。

下面将其转化为交互式的图形:

@interact
def _(t=(0.01,2*pi,0.5)):
u=var(‘u’)
a=2
pic=parametric_plot((a*(u-sin(u)),a*(1-cos(u))),(u,0,t),rgbcolor=’red’)\
+circle((a*t,a),a,rgbcolor=’blue’)\
+point((a*(t-sin(t)),a*(1-cos(t))),pointsize=20,rgbcolor=’red’)\
+line([(a*t,a),(a*(t-sin(t)),a*(1-cos(t)))],rgbcolor=’blue’)
show(pic,xmin=-0.5,xmax=(2*pi+1)*a,ymin=-1, ymax=2*a+1,aspect_ratio=1)

由于刷新的问题,更新不连贯,拖放之后,需要等一下。

使用Sage绘制动画的一个例子

先把代码放在这里,有时间再解释:

t,u=var(‘t,u’)
cc=[point((cos(t),sin(t)),pointsize=30,rgbcolor='red')+parametric_plot((cos(u),sin(u)),(0,t)) for t in srange(0.01,2*pi,0.2)]
myan=animate(cc,xmin=-1.5, xmax=1.5,ymin=-1.5, ymax=1.5)
show(myan)

Sage Tutorial中文版

Sage Tutorial是一份快速了解Sage的入门文档,花了两周多的时间将其翻译为中文。但是“Some more advanced mathematics”一节没有翻译,因为完全不熟悉这一节所涉及到的内容。

第一次翻译开源软件的技术文档,水平有限,希望各位多提意见,任何方面的都可以。如果没有大的问题,大概一周后会向Sage开发组提交。

源文件使用Sphinx管理,PDF文件是由自动转换而来的LaTeX文件编译的,因此其中很多格式不符合中文的习惯。所以请大家先以HTML文件为准,最后发布前,我会手工调整LaTeX文件后再编译。

Email: amao@ai7.org

相关内容:翻译Sage文档的准备工作

Sage Tutorial 中文版rst源文件

Sage Tutorial 中文版(HTML)

Sage Tutorial 中文版(PDF)

无觅相关文章插件,快速提升流量