目次へ
Pythonikaを使って Mathematica notebook から GAP を実行する例
Pythonikaを使うことによりMathematica notebook のなかで GAP とMathematica を実行することができる。GAP を実行する仕組みは、python で動く pexpect を使うことにより GAP にコマンドを送り結果を受け取る。
9. Rubik's Cube: 置換と巡回置換(GAP, Mathematica) で GAP とMathematica のコマンドの比較を行った。この例のノートブックを以下に示す。
(* Pythonika loading. *)
dir = NotebookDirectory[];
PrependTo[$Path, dir];
app = "/home/satouy/Pythonika/Pythonika-master/Pythonika"; (* Ubuntu 12.04 *)
Install[app];
(* Check whether it's alive. *)
Links[app]
{LinkObject["/home/satouy/Pythonika/Pythonika-master/Pythonika", 83, 4]}
(* GAP setting *)
Py["\<
import pexpect
child=pexpect.spawn('/home/satouy/gap/gap4r7/bin/gap.sh')
child.maxread=10000
child.expect('gap> ')
\>"]
(* Main Permutation *)
form = StringReplace[StringTrim[#,(" "|"\r\n")],{"\r\n"->"\n"}]&;
(* (1,2,3)^-1; *)
Py["\<
child.sendline('(1,2,3)^-1;')
child.expect('gap>')
\>"];
Py["child.before"] // form
(1,2,3)^-1;
(1,3,2)
InversePermutation[Cycles[{{1,2,3}}]]
⇒ Cycles[{{1,3,2}}]
(* 2^(1,2,3); *)
Py["\<
child.sendline('2^(1,2,3);')
child.expect('gap>')
\>"];
Py["child.before"] // form
2^(1,2,3);
3
PermutationReplace[2, Cycles[{{1,2,3}}]]
⇒ 3
(* (1,2)*(2,3); *)
Py["\<
child.sendline('(1,2,3)^(1,2);')
child.expect('gap>')
\>"];
Py["child.before"] // form
(1,2,3)^(1,2);
(1,3,2)
PermutationReplace[Cycles[{{1,2,3}}], Cycles[{{1,2}}]]
⇒ Cycles[{{1,3,2}}]
(* 文章を読むと。数の列 (1, 2, 3) したがって次である。 *)
Py["\<
child.sendline('List([1,2,3],i->i^(1,2));')
child.expect('gap>')
\>"];
Py["child.before"] // form
List([1,2,3],i->i^(1,2));
[2,1,3]
PermutationReplace[{1,2,3}, Cycles[{{1,2}}]]
⇒ {2,1,3}
(* p1^p2 GAP *)
Hyperlink["GAP manual: 20 Permutations", \
"http://www.math.jussieu.fr/~jmichel/gap3/htm/chap020.htm"]
(* p1^p2
The operator ^ evaluates to the conjugate p2-1*p1*p2 of the permutation p1 by the permutation p2. *)
p1 = Cycles[{{1,2,3}}];
p2 = Cycles[{{1,2}}];
PermutationProduct[InversePermutation[p2],p1,p2]
⇒ Cycles[{{1,3,2}}]
PermutationReplace[p1,p2]
⇒ Cycles[{{1,3,2}}]
pexpect を使ってGAP を実行するアイデアは Mark MacClure氏の web page に載っていた。残念ながら現在彼のページにアクセスできない状態になっている。幸いにも閉じられる前にノートブックをダウンロードしたので参考にさせていただいている。
Mark MacClure, Calling Sage from Mathematica http://facstaff.unca.edu/mcmcclur/Mathematica/Sage/
まとめ
- Pythonika を使って Mathematica notebook で GAP を実行できる。同時に Mathematica の関数を実行できるので、比較するのに重宝している。
GAP について
GAP - Groups, Algorithms, Programming -a System for Computational Discreye Algebra