"complex.lua" is a simple library for complex numbers written in pure lua.
If you want to use it, just download the file "complex.lua" (here, version 2011-01-05) and put it somewhere in your LUA_PATH. You can byte compile it using luac in order to obtain a "complex.luac" file. Then start lua using lua -l complex or add a require "complex" in your lua file.
A SMALL TUTORIAL
If you want to create the complex number 3+4i, type
If you are lazy, you can also type
As you can notice, "complex.lua" add an new entry in the math table. This entry is the complex number i itself and since "complex.lua" use the lua arithmetic metamethods, everything build from math.i will be a lua complex number (there is no need of a complex.new function). Just to check, type
Let us create a second complex number
Let us try some basic arithmetic
If you want to compute the complex conjugate of x1, you can type
But I do prefer the more compact OO notation
If you want to make a copy of the complex numbers x1 and x2, use
Now you have
If you want to compare two complex numbers, the lua relational metamethod allows you to use ==. For instance, if you want to compare x1 and x3
and what about x1 and x4?
If you want to access the real and the imaginary part of x1, just use
These functions can also be used to modify a complex number. For example, if I want to modify the real part of x3
If I want to modify the imaginary part of x4 and add 1
If you want to print complex numbers, you can simply type
or, if you want to have more control on what you print, you can do something like
> =string.format("%s complex = %3.1f%+3.1fi\n%s complex = %4.2f%+4.2fi\n","1st",x1,"2nd",x2)
1st complex = 3.0+4.0i
2nd complex = 2.00-3.00i
As you can see, the string.format function has been modified in order to take complex numbers into account. For each complex number in the list, you have to define two consecutive numerical formats (one for the real part, one for the imaginary part).
If you want to compute the argument of x1, just type
If you want to compute the modulus (or absolute value) of x1, just type
As you can see, the original math.abs function has been modified in order to handle complex numbers. If the argument is a real number, then you get the usual absolute value and if the argument is a complex number, you get the complex absolute value, i.e. the modulus.
All the following math functions work with complex numbers:
exp, log, sqrt
cos, sin, tan
acos, asin, atan
cosh, sinh, tanh
acosh, asinh, atanh
You can use them using the "complex." form, the ":" form or the "math." form. For instance, the sin of x2 can be computed by any of these forms
Using the "math." form allows to use the same function for both real and complex numbers. For instance, you can type
and it works as usual, but you can also use it for a complex number
In the case where the argument is a real number but the function is not defined for this real value, the "math." form yields a complex result. For example
while, without "complex.lua", you get
Finally, if you want to compute the n-th roots of a complex number, where n >= 1 is an integer, use the complex.roots function. For instance, if you want to compute the 4-th roots of x1, type
> for i=1,4 do print(i,r[i]) end
As tou can see, r is a table containing 4 complex numbers 4-th roots of x1. Let us check if it work
> for i=1,4 do print(r[i]^4) end