Complex Math

This library implements complex number math for JavaScript.


⇓ Download Zip (5 KB)  (Open Source - MIT License)

Project Files:

Overview

Complex numbers are pairs of real numbers. (More information about complex numbers.) This library introduces the "Complex" object, which you create and use to hold a number. The constant i is also defined for you (along with I for compatibility with C code). To calculate math, you must use member functions, since JavaScript does not support operator overloading.

For example:

var z = new Complex(3, 2); // Creates the number 3 + 2*i
var w = Exp(i.mul(Math.PI/3)); // Creates the number e^(i*pi/3)

var g = z.mul(w).div(i).sub(1); // Calculates z*w / i - 1

alert(g.toString());

// Result: 2.598076211353316 + i*0.23205080756887675

Complex Member Functions

z.clone();

Returns a full copy of the Complex object.

z.neg();

Returns -z

z.inverse();

Returns 1 / z

z.inverse();

Returns 1 / z

z.add(w);

Returns z + w

z.sub(w);

Returns z - w

z.mul(w);

Returns z * w

z.div(w);

Returns z / w

z.pow(w);

Returns z ^ w

z.abs();

Returns |z|

z.toString();

Returns a string represention of the complex number z.

Functions


// _
// Complex Conjugate: z = x - iy
function Conj(z);

// Absolute Value: Abs(z) = sqrt(z.x*z.x + z.y*z.y)
function Abs(z);

// Argument: Arg(z) = theta, the polar angle wrapped to the range (-pi, pi]
function Arg(z);

// Real Part: Re(z) = x
function Re(z);

// Imaginary Part: Im(z) = y
function Im(z);

// Exponential: Exp(z) = e^z
function Exp(z);

// Power: Pow(z, w) = z^w
function Pow(a, b);

// Logarithm: Log(z, w) = ln(Abs(z)) + i*Arg(z)
function Log(z);

// Sine: Sin(z) = (e^(i*z) - e^(-i*z)) / (2*i)
function Sin(z);

// Cosine: Cos(z) = (e^(i*z) + e^(-i*z)) / 2
function Cos(z);

// Gamma Function (A complex extension of the factorial.)
function Gamma(z);