Thu, 16 Oct 2008

NYT bogosity

Wolfram Research's Theo Gray uses Mathematica to dismantle, pulverize and incinerate a snarky little chart in a recent issue of a New York "news-paper".

Here's the Mathematica Demonstration Theo wrote.

Mon, 18 Aug 2008

Unspeakably awesome

Ken Levasseur's Exploring Baseball with Retrosheet and Mathematica.

The objective of this project is to use the power of Mathematica to manipulate files that have been made available through retrosheet.org in order to do baseball research.

Take a look at the examples!

Mon, 23 Jun 2008

Mathematica turns 20 today

Stephen Wolfram reflects on 20 years of just starting out and Jean Buck takes a stroll down memory lane and looks at how Mathematica has grown. I started there in May 1996 just after the English edition of Mathematica 3.0 was released - my first job was to help typeset the German and French translations of The Mathematica Book using our homegrown LaTeX-based system.

Tue, 29 Apr 2008

The ten-millionth Bernoulli number

Wolfram Research's Oleksandr Pavlyk recently computed the 10-millionth Bernoulli number using Mathematica:

BernoulliB[10^7]

(It took about 6 days to run).

He has a readable essay about it along with proof that the monster number he generated actually is a Bernoulli number.

Thu, 24 Apr 2008

A look inside the Mathematica build system

Pat Rice on building the product every day.

Fri, 28 Mar 2008

Fun with Mathematica

Once you get used to writing things in the Mathematica language, all sorts of fun things become possible. This morning I printed a set of small two-sided Mexican flags that we'll wrap around toothpicks, glue together and stick into Krispy Kreme doughnuts tonight:

TableForm[
  Partition[
    Table[
      Framed[
        CountryData["Mexico", "Flag"]
      ],
      {24}
    ],
    2
  ],
  TableSpacing -> {Automatic, 2}
]

and I just solved a little regular expression problem posed by my boss:

> What's the regex for matching a three digit version number?
>
> Valid examples (where 'x' is an integer > 0)
> x
> x.
> x.x
> x.x.
> x.x.x

Howzabout -

In[19]:= strings = {"6", "6.", "6.0", "6.0.", "6.0.1"}
Out[19]= {"6", "6.", "6.0", "6.0.", "6.0.1"}

In[30]:= StringMatchQ[#, RegularExpression["[0-9](\.([0-9](\.([0-9])?)?)?)?"]] & /@ strings
Out[30]= {True, True, True, True, True}

Thu, 27 Mar 2008

Something like Subsets

This is the first entry in what I'm calling my "list cookbook". When you deal with sets of things, you sometimes need to transform the sets to do something nifty with them. Here's the example that started me off on the cookbook idea.

Summary

Problem

Given a list {a, b, c, d, e}, what Mathematica commands will turn that into {{a}, {a, b}, {a, b, c}, {a, b, c, d}, {a, b, c, d, e}}?

Solution

Thanks to Mike Pilat of Wolfram Research:

In[1]:= list = {a, b, c, d, e}
Out[1]= {a, b, c, d, e}

In[2]:= Reverse[NestList[Most, list, Length[list] - 1]]
Out[2]= {{a}, {a, b}, {a, b, c}, {a, b, c, d}, {a, b, c, d, e}}

Variations

The beauty of Mike's solution is that by tweaking bits of it you can arrive at other possibly useful list transformations. Change Most to Rest and don't Reverse the list and you get:

In[3]:= NestList[Rest, list, Length@list - 1]
Out[3]= {{a, b, c, d, e}, {b, c, d, e}, {c, d, e}, {d, e}, {e}}

Similarly, but Reverse-ing the list before processing it:

In[4]:= NestList[Most, Reverse@list, Length@list - 1]
Out[4]= {{e, d, c, b, a}, {e, d, c, b}, {e, d, c}, {e, d}, {e}}

which is equivalent to swapping Rest for Most and Reverse-ing each element of the resulting list:

In[5]:= Reverse /@ NestList[Rest, list, Length@list - 1]
Out[5]= {{e, d, c, b, a}, {e, d, c, b}, {e, d, c}, {e, d}, {e}}

One more variation:

In[6]:= NestList[Rest, Reverse@list, Length@list - 1]
Out[6]= {{e, d, c, b, a}, {d, c, b, a}, {c, b, a}, {b, a}, {a}}

which is equivalent to:

In[7]:= Reverse /@ NestList[Most, list, Length@list - 1]
Out[7]= {{e, d, c, b, a}, {d, c, b, a}, {c, b, a}, {b, a}, {a}}

Fix vs IntegerPart

I occasionally play around with computational astronomy, and the book on the subject is Paul J. Heafner's Fundamental Ephemeris Computations, which gives routines for working with JPL's high-accuracy planetary ephemerides.

Since Dr Heafner wrote his functions in PowerBASIC, I needed to translate PowerBASIC's FIX function to some Mathematica equivalent this evening. I read over the scanty docs at powerbasic.com and thought it sounded like Mathematica's IntegerPart; checking the docs for IntegerPart I was amazed at the amount of information provided - from basic definitions and explanations to arcane unexpected applications of the function, plus relevant links to related functions and, of course, the magisterial entry at MathWorld - check out that table of fractional-part/integer-part functions! I'm mighty proud of our documentation.

Not just an employee, but also a satisfied customer.

Sun, 09 Mar 2008

Vexillological Mathematica

One of our kids is a budding vexillologist and he greatly enjoys things like this flag page I made with Mathematica. When you move your mouse over a flag the country's name will pop up in a moment, which makes it nice for flag quizzes. Here's how I did it:

Export["/home/billw/flags.html",
  TableForm[Partition[
    Tooltip[Framed[CountryData[#, "Flag"]], CountryData[#, "Name"]]& /@ CountryData[],
    5, 5, 1, " "]]]

Thu, 06 Mar 2008

Real-world Mathematica

Here are some interviews with Mathematica users filmed in various places around the company headquarters in Champaign during last year's Wolfram Technology Conference.

Mon, 25 Feb 2008

6.0.2 is out the door

Here's Peter Overmann's summary of what's new in Mathematica 6.0.2. Meanwhile, back to excelsior as usual.

Thu, 21 Feb 2008

Immersion

Coming soon: two weeks of deep immersion in Mathematica at the Advanced Mathematica Summer School.

Sun, 17 Feb 2008

Moving mountains

This week I'm revisiting the very first thing I wrote in Mathematica, a documentation testing package. A quick initial survey revealed a vast panorama of tottering kluge towers built high atop mountains of cruft. It's been very pleasing to retain the odd little working pieces and utterly trash the rest of the infrastructure in favor of a tidy little routine that handles more in a dozen lines of code than what I initially squeezed into 600 lines of bizarre rigamarole.

Here it is, all boiled down to the uttermost simplicity:

RunTests[dir_String, tests_List] :=
  Module[{manifest, res},
    manifest = FileNames[{"*.nb"}, dir, Infinity];
    res = Reap[Module[{nb, nbExpr},
      nb = #;
      nbExpr = Quiet@Get@nb;
      Module[{test},
        test = #;
        If[DocumentationCheckQ[nbExpr, test] === False, Sow[nb, test]];
      ]& /@ tests;
    ]& /@ manifest, _, Rule][[2]];
    (* return results sorted from most failures to least *)
    Sort[res, Length[Last[#1]] > Length[Last[#2]] &]]

Tue, 22 Jan 2008

Adding pairs of numbers: programming styles in Mathematica

Via Lunchtime Playground: given a list of pairs of numbers, return a list consisting of the sum of each pair.

pairs = {{58, 96}, {85, 22}, {100, 69}, {5, 37}, {32, 64}, {41, 86}, {14, 0}, {79, 22}, {55, 36}, {86, 39}, {11, 15}};

Six ways to add the numbers in Mathematica:

result = Table[Null, {Length[pairs]}];
Do[result[[k]] = pairs[[k, 1]] + pairs[[k, 2]], {k, 1, Length[pairs]}]
result
Table[pairs[[k, 1]] + pairs[[k, 2]], {k, Length[pairs]}]
Apply[Plus, pairs, {1}]
Map[Total, pairs]
pairs /. {p_, q_} -> p + q
pairs[[All, 1]] + pairs[[All, 2]]

Mon, 07 Jan 2008

A note on compression

I'm working on something that requires me to check out about 10,000 Mathematica notebooks (about 1.4GB) from cvs to home through my cable connection, so I looked into speeding up the transfer with gzip compression.

With no compression, the checkout took 37 minutes; with 'cvs -z1' (using the lightest of gzip's 9 compression settings) the time went down to 18 minutes. Higher settings gained me an extra 30 seconds or a minute or so, but at the cost of more cpu activity.

So it's '-z1' for me, even on my X-redirection-through-ssh connection to work, which is set in ~/.ssh/config:

Compression yes
CompressionLevel 1

Mon, 10 Dec 2007

Apologia pro code sua

It's rather cool to write dense convoluted Mathematica code to solve a problem, but the higher the density the harder it is to modify. Assumptions about the problem get superglued into the code; when the problem changes a bit, you have to go in and atomize the whole thing in order to change the code's basic assumptions. That's what I've been doing this morning with another guy's Mathematica code.

Or I could make the necessary changes by adding even more density and brittleness, guaranteeing that the next guy to work on the code will hunt me down and shoot me.

My coding style is distinctly uncool, like your Dad's station wagon, but all the assumptions are (usually) sitting right there out in the open in small easy pieces that can be changed in a couple of moments.

Sat, 01 Dec 2007

Using etags with Mathematica code

For years I've avoided using emacs' tags table functions, but I finally had to break down and read about them tonight to search eficiently through a huge collection of Mathematica packages and applications.

After reading the fine manual it was easy to create a tags table of Mathematica function definitions and variable assignments:

cd topdir;
find . -name "*.m" -print | grep -v BrowserCategories.m | \
etags \
  --language=none \
  --regex='/\([A-Za-z0-9$]+\)[ \t]*=/\1/' \
  --regex='/\([A-Za-z0-9$]+\).*:=/\1/' -

The second regexp choked on a particular BrowserCategories.m; since they don't contain the definitions I'm looking for I removed them from consideration.

The two regexs find these sorts of things:

fact[1] = 1;
fact[n_] := n fact[n - 1]

Wed, 03 Oct 2007

Untangling

When I fell into the "documentation testing" gig couple of years ago I began by modifying some of my boss's existing tests to extract a bit more information here and there. That file grew and sent out tentacles into other files and nowadays it's grown to a tangled web of functions across half a dozen files. Now my job is to reunite everything under one clean standalone Mathematica package. Finally a chance to sit back and think about all this stuff and get it organized. It will also be my third rethinking of the whole thing - I've found I should plan to throw away my first couple of implementations of anything as I learn what works in the long run and what doesn't. Frederick Brooks said you should plan to throw one away; it takes me two, at least.

Tue, 25 Sep 2007

How to install VMware under ubuntu

Vide: VMware runs beautifully on my new ubuntu 7.04 installation. I'm installing xubuntu under vmware so I can do some Mathematica frontend craziness. Some of the documentation tests open each of the 10000+ files in a Mathematica installation. On this new machine this happens fast enough to short-circuit the mammalian the nervous system if you happen to look at the windows flashing by as they open and close.

I couldn't find a way in gnome to isolate all an application's windows in one workspace and I couldn't get multiple X sessions working, so I'm giving vmware a shot. Very promising so far.