source

# Constructor

``````var algebra = require('algebra');

var Complex      = algebra.Complex.Element;
var ComplexField = algebra.Complex.Field;

var C = new ComplexField();
``````

Accepts 2 arguments.

``````  var z = new Complex(1, 2);
``````

Accepts 1 argument, second one defaults to 0.

``````  var z = new Complex(1);

console.log(z.im()); // 0
``````

Defaults to (0, 0) if no argument is provided.

``````  var z = new Complex();

console.log(z.re()); // 0
console.log(z.im()); // 0
``````

# Methods

## clone()

Returns a copy of the object.

``````  var z = new Complex(-15, 2);
var w = z.clone();

console.log(w instanceof Complex); // true

console.log(z.eq(w)); // true

console.log(z !== w); // true
``````

## re()

Returns the real part.

``````  var z = new Complex(1, 2);
console.log(z.re()); // 1
``````

## im()

Returns the imaginary part.

``````  var z = new Complex(1, 2);
console.log(z.im()); // 2
``````

## xy()

Returns the real and imaginary part.

``````  var z = new Complex(1, 2);
console.log(z.xy()); // [1, 2]
``````

## isZero()

Returns true if element is zero.

``````  var z = new Complex(0, 0);
console.log(z.isZero()); // true
``````

## isOne()

Returns true if element is one.

``````  var z = new Complex(1, 0);
console.log(z.isOne()); // true
``````

## norm()

Returns the euclidean norm.

``````  var z1 = new Complex(1, 2);
console.log(z1.norm(), 5);

var z2 = new Complex(5, -2);
console.log(z2.norm(), 29);

// The norm of the product is the product of the norm.
console.log(z1.mul(z2).norm()); // 5 * 29
``````

## conj()

Implements complex conjugation.

``````var z = new Complex(1, 2);

z.conj();

console.log(z.im()); // -2
``````

Can be chained.

## eq(number|Complex)

Returns true if two elements are log.

``````  var z = new Complex(-1, 5);
var w = new Complex(-1, 5);

console.log(z.eq(w)); // true
console.log(w.eq(z)); // true
``````

Corces number type.

``````  var z = new Complex(-1);
console.log(z.eq(-1)); // true
``````

Has reflection property.

``````  var z = new Complex(-1, 2);
console.log(z.eq(z)); // true
``````

## neg()

``````  var z = new Complex(4, 1);
var w = z.clone().neg();
``````

Can be chained.

``````  var z = new Complex(1, 4);
var w = new Complex(0, 1);

console.log(z.re()); // 1
console.log(z.im()); // 5
``````

Coerces number type.

``````  var z = new Complex(2, 1);

console.log(z.re()); // 5
console.log(z.im()); // 1
``````

Can be chained.

## sub(number|Complex)

Implements the subtraction operator.

``````  var one = new Complex(1);
var i = new Complex(0, 1);

var z = one.sub(i);

console.log(z.re()); // 1
console.log(z.im()); // -1
``````

Coerces number type.

``````  var z = new Complex(20);
z.sub(3);
console.log(z.re()); // 17
``````

Can be chained.

## inv()

Inverts the element.

``````  var z1 = new Complex(0, 1);
z1.inv();
console.log(z1.re()); // 0
console.log(z1.im()); // -1

var z2 = new Complex(2, 0);
z2.inv();
console.log(z2.re()); // 0.5
console.log(z2.im()); // 0
``````

Can be chained.

## mul(number|Complex)

Implements the multiplication operator.

``````  var two = new Complex(2);
var z = new Complex(1, 1);
var twoZ = two.mul(z);
console.log(twoZ.re()); // 2
console.log(twoZ.im()); // 2

z.mul(z);
console.log(z.re()); // 0
console.log(z.im()); // 2
``````

Coerces number type.

``````  var z = new Complex(2, -1);
z.mul(3);
console.log(z.re()); // 6
``````

Can be chained.

## div(number|Complex)

Implements the division operator.

``````  var z1 = new Complex(5);
var w1 = new Complex(1);
z1.div(w1);
console.log(z1.re()); // 5
console.log(z1.im()); // 0

var z2 = new Complex(5);
var w2 = new Complex(2, 1);
z2.div(w2);
console.log(z2.re()); // 2
console.log(z2.im()); // -1
``````

Coerces number type.

``````  var z = new Complex(2, -1);
z.div(2);
console.log(z.re()); // 1
``````

Can be chained.

## abs()

Returns the absolute value.

``````  var z1 = new Complex(2);
console.log(z1.abs()); // 2

var z2 = new Complex(3, 4);
console.log(z2.abs()); // 5
``````

## arg()

Returns the argument.

``````  var z = new Complex(3, 2);
console.log(z.arg()); // Math.atan2(2, 3)
``````

## exp()

Implements Complex exponentiation.

``````  var z1 = new Complex(0, Math.PI);
var eZ1 = z1.exp();
console.log(eZ1.re()); // -1
// N.B. Math.sin(Math.PI) is not -1
console.log(eZ1.im()); // a number close to zero

var z2 = new Complex(1);
var eZ2 = z.exp();
console.log(eZ2.re()); // 2.7182 ...
console.log(eZ2.im()); // 0
``````

Can be chained.