### Moments of Inertiaby Rachel Crawford

About     Archive     Categories     Tags     Feed     Projects     Podcast Recommendations

# Calculus in Lua

Lately I unearthed a file named ‘maths.lua’ which I’ve had sitting around on my hard drives for about two years. It contains a very small library of maths functions, most of which are to do with calculus. I remember writing these functions after learning numerical methods for integration and differentiation and while I was enthusiastically getting to grips with the Lua programming language1. It was satisfying to translate the knowledge I had picked up into code rather than just having it sit in my head waiting to be remembered badly when the exam came around, assuming there even was a question involving them, which there probably wasn’t.

Here are some cool things about functions in Lua:

• They can output multiple return values.
• They can receive a variable number of arguments.
• They can be anonymous.
• They can be defined within other functions and can access variables of their enclosing functions, which is called ‘lexical scoping’.
• Tail calls are done properly, meaning that if a function returns by calling another function, the program will not go back to the first function after the second is complete only to exit it. Hence tail calls can’t overflow the stack.
• Finally, they’re first-class values, so they can be stored in variables, passed as arguments to other functions and returned by other functions as output.

All the calculus functions I wrote take advantage of the last feature on that list, as it makes the conceptual jump from ‘mathematical functions’ to ‘programming language functions’ small, where in other languages like C++ the gap can be quite large. This is easiest to show by example. This Lua function takes another function f(x) and returns a new function which, when called, will return an approximate value for the derivative f’(x):

To show this in action, here’s a function which cubes its input value.

print(cube(2)) gives us 8. fdash = approx_derivative(cube) stores the function generated by approx_derivative in fdash. Now I’ve done that, I can call fdash(x), which should return 3x2.

3 * 2 * 2 = 12. The output isn’t quite right, because it’s an approximate, but it’s close enough.

The reason I dug this file up is because our coursework for this year’s Applied Mathematics module involves using the classical fourth-order Runge-Kutta (RK4) algorithm to numerically solve some ordinary differential equations as part of a dynamics solution. And I distinctly remembered writing a Lua implementation of the algorithm, even though I didn’t remember at all what the algorithm was or even what it did. And lo, here it is:

Having done this already gives me a bit of a head start on my coursework (hopefully), but I might have to adapt it to another language. It’s cool that it’s there, anyway. Thanks, past me!

It might seem like this blog post is ending rather abruptly, but I really need to get on with other things - I’ll tidy the maths.lua file up and post it on Gist or something soonish as it could be a useful resource for others looking to learn Lua.

Note to self: posts involving maths will be much nicer once I’ve set up MathJax. I’d better get on with that.

1. I’m still pretty keen on it, I just don’t have much use for it at the moment.