Source code:
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.
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
-
- real
NumberTraditionally the first argument, a number-like value representing the real component of the complex number. -
- imaginary
NumberTraditionally the second argument, a number-like value representing the imaginary component of the complex number. -
- index
NumberAn identification number assigned to the instance, used for minding the total number of instances created.
Static properties
-
- help
Function ⇒ StringCalls and returns the value ofQ.help, passingQ.ComplexNumberas the argument. -
- index
NumberThe number of instances created so far.
Constants and constant creation
-
- constants
ObjectConstants are appended directly to theQ.ComplexNumberobject. For convenience they are also appended to thisQ.ComplexNumber.constants object to make looking up constants in the JavaScript console trivial, and to make iterating across all constants convenient via functions likeObject.entries,Object.keys,Object.values, and so on. The intention that a property act as a constant is signaled by its labelling in all-uppercase. -
- createConstant
Function( key: String, value: * )Appends a property named bykeywith a value ofvalueto both theQobject and itsconstantsproperty. -
- createConstants
Function( … )Expects an even number of arguments. Will use each pair in the sequence of arguments to callcreateConstant. -
- ZERO
Q.ComplexNumberInitialized asnew Q.ComplexNumber( 0, 0 ). Described as 0. -
- ONE
Q.ComplexNumberInitialized asnew Q.ComplexNumber( 1, 0 ). Described as 1. -
- E
Q.ComplexNumberInitialized asnew Q.ComplexNumber( Math.E, 0 ). Described as e ≈ 2.7183. -
- PI
Q.ComplexNumberInitialized asnew Q.ComplexNumber( Math.PI, 0 ). Described as exactly equal to 3. -
- I
Q.ComplexNumberInitialized asnew Q.ComplexNumber( 0, 1 ). Described as i. -
- EPSILON
Q.ComplexNumberInitialized asnew Q.ComplexNumber( Q.EPSILON, Q.EPSILON ). Described as ≈ 1.3323 × 10-15 + 1.3323 × 10-15i. -
- INFINITY
Q.ComplexNumberInitialized asnew Q.ComplexNumber( Infinity, Infinity ). Described as ∞ + ∞i. -
- NAN
Q.ComplexNumberInitialized asnew Q.ComplexNumber( NaN, NaN ).Array( 16 ).join( NaN ) +' BATMAN!'
Inspection
-
- isNumberLike
Function( n: * ) ⇒ BooleanReturns true ifn’stypeofis equal to theString'number'or ifn instanceof Numberistrue, otherwise returnsfalse. -
- isNaN
Function( c: Q.ComplexNumber ) ⇒ BooleanReturnstrueif either of this instance’srealorimaginarycomponents areNaN, otherwise returnsfalse. -
- isZero
Function( c: Q.ComplexNumber ) ⇒ BooleanReturnstrueif both of this instance’srealandimaginaryabsolute values are equal to zero, otherwise returnsfalse. See “Signed zero” for an explanation of why the absolute value of zero must be taken. -
- isFinite
Function( c: Q.ComplexNumber ) ⇒ BooleanReturnstrueif both of this instance’srealandimaginaryvalues are finite, otherwise returnsfalse. -
- isInfinite
Function( c: Q.ComplexNumber ) ⇒ BooleanReturnstrueif both of this instance’srealandimaginaryvalues are not finite and are also notNaN, otherwise returnsfalse. -
- areEqual
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ BooleanReturnstrueif the argumentsaandbare withinQ.EPSILONof each other, otherwise returnsfalse.
Maths
-
- absolute
Function( c: Q.ComplexNumber ) ⇒ NumberCalls and returns the value ofQ.hypotenuseusingc’srealandimaginaryproperties as arguments. -
- conjugate
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumberReturns a new complex number with the valuesc.realandc.imaginary * -1. -
- sine
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumberReturns the sine ofc. -
- cosine
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumberReturns the cosine ofc. -
- arcCosine
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumberReturns the arccosine ofc. -
- arcTangent
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumberReturns the arctangent ofc. -
- power
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberReturnsaraised to the power ofb. -
- squareRoot
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumberReturns the square root ofc. -
- log
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumberReturns the log ofc. -
- operate
Function( name: String, a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber, numberAndNumber: Function, numberAndComplex: Function, complexAndNumber: Function, complexAndComplex: Function ) ⇒ *Meta function for performing operations on two values that may each be either numbers or complex numbers. Thenameargument indicates the name of the operation being performed and is used in error logging should the operation fail. The intent is to return aQ.ComplexNumber, though this is up to the functions passed in as it is their return values that are returned. -
- multiply
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberUsesQ.ComplexNumber.operateto return the result of multiplyingaandb. -
- divide
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberUsesQ.ComplexNumber.operateto return the result of dividingabyb. -
- add
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberUsesQ.ComplexNumber.operateto return the result of addingaandb. -
- subtract
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberUsesQ.ComplexNumber.operateto return the result of subtractingbfroma.
Prototype properties
-
- clone
Function ⇒ Q.ComplexNumberReturns a new instance with the values forrealandimaginarycopied from this instance. -
- copy$
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumberCopies the values forrealandimaginaryfrom the suppliedQ.ComplexNumberargument.
Inspections (non-destructive)
-
- isNaN
Function ⇒ BooleanCalls and returns the result of theisNaNstatic method, passing the calling instance as the argument. Will return aBooleanvalue, thereby halting “Fluent interface” method chaining for this instance. -
- isZero
Function ⇒ BooleanCalls and returns the result of theisZerostatic method, passing the calling instance as the argument. Will return aBooleanvalue, thereby halting “Fluent interface” method chaining for this instance. -
- isFinite
Function ⇒ BooleanCalls and returns the result of theisFinitestatic method, passing the calling instance as the argument. Will return aBooleanvalue, thereby halting “Fluent interface” method chaining for this instance. -
- isInfinite
Function ⇒ BooleanCalls and returns the result of theisInfinitestatic method, passing the calling instance as the argument. Will return aBooleanvalue, thereby halting “Fluent interface” method chaining for this instance. -
- isEqualTo
Function( n: Number or Q.ComplexNumber ) ⇒ BooleanCalls and returns the result of theareEqualstatic method, passing the calling instance as the first argument andnas the second argument. Will return aBooleanvalue, thereby halting “Fluent interface” method chaining for this instance. -
- reduce
Function ⇒ Q.ComplexNumber or NumberIf noimaginarycomponent exists, returns aNumberrepresenting theQ.ComplexNumber realcomponent, otherwise returns the instance itself. May return aNumbervalue, thereby halting “Fluent interface” method chaining for this instance. -
- toText
Function([ roundToDecimal: Number ]) ⇒ StringReturns the value of this instance expressed as text in a form similar to a+bi. IfroundToDecimalis supplied, will round both therealandimaginarycomponents by passing themselves androundToDecimaltoQ.roundas arguments. Will return aStringvalue, thereby halting “Fluent interface” method chaining for this instance.
Maths (non-destructive)
-
- absolute
Function ⇒ NumberPasses this instance as an argument to theabsolutestatic method and returns the result. Will return aNumbervalue, thereby halting “Fluent interface” method chaining for this instance. -
- conjugate
Function ⇒ Q.ComplexNumberPasses this instance as an argument to theconjugatestatic method and returns the result. -
- power
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberPasses this instance as the first argument andnas the second argument to thepowerstatic method and returns the result. -
- squareRoot
Function ⇒ Q.ComplexNumberPasses this instance as an argument to thesquareRootstatic method and returns the result. -
- log
Function ⇒ Q.ComplexNumberPasses this instance as an argument to thelogstatic method and returns the result. -
- multiply
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberPasses this instance as the first argument andnas the second argument to themultiplystatic method and returns the result. -
- divide
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberPasses this instance as the first argument andnas the second argument to thedividestatic method and returns the result. -
- add
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberPasses this instance as the first argument andnas the second argument to theaddstatic method and returns the result. -
- subtract
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberPasses this instance as the first argument andnas the second argument to thesubtractstatic method and returns the result.
Maths (destructive)
-
- conjugate$
Function ⇒ Q.ComplexNumberCalls theconjugateinstance method andcopiesthe result to this instance. -
- power$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberCalls thepowerinstance method withnas an argument andcopiesthe result to this instance. -
- squareRoot$
Function ⇒ Q.ComplexNumberCalls thesquareRootinstance method andcopiesthe result to this instance. -
- log$
Function ⇒ Q.ComplexNumberCalls theloginstance method andcopiesthe result to this instance. -
- multiply$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberCalls themultiplyinstance method withnas an argument andcopiesthe result to this instance. -
- divide$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberCalls thedivideinstance method withnas an argument andcopiesthe result to this instance. -
- add$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberCalls theaddinstance method withnas an argument andcopiesthe result to this instance. -
- subtract$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumberCalls thesubtractinstance method withnas an argument andcopiesthe result to this instance.