数式処理ソフトSAGE

SAGE

フリーの数式処理ソフトいくつか試してみたけども、
一番しっくり来たのはSAGE(せーじ?)だった。


SAGE: Open Source Mathematics Software
http://sagemath.org/


Creating a viable free open source alternative to
Magma, Maple, Mathematica, and Matlab

↑の謳い文句は、大げさではないと思う。
どの辺が気に入ったかというと、、


1、オープンソース
1、ウェブブラウザからアクセスできる
1、関数名にキャピタル混ざってない
1、コマンドのタブ補完ができる
1、ヘルプとソースへのアクセス方法
1、ドキュメントが充実してる
1、Jmolでクルクル回せる


WindowsだとVMwareがうっとおしいので、Mac OS Xで使ってる。
こんなのが無償で手に入るなんて、ほんとにいい時代だ。


アカウント作成で、ウェブサービスを利用可
Sage Via the Web
http://www.sagenb.org/
(2009/01/28、追加)


Sage Quick Reference (Basic Math)
http://wiki.sagemath.org/quickref
(2009/01/28、リンク修正)


ノート上に式を書いて、
Shift + Enter で計算実行
Alt + Enter で入力行を挿入
(2009/04/15、追加)



実行例.

(微分)
sage: diff(x^2+sin(x),x)
cos(x) + 2*x


(積分)
sage: integral(cos(x)+2*x,x)
sin(x) + x^2


(変数(a)を用いた微分)
sage: a = var('a')
sage: diff(x^a+cos(x),x)
a*x^(a - 1) - sin(x)


(テーラー展開)
sage: taylor(exp(x),x,0,5)
1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120


(ラプラス変換)
sage: var('a')
a
sage: laplace( e^(x+a), x, a)
e^a/(a - 1)


(ラプラス逆変換)
sage: inverse_laplace( e^a/(a-1), x, a)
ilt(e^a/(a - 1), x, a)


(数値近似)
sage: e.n(digits=12)
2.71828182846


(式の代入と偏微分)
sage: f(x,y)=exp(x)*exp(2*y)
sage: diff(f,x)
(x, y) |--> e^(2*y + x)
sage: diff(f,y)
(x, y) |--> 2*e^(2*y + x)
sage: diff(f,y,y)
(x, y) |--> 4*e^(2*y + x)


(式の展開)
sage: expand( (x-1)*(x^2-1) )
x^3 - x^2 - x + 1


(因数分解)
sage: factor(x^3-x^2-x+1)
(x + 1) * (x - 1)^2


(倍精度複素数)
sage: vector(CDF,[(1,2),(3,4),(5,6)])
(1.0 + 2.0*I, 3.0 + 4.0*I, 5.0 + 6.0*I)


(フーリエ変換)
sage: v = vector(CDF,[1,2,3,4])
sage: w = v.fft()
sage: v -w.inv_fft()
(0, 0, 0, 0)


(2Dプロット)
sage: plot(sin(x),(x,0,4*pi))
sage: p=plot(sin(x)+cos(x),(x,0,4*pi))
sage: p.show()


(3Dプロット)
sage: var('x,y')
sage: plot3d(sin(pi*(x^2+y^2))/2,(x,-1,1),(y,-1,1),color='orange',opacity=0.5)
sage: plot(sin(x),(x,0,4*pi))
sage: p=plot(sin(x)+cos(x),(x,0,4*pi))
sage: p.show()


(複数プロット)
sage: sigmoid(a,x)=1/(1+exp(-a*x))
sage: plot([sigmoid(1,x),sigmoid(2,x),sigmoid(4,x),sigmoid(8,x)],[-5,5])


(複数プロット2)
sage: f(x) = x^2
sage: g(x) = (x-5)^2
sage: fp = plot(f, (x, -10, 10), rgbcolor = (0,0,1))
sage: gp = plot(g, (x, -10, 10), rgbcolor = (1,0,0))
sage: (fp+gp).show()


(スライドバー使用例)
var('x')
x0  = 0
f   = sin(x)*exp(-x)
p   = plot(f,-1,5, thickness=2)
dot = point((x0,f(x0)),pointsize=80,rgbcolor=(1,0,0))
@interact
def _(order=(1..12)):
  ft = f.taylor(x,x0,order)
  pt = plot(ft,-1, 5, color='green', thickness=2)
  html('$f(x)\;=\;%s$'%latex(f))
  html('$\hat{f}(x;%s)\;=\;%s+\mathcal{O}(x^{%s})$'%(x0,latex(ft),order+1))
  show(dot + p + pt, ymin = -.5, ymax = 1)


(データの読み書き)
sage: a = matrix(2,[1,2,3,-5/2])
sage: save(a, 'test.sobj')
sage: load('test')

[   1    2]
[   3 -5/2]


(ベクトルの作成)
sage: v=vector([1,2,3]); v; v.parent()
(1, 2, 3)
Ambient free module of rank 3 over the principal ideal domain Integer Ring
sage: v=vector(CDF,[1*i,2,3]); v; v.parent()
(1.0*I, 2.0, 3.0)
Vector space of dimension 3 over Complex Double Field


(ベクトルの積)
sage: v = vector([1,2,3]); w = vector([0,5,-9])
sage: v.cross_product(v)
(0, 0, 0)
sage: u=v.cross_product(w); u
(-33, 9, 5)
sage: u.dot_product(v)
0
sage: u.dot_product(w)
0


(行列の作成)
sage: m=matrix(RDF,[[1,2],[3,4]])
sage: m

[1.0 2.0]
[3.0 4.0]

sage: v1=vector((1,2,3)); v2=vector((4,5,6))
sage: m=matrix([v1,v2]); m; m.parent()

[1 2 3]
[4 5 6]
Full MatrixSpace of 2 by 3 dense matrices over Integer Ring
sage: m=matrix(QQ,2,[1,2,3,4,5,6]); m

[1 2 3]
[4 5 6]
sage: m=matrix(QQ,3,2,[1,2,3,4,5,6]); m

[1 2]
[3 4]
[5 6]
sage: m=matrix(CDF,[[1,2],[3,1+4*i]])
sage: m

[        1.0         2.0]
[        3.0 1.0 + 4.0*I]
sage: diagonal_matrix([1,2,3])

[1 0 0]
[0 2 0]
[0 0 3]


(行列の演算)
sage: m=matrix(CDF,[[1,2],[3,1+4*i]])
sage: m+m

[        2.0         4.0]
[        6.0 2.0 + 8.0*I]
sage: m-m

[0 0]
[0 0]
sage: m*m

[         7.0  4.0 + 8.0*I]
[6.0 + 12.0*I -9.0 + 8.0*I]


(逆行列)
sage: m^(-1)

[  0.268292682927 - 0.585365853659*I    0.243902439024 + 0.19512195122*I]
[  0.365853658537 + 0.292682926829*I -0.121951219512 - 0.0975609756098*I]
sage: m.parent()
Full MatrixSpace of 2 by 2 dense matrices over Complex Double Field
sage: m*m^(-1)

[                                     1.0  2.77555756156e-17 + 2.77555756156e-17*I]
[-1.11022302463e-16 + 2.22044604925e-16*I                                      1.0]


(行列の転置)
sage: m=matrix(RDF,2,3,range(6));m

[0.0 1.0 2.0]
[3.0 4.0 5.0]
sage: m.transpose()

[0.0 3.0]
[1.0 4.0]
[2.0 5.0]


(固有値・固有ベクトルの計算)
sage: m=matrix([[5,-1],[3,1]])
sage: m.eigenvalues()
sage: m.eigenvectors_right()


(ソースの検索)
sage: search_src('文字列')


(ドキュメントの検索)
sage: search_doc('文字列')


(前の結果を参照)
sage: _
※アンダースコアひとつ


(ルービックキューブ)
C = RubiksCube().move("R U R'")
C.show3d()
C = RubiksCube("R*L"); C
C.show()
C.solve(algorithm='gap')
C == RubiksCube("L*R")