tokuhirom's blog.

'; DROP DATABASE database();

モンテカルロ法とラスベガス法

新山さんのところで

(23:12)
ああ、そういえば今まで「Monte Carlo 法」というのは知ってたけど、じ
つは「Las Vegas 法」というのもあるんだってサ。

http://tabesugi.net/memo/cur/cur.html#222312

と書かれていたので、気になって調べてみた。

  • ラスベガス法

選ばれた乱数によって, 計算時間が長かったり短かったりするような確率
アルゴリズムは Las Vegas アルゴリズムと呼ばれる.
http://www.comp.cs.gunma-u.ac.jp/~koichi/RAND/rand/node2.html

選ばれた乱数によって, 計算が成功したり失敗したりするような確率アル
ゴリズムは Monte Carlo アルゴリズムと呼ばれる.
http://www.comp.cs.gunma-u.ac.jp/~koichi/RAND/rand/node5.html

ハイライトを見やすくする

http://apollo.u-gakugei.ac.jp/~yoshiki/chalow/2004-10-22.html#2004-10-22-2

をー。こりゃ便利ですな。うむ。

で、これを見て思ったのだけど、miyagawa さんとこでやってた「マイミ
クシィ日記をハイライトする bookmarklet」なネタも、これを使ってやれ
ば、より便利になりそげじゃないですか?

ということでやってみた。

javascript:(function()%20{a=document.getElementsByTagName('A');for%20(i%20=%200;%20i%20<%20a.length;%20i++)%20{l=a[i].href;if%20(l.match(/view_diary/)%20&&%20!l.match(/owner_id=/))%20{a[i].innerHTML%20=%20''+a[i].innerHTML+'';}}})();

こんな感じで。をぉー!!綺麗で見やすくなったっすよー。

range を使うべからず

range は配列を生成するので、10**7 輪姦す、とかの場合、10**7 の要素
を持つ配列を生成してくれちゃうので使いものにならぬ。

ということに気付いた。いや、たぶん FAQ なんだろうけどさー。で、ど
うしようかなぁ、と思ったわけですよ。while でゴニョるのはスマートじゃ
なくて嫌だなぁ、とか思ったわけですよ。

というわけで、イテレータでやることにした。しかし、10**7 回の反復な
んてのは、誰でもやるわけで、なんかしらのスマートなやり方があるとは
思うんですが。

知ってる方いらっしゃったら、コメント書いていただけると嬉しいです。

Quantian

http://dirk.eddelbuettel.com/quantian.html

科学用 Knoppix

R, including several hundred add-on packages from CRAN and other R
package repositories, out-of-the box support for the powerful ESS
modes for XEmacs as well as the Ggobi visualisation program;

Octave, with add-on packages octave-forge, octave-sp,
octave-epstk, matwrap and Inline::Octave;

bioinformatics tools such over 100 packages from the BioConductor
project, as well as bioperl, biopython and applications such as
blast2, emboss and hmmer;

Computer-algebra systems Maxima (including the X11 front-end and
emacs support), Pari/GP, GAP, GiNaC, YaCaS, and Axiom;

GSL, the Gnu Scientific Library (GSL) including example binaries;

the QuantLib quantitative finance library including its Python
interface;

the Grass geographic information system;

the OpenDX and Mayavi data visualisation systems;

TeXmacs for wysiwyg scientific editing as well as LyX and kile for
wysiwyg (La)TeX editing;

various Python modules including Scientific and Numeric Python;
Cernlib, a large number of programs and libraries from the CERN
particle physic lab;

the bochs, wine and qemu emulators;

office suites such as OpenOffice.org and KOffice;

and various other programs such as apcalc, aplus, aribas,
autoclass, DrGeo, euler, evolver, freefem, ftnchek, gambit, geg,
geomview, ghemical, glpk, gnuplot, gperiodic, gri, gmt, gretl,
ImageMagick, IPE, lam, lp-solve, mcl, mpich, mpqc, multimix,
rasmol, plotutils, pgapack, pspp, pdl, rcalc, scilab, SQLite,
Tclsh, yorick, xaos, XLisp-Stat, and xppaut;

ということで。SciPy だの Numeric Python だのも入ってていい感じなわ
けですよ。

英語版だけど、まぁ、こういう用途だったら問題ないっしょ。

ipython

An enhanced Interactive Python shell

ということで、標準の Interactive Shell よりも、高機能なシェルだ。


スクリーンショット
http://ipython.scipy.org/screenshots/index.html

スクリーンショットを見ればわかるけど、プロンプトがカラーでいい感じ。
標準の奴は色かわらないから、どこが自分の打った行だかわからなくなり
がちですからね!!


タブ補完がいい感じ!

numarray.rand[tab]

と打つと

numarray.random_array

補完されるとか。


プロンプトで「?」と打つとヘルプが出てくる。


http://ipython.scipy.org/screenshots/snapshot4.png

ipython -profile tutorial

とすると、チュートリアルの作成に使える。

行頭に「>>>」と「...」が入っていても無視して実行する。


@logstart とやるとログを取れる。便利。


http://ipython.scipy.org/screenshots/snapshot1.png

例外がカラーリングされてていい感じだぜ!

エレガントな解を求む

今開発しているプログラムは、数値計算ゴリゴーリなので、途中経過を出
力しているのですが、結果は log scale のグラフにするので、結果を毎
回出力する必要はない。全部出すと、結果のファイルが巨大になって困る。

そんな時にはどうしたらいいんだろうか。

pattern = re.compile(r"\d\d?0*$")
for i in range(1000):
	if pattern.match(str(i)):
		print i

ないし

0.upto(1000) do |i|
	print i if i.to_s =~ /^\d\d?0*$/
end

みたいな感じだろうか。

しかし、これだと無駄に計算時間がかかりすぎる。

なんかもっと効率がいい方法がないものでしょうか。