# it’s complicated

I have been busy with complex numbers in JavaScript. I wanted to implement the Durand Kerner root finding algorithm. This algorithm requires complex arithmetic in order to work. So, in order to implement something, you need to implement something else. You just have to hope that you have arrived at the lowest level.

What is a complex number? It is a number of the type: $z = a + bi$ this is called rectangular form, and it is the form most people are familiar with. There is another form, called polar form, which looks like this: $z = re^{i\theta}$

To translate from polar form to rectangular form we use Euler’s formula: $re^{i\theta} = r(\cos \theta + i \sin \theta).$ To translate from rectangular to polar coordinates, we use the special function $$\arctan$$: $a + bi = \sqrt{a^2 + b^2}e^{i \arctan(b, a)}.$

Why do we need these two forms? Well, some of the arithmetic operations make more sense in one form than in the other and, moreover, are easier to compute. To implement most of the functions we are familiar with we need to start with the three basic operators:

• multiplication
• exponentiation

My implementation assumes the rectangular form as canonical. All operators are written for this form. For exponentiation we translate to polar, perform the operations, and translate back to rectangular.

Now for the mathematical definitions: $(a + bi) + (c + di) = (a + c) + (b + d)i$ $(a + bi) \cdot (c + di) = (a \cdot c – b \cdot d) + (a \cdot d + b \cdot c)i$ To define exponentiation we will first define the logarithm and the exponent. Notice how these operations are very similar to the translation operations defined before. $\log(a + bi) = \log(\sqrt{a^2 + b^2}e^{i \arctan(b, a)}) = \frac{1}{2}\log(a^2 + b^2) + \arctan(b, a)i$ $e^{a + bi} = e^a e^{bi} = e^a(\cos b + i\sin b)$ Now we can establish the general complex power: $(a + bi) ^{c + di} = e^{(c + di)\log(a + bi)}$ Which is defined in terms of our previous operations $$\cdot,\log,\exp$$.

And that is most of it. To approximate trigonometric functions we use the identity (also found by using Euler’s formula): $\cos z = \frac{e^{iz} + e^{-iz}}{2}$ Calculating the inverse is left as an exercise to the reader. My code can be found here.

## Join the conversation

1 Comment

1. Luminary Lobster says:

Woah! Great work man, I am definitly gonna us it for my Complex Analysis course.