I am trying to calculate the exponent $a^b$, where a and b are imaginary numbers, being represented here as 2 element arrays.
I am using the equations for this from wolfram mathworld, that is: $\left(a[1]+ia[2]\right)^{(b[1]+ib[2])} \left(a\left[1\right]^2+a\left[2\right]^2\right)^{\frac{b\left[1\right]}{2}}e^{\left(-b\left[2\right]\cdot arg\left(a\right)\right)} \left[\cos \left(b\left[1\right]\cdot a_{rg}\left(a\right)+\frac{1}{2}b\left[2\right]\ln \left(a\left[1\right]^2+a\left[2\right]^2\right)\right), \sin \left(b\left[1\right]\cdot a_{rg}\left(a\right)+\frac{1}{2}b\left[2\right]\ln \left(a\left[1\right]^2+a\left[2\right]^2\right)\right)\right]$
I have implemented $arg(z)$ as $arctan\left(\frac{z[2]}{z[1]}\right)$. This works for most values, however it results in weird, incorrect values when the real part of a is negative. Where is the mistake in these equations? I have checked them multiple times, and the only thing I can think is that maybe my arctan is defined in the wrong range, but I checked and it is going from $-\pi$ to $\pi$, so what is the problem?
The equation from Wolfram looks needlessly complicated. There is a simpler way to compute the complex power $a^b$, but you have to be careful because it is not always perfectly well-defined.
Take for example the real expression $4^{1/2}$: is this equal to $2$? or $-2$? By convention we usually pick the positive one (and negate it separately if the problem calls for the other solution). Similarly, there is a convention whereby we pick the value of the power $a^b$ when there are multiple candidates.
The definition for the general complex exponent is as follows: \begin{align} a^b := e^{b \ln a} \end{align} where $a$ and $b$ are complex numbers and $e^{x+yi} := e^x(\cos y + i \sin y)$
The trouble comes in with $\ln a$. In the real numbers, $e^x$ is a one-to-one function and so has a well-defined inverse. Not so in the complex plane. For example: $e^0 = 1$, but so does $e^{2\pi i}$ and $e^{4\pi i}$ and so on. Hence there are multiple candidates for $\ln(1)$ when $\ln$ is allowed to take on complex values. Specifically: \begin{align} \ln(1) = 2\pi k i \end{align} for any integer $k$.
In general $\ln(z)$ can take on any of these values: \begin{align} \ln z = \ln|z| + i \arg z \end{align} where $\ln|z|$ is the standard real-valued natural log that is well-defined, and $\arg z$ can be any of the possible arguments for a complex number (each one a multiple of $2\pi$ away from the others).$\newcommand{\Arg}{\operatorname{Arg}}$
In choosing a single value for $\ln z$ we usually prescribe the principle argument $\Arg z$ for the general one $\arg z$: \begin{align} \ln z := \ln|z| + i \Arg z \end{align} which is restricted to the range $-\pi < \Arg z \leq \pi$. This principle log is then used to define the principle exponent, so we (finally) get: \begin{align} a^b := e^{b\left(\ln|a| + i \Arg a \right)} \end{align} $\renewcommand{\Re}{\operatorname{Re}}$
Now with all that out of the way, we come to how to compute $\Arg z$. If your $\arctan$ function is computing the standard arctangent, then it should be restricted to the range $-\pi/2 < \arctan \theta < \pi/2$, not $-\pi$ to $\pi$. This, of course, does not cover the full range of possible arguments, and in fact only covers the cases where $\Re(a) > 0$. It will be off by $\pi$ if $\Re(a) < 0$. Thus the correct way to compute an angle is with the following piecewise function: \begin{align} \theta = \begin{cases} \arctan(y/x) & \text{if $x>0$} \\ \pi + \arctan(y/x) & \text{if $x<0$} \end{cases} \end{align} But of course, this does not handle the cases where $x = 0$, and so there are even more cases for that. What's more, the angles may not be in the range $(-\pi, \pi]$.$\newcommand{\atan}{\operatorname{atan2}}$
Because of this, most programs provide a function called $\atan(y,x)$ that computes the angle correctly regardless of the sign of $x$ and also in the correct range $(-\pi, \pi]$. Although if yours does not put it in that range for some reason, you can always add or subtract $2\pi$ accordingly to correct it.