Calculates the square root of this object.
()
| 475 | * Calculates the square root of this object. |
| 476 | */ |
| 477 | public Complex sqrt() { |
| 478 | Complex c; |
| 479 | double absRe, absIm, w, r; |
| 480 | if((re==0)&&(im==0)) { |
| 481 | c = new Complex(0, 0); |
| 482 | } else { |
| 483 | absRe = Math.abs(re); |
| 484 | absIm = Math.abs(im); |
| 485 | if(absRe>=absIm) { |
| 486 | r = absIm/absRe; |
| 487 | w = Math.sqrt(absRe)*Math.sqrt(0.5*(1.0+Math.sqrt(1.0+r*r))); |
| 488 | } else { |
| 489 | r = absRe/absIm; |
| 490 | w = Math.sqrt(absIm)*Math.sqrt(0.5*(r+Math.sqrt(1.0+r*r))); |
| 491 | } |
| 492 | if(re>=0) { |
| 493 | c = new Complex(w, im/(2.0*w)); |
| 494 | } else { |
| 495 | if(im<0) { |
| 496 | w = -w; |
| 497 | } |
| 498 | c = new Complex(im/(2.0*w), w); |
| 499 | } |
| 500 | } |
| 501 | return c; |
| 502 | } |
| 503 | |
| 504 | //------------------------------------------------------------------------ |
| 505 | // Trigonometric functions |