Yesterday I created this piece of code that could calculate z^n, where z is a complex number and n is any positive integer.
--snip--
float real = 0;
float imag = 0;
// d is the power the number is raised to [(x + yi)^d]
for (int n = 0; n <= d; n++) {
if (n == 0) {
real += pow(a, d);
} else { // binomial theorem
switch (n % 4) {
case 1: // i
imag += bCo(d, n) * pow(a, d - n) * pow(b, n);
break;
case 2: // -1
real -= bCo(d, n) * pow(a, d - n) * pow(b, n);
break;
case 3: // -i
imag -= bCo(d, n) * pow(a, d - n) * pow(b, n);
break;
case 0: // 1
real += bCo(d, n) * pow(a, d - n) * pow(b, n);
break;
}
}
}
--snip--
int factorial(int n) {
int total = 1;
for (int i = n; i > 1; i--) { total *= i; }
return total;
}
// binomial cofactor
float bCo(int n, int k) {
return (factorial(n)/(factorial(k) * factorial(n - k)));
}
I use the binomial theorem to expand z^n, and know whether to treat each term as a real or imaginary number depending on the power of the imaginary number.
What I want to do is to be able to calculate z^n, where n is any positive real number (fractions). I know the binomial theorem can be used for powers that aren't whole numbers, but I'm not really sure how to handle the complex numbers. Because i^0.1 has a real and imaginary component I can't just sort it into a real or imaginary variable, nor do I even know how to program something that could calculate it.
Does anyone know of an algorithm that can help me accomplish this, or maybe even a better way to handle complex numbers that will make this possible?
Oh, I'm using java.
Thanks.