「SAGE」の版間の差分
提供: MacWiki
(→プログラミング) |
|||
行32: | 行32: | ||
sage: quit | sage: quit | ||
Exiting SAGE (CPU time 0m0.06s, Wall time 3m59.16s). | Exiting SAGE (CPU time 0m0.06s, Wall time 3m59.16s). | ||
− | $ | + | $ |
=== Python環境 === | === Python環境 === | ||
行56: | 行56: | ||
for m in range(l + 1): # Python's 'int' type | for m in range(l + 1): # Python's 'int' type | ||
print 'P(', l, ',', m, ') =', gen_legendre_P(l, Integer(m), x) | print 'P(', l, ',', m, ') =', gen_legendre_P(l, Integer(m), x) | ||
+ | sys.exit() | ||
$ sage legendre.sage | $ sage legendre.sage | ||
P( 3 , 0 ) = 5/2*(x - 1)^3 + 15/2*(x - 1)^2 + 6*x - 5 | P( 3 , 0 ) = 5/2*(x - 1)^3 + 15/2*(x - 1)^2 + 6*x - 5 | ||
行70: | 行71: | ||
p2 = plot(cos, -pi, pi, rgbcolor='red') | p2 = plot(cos, -pi, pi, rgbcolor='red') | ||
(p1 + p2).show() | (p1 + p2).show() | ||
+ | sys.exit() | ||
$ sage plot.sage | $ sage plot.sage | ||
2012年9月5日 (水) 13:29時点における版
Sage とは?
SageとはGPLでライセンスされているオープンソースの数学ソフトウェアシステムです。 Sageは既に存在する様々のオープンソースパッケージはPythonをベースとした共通のインタフェースで利用できるようにパッケージしたものです。
SageはPackageのひとつにmaximaを採用しており、数式計算が可能です。また、Mathematica のNotebook風のWeb interfaceを提供しています。 Windows, Linux, Mac OS X, Solaris, FreeBDSなどのプラットフォームで可動します。
Mac OS X 用パッケージ
- ダウンロードファイルは公式サイトより入手できる。(例えば sage-4.2-OSX-32bit-10.4-i386-Darwin.dmg )
- Terminal.app上で動くが、Mac OS X アプリケーションの形になっており、普通は「アプリケーション」ディレクトリにコピーすることが想定されている。
- Sageの起動(特に初回)は時間がかかる。起動するとプロンプト(sage: )が出る。アプリケーションアイコン(/Applications/sage/sage)をダブルクリックで起動しても良い。
$ hdiutil mount /Users/me/Downloads/sage-5.2-OSX-64bit-10.6-x86_64-Darwin.dmg $ cp -R -P /Volumes/sage-5.2-OSX-64bit-10.6-x86_64-Darwin/sage /Applications $ open /Applications/sage/sage
$ file /Applications/sage/sage /Applications/sage/sage: a bash script text executable $ /Applications/sage/sage -v Sage Version 5.2, Release Date: 2012-07-25
- 起動した時のTerminal画面例は:
$ /Applications/sage/sage ---------------------------------------------------------------------- | Sage Version 4.2, Release Date: 2009-10-24 | | Type notebook() for the GUI, and license() for information. | ---------------------------------------------------------------------- sage: sage: quit Exiting SAGE (CPU time 0m0.06s, Wall time 3m59.16s). $
Python環境
SageのPython環境とsite-packagesを調べる
$ /Applications/sage/local/bin/python Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import distutils.sysconfig >>> print distutils.sysconfig.get_python_lib() /Applications/sage/local/lib/python2.7/site-packages >>>
プログラミング
- SAGE の標準的整数型は Integer 型(Python の int 型ではなくて)。なお関数 gen_legendre_P(l,m,x) の位数 m はInteger 型で渡さないとエラーとなる。
$ cat legendre.sage var('x') l = 3 #for m in srange(l + 1): # Sage's 'Integer' type for m in range(l + 1): # Python's 'int' type print 'P(', l, ',', m, ') =', gen_legendre_P(l, Integer(m), x) sys.exit() $ sage legendre.sage P( 3 , 0 ) = 5/2*(x - 1)^3 + 15/2*(x - 1)^2 + 6*x - 5 P( 3 , 1 ) = -3/2*sqrt(-x^2 + 1)*(5*(x - 1)^2 + 10*x - 6) P( 3 , 2 ) = -15*(x^2 - 1)*x P( 3 , 3 ) = 15*sqrt(-x^2 + 1)*((x^2 - 1)*x^2 - x^2 + 1)/(x^2 - 1)
- なお上記で、legendre.py という中間ファイルが自動的に作られた後、SAGE内蔵のPython環境がそれを実行している(下記と同じ)。
$ sage -python legendre.py
- プロットする例
$ cat plot.sage p1 = plot(sin, -pi, pi, rgbcolor='blue') p2 = plot(cos, -pi, pi, rgbcolor='red') (p1 + p2).show() sys.exit() $ sage plot.sage
リンク
公式サイト http://www.sagemath.org/