Complex square root

=Introduction=



Let complex numbers $$W = a + b\cdot i$$ and $$w = p + q\cdot i.$$

Let $$W = w^2.$$

When given $$a, b,$$ aim of this page is to calculate $$p, q.$$

In the diagram complex number $$w = p + qi = w_{real} + i\cdot w_{imag} = w_{mod}(\cos w_{\phi} + i\cdot \sin w_{\phi}),$$ where


 * $$w_{real}, w_{imag}$$ are the real and imaginary components of $$w,$$ the rectangular components.


 * $$w_{mod}, w_{\phi}$$ are the modulus and phase of $$w,$$ the polar components.

Similarly, $$W_{real}, W_{imag}, W_{mod}, W_{\phi}$$ are the corresponding components of $$W.$$

$$W_{real}, W_{imag}$$ are given. $$W_{real}, W_{imag} = a,b.$$

$$W = w^2 = ( w_{mod}(\cos w_{\phi} + i\cdot \sin w_{\phi}) )^2$$

$$= w_{mod}^2(\cos (2 w_{\phi}) + i\cdot \sin (2 w_{\phi}))$$

$$= W_{mod}(\cos (W_{\phi}) + i\cdot \sin (W_{\phi}))$$

$$= W$$

There are 3 significant calculations:

$$W_{mod} = \sqrt[2]{a^2 + b^2}$$

$$w_{mod} = \sqrt[2]{W_{mod}}$$ and

$$\cos w_{\phi} = \cos \frac{W_{\phi}}{2}.$$


 * It is not necessary to calculate actual values of $$w_{\phi}, W_{\phi}.$$


 * As with real math, there are 2 complex square roots, $$(p + qi),\ (-p - qi).$$

=Implementation= In the python programming language a complex square root is available for floating point numbers with a precision of 15.

Function  provides clean output:

If it is desired to calculate complex square root with precision greater than that available for python's floating point math, the following code using python's  module will do the job. The following code also shows how complex square root is calculated.

=Examples=

=Method #2. Algebraic=

Introduction
Let $$W = a + b\cdot i$$ and $$w = p + q\cdot i.$$

Let $$W = w^2 = (p + qi)^2 = p^2 - q^2 + 2pqi.$$

Then:

$$a = P - Q\ \dots\ (1)$$ where $$P, Q = p^2, q^2.$$

$$b = 2pq\ \dots\ (2)$$

Square $$(2):\ B = 4PQ\ \dots\ (3)$$ where $$B = b^2$$

$$(1) * 4P:\ 4Pa = 4PP - 4PQ\ \dots\ (4)$$

From $$(3),(4):\ 4Pa = 4PP - B\ \dots\ (5)$$

From $$(5):\ 4PP -4aP - B = 0\ \dots\ (6)$$

From $$(6):$$ $$P = \frac{4a \pm \sqrt{16a^2 - 4(4)(-B)}}{2(4)}$$ $$= \frac{a \pm \sqrt{a^2 + b^2}}{2}$$ $$= \frac{a \pm W_{mod}}{2}$$

Implementation
Because function  contains only two calculations of  and function   contains three, function  is significantly faster than function

=Links to related topics= The Python Standard Library: cmath.sqrt