Mathematica

Mathematica 7 released

As a Mathematica user(MUHAHAHA, that's what's so good about having a parent work at a research institution. MUHAHAHAHA!!!), the release of Mathematica 7 is an exciting news.
Mathematica has becoming Photoshop for math. In the future I will edit photos by typing equations instead of mouse gestures. Love this thing.
Can't wait to get my hands on it...
While it is good. I can't help to notice Mathematica 7 is way bloated and a released too soon. Consider Mathematica 6 was released only a year ago. Does anyone really need speech output system for Mathematica? lol.

I can get my own copy if I gold USAMTS. The last problem for this round is quite hard consider I use the naive and obvious sum up all area and divide method. Still searching for nice identities.

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.

Syndicate content
Honey Pot that kill bots