Returns the value of this complex number raised to the power of a real component (in double precision). This method considers special cases where a simpler algorithm would return "ugly" results. For example when the expression (-1e40)^0.5 is evaluated without considering the special case, the
(double exponent)
| 353 | |
| 354 | */ |
| 355 | public Complex power(double exponent) { |
| 356 | // z^exp = abs(z)^exp * (cos(exp*arg(z)) + i*sin(exp*arg(z))) |
| 357 | double scalar = Math.pow(abs(), exponent); |
| 358 | boolean specialCase = false; |
| 359 | int factor = 0; |
| 360 | // consider special cases to avoid floating point errors |
| 361 | // for power expressions such as (-1e20)^2 |
| 362 | if((im==0)&&(re<0)) { |
| 363 | specialCase = true; |
| 364 | factor = 2; |
| 365 | } |
| 366 | if((re==0)&&(im>0)) { |
| 367 | specialCase = true; |
| 368 | factor = 1; |
| 369 | } |
| 370 | if((re==0)&&(im<0)) { |
| 371 | specialCase = true; |
| 372 | factor = -1; |
| 373 | } |
| 374 | if(specialCase&&(factor*exponent==(int) (factor*exponent))) { |
| 375 | short[] cSin = {0, 1, 0, -1}; // sin of 0, pi/2, pi, 3pi/2 |
| 376 | short[] cCos = {1, 0, -1, 0}; // cos of 0, pi/2, pi, 3pi/2 |
| 377 | int x = ((int) (factor*exponent))%4; |
| 378 | if(x<0) { |
| 379 | x = 4+x; |
| 380 | } |
| 381 | return new Complex(scalar*cCos[x], scalar*cSin[x]); |
| 382 | } |
| 383 | double temp = exponent*arg(); |
| 384 | return new Complex(scalar*Math.cos(temp), scalar*Math.sin(temp)); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Returns a <tt>Complex</tt> from real and imaginary parts. |