Complex numbers are represented by a dictionary of real and imaginary attributes:

complex( x, y ) — complex number returned as { re: x, im: y }

complex( x ) — complex number returned as { re: x, im: 0 }

C( x, y ) — complex number returned as { re: x, im: y }

C( x ) — complex number returned as { re: x, im: 0 }


The separate attributes can be accessed through their respective names,

complex( x, y ).re = x
complex( x, y ).im = y

or with convenience functions:

re( x ) — real part of a real or complex number

real( x ) — real part of a real or complex number

im( x ) — imaginary part of a real or complex number

imag( x ) — imaginary part of a real or complex number


Since JavaScript does not support operator overloading, functions are available for complex arithmetic:

add( x, y ) — add two real or complex numbers

add( x, y, z, … ) — add an arbitrary number of real or complex numbers

sub( x, y ) — subtract two real or complex numbers

mul( x, y ) — multiply two real or complex numbers

mul( x, y, z, … ) — multiply an arbitrary number of real or complex numbers

div( x, y ) — divide two real or complex numbers

neg( x ) — negate a real or complex number

inv( x ) — invert a real or complex number