views:

121

answers:

4

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.

A: 

I'm not really good at math, so probably I understood your task wrong. But as far as I got it - apache commons math can help you: http://commons.apache.org/math/userguide/complex.html

Example:

Complex first  = new Complex(1.0, 3.0);
Complex second = new Complex(2.0, 5.0);

Complex answer = first.log();        // natural logarithm.
        answer = first.cos();        // cosine
        answer = first.pow(second);  // first raised to the power of second
Max
+8  A: 

First of all, it may have multiple solutions. See Wikipedia: Complex number / exponentiation.

Similar considerations show that we can define rational real powers just as for the reals, so z1/n is the n:th root of z. Roots are not unique, so it is already clear that complex powers are multivalued, thus careful treatment of powers is needed; for example (81/3)4 ≠ 16, as there are three cube roots of 8, so the given expression, often shortened to 84/3, is the simplest possible.

I think you should break it down to polar notation and go from there.

aioobe
In addition to the above link, have a look at http://en.wikipedia.org/wiki/De_Moivre%27s_formula#Failure_for_non-integer_powers and section 12.1.1 in http://www.suitcaseofdreams.net/De_Moivre_formula.htm
Steve
Thanks, I hadn't thought of that.Polar notation should make it a bit neater.
Bumzur
+1 for mentioning multiple values.
Moron
+2  A: 
Lets say:
z = x + iy then its polar form will be
z = r*e

where r is the absolute value of z i.e.
r = sqrt(x2 + y2)

and θ is
θ = arctan(y/x)

Once you have done so you can calculate z^n as ( using DeMoivre's Theorem):
zn = rn * einθ
or more simply as:
zn = rn * (cos (nθ) + i*sin(nθ))

http://en.wikipedia.org/wiki/Complex_number#Polar_form
http://en.wikipedia.org/wiki/De_Moivre%27s_formula
Favonius
Thanks, this is what I was looking for.I actually learnt about DeMoivre's Theorem last semester at school, so I feel a bit silly for not thinking to use it.
Bumzur
See also `public Complex pow(double e)` http://jscience.org/api/org/jscience/mathematics/number/Complex.html
trashgod
@Favonius: "z = r*e*i*theta" on the third line should be "z = r*e^(i*theta)" I'd change it myself, but I don't have the rep for that yet.
andand
The formula is valid only for integer values of n
belisarius
+1 this is what I was going to answer. Though DeMoivre's theorem is only valid for integer `n`, [Euler's Theorem](http://en.wikipedia.org/wiki/Euler%27s_formula) gives you the result that lies between the last and second-to-last step, which is valid for all real/complex n. Note that, like `sqrt()`, it only gives *one* of the roots.
BlueRaja - Danny Pflughoeft
A: 

a^n is ill defined when n is not an integer and a is not a positive number.

If z is a complex number, you can still give a meaning to z^a = exp(a log z) but you have to figure out what log z means when z is not a positive number.

And there is no unique choice.

Alexandre C.