new Complex( a [, b ] )

Class representing a complex number.

Description

Complex numbers behave like vectors of length two, in that they can be added and scaled componentwise. However, complex numbers can also be multiplied together and divided to form new complex numbers.

Parameters
Name Type Attributes Default Description
a number

The real part. If a is a Complex number, then this method clones a.

b number <optional>
0

The imaginary part.

Examples

			new Complex(1, 2).toString(1); // "1.0 + 2.0 i"
Details

Members


<static> i :Complex

A copy of i = new Complex(0, 1), a square root of -1.

Description

This returns a new object each time it is accessed.

Details

Re :number

The real part (first entry) of the complex number.

Examples

			let z = new Complex(3, 4);
			z.Re;           // 3
			z.Re = 5;
			z.toString(1);  // "5.0 + 4.0 i"
Details
number

Im :number

The imaginary part part (second entry) of the complex number.

Examples

			let z = new Complex(3, 4);
			z.Im;           // 4
			z.Im = 5;
			z.toString(1);  // "3.0 + 5.0 i"
Details
number

mod :number

The modulus of the complex number.

Description

This is an alias for the inherited property this.size.

Examples

			new Complex(3, 4).mod;  // 5
Details
number

arg :number

The argument of the complex number.

Description

This is the angle component of the polar coordinates for the point (this.Re, this.Im).

The value is between -π and π.

Examples

			new Complex(1, 1).arg;  // Math.PI/4
Details
number

sizesq :number

The square of the geometric length of the vector.

Description

This is the sum of the squares of the entries, which is the dot product of this with itself.

Examples

			Vector.create(3, 4).sizesq;   // 25
Details
number

size :number

The geometric length of the vector.

Description

This is the square root of this.sizesq.

Examples

			Vector.create(3, 4).size;   // 5
Details
number

Methods


<static> fromPolar( r, θ ) → {Complex}

Create a new Complex number with prescribed polar coordinates.

Description

Cartesian and polar coordinates are related by the formula (x, y) = (r cos(θ), r sin(θ)). According to Euler's formula, the output of Complex.fromPolar(r, θ) is equal to r e^{i θ}.

Parameters
Name Type Description
r number

The radial coordinate.

θ number

The angular coordinate.

Returns

The complex number r e^{i θ}.

Examples

			Complex.fromPolar(1, Math.PI/2);  // Complex.i
Details

equals( other [, ε ] ) → {boolean}

Test if this complex number is equal to other.

Description

This is the same as Vector#equals, except that if other is a number, it is promoted to a Complex number first.

Parameters
Name Type Attributes Default Description
other Complex | number

The number to compare.

ε number <optional>
0

Entries will test as equal if they are within ε of each other. This is provided in order to account for rounding errors.

Returns

True if this equals other.

Examples

			new Complex(1, 0).equals(1);  // true
Details

toString( [ precision ] ) → {string}

Return a string representation of the complex number.

Parameters
Name Type Attributes Default Description
precision integer <optional>
4

The number of decimal places to include.

Returns

A string representation of the complex number.

Examples

			new Complex(1, 2).toString(2);  // "1.00 + 2.00 i"
Details

conj() → {Complex}

Replace the Complex number with its complex conjugate.

Description

This modifies this in place by negating the imaginary part.

Returns

this

Examples

			let z = new Complex(1, 2);
			z.conj();
			z.toString(1);  // "1.0 - 2.0 i"
Details

add( other [, factor ] ) → {Complex}

Add another complex number in-place.

Description

This is the same as Vector#add, except that if other is a number, it is promoted to a Complex number first.

Parameters
Name Type Attributes Default Description
other Complex | number

The number to add.

factor number <optional>
1

Add factor times other instead of just adding other.

Returns

this

Examples

			let z = new Complex(1, 2);
			z.add(1);
			z.toString(1);  // "2.0 + 2.0 i"
Details

mult( other ) → {Complex}

Multiply by a complex number in-place.

Description

Multiplication of complex numbers is defined by the formula (a + b i) (c + d i) = (ac - bd) + (ad + bc) i.

Parameters
Name Type Description
other Complex | number

The number to multiply.

Returns

this

Examples

			let z = new Complex(1, 2), w = new Complex(3, 4);
			z.mult(w);
			z.toString(1);  // "-5.0 + 10.0 i"
			z.mult(-2);
			z.toString(1);  // "10.0 - 20.0 i"
Details

pow( x ) → {Complex}

Raise to the power x.

Description

This returns a new Complex number whose modulus is this.mod raised to the power x and whose argument is x*this.arg.

Parameters
Name Type Description
x number

The exponent.

Returns

A new Complex number equal to the this raised to the power x.

Examples

			new Complex( 1, 2).pow(2  ).toString(1);  // "-3.0 + 4.0 i"
			new Complex(-3, 4).pow(1/2).toString(1);  // "1.0 + 2.0 i"
Details

clone() → {Vector}

Create a new Vector with the same entries.

Returns

The new vector.

Details

recip() → {Complex}

Replace the Complex number by its reciprocal.

Description

The reciprocal of a nonzero complex number a + b i is (a - b i)/(a^2 + b^2).

Returns

this

Examples

			let z = new Complex(3, 4);
			z.recip();
			z.toString();  // "0.1200 - 0.1600 i"
Throws

Will throw an error if this is zero.

Details

div( other ) → {Complex}

Divide by a complex number in-place.

Description

This is the same as this.mult(other.recip()), except other is not modified.

Parameters
Name Type Description
other Complex | number

The number to divide.

Returns

this

Examples

			let z = new Complex(1, 2), w = new Complex(3, 4);
			z.div(w);
			z.toString();  // "0.440 + 0.0800 i"
Throws

Will throw an error if other is zero.

Details

toLaTeX( [ precision [, opts ] ] ) → {string}

Return a LaTeX representation of the vector.

Parameters
Name Type Attributes Default Description
precision integer <optional>
4

The number of decimal places to include.

opts Object <optional>
{}

Options.

Name Type Attributes Default Description
env string <optional>
"bmatrix"

The matrix environment to use.

row boolean <optional>
false

Print as a row vector instead of a column vector.

Returns

A string representation of the vector.

Examples

			Vector.create(1, 2, 3).toLaTeX(2);
			   // "\begin{bmatrix} 1.00 \\ 2.00 \\ 3.00 \end{bmatrix}"
Details

isZero( [ ε ] ) → {boolean}

Decide if a vector is zero.

Description

This is functionally equivalent to this.equals(Vector.zero(this.length), ε).

Parameters
Name Type Attributes Default Description
ε number <optional>
0

Entries smaller than this in absolute value will be considered zero.

Returns

True if the vector has all zero entries.

Details

normalize() → {Vector}

Scale the vector by the reciprocal of its length.

Description

This modifies the vector in-place to have size 1.

Returns

this

Examples

			let v = Vector.create(3, 4);
			v.normalize();
			v.toString(2); // "[0.60 0.80]"
Throws

Will throw an error if this is the zero vector.

Details

set( ...entries ) → {Vector}

Set multiple components of a Vector.

Description

This modifies the vector by setting the components to the passed values.

Parameters
Name Type Attributes Description
entries number <repeatable>

The new entries of the vector.

Returns

this

Examples

			let v = Vector.zero(2);
			v.set(3, 4);
			v.toString(1);  // "[3.0, 4.0]"
Throws

Will throw an error if the number of passed entries is incorrect.

Details

sub( other [, start ] ) → {Vector}

Subtract a Vector in-place.

Description

This modifies the vector in-place by subtracting the entries of other.

Parameters
Name Type Attributes Default Description
other Vector

The vector to subtract.

start integer <optional>
0

Only subtract the entries start...this.length. Provided for optimizations when the entries of other before start are known to be zero.

Returns

this

Examples

			let v = Vector.create(1, 2), w = Vector.create(3, 4);
			v.sub(w);
			v.toString(1);  // "[-2.0 -2.0]"
Throws

Will throw an error if the vectors have different lengths.

Details

scale( c [, start ] ) → {Vector}

Multiply a Vector by a scalar in-place.

Description

This modifies the vector in-place by multiplying all entries by c.

Parameters
Name Type Attributes Default Description
c number

The scaling factor.

start integer <optional>
0

Only scale the entries start...this.length. Provided for optimizations when the entries before start are known to be zero.

Returns

this

Examples

			let v = Vector.create(1, 2);
			v.scale(2);
			v.toString(1);  // "[2.0 4.0]"
Details

dot( other ) → {number}

Compute the dot product with another vector.

Description

This is the sum of the pairwise products of the entries of this and other.

Parameters
Name Type Description
other Vector

The vector to dot.

Returns

The dot product.

Examples

			let v = Vector.create(1, 2), w = Vector.create(3, 4);
			v.dot(w);  // 1*3 + 2*4
Throws

Will throw an error if the vectors have different lengths.

Details