Q-ComplexNumber.js

Real numbers ℝ

Our regular, ordinary, everyday numbers are called real numbers. These include integers and decimals. You can visualize real numbers as existing along an infinite number line—with zero in the middle, positive numbers counting up forever to infinity on the right, and negative numbers doing the exact opposite on the left.

When a real number is multiplied by itself the product is always positive. For example, if we choose the number 2 we see that 2 × 2 = 4. Similarly, had we chosen the negative number -2, the product would still be positive because two negative numbers multiplied together also produce a positive result; -2 × -2 = 4. For brevity we could rewrite these equations as 22 = 4 and (-2)2 = 4, respectively.

The square root of a real number has two possible answers. The square root of 4, for example, is both 2 and -2 because both are solutions for x in the equation x = √4.

Imaginary numbers 𝕀

But suppose we wanted to find the square root of a negative number. Is there any number that could solve for x in the equation x = √(-4)? Sadly, there is not. Or more precisely: there is not any real solution for the square root of a negative number.

In his 1991 address on “Creativity in Management”, Monty Python’s John Cleese articulates Edward de Bono’s concept of the intermediate impossible (32:06) as a useful stepping stone towards a novel and useful solution. Imaginary numbers might be considered an intermediate impossible. The symbol i is defined as the imaginary solution to the equation x = √(-1), therefore i2 = -1. With this imaginary device we now have a solution to the above equation x = √(-4) and that solution is 2i. (And also -2i, of course! We can indicate this “plus or minus” possibility as ±2i.) Let’s inspect this more closely.


𝒙 = √(-4)
𝒙 = √( 4 × -1)
𝒙 = √4 × √(-1)
𝒙 = ±2 × √(-1)
𝒙 = ±2 × i
𝒙 = ±2i

2i is an imaginary number that consists of a real number multiplier, 2, and our imaginary solution to √(-1), called i. Like real numbers, imaginary numbers also exist along an infinite number line. We plotted our real number line horizontally, so let’s plot our imaginary number line vertically.

Complex numbers ℂ

We just saw that multiplying a real number by i yields an imaginary number. But what if you add a real number to an imaginary one? Things get complex. A complex number is a number that can be expressed in the form a + bi, where a is the real component and bi is the imaginary component. Some examples might be 1 + 2i or 3 - 4i.

4 5 π 3 e 2 √2 1 0 -1 -2 -3 -4 -5 5i 4i 2i 3i 1i 0i -1i -2i -3i -4i -5i 1+2i 3-4i

This is what the ComplexNumber class was created to handle. Open up your JavaScript console and paste in the following:


var 
cat = new Q.ComplexNumber( 1,  2 ),
dog = new Q.ComplexNumber( 3, -4 )

cat.toText()//  Returns '1 + 2i'
dog.toText()//  Returns '3 - 4i'

Now we have two variables, cat and dog, that we can operate with. As you might guess, ComplexNumber includes instance methods for common operations like addition, subtraction, multiplication, and division. Try the following lines individually in your JavaScript console:


cat.add( dog ).toText()     //  Returns  '4 - 2i'
cat.subtract( dog ).toText()//  Returns '-2 + 6i'
cat.multiply( dog ).toText()//  Returns '11 + 2i'
cat.divide( dog ).toText()  //  Returns '-0.2 + 0.4i'

We can now verify that i2 = -1.


var i = new Q.ComplexNumber( 0, 1 )

i.toText()//  Returns  'i'
i.power( 2 ).toText()//  Returns  '-1'

Operation functions on Q.ComplexNumber instances generally accept as arguments both sibling instances and pure Number instances, though the value returned is always an instance of Q.ComplexNumber.


Constructor

ComplexNumber Function([ real: Number or Q.ComplexNumber ][, imaginary: Number ]]) => Q.ComplexNumber
Expects zero to two arguments. If the first argument is a ComplexNumber then that value is cloned and any remaining arguments are ignored. If either of the arguments are undefined they are assumed to be zero. If the first argument is not a ComplexNumber and either of the arguments are not number-like then an error is thrown.


var 
ape = new Q.ComplexNumber(),
bee = new Q.ComplexNumber( 1 ),
elk = new Q.ComplexNumber( 1, 2 ),
fox = new Q.ComplexNumber( elk )

ape.toText()//  Returns '0'
bee.toText()//  Returns '1'
elk.toText()//  Returns '1 + 2i'
fox.toText()//  Returns '1 + 2i'

elk.isEqualTo( fox )   //  Returns true
elk.index === fox.index//  Returns false

Static properties

Constants and constant creation

Inspection

Maths


Prototype properties

Inspections (non-destructive)

Maths (non-destructive)

Maths (destructive)