Other articles


  1. Fast computation of integer powers

    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 few operations as possible is important for the efficient evaluation of expressions on a computer. Is there a general way to find the smallest number of multiplications needed to compute any given power \(n\)?

    read more ...

    There are comments.

  2. Displaying debug messages coming from LibraryLink

    When extending Mathematica through C or C++ code using the LibraryLink API, 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 ...

    There are comments.

  3. Memoization in Mathematica

    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 …

    read more ...

    There are comments.