Monday, 12 July 2010

Book review: Mathematica Cookbook by Sal Mangano

O'Reilly just published a new book, the Mathematica Cookbook, about Wolfram Research's flagship product. This book contains many interesting examples from various different disciplines. Most of these are derived from freely available examples written by other people (primarily from Wolfram Research's original Mathematica Book and also the excellent Wolfram Demonstrations Project) but the author has simplified some of the programs to make them more accessible.

However, the density of the information in this book is incredibly low. Most pages are filled with superfluous Mathematica output that is often not even described in the text:

  • Dozens of triples on page 15.
  • Page 58 lists hundreds of numbers but the text does not even describe their significance.
  • Page 205 lists all of the words with a subset of the letters "thanksgiv".
  • Page 226 is raw XML data.
  • Pages 264-265 are solid code that renders a snowman with circles and some dots for snow (all in black and white).
  • Pages 303-304 are two more pages of code to plot a snowman in 3D.
  • Page 321 lists all of the properties in Mathematica's polyhedron database and page 322 draws 20 polyhedra before page 324 enumerates exactly the same polyhedron properties again.
  • Pages 334-335 enumerate image data numerically (three times).
  • Page 359 displays quadrant swapping with 25x more matrix elements than necessary.
  • Page 360 is the Fourier transform of an image and page 361 is an apparently identical one.
  • Page 507 lists every chemical element and page 571 lists every property of them.
  • Page 553 lists every property available for financial data and page 554 lists them all over again.
  • Page 572 contains an empty graph.
  • and so on...

Many pages are devoted to TreeForm plots of trees that convey no useful information, e.g. the trees drawn on page 129 in the section about red-black trees:

Would have been nice to see red and black colored nodes or even learn how to do that using Mathematica (assuming it is possible). In particular, half of page 115 is a TreeForm view so badly laid out by Mathematica that it is unreadable.

There are also some problems with the technical content itself. For example, the section on red-black trees is a simple translation from Okasaki's excellent book "Purely functional data structures". The only new functionality Sal Mangano added is a remove function but his implementation is completely wrong.

The book is chocked full of advice about optimization. This is both good and bad. Good because Mathematica users need all the help they can get with performance because Mathematica is so slow. Bad because the best advice for a Mathematica user when performance is even vaguely relevant is to ditch Mathematica in favor of a more modern tool with better performance. For example, section 14.10 "Compiling an Implementation of Explicit Trinomial for Fast Pricing of American Options" states that "You need a very fast pricer for American options" but the fastest Mathematica code given can be a whopping 960× slower than a comparably-simple F# solution. Furthermore, it turns out that this was not written by Sal Mangano (the author of the book) but by Andreas Lauschke (a Mathematica and Java consultant) and it is a translation of MATLAB code written by Ansgar Jüngel (an academic).

There are several multi-page program listings written as plain Mathematica code with embedded comments that are difficult to read (they are not even spaced out, let alone typeset properly!) instead of prose. Page 265 is a block of code, for example:

Although the book does contain a frustrating amount of blatant filler, it is very cheap (O'Reilly) and does contain several thought-provoking examples. Whether you use Mathematica or not, you'll probably learn something useful about the state-of-the-art in the Mathematica world by skimming this book. On a higher level, you will probably learn a lot more about business if you consider what this book and the army of Mathematica devotees behind it represent. After all, there is a lot of money in starting a religion!

FWIW, here are the interesting references from the book to original sources:


裕幸 said...


Your comprehensive comment to the book Mathematica Cookbook is nice and suggestive for all Mathematica enthusiasts. I have no objection to your finding which some examples are not smart and few explanations to some codes, even so this is the first book which explain Mathematica as a programming languages since Roman Maeder published a book Computer Science with Mathematica.

My major was a designing programming languages which specify the syntax and the semantics of languages, including grammar-based, frame-based, OO-based etc.

I have also been programming in functional programming languages like ML, gofer, and Haskell.

As you know Mathematica has an origin of functional programming languages and has several functional constructs including Folding, Nesting, Compositing, FixedPoint, etc. This Cookbook treats a functional aspect of Mathematica, it's so nice I think. How do you think?

You are interested in #F and OCaml? They are also nice though I never used them.

Arigato gozaimashita (thank you very much in Japanese).

Flying Frog Consultancy Ltd. said...


Sal Mangano's Mathematica Cookbook certainly touches upon many functional constructs but I think it fails to play to Mathematica's strengths (e.g. metaprogramming can benefit enormously from Mathematica's ability to simplify symbolic expressions but metaprogramming does not get a mention in this book) and even plays to Mathematica's weaknesses (e.g. Mathematica is fundamentally incapable of exposing red-black trees in a useful way but this book devotes pages to them).

Although Mathematica shares some heritage with functional programming languages its only implementation has a great many weaknesses in this context (e.g. tail calls are not properly eliminated and stack overflows are not caught so idiomatic functional programming typically leads to catastrophic loss of data and program). I would expect a book on this subject to go into detail about these weaknesses in order to warn users away from problem areas but, instead, this book blindly advocates Mathematica for anything and everything regardless of its applicability. That is, of course, hugely counter productive.