source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/complex/Complex.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 58.4 KB
Line 
1/*
2* Class Complex
3*
4* Defines a complex number as an object and includes
5* the methods needed for standard complex arithmetic
6*
7* See class ComplexMatrix for complex matrix manipulations
8* See class ComplexPoly for complex polynomial manipulations
9* See class ComplexErrorProp for the error propogation in complex arithmetic
10*
11* WRITTEN BY: Dr Michael Thomas Flanagan
12*
13* DATE: February 2002
14* UPDATED: 1 August 2006, 29 April 2007, 15,21,22 June 2007, 22 November 2007
15* 20 May 2008, 26 August 2008, 9 November 2009, 6 june 2010
16*
17* DOCUMENTATION:
18* See Michael T Flanagan's Java library on-line web pages:
19* http://www.ee.ucl.ac.uk/~mflanaga/java/Complex.html
20* http://www.ee.ucl.ac.uk/~mflanaga/java/
21*
22* Copyright (c) 2002 - 2009 Michael Thomas Flanagan
23*
24* PERMISSION TO COPY:
25*
26* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
27* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
28* and associated documentation or publications.
29*
30* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
31* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
32*
33* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
34* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
35*
36* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
37* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
38* or its derivatives.
39*
40***************************************************************************************/
41
42package agents.anac.y2015.agentBuyogV2.flanagan.complex;
43
44import agents.anac.y2015.agentBuyogV2.flanagan.math.Fmath;
45
46public class Complex {
47
48 private double real = 0.0D; // Real part of a complex number
49 private double imag = 0.0D; // Imaginary part of a complex number
50 private static char jori = 'j'; // i or j in a + j.b or a + i.b
51 // representaion
52 // default value = j
53 private static boolean infOption = true; // option determining how infinity
54 // is handled
55 // if true (default option):
56 // multiplication with either complex number with either part = infinity
57 // returns infinity
58 // unless the one complex number is zero in both parts
59 // division by a complex number with either part = infinity returns zero
60 // unless the dividend is also infinite in either part
61 // if false:
62 // standard arithmetic performed
63
64 /*********************************************************/
65
66 // CONSTRUCTORS
67 // default constructor - real and imag = zero
68 public Complex() {
69 this.real = 0.0D;
70 this.imag = 0.0D;
71 }
72
73 // constructor - initialises both real and imag
74 public Complex(double real, double imag) {
75 this.real = real;
76 this.imag = imag;
77 }
78
79 // constructor - initialises real, imag = 0.0
80 public Complex(double real) {
81 this.real = real;
82 this.imag = 0.0D;
83 }
84
85 // constructor - initialises both real and imag to the values of an existing
86 // Complex
87 public Complex(Complex c) {
88 this.real = c.real;
89 this.imag = c.imag;
90 }
91
92 /*********************************************************/
93
94 // PUBLIC METHODS
95
96 // SET VALUES
97 // Set the value of real
98 public void setReal(double real) {
99 this.real = real;
100 }
101
102 // Set the value of imag
103 public void setImag(double imag) {
104 this.imag = imag;
105 }
106
107 // Set the values of real and imag
108 public void reset(double real, double imag) {
109 this.real = real;
110 this.imag = imag;
111 }
112
113 // Set real and imag given the modulus and argument (in radians)
114 public void polarRad(double mod, double arg) {
115 this.real = mod * Math.cos(arg);
116 this.imag = mod * Math.sin(arg);
117 }
118
119 // Set real and imag given the modulus and argument (in radians)
120 // retained for compatibility
121 public void polar(double mod, double arg) {
122 this.real = mod * Math.cos(arg);
123 this.imag = mod * Math.sin(arg);
124 }
125
126 // Set real and imag given the modulus and argument (in degrees)
127 public void polarDeg(double mod, double arg) {
128 arg = Math.toRadians(arg);
129 this.real = mod * Math.cos(arg);
130 this.imag = mod * Math.sin(arg);
131 }
132
133 // GET VALUES
134 // Get the value of real
135 public double getReal() {
136 return real;
137 }
138
139 // Get the value of imag
140 public double getImag() {
141 return imag;
142 }
143
144 // INPUT AND OUTPUT
145
146 // READ A COMPLEX NUMBER
147 // Read a complex number from the keyboard console after a prompt message
148 // in a String format compatible with Complex.parse,
149 // e.g 2+j3, 2 + j3, 2+i3, 2 + i3
150 // prompt = Prompt message to vdu
151 public static final synchronized Complex readComplex(String prompt) {
152 int ch = ' ';
153 String cstring = "";
154 boolean done = false;
155
156 System.out.print(prompt + " ");
157 System.out.flush();
158
159 while (!done) {
160 try {
161 ch = System.in.read();
162 if (ch < 0 || (char) ch == '\n')
163 done = true;
164 else
165 cstring = cstring + (char) ch;
166 } catch (java.io.IOException e) {
167 done = true;
168 }
169 }
170 return Complex.parseComplex(cstring);
171 }
172
173 // Read a complex number from the keyboard console after a prompt message
174 // (with String default option)
175 // in a String format compatible with Complex.parse,
176 // e.g 2+j3, 2 + j3, 2+i3, 2 + i3
177 // prompt = Prompt message to vdu
178 // dflt = default value
179 public static final synchronized Complex readComplex(String prompt, String dflt) {
180 int ch = ' ';
181 String cstring = "";
182 boolean done = false;
183
184 System.out.print(prompt + " [default value = " + dflt + "] ");
185 System.out.flush();
186
187 int i = 0;
188 while (!done) {
189 try {
190 ch = System.in.read();
191 if (ch < 0 || (char) ch == '\n' || (char) ch == '\r') {
192 if (i == 0) {
193 cstring = dflt;
194 if ((char) ch == '\r')
195 ch = System.in.read();
196 }
197 done = true;
198 } else {
199 cstring = cstring + (char) ch;
200 i++;
201 }
202 } catch (java.io.IOException e) {
203 done = true;
204 }
205 }
206 return Complex.parseComplex(cstring);
207 }
208
209 // Read a complex number from the keyboard console after a prompt message
210 // (with Complex default option)
211 // in a String format compatible with Complex.parse,
212 // e.g 2+j3, 2 + j3, 2+i3, 2 + i3
213 // prompt = Prompt message to vdu
214 // dflt = default value
215 public static final synchronized Complex readComplex(String prompt, Complex dflt) {
216 int ch = ' ';
217 String cstring = "";
218 boolean done = false;
219
220 System.out.print(prompt + " [default value = " + dflt + "] ");
221 System.out.flush();
222
223 int i = 0;
224 while (!done) {
225 try {
226 ch = System.in.read();
227 if (ch < 0 || (char) ch == '\n' || (char) ch == '\r') {
228 if (i == 0) {
229 if ((char) ch == '\r')
230 ch = System.in.read();
231 return dflt;
232 }
233 done = true;
234 } else {
235 cstring = cstring + (char) ch;
236 i++;
237 }
238 } catch (java.io.IOException e) {
239 done = true;
240 }
241 }
242 return Complex.parseComplex(cstring);
243 }
244
245 // Read a complex number from the keyboard console without a prompt message
246 // in a String format compatible with Complex.parse,
247 // e.g 2+j3, 2 + j3, 2+i3, 2 + i3
248 // prompt = Prompt message to vdu
249 public static final synchronized Complex readComplex() {
250 int ch = ' ';
251 String cstring = "";
252 boolean done = false;
253
254 System.out.print(" ");
255 System.out.flush();
256
257 while (!done) {
258 try {
259 ch = System.in.read();
260 if (ch < 0 || (char) ch == '\n')
261 done = true;
262 else
263 cstring = cstring + (char) ch;
264 } catch (java.io.IOException e) {
265 done = true;
266 }
267 }
268 return Complex.parseComplex(cstring);
269 }
270
271 // PRINT A COMPLEX NUMBER
272 // Print to terminal window with text (message) and a line return
273 public void println(String message) {
274 System.out.println(message + " " + this.toString());
275 }
276
277 // Print to terminal window without text (message) but with a line return
278 public void println() {
279 System.out.println(" " + this.toString());
280 }
281
282 // Print to terminal window with text (message) but without line return
283 public void print(String message) {
284 System.out.print(message + " " + this.toString());
285 }
286
287 // Print to terminal window without text (message) and without line return
288 public void print() {
289 System.out.print(" " + this.toString());
290 }
291
292 // PRINT AN ARRAY OF COMLEX NUMBERS
293 // Print an array to terminal window with text (message) and a line return
294 public static void println(String message, Complex[] aa) {
295 System.out.println(message);
296 for (int i = 0; i < aa.length; i++) {
297 System.out.println(aa[i].toString() + " ");
298 }
299 }
300
301 // Print an array to terminal window without text (message) but with a line
302 // return
303 public static void println(Complex[] aa) {
304 for (int i = 0; i < aa.length; i++) {
305 System.out.println(aa[i].toString() + " ");
306 }
307 }
308
309 // Print an array to terminal window with text (message) but no line returns
310 // except at the end
311 public static void print(String message, Complex[] aa) {
312 System.out.print(message + " ");
313 for (int i = 0; i < aa.length; i++) {
314 System.out.print(aa[i].toString() + " ");
315 }
316 System.out.println();
317 }
318
319 // Print an array to terminal window without text (message) but with no line
320 // returns except at the end
321 public static void print(Complex[] aa) {
322 for (int i = 0; i < aa.length; i++) {
323 System.out.print(aa[i].toString() + " ");
324 }
325 System.out.println();
326 }
327
328 // TRUNCATION
329 // Rounds the mantissae of both the real and imaginary parts of Complex to
330 // prec places
331 // Static method
332 public static Complex truncate(Complex x, int prec) {
333 if (prec < 0)
334 return x;
335
336 double xR = x.getReal();
337 double xI = x.getImag();
338 Complex y = new Complex();
339
340 xR = Fmath.truncate(xR, prec);
341 xI = Fmath.truncate(xI, prec);
342
343 y.reset(xR, xI);
344
345 return y;
346 }
347
348 // instance method
349 public Complex truncate(int prec) {
350 if (prec < 0)
351 return this;
352
353 double xR = this.getReal();
354 double xI = this.getImag();
355 Complex y = new Complex();
356
357 xR = Fmath.truncate(xR, prec);
358 xI = Fmath.truncate(xI, prec);
359
360 y.reset(xR, xI);
361
362 return y;
363 }
364
365 // CONVERSIONS
366 // Format a complex number as a string, a + jb or a + ib[instance method]
367 // < value of real > < + or - > < j or i> < value of imag >
368 // Choice of j or i is set by Complex.seti() or Complex.setj()
369 // j is the default option for j or i
370 // Overides java.lang.String.toString()
371 public String toString() {
372 char ch = '+';
373 if (this.imag < 0.0D)
374 ch = '-';
375 return this.real + " " + ch + " " + jori + Math.abs(this.imag);
376 }
377
378 // Format a complex number as a string, a + jb or a + ib [static method]
379 // See static method above for comments
380 public static String toString(Complex aa) {
381 char ch = '+';
382 if (aa.imag < 0.0D)
383 ch = '-';
384 return aa.real + " " + ch + jori + Math.abs(aa.imag);
385 }
386
387 // Sets the representation of the square root of minus one to j in Strings
388 public static void setj() {
389 jori = 'j';
390 }
391
392 // Sets the representation of the square root of minus one to i in Strings
393 public static void seti() {
394 jori = 'i';
395 }
396
397 // Returns the representation of the square root of minus one (j or i) set
398 // for Strings
399 public static char getjori() {
400 return jori;
401 }
402
403 // Parse a string to obtain Complex
404 // accepts strings 'real''s''sign''s''x''imag'
405 // where x may be i or j and s may be no spaces or any number of spaces
406 // and sign may be + or -
407 // e.g. 2+j3, 2 + j3, 2+i3, 2 + i3
408 public static Complex parseComplex(String ss) {
409 Complex aa = new Complex();
410 ss = ss.trim();
411 double first = 1.0D;
412 if (ss.charAt(0) == '-') {
413 first = -1.0D;
414 ss = ss.substring(1);
415 }
416
417 int i = ss.indexOf('j');
418 if (i == -1) {
419 i = ss.indexOf('i');
420 }
421 if (i == -1)
422 throw new NumberFormatException("no i or j found");
423
424 int imagSign = 1;
425 int j = ss.indexOf('+');
426
427 if (j == -1) {
428 j = ss.indexOf('-');
429 if (j > -1)
430 imagSign = -1;
431 }
432 if (j == -1)
433 throw new NumberFormatException("no + or - found");
434
435 int r0 = 0;
436 int r1 = j;
437 int i0 = i + 1;
438 int i1 = ss.length();
439 String sreal = ss.substring(r0, r1);
440 String simag = ss.substring(i0, i1);
441 aa.real = first * Double.parseDouble(sreal);
442 aa.imag = imagSign * Double.parseDouble(simag);
443 return aa;
444 }
445
446 // Same method as parseComplex
447 // Overides java.lang.Object.valueOf()
448 public static Complex valueOf(String ss) {
449 return Complex.parseComplex(ss);
450 }
451
452 // Return a HASH CODE for the Complex number
453 // Overides java.lang.Object.hashCode()
454 public int hashCode() {
455 long lreal = Double.doubleToLongBits(this.real);
456 long limag = Double.doubleToLongBits(this.imag);
457 int hreal = (int) (lreal ^ (lreal >>> 32));
458 int himag = (int) (limag ^ (limag >>> 32));
459 return 7 * (hreal / 10) + 3 * (himag / 10);
460 }
461
462 // SWAP
463 // Swaps two complex numbers
464 public static void swap(Complex aa, Complex bb) {
465 double holdAreal = aa.real;
466 double holdAimag = aa.imag;
467 aa.reset(bb.real, bb.imag);
468 bb.reset(holdAreal, holdAimag);
469 }
470
471 // ARRAYS
472
473 // Create a one dimensional array of Complex objects of length n
474 // all real = 0 and all imag = 0
475 public static Complex[] oneDarray(int n) {
476 Complex[] a = new Complex[n];
477 for (int i = 0; i < n; i++) {
478 a[i] = Complex.zero();
479 }
480 return a;
481 }
482
483 // Create a one dimensional array of Complex objects of length n
484 // all real = a and all imag = b
485 public static Complex[] oneDarray(int n, double a, double b) {
486 Complex[] c = new Complex[n];
487 for (int i = 0; i < n; i++) {
488 c[i] = Complex.zero();
489 c[i].reset(a, b);
490 }
491 return c;
492 }
493
494 // Arithmetic mean of a one dimensional array of complex numbers
495 public static Complex mean(Complex[] aa) {
496 int n = aa.length;
497 Complex sum = new Complex(0.0D, 0.0D);
498 for (int i = 0; i < n; i++) {
499 sum = sum.plus(aa[i]);
500 }
501 return sum.over((double) n);
502 }
503
504 // Create a one dimensional array of Complex objects of length n
505 // all = the Complex constant
506 public static Complex[] oneDarray(int n, Complex constant) {
507 Complex[] c = new Complex[n];
508 for (int i = 0; i < n; i++) {
509 c[i] = Complex.copy(constant);
510 }
511 return c;
512 }
513
514 // Create a two dimensional array of Complex objects of dimensions n and m
515 // all real = zero and all imag = zero
516 public static Complex[][] twoDarray(int n, int m) {
517 Complex[][] a = new Complex[n][m];
518 for (int i = 0; i < n; i++) {
519 for (int j = 0; j < m; j++) {
520 a[i][j] = Complex.zero();
521 }
522 }
523 return a;
524 }
525
526 // Create a two dimensional array of Complex objects of dimensions n and m
527 // all real = a and all imag = b
528 public static Complex[][] twoDarray(int n, int m, double a, double b) {
529 Complex[][] c = new Complex[n][m];
530 for (int i = 0; i < n; i++) {
531 for (int j = 0; j < m; j++) {
532 c[i][j] = Complex.zero();
533 c[i][j].reset(a, b);
534 }
535 }
536 return c;
537 }
538
539 // Create a two dimensional array of Complex objects of dimensions n and m
540 // all = the Complex constant
541 public static Complex[][] twoDarray(int n, int m, Complex constant) {
542 Complex[][] c = new Complex[n][m];
543 for (int i = 0; i < n; i++) {
544 for (int j = 0; j < m; j++) {
545 c[i][j] = Complex.copy(constant);
546 }
547 }
548 return c;
549 }
550
551 // Create a three dimensional array of Complex objects of dimensions n, m
552 // and l
553 // all real = zero and all imag = zero
554 public static Complex[][][] threeDarray(int n, int m, int l) {
555 Complex[][][] a = new Complex[n][m][l];
556 for (int i = 0; i < n; i++) {
557 for (int j = 0; j < m; j++) {
558 for (int k = 0; k < l; k++) {
559 a[i][j][k] = Complex.zero();
560 }
561 }
562 }
563 return a;
564 }
565
566 // Create a three dimensional array of Complex objects of dimensions n, m
567 // and l
568 // all real = a and all imag = b
569 public static Complex[][][] threeDarray(int n, int m, int l, double a, double b) {
570 Complex[][][] c = new Complex[n][m][l];
571 for (int i = 0; i < n; i++) {
572 for (int j = 0; j < m; j++) {
573 for (int k = 0; k < l; k++) {
574 c[i][j][k] = Complex.zero();
575 c[i][j][k].reset(a, b);
576 }
577 }
578 }
579 return c;
580 }
581
582 // Create a three dimensional array of Complex objects of dimensions n, m
583 // and l
584 // all = the Complex constant
585 public static Complex[][][] threeDarray(int n, int m, int l, Complex constant) {
586 Complex[][][] c = new Complex[n][m][l];
587 for (int i = 0; i < n; i++) {
588 for (int j = 0; j < m; j++) {
589 for (int k = 0; k < l; k++) {
590 c[i][j][k] = Complex.copy(constant);
591 }
592 }
593 }
594 return c;
595 }
596
597 // COPY
598 // Copy a single complex number [static method]
599 public static Complex copy(Complex a) {
600 if (a == null) {
601 return null;
602 } else {
603 Complex b = new Complex();
604 b.real = a.real;
605 b.imag = a.imag;
606 return b;
607 }
608 }
609
610 // Copy a single complex number [instance method]
611 public Complex copy() {
612 if (this == null) {
613 return null;
614 } else {
615 Complex b = new Complex();
616 b.real = this.real;
617 b.imag = this.imag;
618 return b;
619 }
620 }
621
622 // Copy a 1D array of complex numbers (deep copy)
623 // static metod
624 public static Complex[] copy(Complex[] a) {
625 if (a == null) {
626 return null;
627 } else {
628 int n = a.length;
629 Complex[] b = Complex.oneDarray(n);
630 for (int i = 0; i < n; i++) {
631 b[i] = Complex.copy(a[i]);
632 }
633 return b;
634 }
635 }
636
637 // Copy a 2D array of complex numbers (deep copy)
638 public static Complex[][] copy(Complex[][] a) {
639 if (a == null) {
640 return null;
641 } else {
642 int n = a.length;
643 int m = a[0].length;
644 Complex[][] b = Complex.twoDarray(n, m);
645 for (int i = 0; i < n; i++) {
646 for (int j = 0; j < m; j++) {
647 b[i][j] = Complex.copy(a[i][j]);
648 }
649 }
650 return b;
651 }
652 }
653
654 // Copy a 3D array of complex numbers (deep copy)
655 public static Complex[][][] copy(Complex[][][] a) {
656 if (a == null) {
657 return null;
658 } else {
659 int n = a.length;
660 int m = a[0].length;
661 int l = a[0][0].length;
662 Complex[][][] b = Complex.threeDarray(n, m, l);
663 for (int i = 0; i < n; i++) {
664 for (int j = 0; j < m; j++) {
665 for (int k = 0; k < l; k++) {
666 b[i][j][k] = Complex.copy(a[i][j][k]);
667 }
668 }
669 }
670 return b;
671 }
672 }
673
674 // CLONE
675 // Overrides Java.Object method clone
676 // Copy a single complex number [instance method]
677 public Object clone() {
678 Object ret = null;
679
680 if (this != null) {
681 Complex b = new Complex();
682 b.real = this.real;
683 b.imag = this.imag;
684 ret = (Object) b;
685 }
686
687 return ret;
688 }
689
690 // ADDITION
691 // Add two Complex numbers [static method]
692 public static Complex plus(Complex a, Complex b) {
693 Complex c = new Complex();
694 c.real = a.real + b.real;
695 c.imag = a.imag + b.imag;
696 return c;
697 }
698
699 // Add a double to a Complex number [static method]
700 public static Complex plus(Complex a, double b) {
701 Complex c = new Complex();
702 c.real = a.real + b;
703 c.imag = a.imag;
704 return c;
705 }
706
707 // Add a Complex number to a double [static method]
708 public static Complex plus(double a, Complex b) {
709 Complex c = new Complex();
710 c.real = a + b.real;
711 c.imag = b.imag;
712 return c;
713 }
714
715 // Add a double number to a double and return sum as Complex [static method]
716 public static Complex plus(double a, double b) {
717 Complex c = new Complex();
718 c.real = a + b;
719 c.imag = 0.0D;
720 return c;
721 }
722
723 // Add a Complex number to this Complex number [instance method]
724 // this Complex number remains unaltered
725 public Complex plus(Complex a) {
726 Complex b = new Complex();
727 b.real = this.real + a.real;
728 b.imag = this.imag + a.imag;
729 return b;
730 }
731
732 // Add double number to this Complex number [instance method]
733 // this Complex number remains unaltered
734 public Complex plus(double a) {
735 Complex b = new Complex();
736 b.real = this.real + a;
737 b.imag = this.imag;
738 return b;
739 }
740
741 // Add a Complex number to this Complex number and replace this with the sum
742 public void plusEquals(Complex a) {
743 this.real += a.real;
744 this.imag += a.imag;
745 }
746
747 // Add double number to this Complex number and replace this with the sum
748 public void plusEquals(double a) {
749 this.real += a;
750 // this.imag = this.imag;
751 }
752
753 // SUBTRACTION
754 // Subtract two Complex numbers [static method]
755 public static Complex minus(Complex a, Complex b) {
756 Complex c = new Complex();
757 c.real = a.real - b.real;
758 c.imag = a.imag - b.imag;
759 return c;
760 }
761
762 // Subtract a double from a Complex number [static method]
763 public static Complex minus(Complex a, double b) {
764 Complex c = new Complex();
765 c.real = a.real - b;
766 c.imag = a.imag;
767 return c;
768 }
769
770 // Subtract a Complex number from a double [static method]
771 public static Complex minus(double a, Complex b) {
772 Complex c = new Complex();
773 c.real = a - b.real;
774 c.imag = -b.imag;
775 return c;
776 }
777
778 // Subtract a double number to a double and return difference as Complex
779 // [static method]
780 public static Complex minus(double a, double b) {
781 Complex c = new Complex();
782 c.real = a - b;
783 c.imag = 0.0D;
784 return c;
785 }
786
787 // Subtract a Complex number from this Complex number [instance method]
788 // this Complex number remains unaltered
789 public Complex minus(Complex a) {
790 Complex b = new Complex();
791 b.real = this.real - a.real;
792 b.imag = this.imag - a.imag;
793 return b;
794 }
795
796 // Subtract a double number from this Complex number [instance method]
797 // this Complex number remains unaltered
798 public Complex minus(double a) {
799 Complex b = new Complex();
800 b.real = this.real - a;
801 b.imag = this.imag;
802 return b;
803 }
804
805 // Subtract this Complex number from a double number [instance method]
806 // this Complex number remains unaltered
807 public Complex transposedMinus(double a) {
808 Complex b = new Complex();
809 b.real = a - this.real;
810 b.imag = this.imag;
811 return b;
812 }
813
814 // Subtract a Complex number from this Complex number and replace this by
815 // the difference
816 public void minusEquals(Complex a) {
817 this.real -= a.real;
818 this.imag -= a.imag;
819 }
820
821 // Subtract a double number from this Complex number and replace this by the
822 // difference
823 public void minusEquals(double a) {
824 this.real -= a;
825 // this.imag=this.imag;
826 }
827
828 // MULTIPLICATION
829 // Sets the infinity handling option in multiplication and division
830 // infOption -> true; standard arithmetic overriden - see above (instance
831 // variable definitions) for details
832 // infOption -> false: standard arithmetic used
833 public static void setInfOption(boolean infOpt) {
834 Complex.infOption = infOpt;
835 }
836
837 // Sets the infinity handling option in multiplication and division
838 // opt = 0: infOption -> true; standard arithmetic overriden - see above
839 // (instance variable definitions) for details
840 // opt = 1: infOption -> false: standard arithmetic used
841 public static void setInfOption(int opt) {
842 if (opt < 0 || opt > 1)
843 throw new IllegalArgumentException("opt must be 0 or 1");
844 Complex.infOption = true;
845 if (opt == 1)
846 Complex.infOption = false;
847 }
848
849 // Gets the infinity handling option in multiplication and division
850 // infOption -> true; standard arithmetic overriden - see above (instance
851 // variable definitions) for details
852 // infOption -> false: standard arithmetic used
853 public static boolean getInfOption() {
854 return Complex.infOption;
855 }
856
857 // Multiply two Complex numbers [static method]
858 public static Complex times(Complex a, Complex b) {
859 Complex c = new Complex(0.0D, 0.0D);
860 if (Complex.infOption) {
861 if (a.isInfinite() && !b.isZero()) {
862 c.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
863 return c;
864 }
865 if (b.isInfinite() && !a.isZero()) {
866 c.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
867 return c;
868 }
869 }
870
871 c.real = a.real * b.real - a.imag * b.imag;
872 c.imag = a.real * b.imag + a.imag * b.real;
873 return c;
874 }
875
876 // Multiply a Complex number by a double [static method]
877 public static Complex times(Complex a, double b) {
878 Complex c = new Complex();
879 if (Complex.infOption) {
880 if (a.isInfinite() && b != 0.0D) {
881 c.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
882 return c;
883 }
884 if (Fmath.isInfinity(b) && !a.isZero()) {
885 c.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
886 return c;
887 }
888 }
889 c.real = a.real * b;
890 c.imag = a.imag * b;
891 return c;
892 }
893
894 // Multiply a double by a Complex number [static method]
895 public static Complex times(double a, Complex b) {
896 Complex c = new Complex();
897 if (Complex.infOption) {
898 if (b.isInfinite() && a != 0.0D) {
899 c.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
900 return c;
901 }
902 if (Fmath.isInfinity(a) && !b.isZero()) {
903 c.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
904 return c;
905 }
906 }
907
908 c.real = a * b.real;
909 c.imag = a * b.imag;
910 return c;
911 }
912
913 // Multiply a double number to a double and return product as Complex
914 // [static method]
915 public static Complex times(double a, double b) {
916 Complex c = new Complex();
917 c.real = a * b;
918 c.imag = 0.0D;
919 return c;
920 }
921
922 // Multiply this Complex number by a Complex number [instance method]
923 // this Complex number remains unaltered
924 public Complex times(Complex a) {
925 Complex b = new Complex();
926 if (Complex.infOption) {
927 if (this.isInfinite() && !a.isZero()) {
928 b.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
929 return b;
930 }
931 if (a.isInfinite() && !this.isZero()) {
932 b.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
933 return b;
934 }
935 }
936
937 b.real = this.real * a.real - this.imag * a.imag;
938 b.imag = this.real * a.imag + this.imag * a.real;
939 return b;
940 }
941
942 // Multiply this Complex number by a double [instance method]
943 // this Complex number remains unaltered
944 public Complex times(double a) {
945 Complex b = new Complex();
946 if (Complex.infOption) {
947 if (this.isInfinite() && a != 0.0D) {
948 b.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
949 return b;
950 }
951 if (Fmath.isInfinity(a) && !this.isZero()) {
952 b.reset(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
953 return b;
954 }
955 }
956
957 b.real = this.real * a;
958 b.imag = this.imag * a;
959 return b;
960 }
961
962 // Multiply this Complex number by a Complex number and replace this by the
963 // product
964 public void timesEquals(Complex a) {
965 Complex b = new Complex();
966 boolean test = true;
967 if (Complex.infOption) {
968 if ((this.isInfinite() && !a.isZero()) || (a.isInfinite() && !this.isZero())) {
969 this.real = Double.POSITIVE_INFINITY;
970 this.imag = Double.POSITIVE_INFINITY;
971 test = false;
972 }
973 }
974 if (test) {
975 b.real = a.real * this.real - a.imag * this.imag;
976 b.imag = a.real * this.imag + a.imag * this.real;
977 this.real = b.real;
978 this.imag = b.imag;
979 }
980 }
981
982 // Multiply this Complex number by a double and replace this by the product
983 public void timesEquals(double a) {
984 boolean test = true;
985 if (Complex.infOption) {
986 if ((this.isInfinite() && a != 0.0D) || (Fmath.isInfinity(a) && !this.isZero())) {
987 this.real = Double.POSITIVE_INFINITY;
988 this.imag = Double.POSITIVE_INFINITY;
989 test = false;
990 }
991 }
992 if (test) {
993 this.real = this.real * a;
994 this.imag = this.imag * a;
995 }
996 }
997
998 // DIVISION
999 // Division of two Complex numbers a/b [static method]
1000 public static Complex over(Complex a, Complex b) {
1001 Complex c = new Complex(0.0D, 0.0D);
1002 if (Complex.infOption && !a.isInfinite() && b.isInfinite())
1003 return c;
1004
1005 double denom = 0.0D, ratio = 0.0D;
1006 if (a.isZero()) {
1007 if (b.isZero()) {
1008 c.real = Double.NaN;
1009 c.imag = Double.NaN;
1010 } else {
1011 c.real = 0.0D;
1012 c.imag = 0.0D;
1013 }
1014 } else {
1015 if (Math.abs(b.real) >= Math.abs(b.imag)) {
1016 ratio = b.imag / b.real;
1017 denom = b.real + b.imag * ratio;
1018 c.real = (a.real + a.imag * ratio) / denom;
1019 c.imag = (a.imag - a.real * ratio) / denom;
1020 } else {
1021 ratio = b.real / b.imag;
1022 denom = b.real * ratio + b.imag;
1023 c.real = (a.real * ratio + a.imag) / denom;
1024 c.imag = (a.imag * ratio - a.real) / denom;
1025 }
1026 }
1027 return c;
1028 }
1029
1030 // Division of a Complex number, a, by a double, b [static method]
1031 public static Complex over(Complex a, double b) {
1032 Complex c = new Complex(0.0D, 0.0D);
1033 if (Complex.infOption && Fmath.isInfinity(b))
1034 return c;
1035
1036 c.real = a.real / b;
1037 c.imag = a.imag / b;
1038 return c;
1039 }
1040
1041 // Division of a double, a, by a Complex number, b [static method]
1042 public static Complex over(double a, Complex b) {
1043 Complex c = new Complex();
1044 if (Complex.infOption && !Fmath.isInfinity(a) && b.isInfinite())
1045 return c;
1046
1047 double denom, ratio;
1048
1049 if (a == 0.0D) {
1050 if (b.isZero()) {
1051 c.real = Double.NaN;
1052 c.imag = Double.NaN;
1053 } else {
1054 c.real = 0.0D;
1055 c.imag = 0.0D;
1056 }
1057 } else {
1058 if (Math.abs(b.real) >= Math.abs(b.imag)) {
1059 ratio = b.imag / b.real;
1060 denom = b.real + b.imag * ratio;
1061 c.real = a / denom;
1062 c.imag = -a * ratio / denom;
1063 } else {
1064 ratio = b.real / b.imag;
1065 denom = b.real * ratio + b.imag;
1066 c.real = a * ratio / denom;
1067 c.imag = -a / denom;
1068 }
1069 }
1070 return c;
1071 }
1072
1073 // Divide a double number by a double and return quotient as Complex [static
1074 // method]
1075 public static Complex over(double a, double b) {
1076 Complex c = new Complex();
1077 c.real = a / b;
1078 c.imag = 0.0;
1079 return c;
1080 }
1081
1082 // Division of this Complex number by a Complex number [instance method]
1083 // this Complex number remains unaltered
1084 public Complex over(Complex a) {
1085 Complex b = new Complex(0.0D, 0.0D);
1086 if (Complex.infOption && !this.isInfinite() && a.isInfinite())
1087 return b;
1088
1089 double denom = 0.0D, ratio = 0.0D;
1090 if (Math.abs(a.real) >= Math.abs(a.imag)) {
1091 ratio = a.imag / a.real;
1092 denom = a.real + a.imag * ratio;
1093 b.real = (this.real + this.imag * ratio) / denom;
1094 b.imag = (this.imag - this.real * ratio) / denom;
1095 } else {
1096 ratio = a.real / a.imag;
1097 denom = a.real * ratio + a.imag;
1098 b.real = (this.real * ratio + this.imag) / denom;
1099 b.imag = (this.imag * ratio - this.real) / denom;
1100 }
1101 return b;
1102 }
1103
1104 // Division of this Complex number by a double [instance method]
1105 // this Complex number remains unaltered
1106 public Complex over(double a) {
1107 Complex b = new Complex(0.0D, 0.0D);
1108
1109 b.real = this.real / a;
1110 b.imag = this.imag / a;
1111 return b;
1112 }
1113
1114 // Division of a double by this Complex number [instance method]
1115 // this Complex number remains unaltered
1116 public Complex transposedOver(double a) {
1117 Complex c = new Complex(0.0D, 0.0D);
1118 if (Complex.infOption && !Fmath.isInfinity(a) && this.isInfinite())
1119 return c;
1120
1121 double denom = 0.0D, ratio = 0.0D;
1122 if (Math.abs(this.real) >= Math.abs(this.imag)) {
1123 ratio = this.imag / this.real;
1124 denom = this.real + this.imag * ratio;
1125 c.real = a / denom;
1126 c.imag = -a * ratio / denom;
1127 } else {
1128 ratio = this.real / this.imag;
1129 denom = this.real * ratio + this.imag;
1130 c.real = a * ratio / denom;
1131 c.imag = -a / denom;
1132 }
1133 return c;
1134 }
1135
1136 // Division of this Complex number by a Complex number and replace this by
1137 // the quotient
1138 public void overEquals(Complex b) {
1139 Complex c = new Complex(0.0D, 0.0D);
1140
1141 boolean test = true;
1142 if (Complex.infOption && !this.isInfinite() && b.isInfinite()) {
1143 this.real = 0.0D;
1144 this.imag = 0.0D;
1145 test = false;
1146 }
1147 if (test) {
1148 double denom = 0.0D, ratio = 0.0D;
1149 if (Math.abs(b.real) >= Math.abs(b.imag)) {
1150 ratio = b.imag / b.real;
1151 denom = b.real + b.imag * ratio;
1152 c.real = (this.real + this.imag * ratio) / denom;
1153 c.imag = (this.imag - this.real * ratio) / denom;
1154 } else {
1155 ratio = b.real / b.imag;
1156 denom = b.real * ratio + b.imag;
1157 c.real = (this.real * ratio + this.imag) / denom;
1158 c.imag = (this.imag * ratio - this.real) / denom;
1159 }
1160 this.real = c.real;
1161 this.imag = c.imag;
1162 }
1163 }
1164
1165 // Division of this Complex number by a double and replace this by the
1166 // quotient
1167 public void overEquals(double a) {
1168 this.real = this.real / a;
1169 this.imag = this.imag / a;
1170 }
1171
1172 // RECIPROCAL
1173 // Returns the reciprocal (1/a) of a Complex number (a) [static method]
1174 public static Complex inverse(Complex a) {
1175 Complex b = new Complex(0.0D, 0.0D);
1176 if (Complex.infOption && a.isInfinite())
1177 return b;
1178
1179 b = Complex.over(1.0D, a);
1180 return b;
1181 }
1182
1183 // Returns the reciprocal (1/a) of a Complex number (a) [instance method]
1184 public Complex inverse() {
1185 Complex b = new Complex(0.0D, 0.0D);
1186 b = Complex.over(1.0D, this);
1187 return b;
1188 }
1189
1190 // FURTHER MATHEMATICAL FUNCTIONS
1191
1192 // Negates a Complex number [static method]
1193 public static Complex negate(Complex a) {
1194 Complex c = new Complex();
1195 c.real = -a.real;
1196 c.imag = -a.imag;
1197 return c;
1198 }
1199
1200 // Negates a Complex number [instance method]
1201 public Complex negate() {
1202 Complex c = new Complex();
1203 c.real = -this.real;
1204 c.imag = -this.imag;
1205 return c;
1206 }
1207
1208 // Absolute value (modulus) of a complex number [static method]
1209 public static double abs(Complex a) {
1210 double rmod = Math.abs(a.real);
1211 double imod = Math.abs(a.imag);
1212 double ratio = 0.0D;
1213 double res = 0.0D;
1214
1215 if (rmod == 0.0D) {
1216 res = imod;
1217 } else {
1218 if (imod == 0.0D) {
1219 res = rmod;
1220 }
1221 if (rmod >= imod) {
1222 ratio = a.imag / a.real;
1223 res = rmod * Math.sqrt(1.0D + ratio * ratio);
1224 } else {
1225 ratio = a.real / a.imag;
1226 res = imod * Math.sqrt(1.0D + ratio * ratio);
1227 }
1228 }
1229 return res;
1230 }
1231
1232 // Absolute value (modulus) of a complex number [instance method]
1233 public double abs() {
1234 double rmod = Math.abs(this.real);
1235 double imod = Math.abs(this.imag);
1236 double ratio = 0.0D;
1237 double res = 0.0D;
1238
1239 if (rmod == 0.0D) {
1240 res = imod;
1241 } else {
1242 if (imod == 0.0D) {
1243 res = rmod;
1244 }
1245 if (rmod >= imod) {
1246 ratio = this.imag / this.real;
1247 res = rmod * Math.sqrt(1.0D + ratio * ratio);
1248 } else {
1249 ratio = this.real / this.imag;
1250 res = imod * Math.sqrt(1.0D + ratio * ratio);
1251 }
1252 }
1253 return res;
1254 }
1255
1256 // Square of the absolute value (modulus) of a complex number [static
1257 // method]
1258 public static double squareAbs(Complex a) {
1259 return a.real * a.real + a.imag * a.imag;
1260 }
1261
1262 // Square of the absolute value (modulus) of a complex number [instance
1263 // method]
1264 public double squareAbs() {
1265 return this.real * this.real + this.imag * this.imag;
1266 }
1267
1268 // Argument of a complex number (in radians) [static method]
1269 public static double arg(Complex a) {
1270 return Math.atan2(a.imag, a.real);
1271 }
1272
1273 // Argument of a complex number (in radians)[instance method]
1274 public double arg() {
1275 return Math.atan2(this.imag, this.real);
1276 }
1277
1278 // Argument of a complex number (in radians) [static method]
1279 public static double argRad(Complex a) {
1280 return Math.atan2(a.imag, a.real);
1281 }
1282
1283 // Argument of a complex number (in radians)[instance method]
1284 public double argRad() {
1285 return Math.atan2(this.imag, this.real);
1286 }
1287
1288 // Argument of a complex number (in degrees) [static method]
1289 public static double argDeg(Complex a) {
1290 return Math.toDegrees(Math.atan2(a.imag, a.real));
1291 }
1292
1293 // Argument of a complex number (in degrees)[instance method]
1294 public double argDeg() {
1295 return Math.toDegrees(Math.atan2(this.imag, this.real));
1296 }
1297
1298 // Complex conjugate of a complex number [static method]
1299 public static Complex conjugate(Complex a) {
1300 Complex c = new Complex();
1301 c.real = a.real;
1302 c.imag = -a.imag;
1303 return c;
1304 }
1305
1306 // Complex conjugate of a complex number [instance method]
1307 public Complex conjugate() {
1308 Complex c = new Complex();
1309 c.real = this.real;
1310 c.imag = -this.imag;
1311 return c;
1312 }
1313
1314 // Returns the length of the hypotenuse of a and b i.e.
1315 // sqrt(abs(a)*abs(a)+abs(b)*abs(b))
1316 // where a and b are Complex [without unecessary overflow or underflow]
1317 public static double hypot(Complex aa, Complex bb) {
1318 double amod = Complex.abs(aa);
1319 double bmod = Complex.abs(bb);
1320 double cc = 0.0D, ratio = 0.0D;
1321
1322 if (amod == 0.0D) {
1323 cc = bmod;
1324 } else {
1325 if (bmod == 0.0D) {
1326 cc = amod;
1327 } else {
1328 if (amod >= bmod) {
1329 ratio = bmod / amod;
1330 cc = amod * Math.sqrt(1.0 + ratio * ratio);
1331 } else {
1332 ratio = amod / bmod;
1333 cc = bmod * Math.sqrt(1.0 + ratio * ratio);
1334 }
1335 }
1336 }
1337 return cc;
1338 }
1339
1340 // Exponential of a complex number (instance method)
1341 public Complex exp() {
1342 return Complex.exp(this);
1343 }
1344
1345 // Exponential of a complex number (static method)
1346 public static Complex exp(Complex aa) {
1347 Complex z = new Complex();
1348
1349 double a = aa.real;
1350 double b = aa.imag;
1351
1352 if (b == 0.0D) {
1353 z.real = Math.exp(a);
1354 z.imag = 0.0D;
1355 } else {
1356 if (a == 0D) {
1357 z.real = Math.cos(b);
1358 z.imag = Math.sin(b);
1359 } else {
1360 double c = Math.exp(a);
1361 z.real = c * Math.cos(b);
1362 z.imag = c * Math.sin(b);
1363 }
1364 }
1365 return z;
1366 }
1367
1368 // Exponential of a real number returned as a complex number
1369 public static Complex exp(double aa) {
1370 Complex bb = new Complex(aa, 0.0D);
1371 return Complex.exp(bb);
1372 }
1373
1374 // Returns exp(j*arg) where arg is real (a double)
1375 public static Complex expPlusJayArg(double arg) {
1376 Complex argc = new Complex(0.0D, arg);
1377 return Complex.exp(argc);
1378 }
1379
1380 // Returns exp(-j*arg) where arg is real (a double)
1381 public static Complex expMinusJayArg(double arg) {
1382 Complex argc = new Complex(0.0D, -arg);
1383 return Complex.exp(argc);
1384 }
1385
1386 // Principal value of the natural log of an Complex number (instance method)
1387 public Complex log() {
1388
1389 double a = this.real;
1390 double b = this.imag;
1391 Complex c = new Complex();
1392
1393 c.real = Math.log(Complex.abs(this));
1394 c.imag = Math.atan2(b, a);
1395
1396 return c;
1397 }
1398
1399 // Principal value of the natural log of an Complex number
1400 public static Complex log(Complex aa) {
1401
1402 double a = aa.real;
1403 double b = aa.imag;
1404 Complex c = new Complex();
1405
1406 c.real = Math.log(Complex.abs(aa));
1407 c.imag = Math.atan2(b, a);
1408
1409 return c;
1410 }
1411
1412 // Roots
1413 // Principal value of the square root of a complex number (instance method)
1414 public Complex sqrt() {
1415 return Complex.sqrt(this);
1416 }
1417
1418 // Principal value of the square root of a complex number
1419 public static Complex sqrt(Complex aa) {
1420 double a = aa.real;
1421 double b = aa.imag;
1422 Complex c = new Complex();
1423
1424 if (b == 0.0D) {
1425 if (a >= 0.0D) {
1426 c.real = Math.sqrt(a);
1427 c.imag = 0.0D;
1428 } else {
1429 c.real = 0.0D;
1430 c.imag = Math.sqrt(-a);
1431 }
1432 } else {
1433 double w, ratio;
1434 double amod = Math.abs(a);
1435 double bmod = Math.abs(b);
1436 if (amod >= bmod) {
1437 ratio = b / a;
1438 w = Math.sqrt(amod) * Math.sqrt(0.5D * (1.0D + Math.sqrt(1.0D + ratio * ratio)));
1439 } else {
1440 ratio = a / b;
1441 w = Math.sqrt(bmod) * Math.sqrt(0.5D * (Math.abs(ratio) + Math.sqrt(1.0D + ratio * ratio)));
1442 }
1443 if (a >= 0.0) {
1444 c.real = w;
1445 c.imag = b / (2.0D * w);
1446 } else {
1447 if (b >= 0.0) {
1448 c.imag = w;
1449 c.real = b / (2.0D * c.imag);
1450 } else {
1451 c.imag = -w;
1452 c.real = b / (2.0D * c.imag);
1453 }
1454 }
1455 }
1456 return c;
1457 }
1458
1459 // Principal value of the nth root of a complex number (n = integer > 1)
1460 // [instance method]
1461 public Complex nthRoot(int n) {
1462 return Complex.nthRoot(this, n);
1463 }
1464
1465 // Principal value of the nth root of a complex number (n = integer > 1)
1466 // [static method]
1467 public static Complex nthRoot(Complex aa, int n) {
1468 Complex c = new Complex();
1469 if (n == 0) {
1470 c = new Complex(Double.POSITIVE_INFINITY, 0.0);
1471 } else {
1472 if (n == 1) {
1473 c = aa;
1474 } else {
1475 c = Complex.exp((Complex.log(aa)).over((double) n));
1476 }
1477 }
1478
1479 return c;
1480 }
1481
1482 // Powers
1483 // Square of a complex number (static method)
1484 public static Complex square(Complex aa) {
1485 Complex c = new Complex();
1486 c.real = aa.real * aa.real - aa.imag * aa.imag;
1487 c.imag = 2.0D * aa.real * aa.imag;
1488 return c;
1489 }
1490
1491 // Square of a complex number (instance method)
1492 public Complex square() {
1493 return this.times(this);
1494 }
1495
1496 // returns a Complex number raised to a Complex power (instance method)
1497 public Complex pow(Complex b) {
1498 Complex c = new Complex();
1499 if (this.isZero()) {
1500 if (b.imag == 0) {
1501 if (b.real == 0) {
1502 c = new Complex(1.0, 0.0);
1503 } else {
1504 if (b.real > 0.0) {
1505 c = new Complex(0.0, 0.0);
1506 } else {
1507 if (b.real < 0.0) {
1508 c = new Complex(Double.POSITIVE_INFINITY, 0.0);
1509 }
1510 }
1511 }
1512 } else {
1513 c = Complex.exp(b.times(Complex.log(this)));
1514 }
1515 } else {
1516 c = Complex.exp(b.times(Complex.log(this)));
1517 }
1518
1519 return c;
1520 }
1521
1522 // returns a Complex number raised to a Complex power
1523 public static Complex pow(Complex a, Complex b) {
1524 Complex c = new Complex();
1525 if (a.isZero()) {
1526 if (b.imag == 0) {
1527 if (b.real == 0) {
1528 c = new Complex(1.0, 0.0);
1529 } else {
1530 if (a.real > 0.0) {
1531 c = new Complex(0.0, 0.0);
1532 } else {
1533 if (a.real < 0.0) {
1534 c = new Complex(Double.POSITIVE_INFINITY, 0.0);
1535 }
1536 }
1537 }
1538 } else {
1539 c = Complex.exp(b.times(Complex.log(a)));
1540 }
1541 } else {
1542 c = Complex.exp(b.times(Complex.log(a)));
1543 }
1544
1545 return c;
1546 }
1547
1548 // returns a Complex number raised to a double power [instance method]
1549 public Complex pow(double b) {
1550 return powDouble(this, b);
1551 }
1552
1553 // returns a Complex number raised to a double power
1554 public static Complex pow(Complex a, double b) {
1555 return powDouble(a, b);
1556 }
1557
1558 // returns a Complex number raised to an integer, i.e. int, power [instance
1559 // method]
1560 public Complex pow(int n) {
1561 double b = (double) n;
1562 return powDouble(this, b);
1563 }
1564
1565 // returns a Complex number raised to an integer, i.e. int, power
1566 public static Complex pow(Complex a, int n) {
1567 double b = (double) n;
1568 return powDouble(a, b);
1569 }
1570
1571 // returns a double raised to a Complex power
1572 public static Complex pow(double a, Complex b) {
1573 Complex c = new Complex();
1574 if (a == 0) {
1575 if (b.imag == 0) {
1576 if (b.real == 0) {
1577 c = new Complex(1.0, 0.0);
1578 } else {
1579 if (b.real > 0.0) {
1580 c = new Complex(0.0, 0.0);
1581 } else {
1582 if (b.real < 0.0) {
1583 c = new Complex(Double.POSITIVE_INFINITY, 0.0);
1584 }
1585 }
1586 }
1587 } else {
1588 double z = Math.pow(a, b.real);
1589 c = Complex.exp(Complex.times(Complex.plusJay(), b.imag * Math.log(a)));
1590 c = Complex.times(z, c);
1591 }
1592 } else {
1593 double z = Math.pow(a, b.real);
1594 c = Complex.exp(Complex.times(Complex.plusJay(), b.imag * Math.log(a)));
1595 c = Complex.times(z, c);
1596 }
1597
1598 return c;
1599
1600 }
1601
1602 // Complex trigonometric functions
1603
1604 // Sine of an Complex number
1605 public Complex sin() {
1606 return Complex.sin(this);
1607 }
1608
1609 public static Complex sin(Complex aa) {
1610 Complex c = new Complex();
1611 double a = aa.real;
1612 double b = aa.imag;
1613 c.real = Math.sin(a) * Fmath.cosh(b);
1614 c.imag = Math.cos(a) * Fmath.sinh(b);
1615 return c;
1616 }
1617
1618 // Cosine of an Complex number
1619 public Complex cos() {
1620 return Complex.cos(this);
1621 }
1622
1623 public static Complex cos(Complex aa) {
1624 Complex c = new Complex();
1625 double a = aa.real;
1626 double b = aa.imag;
1627 c.real = Math.cos(a) * Fmath.cosh(b);
1628 c.imag = -Math.sin(a) * Fmath.sinh(b);
1629 return c;
1630 }
1631
1632 // Secant of an Complex number
1633 public Complex sec() {
1634 return Complex.sec(this);
1635 }
1636
1637 public static Complex sec(Complex aa) {
1638 Complex c = new Complex();
1639 double a = aa.real;
1640 double b = aa.imag;
1641 c.real = Math.cos(a) * Fmath.cosh(b);
1642 c.imag = -Math.sin(a) * Fmath.sinh(b);
1643 return c.inverse();
1644 }
1645
1646 // Cosecant of an Complex number
1647 public Complex csc() {
1648 return Complex.csc(this);
1649 }
1650
1651 public static Complex csc(Complex aa) {
1652 Complex c = new Complex();
1653 double a = aa.real;
1654 double b = aa.imag;
1655 c.real = Math.sin(a) * Fmath.cosh(b);
1656 c.imag = Math.cos(a) * Fmath.sinh(b);
1657 return c.inverse();
1658 }
1659
1660 // Tangent of an Complex number
1661 public Complex tan() {
1662 return Complex.tan(this);
1663 }
1664
1665 public static Complex tan(Complex aa) {
1666 Complex c = new Complex();
1667 double denom = 0.0D;
1668 double a = aa.real;
1669 double b = aa.imag;
1670
1671 Complex x = new Complex(Math.sin(a) * Fmath.cosh(b), Math.cos(a) * Fmath.sinh(b));
1672 Complex y = new Complex(Math.cos(a) * Fmath.cosh(b), -Math.sin(a) * Fmath.sinh(b));
1673 c = Complex.over(x, y);
1674 return c;
1675 }
1676
1677 // Cotangent of an Complex number
1678 public Complex cot() {
1679 return Complex.cot(this);
1680 }
1681
1682 public static Complex cot(Complex aa) {
1683 Complex c = new Complex();
1684 double denom = 0.0D;
1685 double a = aa.real;
1686 double b = aa.imag;
1687
1688 Complex x = new Complex(Math.sin(a) * Fmath.cosh(b), Math.cos(a) * Fmath.sinh(b));
1689 Complex y = new Complex(Math.cos(a) * Fmath.cosh(b), -Math.sin(a) * Fmath.sinh(b));
1690 c = Complex.over(y, x);
1691 return c;
1692 }
1693
1694 // Exsecant of an Complex number
1695 public Complex exsec() {
1696 return Complex.exsec(this);
1697 }
1698
1699 public static Complex exsec(Complex aa) {
1700 return Complex.sec(aa).minus(1.0D);
1701 }
1702
1703 // Versine of an Complex number
1704 public Complex vers() {
1705 return Complex.vers(this);
1706 }
1707
1708 public static Complex vers(Complex aa) {
1709 return Complex.plusOne().minus(Complex.cos(aa));
1710 }
1711
1712 // Coversine of an Complex number
1713 public Complex covers() {
1714 return Complex.covers(this);
1715 }
1716
1717 public static Complex covers(Complex aa) {
1718 return Complex.plusOne().minus(Complex.sin(aa));
1719 }
1720
1721 // Haversine of an Complex number
1722 public Complex hav() {
1723 return Complex.hav(this);
1724 }
1725
1726 public static Complex hav(Complex aa) {
1727 return Complex.vers(aa).over(2.0D);
1728 }
1729
1730 // Hyperbolic sine of a Complex number
1731 public Complex sinh() {
1732 return Complex.sinh(this);
1733 }
1734
1735 public static Complex sinh(Complex a) {
1736 Complex c = new Complex();
1737 c = a.times(plusJay());
1738 c = (Complex.minusJay()).times(Complex.sin(c));
1739 return c;
1740 }
1741
1742 // Hyperbolic cosine of a Complex number
1743 public Complex cosh() {
1744 return Complex.cosh(this);
1745 }
1746
1747 public static Complex cosh(Complex a) {
1748 Complex c = new Complex();
1749 c = a.times(Complex.plusJay());
1750 c = Complex.cos(c);
1751 return c;
1752 }
1753
1754 // Hyperbolic tangent of a Complex number
1755 public Complex tanh() {
1756 return Complex.tanh(this);
1757 }
1758
1759 public static Complex tanh(Complex a) {
1760 Complex c = new Complex();
1761 c = (Complex.sinh(a)).over(Complex.cosh(a));
1762 return c;
1763 }
1764
1765 // Hyperbolic cotangent of a Complex number
1766 public Complex coth() {
1767 return Complex.coth(this);
1768 }
1769
1770 public static Complex coth(Complex a) {
1771 Complex c = new Complex();
1772 c = (Complex.cosh(a)).over(Complex.sinh(a));
1773 return c;
1774 }
1775
1776 // Hyperbolic secant of a Complex number
1777 public Complex sech() {
1778 return Complex.sech(this);
1779 }
1780
1781 public static Complex sech(Complex a) {
1782 Complex c = new Complex();
1783 c = (Complex.cosh(a)).inverse();
1784 return c;
1785 }
1786
1787 // Hyperbolic cosecant of a Complex number
1788 public Complex csch() {
1789 return Complex.csch(this);
1790 }
1791
1792 public static Complex csch(Complex a) {
1793 Complex c = new Complex();
1794 c = (Complex.sinh(a)).inverse();
1795 return c;
1796 }
1797
1798 // Inverse sine of a Complex number
1799 public Complex asin() {
1800 return Complex.asin(this);
1801 }
1802
1803 public static Complex asin(Complex a) {
1804 Complex c = new Complex();
1805 c = Complex.sqrt(Complex.minus(1.0D, Complex.square(a)));
1806 c = (Complex.plusJay().times(a)).plus(c);
1807 c = Complex.minusJay().times(Complex.log(c));
1808 return c;
1809 }
1810
1811 // Inverse cosine of a Complex number
1812 public Complex acos() {
1813 return Complex.acos(this);
1814 }
1815
1816 public static Complex acos(Complex a) {
1817 Complex c = new Complex();
1818 c = Complex.sqrt(Complex.minus(Complex.square(a), 1.0));
1819 c = a.plus(c);
1820 c = Complex.minusJay().times(Complex.log(c));
1821 return c;
1822 }
1823
1824 // Inverse tangent of a Complex number
1825 public Complex atan() {
1826 return Complex.atan(this);
1827 }
1828
1829 public static Complex atan(Complex a) {
1830 Complex c = new Complex();
1831 Complex d = new Complex();
1832
1833 c = Complex.plusJay().plus(a);
1834 d = Complex.plusJay().minus(a);
1835 c = c.over(d);
1836 c = Complex.log(c);
1837 c = Complex.plusJay().times(c);
1838 c = c.over(2.0D);
1839 return c;
1840 }
1841
1842 // Inverse cotangent of a Complex number
1843 public Complex acot() {
1844 return Complex.acot(this);
1845 }
1846
1847 public static Complex acot(Complex a) {
1848 return Complex.atan(a.inverse());
1849 }
1850
1851 // Inverse secant of a Complex number
1852 public Complex asec() {
1853 return Complex.asec(this);
1854 }
1855
1856 public static Complex asec(Complex a) {
1857 return Complex.acos(a.inverse());
1858 }
1859
1860 // Inverse cosecant of a Complex number
1861 public Complex acsc() {
1862 return Complex.acsc(this);
1863 }
1864
1865 public static Complex acsc(Complex a) {
1866 return Complex.asin(a.inverse());
1867 }
1868
1869 // Inverse exsecant of a Complex number
1870 public Complex aexsec() {
1871 return Complex.aexsec(this);
1872 }
1873
1874 public static Complex aexsec(Complex a) {
1875 Complex c = a.plus(1.0D);
1876 return Complex.asin(c.inverse());
1877 }
1878
1879 // Inverse versine of a Complex number
1880 public Complex avers() {
1881 return Complex.avers(this);
1882 }
1883
1884 public static Complex avers(Complex a) {
1885 Complex c = Complex.plusOne().plus(a);
1886 return Complex.acos(c);
1887 }
1888
1889 // Inverse coversine of a Complex number
1890 public Complex acovers() {
1891 return Complex.acovers(this);
1892 }
1893
1894 public static Complex acovers(Complex a) {
1895 Complex c = Complex.plusOne().plus(a);
1896 return Complex.asin(c);
1897 }
1898
1899 // Inverse haversine of a Complex number
1900 public Complex ahav() {
1901 return Complex.ahav(this);
1902 }
1903
1904 public static Complex ahav(Complex a) {
1905 Complex c = Complex.plusOne().minus(a.times(2.0D));
1906 return Complex.acos(c);
1907 }
1908
1909 // Inverse hyperbolic sine of a Complex number
1910 public Complex asinh() {
1911 return Complex.asinh(this);
1912 }
1913
1914 public static Complex asinh(Complex a) {
1915 Complex c = new Complex(0.0D, 0.0D);
1916 c = Complex.sqrt(Complex.square(a).plus(1.0D));
1917 c = a.plus(c);
1918 c = Complex.log(c);
1919
1920 return c;
1921 }
1922
1923 // Inverse hyperbolic cosine of a Complex number
1924 public Complex acosh() {
1925 return Complex.acosh(this);
1926 }
1927
1928 public static Complex acosh(Complex a) {
1929 Complex c = new Complex();
1930 c = Complex.sqrt(Complex.square(a).minus(1.0D));
1931 c = a.plus(c);
1932 c = Complex.log(c);
1933 return c;
1934 }
1935
1936 // Inverse hyperbolic tangent of a Complex number
1937 public Complex atanh() {
1938 return Complex.atanh(this);
1939 }
1940
1941 public static Complex atanh(Complex a) {
1942 Complex c = new Complex();
1943 Complex d = new Complex();
1944 c = Complex.plusOne().plus(a);
1945 d = Complex.plusOne().minus(a);
1946 c = c.over(d);
1947 c = Complex.log(c);
1948 c = c.over(2.0D);
1949 return c;
1950 }
1951
1952 // Inverse hyperbolic cotangent of a Complex number
1953 public Complex acoth() {
1954 return Complex.acoth(this);
1955 }
1956
1957 public static Complex acoth(Complex a) {
1958 Complex c = new Complex();
1959 Complex d = new Complex();
1960 c = Complex.plusOne().plus(a);
1961 d = a.plus(1.0D);
1962 c = c.over(d);
1963 c = Complex.log(c);
1964 c = c.over(2.0D);
1965 return c;
1966 }
1967
1968 // Inverse hyperbolic secant of a Complex number
1969 public Complex asech() {
1970 return Complex.asech(this);
1971 }
1972
1973 public static Complex asech(Complex a) {
1974 Complex c = a.inverse();
1975 Complex d = (Complex.square(a)).minus(1.0D);
1976 return Complex.log(c.plus(Complex.sqrt(d)));
1977 }
1978
1979 // Inverse hyperbolic cosecant of a Complex number
1980 public Complex acsch() {
1981 return Complex.acsch(this);
1982 }
1983
1984 public static Complex acsch(Complex a) {
1985 Complex c = a.inverse();
1986 Complex d = (Complex.square(a)).plus(1.0D);
1987 return Complex.log(c.plus(Complex.sqrt(d)));
1988 }
1989
1990 // LOGICAL FUNCTIONS
1991 // Returns true if the Complex number has a zero imaginary part, i.e. is a
1992 // real number
1993 public static boolean isReal(Complex a) {
1994 boolean test = false;
1995 if (Math.abs(a.imag) == 0.0D)
1996 test = true;
1997 return test;
1998 }
1999
2000 public static boolean isReal(Complex[] a) {
2001 boolean test = true;
2002 int n = a.length;
2003 for (int i = 0; i < n; i++) {
2004 if (Math.abs(a[i].imag) != 0.0D)
2005 test = false;
2006 }
2007 return test;
2008 }
2009
2010 public boolean isReal() {
2011 boolean test = false;
2012 if (Math.abs(this.imag) == 0.0D)
2013 test = true;
2014 return test;
2015 }
2016
2017 // Returns true if the Complex number has a zero imaginary part within the
2018 // limit lim, i.e. is a real number
2019 public static boolean isReal(Complex a, double lim) {
2020 boolean test = false;
2021 if (Math.abs(a.imag) <= Math.abs(lim))
2022 test = true;
2023 return test;
2024 }
2025
2026 public static boolean isReal(Complex[] a, double lim) {
2027 boolean test = true;
2028 int n = a.length;
2029 for (int i = 0; i < n; i++) {
2030 if (Math.abs(a[i].imag) > Math.abs(lim))
2031 test = false;
2032 }
2033 return test;
2034 }
2035
2036 public boolean isReal(double lim) {
2037 boolean test = false;
2038 if (Math.abs(this.imag) <= Math.abs(lim))
2039 test = true;
2040 return test;
2041 }
2042
2043 // Returns true if the Complex number has a zero imaginary part less than
2044 // the percentage precent ofv the real part
2045 public static boolean isRealPerCent(Complex a, double percent) {
2046 boolean test = false;
2047 if (Math.abs(a.imag * 100.0 / a.real) <= Math.abs(percent))
2048 test = true;
2049 return test;
2050 }
2051
2052 public static boolean isRealPerCent(Complex[] a, double percent) {
2053 boolean test = true;
2054 int n = a.length;
2055 for (int i = 0; i < n; i++) {
2056 if (Math.abs(a[i].imag * 100.0 / a[i].real) > Math.abs(percent))
2057 test = false;
2058 }
2059 return test;
2060 }
2061
2062 public boolean isRealperCent(double percent) {
2063 boolean test = false;
2064 if (Math.abs(this.imag * 100.0 / this.real) <= Math.abs(percent))
2065 test = true;
2066 return test;
2067 }
2068
2069 // Returns true if the Complex number has a zero real and a zero imaginary
2070 // part
2071 // i.e. has a zero modulus
2072 public static boolean isZero(Complex a) {
2073 boolean test = false;
2074 if (Math.abs(a.real) == 0.0D && Math.abs(a.imag) == 0.0D)
2075 test = true;
2076 return test;
2077 }
2078
2079 public boolean isZero() {
2080 boolean test = false;
2081 if (Math.abs(this.real) == 0.0D && Math.abs(this.imag) == 0.0D)
2082 test = true;
2083 return test;
2084 }
2085
2086 // Returns true if either the real or the imaginary part of the Complex
2087 // number
2088 // is equal to plus infinity
2089 public boolean isPlusInfinity() {
2090 boolean test = false;
2091 if (this.real == Double.POSITIVE_INFINITY || this.imag == Double.POSITIVE_INFINITY)
2092 test = true;
2093 return test;
2094 }
2095
2096 public static boolean isPlusInfinity(Complex a) {
2097 boolean test = false;
2098 if (a.real == Double.POSITIVE_INFINITY || a.imag == Double.POSITIVE_INFINITY)
2099 test = true;
2100 return test;
2101 }
2102
2103 // Returns true if either the real or the imaginary part of the Complex
2104 // number
2105 // is equal to minus infinity
2106 public boolean isMinusInfinity() {
2107 boolean test = false;
2108 if (this.real == Double.NEGATIVE_INFINITY || this.imag == Double.NEGATIVE_INFINITY)
2109 test = true;
2110 return test;
2111 }
2112
2113 public static boolean isMinusInfinity(Complex a) {
2114 boolean test = false;
2115 if (a.real == Double.NEGATIVE_INFINITY || a.imag == Double.NEGATIVE_INFINITY)
2116 test = true;
2117 return test;
2118 }
2119
2120 // Returns true if either the real or the imaginary part of the Complex
2121 // number
2122 // is equal to either infinity or minus plus infinity
2123 public static boolean isInfinite(Complex a) {
2124 boolean test = false;
2125 if (a.real == Double.POSITIVE_INFINITY || a.imag == Double.POSITIVE_INFINITY)
2126 test = true;
2127 if (a.real == Double.NEGATIVE_INFINITY || a.imag == Double.NEGATIVE_INFINITY)
2128 test = true;
2129 return test;
2130 }
2131
2132 public boolean isInfinite() {
2133 boolean test = false;
2134 if (this.real == Double.POSITIVE_INFINITY || this.imag == Double.POSITIVE_INFINITY)
2135 test = true;
2136 if (this.real == Double.NEGATIVE_INFINITY || this.imag == Double.NEGATIVE_INFINITY)
2137 test = true;
2138 return test;
2139 }
2140
2141 // Returns true if the Complex number is NaN (Not a Number)
2142 // i.e. is the result of an uninterpretable mathematical operation
2143 public static boolean isNaN(Complex a) {
2144 boolean test = false;
2145 if (a.real != a.real || a.imag != a.imag)
2146 test = true;
2147 return test;
2148 }
2149
2150 public boolean isNaN() {
2151 boolean test = false;
2152 if (this.real != this.real || this.imag != this.imag)
2153 test = true;
2154 return test;
2155 }
2156
2157 // Returns true if two Complex number are identical
2158 // Follows the Sun Java convention of treating all NaNs as equal
2159 // i.e. does not satisfies the IEEE 754 specification
2160 // but does let hashtables operate properly
2161 public boolean equals(Complex a) {
2162 boolean test = false;
2163 if (this.isNaN() && a.isNaN()) {
2164 test = true;
2165 } else {
2166 if (this.real == a.real && this.imag == a.imag)
2167 test = true;
2168 }
2169 return test;
2170 }
2171
2172 public boolean isEqual(Complex a) {
2173 boolean test = false;
2174 if (this.isNaN() && a.isNaN()) {
2175 test = true;
2176 } else {
2177 if (this.real == a.real && this.imag == a.imag)
2178 test = true;
2179 }
2180 return test;
2181 }
2182
2183 public static boolean isEqual(Complex a, Complex b) {
2184 boolean test = false;
2185 if (isNaN(a) && isNaN(b)) {
2186 test = true;
2187 } else {
2188 if (a.real == b.real && a.imag == b.imag)
2189 test = true;
2190 }
2191 return test;
2192 }
2193
2194 // returns true if the differences between the real and imaginary parts of
2195 // two complex numbers
2196 // are less than fract times the larger real and imaginary part
2197 public boolean equalsWithinLimits(Complex a, double fract) {
2198 return isEqualWithinLimits(a, fract);
2199 }
2200
2201 public boolean isEqualWithinLimits(Complex a, double fract) {
2202 boolean test = false;
2203
2204 double rt = this.getReal();
2205 double ra = a.getReal();
2206 double it = this.getImag();
2207 double ia = a.getImag();
2208 double rdn = 0.0D;
2209 double idn = 0.0D;
2210 double rtest = 0.0D;
2211 double itest = 0.0D;
2212
2213 if (rt == 0.0D && it == 0.0D && ra == 0.0D && ia == 0.0D)
2214 test = true;
2215 if (!test) {
2216 rdn = Math.abs(rt);
2217 if (Math.abs(ra) > rdn)
2218 rdn = Math.abs(ra);
2219 if (rdn == 0.0D) {
2220 rtest = 0.0;
2221 } else {
2222 rtest = Math.abs(ra - rt) / rdn;
2223 }
2224 idn = Math.abs(it);
2225 if (Math.abs(ia) > idn)
2226 idn = Math.abs(ia);
2227 if (idn == 0.0D) {
2228 itest = 0.0;
2229 } else {
2230 itest = Math.abs(ia - it) / idn;
2231 }
2232 if (rtest < fract && itest < fract)
2233 test = true;
2234 }
2235
2236 return test;
2237 }
2238
2239 public static boolean isEqualWithinLimits(Complex a, Complex b, double fract) {
2240 boolean test = false;
2241
2242 double rb = b.getReal();
2243 double ra = a.getReal();
2244 double ib = b.getImag();
2245 double ia = a.getImag();
2246 double rdn = 0.0D;
2247 double idn = 0.0D;
2248
2249 if (ra == 0.0D && ia == 0.0D && rb == 0.0D && ib == 0.0D)
2250 test = true;
2251 if (!test) {
2252 rdn = Math.abs(rb);
2253 if (Math.abs(ra) > rdn)
2254 rdn = Math.abs(ra);
2255 idn = Math.abs(ib);
2256 if (Math.abs(ia) > idn)
2257 idn = Math.abs(ia);
2258 if (Math.abs(ra - rb) / rdn < fract && Math.abs(ia - ia) / idn < fract)
2259 test = true;
2260 }
2261
2262 return test;
2263 }
2264
2265 // SOME USEFUL NUMBERS
2266 // returns the number zero (0) as a complex number
2267 public static Complex zero() {
2268 Complex c = new Complex();
2269 c.real = 0.0D;
2270 c.imag = 0.0D;
2271 return c;
2272 }
2273
2274 // returns the number one (+1) as a complex number
2275 public static Complex plusOne() {
2276 Complex c = new Complex();
2277 c.real = 1.0D;
2278 c.imag = 0.0D;
2279 return c;
2280 }
2281
2282 // returns the number minus one (-1) as a complex number
2283 public static Complex minusOne() {
2284 Complex c = new Complex();
2285 c.real = -1.0D;
2286 c.imag = 0.0D;
2287 return c;
2288 }
2289
2290 // returns plus j
2291 public static Complex plusJay() {
2292 Complex c = new Complex();
2293 c.real = 0.0D;
2294 c.imag = 1.0D;
2295 return c;
2296 }
2297
2298 // returns minus j
2299 public static Complex minusJay() {
2300 Complex c = new Complex();
2301 c.real = 0.0D;
2302 c.imag = -1.0D;
2303 return c;
2304 }
2305
2306 // returns pi as a Complex number
2307 public static Complex pi() {
2308 Complex c = new Complex();
2309 c.real = Math.PI;
2310 c.imag = 0.0D;
2311 return c;
2312 }
2313
2314 // returns 2.pi.j
2315 public static Complex twoPiJay() {
2316 Complex c = new Complex();
2317 c.real = 0.0D;
2318 c.imag = 2.0D * Math.PI;
2319 return c;
2320 }
2321
2322 // infinity + infinity.j
2323 public static Complex plusInfinity() {
2324 Complex c = new Complex();
2325 c.real = Double.POSITIVE_INFINITY;
2326 c.imag = Double.POSITIVE_INFINITY;
2327 return c;
2328 }
2329
2330 // -infinity - infinity.j
2331 public static Complex minusInfinity() {
2332 Complex c = new Complex();
2333 c.real = Double.NEGATIVE_INFINITY;
2334 c.imag = Double.NEGATIVE_INFINITY;
2335 return c;
2336 }
2337
2338 // PRIVATE METHODS
2339 // returns a Complex number raised to a double power
2340 // this method is used for calculation within this class file
2341 // see above for corresponding public method
2342 private static Complex powDouble(Complex a, double b) {
2343 Complex z = new Complex();
2344 double re = a.real;
2345 double im = a.imag;
2346
2347 if (a.isZero()) {
2348 if (b == 0.0) {
2349 z = new Complex(1.0, 0.0);
2350 } else {
2351 if (b > 0.0) {
2352 z = new Complex(0.0, 0.0);
2353 } else {
2354 if (b < 0.0) {
2355 z = new Complex(Double.POSITIVE_INFINITY, 0.0);
2356 }
2357 }
2358 }
2359 } else {
2360 if (im == 0.0D && re > 0.0D) {
2361 z.real = Math.pow(re, b);
2362 z.imag = 0.0D;
2363 } else {
2364 if (re == 0.0D) {
2365 z = Complex.exp(Complex.times(b, Complex.log(a)));
2366 } else {
2367 double c = Math.pow(re * re + im * im, b / 2.0D);
2368 double th = Math.atan2(im, re);
2369 z.real = c * Math.cos(b * th);
2370 z.imag = c * Math.sin(b * th);
2371 }
2372 }
2373 }
2374 return z;
2375 }
2376
2377}
Note: See TracBrowser for help on using the repository browser.