Tuesday, 26 May 2009

F# vs Mathematica: Pythagoras tree

The F# News blog recently published an article about the Pythagoras tree that included the complete F# source code for a program that renders the tree. Interestingly, the Wolfram Demonstrations Project contains a similar implementation written in the Mathematica language, which is a domain-specific language designed for interactive technical computing.

Surprisingly, the implementation written in the general purpose F# programming language is not only much faster but is also substantially shorter and clearer. Here is the F# code:

And here is the substantially more complicated Mathematica code:

This example really highlights just how versatile the F# programming language has become without sacrificing the benefits of more conventional languages.

10 comments:

fededevita said...

Surprisingly? I'm surprised you wrote "surprisingly"!

Flying Frog Consultancy Ltd. said...

Mathematica's brevity is heavily hyped by Wolfram Research and it is, indeed, a very concise language in the context of interactive technical computing (including graphics).

Hence I was surprised to see that this program is substantially shorter when written in F#.

gustavolk-swagen said...

Pretty outstanding. Thanks.

Ilya said...

Because people who wrote these examples for mathematica have no idea of how to use it. The only somewhat ugly part of mathematica implementation should be drawing the tree.

Ilya said...

Not that I want to bash f#, it's just mathematica has pretty equivalent functional set of tools, and somewhat more cumbersome graphics manipilation/options.

In F#:

let f a = a*a

In mathematica

f[a_]:=a^2;

is pretty equivalent. Also, using ready-made functions like RotateTransform, while hiding hte implementation in the libraries, only makes comparison a bit unfair.

planetmarshalluk said...

I'm a big fan of functional programming, including F#; but this is a really unfair comparison.
For one thing, the Mathematica code is implementing an iterative solution rather than a recursive one, sacrificing a great deal of brevity ( and clarity ) in the process. Below is how I would implement the Pythagoras Tree function in Mathematica. I have purposely omitted any interaction or styling code for clarity. ( unfortunately, formatting may mangle this somewhat ).

Regards,
Andrew.


pythagorasTree[n_,bend_]:=Module[{drawLeaf,left,right,leftv,rightv,t},
t=(1-bend)Pi/10+bend 9 Pi/20 //N;
{left,right}=(RotationMatrix[#].ScalingMatrix[{Cos[#],Cos[#]}]&)/@{t,-Pi/2+t};
{leftv,rightv}={{0,1},{Cos[t]^2,1+1/2Sin[2t]}};
drawLeaf[0,trans_,p_]:=Sow[GeometricTransformation[Rectangle[],{trans,p}]];
drawLeaf[m_,trans_,p_]:=Module[{},
Sow[GeometricTransformation[Rectangle[],{trans,p}]];
drawLeaf[m-1,left.trans,left.p+leftv];
drawLeaf[m-1,right.trans,right.p+rightv]
];
Flatten[Reap[drawLeaf[n,IdentityMatrix[2],{0,0}]]//Last]//Graphics
]

Flying Frog Consultancy Ltd. said...

@planetmarshalluk: Your code is better but it does not recurse to a consistent depth, does not use color and needs to have the value of "bend" calculated to give a 3-4-5 triangle.

Nate Parsons said...

Sure, the F# code is more concise, but you are using your own graphics library. I'm sure your library's great and concise and super-fast and all, but it's pretty disingenuous to compare Mathematica out-of-the-box to F#+some library, even if the library is just a thin wrapper around Microsoft's code (I wouldn't know).

jay paul said...

Really nice post, you got great blog and Thank you for sharing This excellently written content. Waiting for next one.

HP - PRO 15.6" Laptop - 4GB Memory - 500GB Hard Drive

HP - PRO 15.6" Laptop - 4GB Memory - 500GB Hard Drive - Tungsten

YouLoseBellyFat said...

Great code website on Java Codings