2008/04/23

Why I use Mathematica and not Matlab

I have to admit that I am somewhat of a Mathematica partisan.

My first serious use of the computer was Mathematica (except for a very heated PC-Tetris phase--that was pretty serious). My father got me an introductory student license of v2.4 and the Gray/Glynn book, and all through high school, I enjoyed physics by making (what I thought were) cool simulations of whatever motion equations and statistical physics that we learned.

In college, I was a biochemist, without programming. In medical school, I switched to neurophysiology, and picked up Matlab in the process. But whenever I could, I used Mathematica. For my use, Mathematica v5 was just as fast as Matlab, and the graphing capabilities were somewhat superior. At that time, Matlab and Mathematica were mostly equal in functionality, but I preferred Mathematica.




That was then. Now that Mathematica is up to v6, I see no compelling reasons to use Matlab instead of Mathematica, at least in Neurophysiology (except maybe that you and your institution cannot afford a license). Right now, the Mathematica graphics capabilities far surpass anything that Matlab can dream of, and the data crunching is lightning fast (I admit that v2.4 was pretty sluggish sometimes--for me, this hasn't been a problem since v4).

Even if you are working with a large legacy Matlab library, you can phase out by gradually translating parts to Java code (which can be called easily from both Mathematica and Matlab). The improvement in code readability, debugging, productivity and maintenance should pay off in a relatively short time, even for a large switch.

It is true that some Matlab toolbox functions, such as DAQ functionality, are not available in Mathematica--but these toolboxes often have superior counterparts outside of Matlab anyway. (For example with DAQ, LabView is much easier. It just takes some extra glucose upstairs and a few weekends to figure out--but it is well worth the effort in saved time.) Most mathematics/engineering functions are also readily available as open source Java libraries.




Here are my top reasons why I use Mathematica and not Matlab:-

  1. Easy output of high quality graphics... This is definitely my biggest reason to choose Mathematica over Matlab. Mathematica graphics are all vector-based, and even on screen, the smooth rendering is not out of place on a sleek modern operating system. Matlab reeks of Win 3.1.


    What's more, Mathematica graphics are all well-defined vector objects, and therefore Mathematica graphics can be directly output at publication quality to .pdf or .ps, without any hassle. Last time I checked, Matlab graphics output was still struggling with pixels and dpi. This could be a very compelling reason for most physiologists to switch to Mathematica.

    Since Mathematica graphics are well-defined vector objects, it is trivial to combine two plots to come up with a third. All plots can, of course, be serialized by using Save["filename", plot], so that you can reload the same plots later and instantly make new combinations, at full resolution. This may seem trivial, but it's a real time-saver when you're playing around with various plots and trying to figure out how to present your data most effectively.


    There are also some extra benefits to Mathematica graphics. For instance, 3D plots in Mathematica can now be rotated interactively, in situ, by click-dragging. Movies can be exported to Flash movies, which can be easily pasted into powerpoint or a webpage. V6 now also supports alpha blends (Matlab can also do transparency in 3D graphics, but last time I checked, not in regular graphics). The following simple graph, for instance, should be a huge hassle (if not impossible) to make with Matlab.


  2. Mathematica syntax makes sense... Matlab syntax is notoriously incoherent, which is unsurprising given its origin as a DOS hack. Similar functions often take positional arguments in different order. Some functions take arguments as strings, whereas similar functions take the arguments as symbol names, without the ' '. There is even that arcane signature where you put an option name, as a string, and in the next argument, the option value. Naming is also incoherent--for example, to plot a matrix, you use plot(), a verb-name, but to play the data as a sound, you use sound(), a noun-name. I'm probably not the only one who spends way too much time in the Matlab help window, looking up how to call functions and the name of functions, compared to actual work in the main Matlab window... And, bad language design also leads to buggier code.

    In contrast, Mathematica functions which do similar things always take the same arguments, in the same order. Naming is logical, and coherent. So for instance, if you know how to use Plot[] to plot a sin wave, you also know how to use Play[] to hear it. And Mathematica function names are not abbreviated (except for a handful like Sin, Cos, Exp, Log), but spelled out fully. So to get a Chi-square distribution, you type ChiSquareDistribution[] instead of chi2distrib or XsqDistrib or chi2d or whatever. At first, this may seem like a pain because of the extra typing, but the longer function names are usually the less common ones, and this full-name convention actually saves a lot of time, because you need to use the help screen less--even in the more common functions, you don't have to worry at all whether the function you want is num2str or num2string or numtostring, you just type ToString[].

    I read recently that this linguistic coherence is because the creator of Mathematica, Stephen Wolfram, still reviews all new Mathematica syntax for each edition... maybe a guy as smart as this should be doing other things with his time, but if he is willing to put in the effort, I will definitely, gratefully, reap the benefits.

  3. Cross-platform compatibility, version-to-version compatibility... I didn't realize just how bad Matlab was for compatibility, until I had to run a pretty spiffy Matlab GUI (that I'd written on Win) on a Mac. Now that Matlab has a Java core, you would think that cross-platform compatibility would come for free...

    Also, everybody knows of Matlab toolboxes and data analysis packages that don't quite run correctly unless it's on version 7.0.2 (or whatever). One look at some of the internal Matlab functions which are implemented as .m files, and it will be immediately obvious why they have compatibility bugs--the Matlab syntax is definitely NOT designed for readability. What's even scarier is, how many other bugs (or pseudo-bugs) are there that you don't notice, that are screwing your results? And if they themselves can only write such unreadable code, how do they expect end users to use Matlab well?

  4. Cool new features... Check the Wolfram Demonstration site for many examples of how people have used the cool new features of Mathematica. Many of the new features are pretty useless for my day job (biomedical research), but hey, you never know.

    I've been using Mathematica for over a decade now, and the basic structure of the language is as solid as ever. That structural soundness probably allows cool new features to be incorporated without creating balls of spaghetti. I wouldn't be surprised at all if I'm still using essentially the same language for technical computing, another 10 years from now.

  5. Symbolic capacities... This one is not so important for physiologists, but it comes in handy occasionally, and it is the original strength of Mathematica. For a physiologist, it is nice that you can play around with formulae, simplify and symbolically solve differential equations, etc., without digging out your math books and reliving the trauma of high school.

No comments: