001package algs12; 002import stdlib.*; 003/* *********************************************************************** 004 * Compilation: javac Complex.java 005 * Execution: java Complex 006 * 007 * Data type for complex numbers. 008 * 009 * The data type is "immutable" so once you create and initialize 010 * a Complex object, you cannot change it. The "final" keyword 011 * when declaring re and im enforces this rule, making it a 012 * compile-time error to change the .re or .im fields after 013 * they've been initialized. 014 * 015 * % java Complex 016 * a = 5.0 + 6.0i 017 * b = -3.0 + 4.0i 018 * Re(a) = 5.0 019 * Im(a) = 6.0 020 * b + a = 2.0 + 10.0i 021 * a - b = 8.0 + 2.0i 022 * a * b = -39.0 + 2.0i 023 * b * a = -39.0 + 2.0i 024 * a / b = 0.36 - 1.52i 025 * (a / b) * b = 5.0 + 6.0i 026 * conj(a) = 5.0 - 6.0i 027 * |a| = 7.810249675906654 028 * tan(a) = -6.685231390246571E-6 + 1.0000103108981198i 029 * 030 *************************************************************************/ 031 032public class Complex { 033 private final double re; // the real part 034 private final double im; // the imaginary part 035 036 // create a new object with the given real and imaginary parts 037 public Complex(double real, double imag) { 038 re = real; 039 im = imag; 040 } 041 042 // return a string representation of the invoking Complex object 043 public String toString() { 044 if (im == 0) return re + ""; 045 if (re == 0) return im + "i"; 046 if (im < 0) return re + " - " + (-im) + "i"; 047 return re + " + " + im + "i"; 048 } 049 050 // return abs/modulus/magnitude and angle/phase/argument 051 public double abs() { return Math.hypot(re, im); } // Math.sqrt(re*re + im*im) 052 public double phase() { return Math.atan2(im, re); } // between -pi and pi 053 054 // return a new Complex object whose value is (this + b) 055 public Complex plus(Complex b) { 056 Complex a = this; // invoking object 057 double real = a.re + b.re; 058 double imag = a.im + b.im; 059 return new Complex(real, imag); 060 } 061 062 // return a new Complex object whose value is (this - b) 063 public Complex minus(Complex b) { 064 Complex a = this; 065 double real = a.re - b.re; 066 double imag = a.im - b.im; 067 return new Complex(real, imag); 068 } 069 070 // return a new Complex object whose value is (this * b) 071 public Complex times(Complex b) { 072 Complex a = this; 073 double real = a.re * b.re - a.im * b.im; 074 double imag = a.re * b.im + a.im * b.re; 075 return new Complex(real, imag); 076 } 077 078 // scalar multiplication 079 // return a new object whose value is (this * alpha) 080 public Complex times(double alpha) { 081 return new Complex(alpha * re, alpha * im); 082 } 083 084 // return a new Complex object whose value is the conjugate of this 085 public Complex conjugate() { return new Complex(re, -im); } 086 087 // return a new Complex object whose value is the reciprocal of this 088 public Complex reciprocal() { 089 double scale = re*re + im*im; 090 return new Complex(re / scale, -im / scale); 091 } 092 093 // return the real or imaginary part 094 public double re() { return re; } 095 public double im() { return im; } 096 097 // return a / b 098 public Complex divides(Complex b) { 099 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 StdOut.println("a = " + a); 141 StdOut.println("b = " + b); 142 StdOut.println("Re(a) = " + a.re()); 143 StdOut.println("Im(a) = " + a.im()); 144 StdOut.println("b + a = " + b.plus(a)); 145 StdOut.println("a - b = " + a.minus(b)); 146 StdOut.println("a * b = " + a.times(b)); 147 StdOut.println("b * a = " + b.times(a)); 148 StdOut.println("a / b = " + a.divides(b)); 149 StdOut.println("(a / b) * b = " + a.divides(b).times(b)); 150 StdOut.println("conj(a) = " + a.conjugate()); 151 StdOut.println("|a| = " + a.abs()); 152 StdOut.println("tan(a) = " + a.tan()); 153 } 154 155}