# Complex Numbers in Javascript The math.js library which I used in my post on SI Prefixes in JavaScript also includes comprehensive functionality for handling complex numbers. In this post I will demonstrate how to use this, as well as providing an insight into the underlying arithmetic involved.

In this article I am assuming you know what a complex number is - if not please read the first few paragraphs of the Wikipedia article.

Before starting to code I will show the actual formulae for addition, subtraction, multiplication and division as they are not obvious or intuitive. In each of these we have two complex numbers on the left of the = sign, with the +, -, * and / operations applied to them. The real and imaginary parts of each are labelled with subscript 1 and 2 so they can be identified in the part to the right of the = sign.

(a1 + bi1) + (a2 + bi2) = (a1 + a2) + (bi1 + bi2)
(a1 - bi1) + (a2 - bi2) = (a1 + a2) - (bi1 + bi2)

Multiplication

(a1 + bi1)(a2 + bi2) = (a1a2 - b1b2) + (a1b2 + b1a2)i

Division

(a1 + bi1) / (a2 + bi2) = ((a1 + b1)(a2 - bi2)) / ((a2 - bi2)(a2 + bi2))

## The Code

This project consists of a JavaScript file called complexnumbers.js as well as an HTML page and a few ancilliary files. You can download them as a zip or clone/download from Github if you prefer. The zip and repo include the minimized version of math.js called math.min.js.

This is the source code for complexnumbers.js.

complexnumbers.js

{
complexnumbers();
}

function complexnumbers()
{
writeToConsole("Complex Numbers with math.js<br />============================<br /><br />");

const z1 = math.complex(7, 12);
const z2 = math.complex(14, 24);
writeToConsole(`(\${z1}) + (\${z2}) = (\${z3})<br /><br />`);

writeToConsole("Subtraction<br />===========<br /><br />");

const z4 = math.complex(10, 6);
const z5 = math.complex(4, 2);
const z6 = math.subtract(z4, z5);
writeToConsole(`(\${z4}) - (\${z5}) = (\${z6})<br /><br />`);

writeToConsole("Multiplication<br />==============<br /><br />");

const z7 = math.complex(3, 2);
const z8 = math.complex(4, 5);
const z9 = math.multiply(z7, z8);
writeToConsole(`(\${z7}) * (\${z8}) = (\${z9})<br /><br />`);

writeToConsole("Division<br />========<br /><br />");

const z10 = math.complex(4, 7);
const z11 = math.complex(1, -3);
const z12 = math.divide(z10, z11);
writeToConsole(`(\${z10}) / (\${z11}) = (\${z12})<br /><br />`);

writeToConsole("Complex Conjugate<br />=================<br /><br />");

const z13 = math.complex(3, 4);
const z14 = z13.conjugate();
writeToConsole(`complex conjugate of (\${z13}) = (\${z14})<br /><br />`);

writeToConsole("Modulus and Argument<br />====================<br /><br />");

const z15 = math.complex(3, 4);
const polar = z15.toPolar();
writeToConsole(`Modulus r of (\${z15}) = \${polar.r}<br />`);
writeToConsole(`Argument φ of (\${z15}) = \${polar.phi} rad`);
}

For each operation, add, subtract etc., I have created variables using the math.js math.complex function. The arguments I have used are the real and imaginary parts of a complex number although there are a couple of other options; see the documentation at https://mathjs.org/docs/datatypes/complex_numbers.html for full details.

The relevant functions, math.add, math.divide etc., are then called on the complex numbers. Most are self-explanatory but I will look at conjugate() and toPolar() in a moment.

That's the code finished so open complexnumbers.htm. The conjugate of a complex number is simply that number but with the sign of the imaginary part reversed: positive becomes negative and vice versa.

The toPolar() function returns the complex number in a different form. If you think of the real and imaginary parts of a complex number as x and y coordinates of a point on a graph then toPolar() returns the straight-line distance of the point from the origin (0,0) and the angle from the x-axis.

This is best illustrated with an Argand diagram, a specialised type of graph showing complex numbers on x- and y-axes as mentioned above. Here you can see 3 + 4i plotted as a small circle, and also the modulus (distance from the origin) and argument (angle from the x-axis). The real/imaginary form and modulus/argument form are therefore two interchangeable ways of representing the same complex number. 