Complex Number
Algorithm · Stretch
A complex number is a number that can be expressed in the
form a + b * i, where a and b are real numbers, and i is a solution of
the equation x^2 = −1. Because no real number satisfies this
equation, i is called an imaginary number. For the complex
number a + b * i, a is called the real part, and b is called
the imaginary part.
A Complex Number is a combination of a Real Number and an Imaginary Number:
Geometrically, complex numbers extend the concept of the one-dimensional number
line to the two-dimensional complex plane by using the horizontal axis for the
real part and the vertical axis for the imaginary part. The complex
number a + b * i can be identified with the point (a, b) in the complex plane.
A complex number whose real part is zero is said to be purely imaginary; the points for these numbers lie on the vertical axis of the complex plane. A complex number whose imaginary part is zero can be viewed as a real number; its point lies on the horizontal axis of the complex plane.
| Complex Number | Real Part | Imaginary Part | |
|---|---|---|---|
| 3 + 2i | 3 | 2 | |
| 5 | 5 | 0 | Purely Real |
| −6i | 0 | -6 | Purely Imaginary |
A complex number can be visually represented as a pair of numbers (a, b) forming
a vector on a diagram called an Argand diagram, representing the complex plane.
Re is the real axis, Im is the imaginary axis, and i satisfies i^2 = −1.
Complex does not mean complicated. It means the two types of numbers, real and imaginary, together form a complex, just like a building complex (buildings joined together).
Polar Form
An alternative way of defining a point P in the complex plane, other than using
the x- and y-coordinates, is to use the distance of the point from O, the point
whose coordinates are (0, 0) (the origin), together with the angle subtended
between the positive real axis and the line segment OP in a counterclockwise
direction. This idea leads to the polar form of complex numbers.
The absolute value (or modulus or magnitude) of a complex number z = x + yi is:
The argument of z (in many applications referred to as the "phase") is the angle
of the radius OP with the positive real axis, and is written as arg(z). As
with the modulus, the argument can be found from the rectangular form x+yi:
Together, r and φ give another way of representing complex numbers, the
polar form, as the combination of modulus and argument fully specify the
position of a point on the plane. Recovering the original rectangular
co-ordinates from the polar form is done by the formula called trigonometric
form:
Using Euler's formula this can be written as:
Basic Operations
Adding
To add two complex numbers we add each part separately:
(a + b * i) + (c + d * i) = (a + c) + (b + d) * iExample
(3 + 5i) + (4 − 3i) = (3 + 4) + (5 − 3)i = 7 + 2iOn complex plane the adding operation will look like the following:
Subtracting
To subtract two complex numbers we subtract each part separately:
(a + b * i) - (c + d * i) = (a - c) + (b - d) * iExample
(3 + 5i) - (4 − 3i) = (3 - 4) + (5 + 3)i = -1 + 8iMultiplying
To multiply complex numbers each part of the first complex number gets multiplied by each part of the second complex number:
Just use "FOIL", which stands for "Firsts, Outers, Inners, Lasts" ( see Binomial Multiplication for more details):
- Firsts:
a × c - Outers:
a × di - Inners:
bi × c - Lasts:
bi × di
In general it looks like this:
(a + bi)(c + di) = ac + adi + bci + bdi^2But there is also a quicker way!
Use this rule:
(a + bi)(c + di) = (ac − bd) + (ad + bc)iExample
(3 + 2i)(1 + 7i)
= 3×1 + 3×7i + 2i×1+ 2i×7i
= 3 + 21i + 2i + 14i^2
= 3 + 21i + 2i − 14 (because i^2 = −1)
= −11 + 23i(3 + 2i)(1 + 7i) = (3×1 − 2×7) + (3×7 + 2×1)i = −11 + 23iConjugates
We will need to know about conjugates in a minute!
A conjugate is where we change the sign in the middle like this:
A conjugate is often written with a bar over it:
______
5 − 3i = 5 + 3iOn the complex plane the conjugate number will be mirrored against real axes.
Dividing
The conjugate is used to help complex division.
The trick is to multiply both top and bottom by the conjugate of the bottom.
Example
2 + 3i
------
4 − 5iMultiply top and bottom by the conjugate of 4 − 5i:
(2 + 3i) * (4 + 5i) 8 + 10i + 12i + 15i^2
= ------------------- = ----------------------
(4 − 5i) * (4 + 5i) 16 + 20i − 20i − 25i^2Now remember that i^2 = −1, so:
8 + 10i + 12i − 15 −7 + 22i −7 22
= ------------------- = -------- = -- + -- * i
16 + 20i − 20i + 25 41 41 41
There is a faster way though.
In the previous example, what happened on the bottom was interesting:
(4 − 5i)(4 + 5i) = 16 + 20i − 20i − 25iThe middle terms (20i − 20i) cancel out! Also i^2 = −1 so we end up with this:
(4 − 5i)(4 + 5i) = 4^2 + 5^2Which is really quite a simple result. The general rule is:
(a + bi)(a − bi) = a^2 + b^2Code
import radianToDegree from '../radian/radianToDegree';
export default class ComplexNumber {
/**
* z = re + im * i
* z = radius * e^(i * phase)
*
* @param {number} [re]
* @param {number} [im]
*/
constructor({ re = 0, im = 0 } = {}) {
this.re = re;
this.im = im;
}
/**
* @param {ComplexNumber|number} addend
* @return {ComplexNumber}
*/
add(addend) {
// Make sure we're dealing with complex number.
const complexAddend = this.toComplexNumber(addend);
return new ComplexNumber({
re: this.re + complexAddend.re,
im: this.im + complexAddend.im,
});
}
/**
* @param {ComplexNumber|number} subtrahend
* @return {ComplexNumber}
*/
subtract(subtrahend) {
// Make sure we're dealing with complex number.
const complexSubtrahend = this.toComplexNumber(subtrahend);
return new ComplexNumber({
re: this.re - complexSubtrahend.re,
im: this.im - complexSubtrahend.im,
});
}
/**
* @param {ComplexNumber|number} multiplicand
* @return {ComplexNumber}
*/
multiply(multiplicand) {
// Make sure we're dealing with complex number.
const complexMultiplicand = this.toComplexNumber(multiplicand);
return new ComplexNumber({
re: this.re * complexMultiplicand.re - this.im * complexMultiplicand.im,
im: this.re * complexMultiplicand.im + this.im * complexMultiplicand.re,
});
}
/**
* @param {ComplexNumber|number} divider
* @return {ComplexNumber}
*/
divide(divider) {
// Make sure we're dealing with complex number.
const complexDivider = this.toComplexNumber(divider);
// Get divider conjugate.
const dividerConjugate = this.conjugate(complexDivider);
// Multiply dividend by divider's conjugate.
const finalDivident = this.multiply(dividerConjugate);
// Calculating final divider using formula (a + bi)(a − bi) = a^2 + b^2
const finalDivider = (complexDivider.re ** 2) + (complexDivider.im ** 2);
return new ComplexNumber({
re: finalDivident.re / finalDivider,
im: finalDivident.im / finalDivider,
});
}
/**
* @param {ComplexNumber|number} number
*/
conjugate(number) {
// Make sure we're dealing with complex number.
const complexNumber = this.toComplexNumber(number);
return new ComplexNumber({
re: complexNumber.re,
im: -1 * complexNumber.im,
});
}
/**
* @return {number}
*/
getRadius() {
return Math.sqrt((this.re ** 2) + (this.im ** 2));
}
/**
* @param {boolean} [inRadians]
* @return {number}
*/
getPhase(inRadians = true) {
let phase = Math.atan(Math.abs(this.im) / Math.abs(this.re));
if (this.re < 0 && this.im > 0) {
phase = Math.PI - phase;
} else if (this.re < 0 && this.im < 0) {
phase = -(Math.PI - phase);
} else if (this.re > 0 && this.im < 0) {
phase = -phase;
} else if (this.re === 0 && this.im > 0) {
phase = Math.PI / 2;
} else if (this.re === 0 && this.im < 0) {
phase = -Math.PI / 2;
} else if (this.re < 0 && this.im === 0) {
phase = Math.PI;
} else if (this.re > 0 && this.im === 0) {
phase = 0;
} else if (this.re === 0 && this.im === 0) {
// More correctly would be to set 'indeterminate'.
// But just for simplicity reasons let's set zero.
phase = 0;
}
if (!inRadians) {
phase = radianToDegree(phase);
}
return phase;
}
/**
* @param {boolean} [inRadians]
* @return {{radius: number, phase: number}}
*/
getPolarForm(inRadians = true) {
return {
radius: this.getRadius(),
phase: this.getPhase(inRadians),
};
}
/**
* Convert real numbers to complex number.
* In case if complex number is provided then lefts it as is.
*
* @param {ComplexNumber|number} number
* @return {ComplexNumber}
*/
toComplexNumber(number) {
if (number instanceof ComplexNumber) {
return number;
}
return new ComplexNumber({ re: number });
}
}References
Spotted a mistake or something unclear? Report it on GitHub.