Archive - Dec 2, 2007

Date

Work with Mathematica in PHP in windows

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.

if(is_resource($process)){
    fwrite($pipes[0], $command."\n");
    fclose($pipes[0]);
    $content = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    proc_close($process);
} 
echo trim($content);//the output contains some white spaces

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.

What to use to serving dynamic plotted math functions

I was trying hard to find a alternative for showing mathematical graphs on my site. I want a grapher designed for plotting math equations (not statistical charts) and crap free.
I'm using a shared hosting, GNUplot + Simple PHP interface is not the most simple choice, especially the interface is far away from complete, more of a proof of concept script.

webMathematica, well, the Amateur version is free, but requires Premier Service, which requires a mathematica license. Let's say I brought mathematica student edition for $129 just to get the license and then pay for premier service to get my "free webMathematica amateur", that cost more than 2 years of quality hosting on site5. Other than it's expensiveness, it's totally amazing because it's the web front end of Mathematica.

If you have mathematica on your server, you can use WITM as a front end, or write your own [there's a command line version of mathematica comes with your beloved $129]. Remember to make cache system. Trust me, plotting the factorial from 1 to 10^5 is not cpu friendly. I will write about that really soon.

openPlaG is simple to use and take less than 1 minute to install (or even less.. download.. extract) and free. Over 150 built in math functions, include derivative and integral. But implemented in PHP result it's slowness. Write a cache module will avoid the problem of generating images on the fly with a interpreting language.

But openPlaG is only a back up for what I'm really using right now, a duplicate of Walter Zorn's Online Function Grapher.

It uses almost no server CPU (except output the js file) when compared with openPlaG. I believe it's the best choice for anyone who don't have the money, the time and the CPU. In the Drupal implementation [mathfilter], it is iframe a php file on the server, increases some CPU usage.

Calc5's graph calculator would be exactly what I would like to use to show graphs on this site. Compare to Walter's, this script uses canvas, and easy to zoom in/out, drag the plot to move around like in Google map. too bad there is no "embed this graph" link anywhere and does not show the corrdinate of the mouse either.

JS masters really can make a graphing calculator in JS and save all mathematicians.

Honey Pot that kill bots