1+ /*************************************************************************
2+ * From: http://introcs.cs.princeton.edu/java/home/ (Section 9.7)
3+ *
4+ * Compilation: javac Complex.java
5+ * Execution: java Complex
6+ *
7+ * Data type for complex numbers.
8+ *
9+ * The data type is "immutable" so once you create and initialize
10+ * a Complex object, you cannot change it. The "final" keyword
11+ * when declaring re and im enforces this rule, making it a
12+ * compile-time error to change the .re or .im fields after
13+ * they've been initialized.
14+ *
15+ * % java Complex
16+ * a = 5.0 + 6.0i
17+ * b = -3.0 + 4.0i
18+ * Re(a) = 5.0
19+ * Im(a) = 6.0
20+ * b + a = 2.0 + 10.0i
21+ * a - b = 8.0 + 2.0i
22+ * a * b = -39.0 + 2.0i
23+ * b * a = -39.0 + 2.0i
24+ * a / b = 0.36 - 1.52i
25+ * (a / b) * b = 5.0 + 6.0i
26+ * conj(a) = 5.0 - 6.0i
27+ * |a| = 7.810249675906654
28+ * tan(a) = -6.685231390246571E-6 + 1.0000103108981198i
29+ *
30+ *************************************************************************/
31+
32+ public class Complex {
33+ private final double re ; // the real part
34+ private final double im ; // the imaginary part
35+
36+ // create a new object with the given real and imaginary parts
37+ public Complex (double real , double imag ) {
38+ re = real ;
39+ im = imag ;
40+ }
41+
42+ // return a string representation of the invoking Complex object
43+ public String toString () {
44+ if (im == 0 ) return re + "" ;
45+ if (re == 0 ) return im + "i" ;
46+ if (im < 0 ) return re + " - " + (-im ) + "i" ;
47+ return re + " + " + im + "i" ;
48+ }
49+
50+ // return abs/modulus/magnitude and angle/phase/argument
51+ public double abs () { return Math .hypot (re , im ); } // Math.sqrt(re*re + im*im)
52+ public double phase () { return Math .atan2 (im , re ); } // between -pi and pi
53+
54+ // return a new Complex object whose value is (this + b)
55+ public Complex plus (Complex b ) {
56+ Complex a = this ; // invoking object
57+ double real = a .re + b .re ;
58+ double imag = a .im + b .im ;
59+ return new Complex (real , imag );
60+ }
61+
62+ // return a new Complex object whose value is (this - b)
63+ public Complex minus (Complex b ) {
64+ Complex a = this ;
65+ double real = a .re - b .re ;
66+ double imag = a .im - b .im ;
67+ return new Complex (real , imag );
68+ }
69+
70+ // return a new Complex object whose value is (this * b)
71+ public Complex times (Complex b ) {
72+ Complex a = this ;
73+ double real = a .re * b .re - a .im * b .im ;
74+ double imag = a .re * b .im + a .im * b .re ;
75+ return new Complex (real , imag );
76+ }
77+
78+ // scalar multiplication
79+ // return a new object whose value is (this * alpha)
80+ public Complex times (double alpha ) {
81+ return new Complex (alpha * re , alpha * im );
82+ }
83+
84+ // return a new Complex object whose value is the conjugate of this
85+ public Complex conjugate () { return new Complex (re , -im ); }
86+
87+ // return a new Complex object whose value is the reciprocal of this
88+ public Complex reciprocal () {
89+ double scale = re *re + im *im ;
90+ return new Complex (re / scale , -im / scale );
91+ }
92+
93+ // return the real or imaginary part
94+ public double re () { return re ; }
95+ public double im () { return im ; }
96+
97+ // return a / b
98+ public Complex divides (Complex b ) {
99+ Complex a = this ;
100+ return a .times (b .reciprocal ());
101+ }
102+
103+ // return a new Complex object whose value is the complex exponential of this
104+ public Complex exp () {
105+ return new Complex (Math .exp (re ) * Math .cos (im ), Math .exp (re ) * Math .sin (im ));
106+ }
107+
108+ // return a new Complex object whose value is the complex sine of this
109+ public Complex sin () {
110+ return new Complex (Math .sin (re ) * Math .cosh (im ), Math .cos (re ) * Math .sinh (im ));
111+ }
112+
113+ // return a new Complex object whose value is the complex cosine of this
114+ public Complex cos () {
115+ return new Complex (Math .cos (re ) * Math .cosh (im ), -Math .sin (re ) * Math .sinh (im ));
116+ }
117+
118+ // return a new Complex object whose value is the complex tangent of this
119+ public Complex tan () {
120+ return sin ().divides (cos ());
121+ }
122+
123+
124+
125+ // a static version of plus
126+ public static Complex plus (Complex a , Complex b ) {
127+ double real = a .re + b .re ;
128+ double imag = a .im + b .im ;
129+ Complex sum = new Complex (real , imag );
130+ return sum ;
131+ }
132+
133+
134+
135+ // sample client for testing
136+ public static void main (String [] args ) {
137+ Complex a = new Complex (5.0 , 6.0 );
138+ Complex b = new Complex (-3.0 , 4.0 );
139+
140+ System .out .println ("a = " + a );
141+ System .out .println ("b = " + b );
142+ System .out .println ("Re(a) = " + a .re ());
143+ System .out .println ("Im(a) = " + a .im ());
144+ System .out .println ("b + a = " + b .plus (a ));
145+ System .out .println ("a - b = " + a .minus (b ));
146+ System .out .println ("a * b = " + a .times (b ));
147+ System .out .println ("b * a = " + b .times (a ));
148+ System .out .println ("a / b = " + a .divides (b ));
149+ System .out .println ("(a / b) * b = " + a .divides (b ).times (b ));
150+ System .out .println ("conj(a) = " + a .conjugate ());
151+ System .out .println ("|a| = " + a .abs ());
152+ }
153+
154+ }
0 commit comments