WITM is flawless for accessing Mathematica. But windows users are not so lucky, WITM's default command's are for Linux. This article will help you learn a few tricks, include how to input and capture output in programs with command line interface.
I'm working with Mathematica version 6.01, I didn't test on other versions, but it should be the same.
First, I would like to thank WITM, it showed me there is a command line version of Mathematica. In windows, it would be math.exe, inside the directory where Mathematica is installed. Let's use it find 10!.
$io= array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
);
$location = 'C:\progra~1\wolfra~1\mathem~1\6.0\math.exe';
//The location of mathemacia's math.exe
$p = ' -batchinput -batchoutput';
$process = proc_open($location.$p,$io, $pipes);
$command = '10!'; //the Mathematica command
The above code opens a process, and declares $pipes[0] as the standard input and $pipes[1] as the stand output. Both are used like files.
Run it, it correctly output 3628800. Obviously.
The real reason for me to use Mathematica is to output images of math functions. I reused the command from WITM, made a small improvement so more than one user can use it at the same time.
$command='expr=Plot3D[Sin[x y],{x, 0, 4}, {y, 0, 4}];
head=ToString[Head[expr]];
If[head==ToString[Graphics]
|| head==ToString[SurfaceGraphics]
|| head==ToString[ContourGraphics]
|| head ==ToString[DensityGraphics]
|| head==ToString[GraphicsArray]
|| head==ToString[Graphics3D] ,
Export[IntegerString[Hash[expr,"MD5"],16]<>".gif",
expr,ImageSize->500],
expr]';
if(is_resource($process)){
fwrite($pipes[0], $command."\n");
fclose($pipes[0]);
$content = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
}
$filename = substr(substr(trim($content) ,1),0,-1);
The above command will hash the image with MD5 and output it the name to the standard output. The only important thing to do is to also add the directory you want to output to.
The $content have to be trimmed, and remove the double quotes in both side of the string.
Below is the output generated by the combination of the above script and Mathematica.

With this, it's possible to create a Drupal module, include a filter capture the Mathematica commands, run Mathematica and generate result. The image caches in the file system until the author changes the commands. Neatness!
Most other command line program can work with PHP in this way.
Bookmark/Search this post with:
Hello. I think your code has
Hello. I think your code has a lot of bugs because it doesn't work when i'm trying to run it on server. One bug I found in the line $p = ' -batchinput -batchoutput'; There should be "\n" at the end. After this arithmetical expressions are solved successful. Next bugs i didn't find unfortunately. Even I tried to make this simple command
Export["test.gif", Plot[Sin[x], {x, 0, 10}]]
the result is the error
Syntax::sntxq: The string starting at "Export[\"test.gif\", Plot[Sin[x], {x, 0, 10}]]" has no closing quote. ^
In your example there is such problem. May be my version of Mathematica is too old, or you really made a mistake? In cmd all is right, math.exe drawing the picture, but through the PHP there are explained problems. My version is 5.1
I don't know... and
I don't know... and currently I can't test it due to my busyness.
I also agree
I also agree with OWK . But I liked it and although i am not expert in mathematics but I have understood it very well.
Post new comment