Saturday, November 12, 2011

Introduction to my Mathematica guide: The Way of Mathematica

Greetings! Several years ago I began creating a guide to Mathematica for my own use--as a reference to what I wanted to learn and remember, especially about Mathematica programming. I also wanted to capture a philosophy about Mathematica, like the Tao--as in the Tao Teh Ching--or Way of Mathematica. How does one 'go with the flow' when programming in Mathematica? It evolved into a book and here I'm posting drafts from the book. Please keep in mind these are drafts and may contain errors or omissions. Of course I'm very interested in your comments and corrections.

One device I use that you may not like is capitalizing Mathematica functions, such as Map or Import, as proper nouns. The idea is to highlight them and impress them on our minds. However I can also see that this usage can be irritating--if so let me know.

More significantly, I use a new programming dialect that I first presented at the Wolfram Technology Conference in 2007--you can download the presentation here:


This dialect, which features heavy usage of Prefix (f@x) and Postfix (x//f) syntax with pure Function (x//f [#, {parameters}]&, offers a number of significant advantages over the traditional Matchfix style. Specifically, these benefits include:

  1. Code is more readable
  2. We have less typing because we finally depart from the confusing matching brackets f[[[parameter1, g[parameter2], h[parameter3]]]] syntax that goes at least as far back as Lisp
  3. Functions are listed in the order in which they are applied as in procedural programming
  4. We have more options to organize our code so as to highlight what's important and bury what is not.

We can think of this dialect as a "functional-procedural fusion" and I haven't come up with a better name for it than that. Accordingly another benefit is that the syntax renders Mathematica easier to learn for procedural programmers. Schematically it looks like this:

Traditional Matchfixh [ g [ f [ x, y ], z ], j ]
Postfix-Pure Functionf [ x, y ] // g [ #, z ]& // h [#, j ]&
Prefixf @ g @ h @ i

Using Prefix and Postfix with abbreviated operators (such as +, -, x, /, ^, as in f@x+5) necessitates knowing Mathematica's Precedence table, included in the 2007 presentation and I will post the code and table for Mathematica 8 here. You can always determine the Precedence of any operators using that undocumented function:

In[68]:= Precedence /@ {Plus, Subtract, Times, Divide, Power}

Out[68]= {310., 310., 400., 470., 590.}

And you can always just use Matchfix, or group with parentheses, if you don't want to deal with Precedence. Of course we all deal with Precedence, and few would advise abolishing all abbreviated operator usage, in mathematics and Mathematica. So think of Prefix in Mathematica as a greatly extended usage of the power of abbreviated operators that harks to the origins of math.

http://reference.wolfram.com/mathematica/ref/Prefix.html
http://reference.wolfram.com/mathematica/ref/Postfix.html

No comments:

Post a Comment