Complex Numbers | work with complex numbers easily!

Complex Numbers


Info: This resource is for extending the Real Mathematics in Lua to the Complex ones. Notice that the square root of -1 does not have a clear solution, since real numbers multiplied by themselves will always return 0 or a positive number. This is when complex numbers come here; since they are not real numbers some operations will mess up (like Lua math functions), giving results such as NaN (not a number). Some people requested to add Complex Numbers support, this is why this resource was created.


Usage: To use this, just download or use “require()” to install the Complex Math
library. To start using the operations just call the functions with a dot ‘.’ :

local cmplxMath = require(8152231789) -- or the ModuleScript you have in your game
print(cmplxMath.asin(2)) -- calls 'asin' function

--Expected output: 1.5707963267949-1.3169578969248i

Take in mind that almost all ‘cmplxMath’ functions creates CmplxNumber datatype, which is a table (attached to a metatable) that behaves like a number and at the same time as a table. This datatype cannot be used with functions that allow numbers (since it is a table), you can use ‘re’, ‘im’ and ‘ord’ functions to convert this datatype to a number.


Functions:

number cmplxMath.abs (number x)
Returns the length between (0, 0) to x

number cmplxMath.acos (number x)
Returns the arc cosine of x.

number cmplxMath.arg (number x)
Returns the angle between Re axis and x

number cmplxMath.atan (number x)
Returns the arc tangent of x (in radians).

number cmplxMath.atan2 (number y, number x)
Returns the angle between the ray of the point (x, y)* **.

number cmplxMath.asin (number x)
Returns the arc sine of x.

int cmplxMath.ceil (number x)
Returns the smallest integer larger than or equal to x.

number cmplxMath.clamp (number x, number min, number max)
Fixes x between min and max.

bool cmplxMath.cmplx (Variant x)
Verifies if x is a valid CmplxNumber datatype.

CmplxNumber cmplxMath.cmplx (number x, number y)
Converts x+yi to a CmplxNumber datatype*.

number cmplxMath.conj (number x)
Returns the Re part with opposite Im part.

number cmplxMath.cos (number x)
Returns the cosine of x (in radians).

number cmplxMath.cosh (number x)
Returns the hyperbolic cosine of x.

number cmplxMath.deg (number x)
Converts x to degrees (x in radians).

number cmplxMath.exp (number x)
Returns e ^ x.

int cmplxMath.floor (number x)
Returns the largest integer smaller than or equal to x.

number cmplxMath.fmod (number x, number y)
Returns the remainder of x / y (truncated division)* **.

number, int cmplxMath.frexp (number x)
Returns m and e such that x = m*2^e * **.

number cmplxMath.gamma (number x)
Extension of the factorial function.

number cmplxMath.im (number x)
Returns the Im part of x**.

number cmplxMath.ldexp (number x, number e)
Returns x*2^e (e should be an integer)* **.

number cmplxMath.log (number x, number base = 2.7182818)
Returns the logarithm of x using the given base.

number cmplxMath.log10 (number x)
Returns the base-10 logarithm of x.

number cmplxMath.max (number x, number …)
Returns the maximum value among the numbers passed to the function***.

number cmplxMath.min (number x, number …)
Returns the minimum value among the numbers passed to the function***.

int, number cmplxMath.modf (number x)
Returns the integral part of x and the fractional part of x.

number cmplxMath.noise (number x, number y = 0, number z = 0)
Returns a perlin noise value* **.

number, number cmplxMath.ord (number x)
Returns the Re part and Im part of x**.

number cmplxMath.polar (number x, number y)
Returns the position in the complex plane (x is length, y is angle).

number cmplxMath.pow (number x, number y)
Returns x ^ y.

number cmplxMath.rad (number x)
Converts x to radians (x in degrees).

number cmplxMath.random (number m = 0.0, number n = 1.0)
Gives a random number between m and n.

void cmplxMath.randomseed (number x)
Sets the seed of the pseudo-random generator*.

number cmplxMath.re (number x)
Returns the Re part of x**.

int cmplxMath.round (number x)
Returns the integer with the smallest difference between it and the given number.

number cmplxMath.sign (number x)
Extracts the sign of x.

number cmplxMath.sin (number x)
Returns the sine of x (in radians).

number cmplxMath.sinh (number x)
Returns the hyperbolic sine of x.

number cmplxMath.sqrt (number x)
Returns the square root of x.

number cmplxMath.tan (number x)
Returns the tangent of x (in radians).

number cmplxMath.tanh (number x)
Returns the hyperbolic tangent of x.

Constants:

number cmplxMath.huge
A value larger than or equal to any other numerical value**.

CmplxNumber cmplxMath.imag
Gives sqrt(-1), instead of NaN.

number cmplxMath.pi
The value of pi**.

* This function only accepts real numbers.
** Returns number datatype.
*** Only accepts pure real or imaginary numbers.


Notes:

  • Almost all operations supports complex numbers and returns complex numbers.
Operation
Lua Built-in Library
Complex Math Library
math.sqrt(-1)
-nan(ind)
i
math.asin(2)
-nan(ind)
1.570796...-1.316957...i
math.sqrt(-1) ^ 2
-nan(ind)
-1
math.log10(-10)
-nan(ind)
1+1.3643763538418i
  • This library uses identities and formulas (such as Euler’s Identity).
Operation
Lua Built-in Library
Complex Math Library
i ^ i
-nan(ind)
0.20787957635076
e ^ (i * pi)
-nan(ind)
-1
cos(i * pi)
-nan(ind)
11.591953275522
  • Some operations have been modificated for this Math library.
Operation
Lua Built-in Library
Complex Math Library
inf - inf
-nan(ind)
0
math.sqrt(inf)
inf
-nan(ind)-nan(ind)*i
  • This library is accurate to over 1 part in 100 quintillion.
  • Gamma function might have little errors.

Feel free to use it, no credit is needed. Please comment if you have any problem :happy1:
(v1.0.2)

11 Likes

Very interesting! I have no idea if I’m going to ever use this, but hey, it seems cool, so I’ll bookmark it!

1 Like

math.sqrt(-1) ^ 2 or i ^ 2 is equal to -1

did you use the wrong operation?

2 Likes

v1.0.1 Released


Changes:

  • Fixed some bugs with ‘exp’ function.

number cmplxMath.exp (number x)
Returns e ^ x .

  • Fixed ‘pow’ function error handler that could give incorrect text.

number cmplxMath.pow (number x, number y)
Returns x ^ y .


This version will not change anymore. Please comment if you find an error or you have a problem :happy1:

This is pretty neat! Does the library allow you to add and multiply complex numbers as well? I’m also curious what happens if you pass cmplxMath.pow() a fractional exponent or an exponent less than 1.

v1.0.2 Released


Changes:

  • Fixed ‘fmod’, ‘ldexp’ and ‘polar’ functions error handlers that could give incorrect text.

number cmplxMath.fmod (number x, number y)
Returns the remainder of x / y (truncated division)* **.

number cmplxMath.ldexp (number x, number e)
Returns x*2^e (e should be an integer)* **.

number cmplxMath.polar (number x, number y)
Returns the position in the complex plane (x is length, y is angle).

  • Title now shows the correct version

  • ‘huge’ and ‘pi’ constants now show the correct information.

number cmplxMath.huge
A value larger than or equal to any other numerical value**.

number cmplxMath.pi
The value of pi**.


This version can change anytime. Please comment if you find an error or you have a problem :happy1:

2 Likes