Saturday, October 1, 2011

Three Programming Languages Compared

Let's take a look at three different programming languages--MATLAB, Python, and LUSH--and compare the ease of plotting with each as measured by the number of commands required to generate a simple sine wave on the interval [0, 2π] and save it to some relatively portable image format.



MATLAB:

t = 0:0.001:(2 * pi);
x = sin(t);
plot(t, x);
axis tight;
xlabel('$t$', 'interpreter', 'latex');
ylabel('$\sin(t)$', 'interpreter', 'latex');
title('$\sin(t)$', 'interpreter', 'latex');
legend({'$\sin(t)$'}, 'interpreter', 'latex');
set(gcf, 'PaperOrientation', 'landscape', 'PaperPosition', [0 0 11.5 8], ...
    'PaperSize', [11.5 8]);
saveas('temp', 'png');



Python using the IPython Shell:

t = arange(0, 2 * pi, 0.001)
x = sin(t)
plot(t, x)
xlabel('$t$')
ylabel('$\sin(t)$')
title('$\sin(t)$')
legend(['$\sin(t)$'])
savefig('temp', format='png')


LUSH:
(libload "libplot/plotter")
(defvar p (new Plotter))
(let ((w (ps-window "/home/cpcloud/Desktop/temp.ps")))
  (==> p PlotFunc "sin(t)" sin 0 +2pi+ 0.001 (alloccolor 0 0 1))
  (==> p SetXScale 0 +2pi+)
  (==> p SetYScale (- 1) 1)
  (==> p SetXLabel "t")
  (==> p SetYLabel "sin(t)")
  (==> p SetLegend "sin(t)")
  (==> p SetTitle "sin(t)")
  (==> p Redisplay))



;; not strictly necessary--it outputs a PS file without this which is perfectly fine
(sh "convert -density 150 -geometry 100% temp.png")


Clearly the winner here using the metric mentioned above is Python using the IPython shell. MATLAB  and LUSH are tied for second, however you might change your mind after seeing their respective plots.

Sadly, the plot created by LUSH leaves much to be desired and sends me running back to matplotlib and (GASP!) even MATLAB. LUSH's plotter isn't buggy per se, it's just not as refined as Python's or MATLAB's.

For some reason though, I still like programming in LUSH much more than programming in MATLAB. Lush is WAAAAY faster during matrix computations and the anonymous function syntax is much more forgiving, e.g., (lambda (x) (somefunction x)) versus @(x) somefunction(x). The ability to write C/C++ code mixed with LUSH code certainly doesn't hurt its coolness factor either.

I have to admit, though, when I took Yann LeCun's machine learning course at NYU I thought Lush was a crazy little language and all those parentheses were going to drive me insane!! Little did I know I would become slightly obsessed with it just under a year later...

Check out LUSH, it has a lot of potential!

* Next up: the LUSH code to make its plotting facility much more user-friendly. Stay tuned.

No comments:

Post a Comment