1. 2014
Nov
20

## Another Mathematica bug

Math is hard.

Not for Barbie, but for Mathematica.

I ran into a weird Mathematica bug while trying to evaluate the sum

$$\sum_{k=1}^{\infty} \biggl[-\frac{\pi^2}{6} + \psi'(k + 1) + H_k^2\biggr]\frac{z^k}{k!}$$

Split this into three parts. The first one is the well-known expansion of the exponential function

$$-\frac{\pi^2}{6}\sum_{k=1}^{\infty} \frac{z^k}{k!} = -\frac{\pi^2}{6}(e^z - 1)$$

The second is not the well-known expansion of the exponential function.

$$\sum_{k=1}^{\infty} \psi'(k + 1)\frac{z^k}{k!} \neq \frac{\pi^2}{6}(e^z - 1)$$

Obviously not, in fact, since if two power series are equal, $$\sum_i a_n z^n = \sum_i b_n z^n$$, for an infinite number of points, each of their coefficients have to be equal: $$\forall n,\ a_n = b_n$$. (You can show this by taking the difference of the two sides and plugging in a bunch of different values of $$z$$.)

I guess Mathematica doesn’t know that.

In[1] = Sum[PolyGamma[1, k + 1] z^k/k!, {k, 1, Infinity}]
Out[1] = 1/6(-1 + E^z)Pi^2


I had my hopes up for …

2. 2012
Nov
13

## A reminder to always check your definitions

I’ve been working on some code using GSL and wondering why it didn’t match the results I was getting from Mathematica and other programs. Well, one (of perhaps many) differences is that GSL defines the sinc function as

$$\operatorname{sinc}(x) = \frac{\sin \pi x}{\pi x}$$

for $$x\neq 0$$, but Mathematica defines it as

$$\operatorname{sinc}(x) = \frac{\sin x}{x}$$

The latter is the definition I was using in my math, and I didn’t realize it didn’t match the one in GSL until I broke the code down to individual mathematical terms.

So remember to make sure that any code you use is actually doing what you think it’s doing!

3. 2011
Oct
31

## Running Mathematica notebooks in batch mode

If you’re like me, you’re used to thinking of Mathematica as an interactive calculator, where you type in an expression and Mathematica evaluates it and spits out the result. But the Mathematica system actually incorporates a whole programming language, and as you might expect of any programming language, it’s possible to write Mathematica programs (scripts) that run non-interactively.

In order to write a Mathematica script, you create a text file which contains the expressions you want to evaluate (in normal programming terminology, the statements to execute). The conventional file extension for Mathematica scripts is .m. Each expression that you would normally type into the Mathematica window, you type into the script file, one per line. (Line breaks are allowed within expressions under certain circumstances.) You can then run the script using

math -script filename.m


(on Linux). The evaluated expressions, which Mathematica would normally show you right below where you typed in the input, are printed to standard output, so I recommend ending lines with a semicolon (which suppresses the output) by default. Only leave the semicolon off for the results you actually care about.

Keep in mind that a Mathematica script file is not what you …