2007-10-17 21:22:02
今度はMaximaをproc_openで操作してみた。
#!/usr/bin/php
<?php
$array = array(1,2,0,2,1,1,0,1,1);
$matrix = "[";
$i=1;
foreach($array as $val){
$matrix .= $val;
if(fmod($i,3)==0){
$matrix .= "],[";
}else{
$matrix .=",";
}
$i++;
}
$matrix = preg_replace("/,\[$/","",$matrix);
$descriptorspec = array(
0 => array("pipe","r"),
1 => array("pipe","w"),
);
$process = proc_open("maxima",$descriptorspec,$pipes);
if(is_resource($process)){
fwrite($pipes[0], "load(eigen);\n");
fwrite($pipes[0], "A:matrix(".$matrix.");\n");
fwrite($pipes[0], "eigenvalues(A);\n");
fwrite($pipes[0], "eigenvectors(A);\n");
fwrite($pipes[0], "quit();\n");
fclose($pipes[0]);
while(!feof($pipes[1])){
$result .= fread($pipes[1], 4096);
}
fclose($pipes[1]);
proc_close($process);
}
echo $result;
?>
行列を入力する形式が面倒臭い。正しく行列を入力して固有値と固有ベクトルを求めてみると、
Maxima 5.10.0 http://maxima.sourceforge.net
Using Lisp GNU Common Lisp (GCL) GCL 2.6.7 (aka GCL)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
This is a development version of Maxima. The function bug_report()
provides bug reporting information.
(%i1) (%o1) /usr/share/maxima/5.10.0/share/matrix/eigen.mac
(%i2) [ 1 2 0 ]
[ ]
(%o2) [ 2 1 1 ]
[ ]
[ 0 1 1 ]
(%i3) (%o3) [[1 - sqrt(5), sqrt(5) + 1, 1], [1, 1, 1]]
(%i4) sqrt(5) 1
(%o4) [[[1 - sqrt(5), sqrt(5) + 1, 1], [1, 1, 1]], [1, - -------, -],
2 2
sqrt(5) 1
[1, -------, -], [1, 0, - 2]]
2 2
(%i5)
という具合になる。正しく結果を受け取れるのだが、最初にMaximaの説明がついてくるとか、計算結果の表示形式も、あまり受け取った後の処理には向いていないようだ。コマンドラインで操作できるものなら大抵のものはproc_openで操作出きそうなのだけれども、それに向いているものと向いていないものがあるようだ。
R 2.6.0が出ていた。全然気づかなかった。何度かRのトップページを見ているのに。