Thursday, August 28, 2008

How to mess with Comp Sci Students

Assignment: Write a compiler that compiles all programs that can't compile themselves.
Extra Credit: Use the compiler to compile assignment.

Tuesday, August 26, 2008

F# For Scientists Misses the Boat On Mathematica Performance

I recently purchased F# For Scientists by Dr. Jon Harrop after the author mentioned it on the Mathematica Mailing List. According to Dr. Harrop,

Mathematica's .NET-Link technology allows Mathematica and .NET programs to interoperate seamlessly. Moreover, Microsoft's new functional programming language F# provides many familiar benefits to Mathematica programmers:
The marriage of Mathematica with F# can greatly improve productivity for a wide variety of tasks.

I am a big fan of Mathematica and functional programming and have been wanting to check out F# for some time so I decided to give the book a shot. It just arrived today so I can't post a full review but I did jump directly to the small section (5 pages) on using F# with Mathematica.

What did I learn? Well this section rightly claims that Mathematica has awesome symbolic math capabilities (it does). But then it goes on to claim that F# can beat the pants off of Mathematica on raw calculation. Thus it suggested F# programmers should call out to Mathematica for symbolic integration but then evaluate the result in F# for speed (to the tune of 3.4 times Mathematica's speed). I was naturally dubious. The explanation of this speed up is give as

The single most important reason for this speed boost is the specialization of the F# code compared to Mathematica's own general purpose term rewriter. ... Moreover, the F# programming language also excels at compiler writing and the JIT-compilation capabilities of the .NET platform make it ideally suited to the construction of custom evaluators that are compiled down to native code before being executed. This approach is typically orders of magnitude faster than evaluation in a standalone generic term rewriting system like Mathematica.


Okay, hold the phone! First off, I did not know the F# language could write compilers. I'll forgive this as poetic use of language. I guess I sort of know what he meant to say. More interesting is that we have gone from 3.7 times to "orders of magnitude". Now, I don't take anything away from the brilliant folks at Microsoft, but the equally brilliant folks at Wolfram have been focusing exclusively on mathematics software for 20 years and you might think they learned a thing or two about computational speed!

Here is the example from the book...

First, he uses Mathematica to integrate a function.


Integrate[Sqrt[Tan[x]],x]

(-2*ArcTan[1 - Sqrt[2]*Sqrt[Tan[x]]] +
2*ArcTan[1 + Sqrt[2]*Sqrt[Tan[x]]] +
Log[-1 + Sqrt[2]*Sqrt[Tan[x]] - Tan[x]] -
Log[1 + Sqrt[2]*Sqrt[Tan[x]] + Tan[x]])/(2*Sqrt[2])


He then goes to show that Mathematica takes 26 secondsto evaluate this function in loop for 360,000 iterations.

He then shows a translator that converts the Mathematica to F# and the F# code does the same work in 7.595 seconds.

So far Dr. Harrop is correct but like some many others who are in a rush to show their new favorite language superior to another's, he forgets to read the manual! Particularly, the section on optimization! If he had he would have found a handy little Mathematica function called Compile. Hmm, sounds promising. And in fact....


cf = Compile[{{x, _Complex}}, Evaluate[Integrate[Sqrt[Tan[x]],x]]]
Timing[Do[cf[x + y I],{x,-3.0,3.0,0.01},{y,-3.0,3.0,0.01}]]

{5.281,Null}

That's 5.281 seconds on my relatively underpowered laptop (Thinkpad X60) !

Some might feel I'm being a bit harsh on Dr. Harrop but after all he made me layout bucks for a book that promised me "many familiar benefits" only to deliver 5 measly pages of half truth. F# programmers may benefit from Mathematica but the jury is still out as to whether the reverse is true.

Monday, August 25, 2008

Drilling Square Holes

One of the fringe benefits of working on a book is all the tidbits of knowledge you come across while doing research. While working on the graphics chapters I cam across a shape known as a Reuleaux triangle.




It turns out that this shape is the key to doing what on the surface may seem impossible, Drilling a nearly square hole.


Saturday, July 26, 2008

A Biological Programming Language

I've often said to friends that if I could start my career over again I would go into biology instead of computer science. Now, perhaps, there is a way to have a foot in both worlds.

Little b is a programming language for modeling biological systems. Quoting from the languages site...
The little b project is an effort to provide an open source language which
allows scientists to build mathematical models of complex systems. The
initial focus is systems biology. The goal is to stimulate widespread sharing
and reuse of models. The little b language to allow biologists to build
models quickly and easily from shared parts, and to allow theorists to program
new ways of describing complex systems.


Currently, libraries have been developed for building ODE models of
molecular networks in multi-compartment systems such as cellular epithelia.
Aneil Mallavarapu is the author and inventor of little b, and runs the
project. Little b is based in Common Lisp and contains mechanisms for rule-based
reasoning, symbolic mathematics and object-oriented definitions. The syntax is
designed to be terse and human-readable to facilitate communication. The
environment is both interactive and compilable.

Makes me wonder if Mathematica would be a good enviornment for similar exploration but with more sophisticated tools already built in.

Friday, July 18, 2008

Find the next row. Win $25

Each row below is produced by a definite rule. What is the next row and what is the rule? $25 Prize to the first person who posts the answer in the comments or emails me at [s m a n g a n o [at] i n t o - t e c h n o l o g y [dot] c o m].


3
4
5
5,3
6,4
7
7,4,1
7,5,1
8,3
8,6
8,6,3
9,4
9,5,3
9,6,1
9,7
9,7,5,1
10,4,1
10,5,1
10,6,4,1
10,7,4
10,7,5
10,8,4
10,8,5,3
11
11,6
11,6,4,1
11,7,1
11,7,5
11,7,5,3
11,8,4
11,9,4,1
11,9,6
11,9,7,1
11,9,7,4
12,5
12,5,3
12,7
12,7,5,1
12,8,3
12,8,6
12,9,1
12,9,4
12,9,7
12,9,7,3
12,9,7,5,1
12,10
12,10,6,4,1
12,10,8,4
12,10,8,5,3
12,10,8,6,1
13
13,5,1
13,6
13,7,5
13,8,4
13,8,6,1
13,9,3
13,9,4,1
13,9,6,3
13,9,7,1
13,9,7,4
13,10,5
13,10,7,5,1
13,10,8,3
13,10,8,4,1
13,10,8,6
13,11,6,1
13,11,7,3
13,11,8,4,1
13,11,8,5,1
13,11,8,6,3
13,11,9,4
13,11,9,6,4
13,11,9,7,4,1
14,3
14,5,1
14,6,4,1
14,7,5,3
14,8,4
14,8,6,4
14,9,6
14,9,6,3
14,9,7,5,3
14,10,1
14,10,5,3
14,10,6,4
14,10,7,4,1
14,10,8,4,1
14,10,8,6
14,10,8,6,3
14,11,1
14,11,7
14,11,8
14,11,8,4,1
14,11,8,6,4,1
14,11,9,4
14,11,9,6,1
14,12
14,12,3
14,12,7,5,3