• Subject: Re: A pure-lua complex math library with some new design choices
• From: Greg Johnson <gregfjohnson@...>
• Date: Sat, 17 Dec 2016 21:44:48 +0000 (UTC)

After several helpful offline interactions with Luiz, here is the description from the rockspec.

-----------------------------------
cmath.lua is a pure-lua extension of the lua standard math library
that includes complex arithmetic.  The goal of this complex arithmetic
library is complete and seamless integration into lua.  cmath includes
with extensions of all math library functions.  Complex operations
return real values where possible, enabling equality comparisons with
standard numbers.  As with lua strings, different expressions that
evaluate to the same complex number refer to the same internal stored
object.  This makes it possible to use complex values as table indices.

cmath.sqrt(-1) == i          ==>  true
e^(i*pi) == -1               ==>  true
t = {}
t[3 + 7*i] = 12
t[1 + 2 + 3*i + 4*i] == 12   ==>  true

cmath is not "industrial-grade" from the perspectives of high
performance or high numerical accuracy.  It is intended to be fun,
easy to install and use, and good for playing around with complex
numbers on small projects.

If you need optimized performance or high-quality numerical results,
you might want to look at lcomplex:
http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua.
-----------------------------------

You can install cmath using "luarocks install cmath" or "git://github.com/gregfjohnson/cmath".

This was a really interesting project, and I learned a lot about lua in the process of developing it.  Getting different expressions that evaluate to the same complex number to refer to the same internal stored object was tricky.  It was the first time I've had to look carefully at garbage collection, weak tables, etc.  I believe I finally got that right, but if anyone sees any problems or issues in that area, please let me know.

I hope that someone out there finds this module useful and fun to play with!

Any thoughts, comments, suggestions, bug reports, etc. would be welcome.

Greg Johnson