#
**Complex Numbers**

**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:**

numbercmplxMath.abs (numberx)

Returns the length between (0, 0) to x

numbercmplxMath.acos (numberx)

Returns the arc cosine of x.

numbercmplxMath.arg (numberx)

Returns the angle between Re axis and x

numbercmplxMath.atan (numberx)

Returns the arc tangent of x (in radians).

numbercmplxMath.atan2 (numbery,numberx)

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

numbercmplxMath.asin (numberx)

Returns the arc sine of x.

intcmplxMath.ceil (numberx)

Returns the smallest integer larger than or equal to x.

numbercmplxMath.clamp (numberx,numbermin,numbermax)

Fixes x between min and max.

boolcmplxMath.cmplx (Variantx)

Verifies if x is a valid`CmplxNumber`

datatype.

CmplxNumbercmplxMath.cmplx (numberx,numbery)

Converts`x+yi`

to a`CmplxNumber`

datatype*.

numbercmplxMath.conj (numberx)

Returns the Re part with opposite Im part.

numbercmplxMath.cos (numberx)

Returns the cosine of x (in radians).

numbercmplxMath.cosh (numberx)

Returns the hyperbolic cosine of x.

numbercmplxMath.deg (numberx)

Converts x to degrees (x in radians).

numbercmplxMath.exp (numberx)

Returns`e ^ x`

.

intcmplxMath.floor (numberx)

Returns the largest integer smaller than or equal to x.

numbercmplxMath.fmod (numberx,numbery)

Returns the remainder of`x / y`

(truncated division)* **.

number,intcmplxMath.frexp (numberx)

Returns m and e such that`x = m*2^e`

* **.

numbercmplxMath.gamma (numberx)

Extension of the factorial function.

numbercmplxMath.im (numberx)

Returns the Im part of x**.

numbercmplxMath.ldexp (numberx,numbere)

Returns`x*2^e`

(e should be an integer)* **.

numbercmplxMath.log (numberx,numberbase = 2.7182818)

Returns the logarithm of x using the given base.

numbercmplxMath.log10 (numberx)

Returns the base-10 logarithm of x.

numbercmplxMath.max (numberx,number…)

Returns the maximum value among the numbers passed to the function***.

numbercmplxMath.min (numberx,number…)

Returns the minimum value among the numbers passed to the function***.

int,numbercmplxMath.modf (numberx)

Returns the integral part of x and the fractional part of x.

numbercmplxMath.noise (numberx,numbery = 0,numberz = 0)

Returns a perlin noise value* **.

number,numbercmplxMath.ord (numberx)

Returns the Re part and Im part of x**.

numbercmplxMath.polar (numberx,numbery)

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

numbercmplxMath.pow (numberx,numbery)

Returns`x ^ y`

.

numbercmplxMath.rad (numberx)

Converts x to radians (x in degrees).

numbercmplxMath.random (numberm = 0.0,numbern = 1.0)

Gives a random number between m and n.

voidcmplxMath.randomseed (numberx)

Sets the seed of the pseudo-random generator*.

numbercmplxMath.re (numberx)

Returns the Re part of x**.

intcmplxMath.round (numberx)

Returns the integer with the smallest difference between it and the given number.

numbercmplxMath.sign (numberx)

Extracts the sign of x.

numbercmplxMath.sin (numberx)

Returns the sine of x (in radians).

numbercmplxMath.sinh (numberx)

Returns the hyperbolic sine of x.

numbercmplxMath.sqrt (numberx)

Returns the square root of x.

numbercmplxMath.tan (numberx)

Returns the tangent of x (in radians).

numbercmplxMath.tanh (numberx)

Returns the hyperbolic tangent of x.

**Constants:**

numbercmplxMath.huge

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

CmplxNumbercmplxMath.imag

Gives sqrt(-1), instead of NaN.

numbercmplxMath.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

**(v1.0.2)**