Python

A Simple Approximation for the Airy Function

Besides the Normal Distribution Function, I occasionally need the Airy Function Ai(x): it arises in perturbation theory and some other contexts. This function is most definitely not part of most standard numerics libraries! While high-quality implementations of it are part of most “serious” numerics libraries (such as SciPy or GSL), these libraries are not always available or convenient.

Here is a really simple numerical approximation: it is numerically “good enough” for casual work, and simple enough to be implemented on the fly as needed.

Exploring SymPy or: What, Really, is the Purpose of Computer Algebra Systems?

I recently got interested in singular perturbation theory , and to get help with the algebra, I turned to SymPy. I had tried to use Mathematica in graduate school in the early 90s, but the experience had been sufficiently frustrating that I had steered clear of computer algebra systems since.

SymPy presents itself as a “friendly”, less intimidating alternative, with a more familiar and conventional language and operating model.

Case Study: Singular Perturbation Theory with SymPy

Formal perturbation theory, and in particular singular perturbation theory, are standard topics in applied mathematics, but seem to be largely unknown outside that specific community. Which is a shame, because they are both useful, and intellectually fascinating.

Perturbation expansions, in particular for higher orders, have a reputation for being “cumbersome”, as the say: the algebra quickly becomes both tedious and error-prone. Which is true, but it turns out that the SymPy computer-algebra system can be put to good use in this context.

Computing the Normal Distribution Function

Every once in a while, I need to evaluate the normal distribution function $\Phi(x)$:

$$ \Phi(x) = \frac{1}{\sqrt{2 \pi}} \int_{-\infty}^x \! e^{-\frac{1}{2}t^2} \, dt $$

Unfortunately, it is not always available in the standard math libraries, and hence I have to implement a “good-enough” version myself. Here are some options.

Random Shuffles

Shuffling a collection of items is a surprisingly frequent task in programming: essentially, it comes up whenever a known input must be processed in random order. What is more, there is a delightful, three-line algorithm to accomplish this task correctly, in-place, and in optimal time. Unfortunately, this simple three-line solution seems to be insufficiently known, leading to various, less-than-optimal ad-hoc alternatives being used in practice — but that is entirely unnecessary!