PHP

Plotting my mood

I did a plot of the mood wave though out the the day.

Sometimes, I wish I have stereotypical white parents(SWP). If I have a goal, SWP will support me. SWP will bring back pizza once in a while, SWP eat out with family once in a while, SWP permits food like chips and soda around the house, most importantly, SWP allows their kids to stay at home if they are really sick. While my Asian parents (MAP) don't eat out with family, does not allow rubbish food because they are just cheap, does not have any form of popular entertainment, only cares about the SATs and have extremely high expectation on me. I hate having MAP always nagging about the SATs. I can not just throw away everything and do the SATs, let me be what I want to be. It's such a sad thing that I can not select my own family member. >.< My god they don't even believe in me. I don't drink, smoke or do drugs, and the SWP can believe kids who does all of the above and MAP will not. I lived too long in a White neighborhood, I should know I can't expect much from MAP. I will get to college and finally I can move away from them.

The day is quite bad. The worst time is when some students got a +20 bonus points in their test because others failed to do a simple lab. WTF!!!,if it's like 5 points I can understand... 20 points, is 1/5 of a test!? Of course, I'm suppose to be happy about it, I'm sure the reason we did not finish the simple lab is not me or my partner, but the equipment. My group did the exact same test 3 times with no result, that wasted us 40 minutes, then we did it again with another set(the 3rd set we tried) of chemical and equipment, and everything start to go well. But the 40 minutes is already wasted so our group can not finish the lab.
There are only 8 set of equipment out there, and strangely, twice, we chose the ones that does not work. What's the odd. If there is a god, it must hate us.

After chem, it's pre-calc, and I'm doing math problems from the Five Hundred Mathematical Challenges. I used the entire period solve 2 questions. Turns out the answer have really elegant proof can be done in less than 5 minutes. That's way to depressing. In fact, doing math problems is really depressing until I found the solution, then get destroyed by seeing other's solutions. This joke would perfectly explain what I'm like when I do math. From a page filled with corny math jokes.

"So how's your boyfriend doing, the math student?"
"Don't mention that crazy pervert to me anymore! We broke up."
"How can you say such a nasty thing about him? He seemed to be such a nice boy."
"Imagine! He was restless during the days and couldn't sleep at night - always trying to solve his math problems. When he had finally done it, he wasn't happy: he would call himself a complete idiot and throw all his notes into the garbage. One day, I couldn't take it anymore, and I told him to drop math. You know what he told me?"
"No."
"He said, he enjoyed it!!!"

The graph is created with JPgraph with cubic spline connects the points.

One of the MAP is such a control freak, she want to control everything, include my future occupation and marriage. Just wait... when I get to college, I will tell her to stay out of my life. Can't wait till that day.

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.

PHP Abstract, podcast even I can enjoy

Podcast isn't the kind of media I would like to receive, since reading is much faster than listening.

The above image states the only reason I ever tried the PHP Abstract, Cal Evans is giving out free books to blog about it1! Running Happily
So I picked out 2 podcasts and listened to them...
Oh... cheesy opening...

After hearing the Five Ways To Kill a Software Project. It's mostly about how to not do software project for clients. Some things I definitely want to remember. Not now though. the only client is myself....

The opinionated software one is one thing that's really struck me. I don't mean I'm a person creating millions of configurations for the users, I don't give the users options for configure them because everything I wrote is so delicate2. Sounds like PHP codes for embedded systems. I have the feelings I can never write something suitable for anyone else but me.

The podcast's opening + ending took 1 minute in total, that's quite long compare to the entire podcast. Come on, it's an abstract, extended brief news, cut to the point...

Oh, that reminds me another reason I like reading better. No one can jump though sections in podcast as easy as textbooks.
[Nothing against podcast, I'm just jealous about the people who have a iPod to listening to podcast on the go]

So, I recommend PHP Abstract to anyone who have interest in new(or interesting) stuff in PHP and can find 10 minute on a average week3 sitting in the bus with an ipod.

  1. 1. won't it be cool if I can get something not from my book collection
  2. 2. configuration for prime spirals and matrix rain effect? Don't think so
  3. 3. that's 59.52 milliseconds per average minute

PHP class for gif animations

The gif class is made by SuperRembo back in 2006, very short and compact. View the source here. The old time favorite, GIFEncoder is still at hand, so I did a test and the result shows SuperRembo's gif class is faster after a little modification(my test could be inaccurate because the structure of 2 gif system is different). Take out the ob_start and other part, let the user input the output image instead of the resource provide better result improves speed a thousandfold.
SuperRembo's gif class could be faster because it uses the PHP built in function pack instead of a massive amount of bitwise operators.
Anyone who thinks they can do a better job, read the gif89a specification. Strange, I don't see anything about animated gif in there.

Syndicate content
Honey Pot that kill bots