How many multiplications are needed to calculate \(x^6\)? The naïve way to do it is \(x^6 = x\cdot x\cdot x\cdot x\cdot x\cdot x\). That is five multiplications. But we can do better: \((x\cdot x\cdot x)^2\) only requires three. Using as …

When extending Mathematica through C or C++ code using the LibraryLinkAPI, one common annoyance is that it’s not possible to see messages that the C code sends to stdout or stderr when using the graphical interface. While there are ways to send output directly to the active Mathematica notebook, often the C code is not written to be used exclusively with Mathematica and it simply prints debug messages to stderr. Standard C assertion failures also cause messages to be printed to stderr.

Here I will show how to view the Mathematica kernel’s output in a terminal window, even when using the graphical notebook interface.
read more ...

I wrote this short tutorial on memoization years ago. Since it turned out to be somewhat popular, and my old website is going away, I am reproducing it here.

Memoization is a very well known programming pattern in Mathematica. Memoization is an optimisation technique: it means making a function “remember …