Complex numbers

A complex is a scalar, independent of any lattice.

complex

complex(re, im)                                   --> complex

The constructor. Both arguments are numbers.

A complex number is a value, not a container: like a Lua number it is used whole and never modified. There is no way to set its real or imaginary part, and z.real = 1 raises. z:real() and z:imag() read the parts out; to change one, build a new number from the parts you want.

local z = complex(1, 2)
local w = complex(3, z:imag())                    -- not z:real(3)

Complex values take part in ordinary arithmetic; see Arithmetic.

complex:real

z:real()                                          --> number

complex:imag

z:imag()                                          --> number

complex:conj

z:conj()                                          --> complex

complex:abs

z:abs()                                           --> number

The modulus.

complex:arg

z:arg()                                           --> number

The argument, in radians.

complex:norm2

z:norm2()                                         --> number

The squared modulus — re*re + im*im, with no square root.

complex:sqrt

z:sqrt()                                          --> complex

The principal square root: the one whose real part is non-negative, so the result lies in the right half-plane with the positive imaginary axis included. The branch cut is the negative real axis, approached from above: for a negative real x, complex(x, 0):sqrt() is +i·√|x|, and the value just below the cut is its negative. sqrt is discontinuous across that line.

complex:exp

z:exp()                                           --> complex

e^z. Entire — no cut.

complex:log

z:log()                                           --> complex

The principal logarithm: the real part is log|z| and the imaginary part is z:arg(), which lies in (−π, π]. The branch cut is again the negative real axis, approached from above, so complex(-1, 0):log() is +iπ while a point just below the cut gives nearly −iπ. log is undefined at zero.

Both cuts are the C library's, inherited unchanged; arg is atan2(im, re) and carries the same convention.