Skip to content

Complex

Overview

A complex number with mutable and immutable methods.

@author Jonathan Olson <jonathan.olson@colorado.edu> @author Chris Malley (PixelZoom, Inc.) @author Matt Pennington (PhET Interactive Simulations) @author Sam Reid (PhET Interactive Simulations)

Class Complex

import { Complex } from 'scenerystack/dot';

Constructor

new Complex( real : number, imaginary : number )

Instance Methods

copy( complex? : Complex ) : Complex

Creates a copy of this complex, or if a complex is passed in, set that complex's values to ours.

This is the immutable form of the function set(), if a complex is provided. This will return a new complex, and will not modify this complex.

@param [complex] - If not provided, creates a new Complex with filled in values. Otherwise, fills in the values of the provided complex so that it equals this complex.

phase() : number

The phase / argument of the complex number.

getMagnitude() : number

The magnitude (Euclidean/L2 Norm) of this complex number, i.e. \(\sqrt{a^2+b^2}\).

getMagnitudeSquared() : number

The squared magnitude (square of the Euclidean/L2 Norm) of this complex, i.e. \(a^2+b^2\).

getArgument() : number

Returns the argument of this complex number (immutable)

equals( other : Complex ) : boolean

Exact equality comparison between this Complex and another Complex.

@returns Whether the two complex numbers have equal components

equalsEpsilon( other : Complex, epsilon ) : boolean

Approximate equality comparison between this Complex and another Complex.

@returns - Whether difference between the two complex numbers has no component with an absolute value greater than epsilon.

plus( c : Complex ) : Complex

Addition of this Complex and another Complex, returning a copy.

This is the immutable form of the function add(). This will return a new Complex, and will not modify this Complex.

minus( c : Complex ) : Complex

Subtraction of this Complex by another Complex c, returning a copy.

This is the immutable form of the function subtract(). This will return a new Complex, and will not modify this Complex.

times( c : Complex ) : Complex

Complex multiplication. Immutable version of multiply

dividedBy( c : Complex ) : Complex

Complex division. Immutable version of divide

negated() : Complex

Complex negation Immutable version of negate

sqrtOf() : Complex

Square root. Immutable form of sqrt.

powerByReal( realPower : number ) : Complex

Returns the power of this complex number by a real number.

sinOf() : Complex

Sine. Immutable form of sin.

cosOf() : Complex

Cosine. Immutable form of cos.

squared() : Complex

Returns the square of this complex number and does not modify it. This is the immutable version of square.

conjugated() : Complex

Complex conjugate. Immutable form of conjugate

exponentiated() : Complex

Takes e to the power of this complex number. \(e^{a+bi}=e^a\cos b + i\sin b\). This is the immutable form of exponentiate.

setRealImaginary( real : number, imaginary : number ) : Complex

Sets all components of this complex, returning this

setReal( real : number ) : Complex

Sets the real component of this complex, returning this

setImaginary( imaginary : number ) : Complex

Sets the imaginary component of this complex, returning this

set( c : Complex ) : Complex

Sets the components of this complex to be a copy of the parameter

This is the mutable form of the function copy(). This will mutate (change) this complex, in addition to returning this complex itself.

setPolar( magnitude : number, phase : number ) : Complex

Sets this Complex's value to be the a,b values matching the given magnitude and phase (in radians), changing this Complex, and returning itself.

@param magnitude @param phase - In radians

add( c : Complex ) : Complex

Addition of this Complex and another Complex, returning a copy.

This is the mutable form of the function plus(). This will modify and return this.

subtract( c : Complex ) : Complex

Subtraction of another Complex from this Complex, returning a copy.

This is the mutable form of the function minus(). This will modify and return this.

multiply( c : Complex ) : Complex

Mutable Complex multiplication.

divide( c : Complex ) : Complex

Mutable Complex division. The immutable form is dividedBy.

negate() : Complex

Mutable Complex negation

exponentiate() : Complex

Sets this Complex to e to the power of this complex number. \(e^{a+bi}=e^a\cos b + i\sin b\). This is the mutable version of exponentiated

square() : Complex

Squares this complex number. This is the mutable version of squared.

sqrt() : Complex

Square root. Mutable form of sqrtOf.

sin() : Complex

Sine. Mutable form of sinOf.

cos() : Complex

Cosine. Mutable form of cosOf.

conjugate() : Complex

Complex conjugate. Mutable form of conjugated

getCubeRoots() : Complex[]

Returns the cube roots of this complex number.

toString() : string

Debugging string for the complex number (provides real and imaginary parts).

Instance Properties

real : number

The real part. For a complex number \(a+bi\), this is \(a\).

imaginary : number

The imaginary part. For a complex number \(a+bi\), this is \(b\).

Static Methods

real( real : number ) : Complex

Constructs a complex number from just the real part (assuming the imaginary part is 0).

imaginary( imaginary : number ) : Complex

Constructs a complex number from just the imaginary part (assuming the real part is 0).

createPolar( magnitude : number, phase : number ) : Complex

Constructs a complex number from the polar form. For a magnitude \(r\) and phase \(\varphi\), this will be \(\cos\varphi+i r\sin\varphi\).

solveLinearRoots( a : Complex, b : Complex ) : Complex[] | null

Returns an array of the roots of the quadratic equation \(ax + b=0\), or null if every value is a solution.

@returns The roots of the equation, or null if all values are roots.

solveQuadraticRoots( a : Complex, b : Complex, c : Complex ) : Complex[] | null

Returns an array of the roots of the quadratic equation \(ax^2 + bx + c=0\), or null if every value is a solution.

@returns The roots of the equation, or null if all values are roots (if multiplicity>1, returns multiple copies)

solveCubicRoots( a : Complex, b : Complex, c : Complex, d : Complex ) : Complex[] | null

Returns an array of the roots of the cubic equation \(ax^3 + bx^2 + cx + d=0\), or null if every value is a solution.

@returns The roots of the equation, or null if all values are roots (if multiplicity>1, returns multiple copies)

Static Properties

ZERO : Complex

(readonly)

Immutable constant \(0\). @constant

ONE : Complex

(readonly)

Immutable constant \(1\). @constant

I : Complex

(readonly)

Immutable constant \(i\), the imaginary unit. @constant

Source Code

See the source for Complex.ts in the dot repository.