Complex.java (6016B)
1 package be.tarsos.dsp.util; 2 3 4 5 /** 6 * Complex implements a complex number and defines complex 7 arithmetic and mathematical functions 8 Last Updated February 27, 2001 9 Copyright 1997-2001 10 @version 1.0 11 @author Andrew G. Bennett 12 * @author joren 13 * 14 */ 15 public class Complex { 16 17 private double x,y; 18 19 /** 20 Constructs the complex number z = u + i*v 21 @param u Real part 22 @param v Imaginary part 23 */ 24 public Complex(double u,double v) { 25 x=u; 26 y=v; 27 } 28 29 /** 30 Real part of this Complex number 31 (the x-coordinate in rectangular coordinates). 32 @return Re[z] where z is this Complex number. 33 */ 34 public double real() { 35 return x; 36 } 37 38 /** 39 Imaginary part of this Complex number 40 (the y-coordinate in rectangular coordinates). 41 @return Im[z] where z is this Complex number. 42 */ 43 public double imag() { 44 return y; 45 } 46 47 /** 48 Modulus of this Complex number 49 (the distance from the origin in polar coordinates). 50 @return |z| where z is this Complex number. 51 */ 52 public double mod() { 53 if (x!=0 || y!=0) { 54 return Math.sqrt(x*x+y*y); 55 } else { 56 return 0d; 57 } 58 } 59 60 /** 61 Argument of this Complex number 62 (the angle in radians with the x-axis in polar coordinates). 63 @return arg(z) where z is this Complex number. 64 */ 65 public double arg() { 66 return Math.atan2(y,x); 67 } 68 69 /** 70 Complex conjugate of this Complex number 71 (the conjugate of x+i*y is x-i*y). 72 @return z-bar where z is this Complex number. 73 */ 74 public Complex conj() { 75 return new Complex(x,-y); 76 } 77 78 /** 79 Addition of Complex numbers (doesn't change this Complex number). 80 <br>(x+i*y) + (s+i*t) = (x+s)+i*(y+t). 81 @param w is the number to add. 82 @return z+w where z is this Complex number. 83 */ 84 public Complex plus(Complex w) { 85 return new Complex(x+w.real(),y+w.imag()); 86 } 87 88 /** 89 Subtraction of Complex numbers (doesn't change this Complex number). 90 <br>(x+i*y) - (s+i*t) = (x-s)+i*(y-t). 91 @param w is the number to subtract. 92 @return z-w where z is this Complex number. 93 */ 94 public Complex minus(Complex w) { 95 return new Complex(x-w.real(),y-w.imag()); 96 } 97 98 /** 99 Complex multiplication (doesn't change this Complex number). 100 @param w is the number to multiply by. 101 @return z*w where z is this Complex number. 102 */ 103 public Complex times(Complex w) { 104 return new Complex(x*w.real()-y*w.imag(),x*w.imag()+y*w.real()); 105 } 106 107 /** 108 Division of Complex numbers (doesn't change this Complex number). 109 <br>(x+i*y)/(s+i*t) = ((x*s+y*t) + i*(y*s-y*t)) / (s^2+t^2) 110 @param w is the number to divide by 111 @return new Complex number z/w where z is this Complex number 112 */ 113 public Complex div(Complex w) { 114 double den=Math.pow(w.mod(),2); 115 return new Complex((x*w.real()+y*w.imag())/den,(y*w.real()-x*w.imag())/den); 116 } 117 118 /** 119 Complex exponential (doesn't change this Complex number). 120 @return exp(z) where z is this Complex number. 121 */ 122 public Complex exp() { 123 return new Complex(Math.exp(x)*Math.cos(y),Math.exp(x)*Math.sin(y)); 124 } 125 126 /** 127 Principal branch of the Complex logarithm of this Complex number. 128 (doesn't change this Complex number). 129 The principal branch is the branch with -pi < arg <= pi. 130 @return log(z) where z is this Complex number. 131 */ 132 public Complex log() { 133 return new Complex(Math.log(this.mod()),this.arg()); 134 } 135 136 /** 137 Complex square root (doesn't change this complex number). 138 Computes the principal branch of the square root, which 139 is the value with 0 <= arg < pi. 140 @return sqrt(z) where z is this Complex number. 141 */ 142 public Complex sqrt() { 143 double r=Math.sqrt(this.mod()); 144 double theta=this.arg()/2; 145 return new Complex(r*Math.cos(theta),r*Math.sin(theta)); 146 } 147 148 // Real cosh function (used to compute complex trig functions) 149 private double cosh(double theta) { 150 return (Math.exp(theta)+Math.exp(-theta))/2; 151 } 152 153 // Real sinh function (used to compute complex trig functions) 154 private double sinh(double theta) { 155 return (Math.exp(theta)-Math.exp(-theta))/2; 156 } 157 158 /** 159 Sine of this Complex number (doesn't change this Complex number). 160 <br>sin(z) = (exp(i*z)-exp(-i*z))/(2*i). 161 @return sin(z) where z is this Complex number. 162 */ 163 public Complex sin() { 164 return new Complex(cosh(y)*Math.sin(x),sinh(y)*Math.cos(x)); 165 } 166 167 /** 168 Cosine of this Complex number (doesn't change this Complex number). 169 <br>cos(z) = (exp(i*z)+exp(-i*z))/ 2. 170 @return cos(z) where z is this Complex number. 171 */ 172 public Complex cos() { 173 return new Complex(cosh(y)*Math.cos(x),-sinh(y)*Math.sin(x)); 174 } 175 176 /** 177 Hyperbolic sine of this Complex number 178 (doesn't change this Complex number). 179 <br>sinh(z) = (exp(z)-exp(-z))/2. 180 @return sinh(z) where z is this Complex number. 181 */ 182 public Complex sinh() { 183 return new Complex(sinh(x)*Math.cos(y),cosh(x)*Math.sin(y)); 184 } 185 186 /** 187 Hyperbolic cosine of this Complex number 188 (doesn't change this Complex number). 189 <br>cosh(z) = (exp(z) + exp(-z)) / 2. 190 @return cosh(z) where z is this Complex number. 191 */ 192 public Complex cosh() { 193 return new Complex(cosh(x)*Math.cos(y),sinh(x)*Math.sin(y)); 194 } 195 196 /** 197 Tangent of this Complex number (doesn't change this Complex number). 198 <br>tan(z) = sin(z)/cos(z). 199 @return tan(z) where z is this Complex number. 200 */ 201 public Complex tan() { 202 return (this.sin()).div(this.cos()); 203 } 204 205 /** 206 Negative of this complex number (chs stands for change sign). 207 This produces a new Complex number and doesn't change 208 this Complex number. 209 <br>-(x+i*y) = -x-i*y. 210 @return -z where z is this Complex number. 211 */ 212 public Complex chs() { 213 return new Complex(-x,-y); 214 } 215 216 /** 217 String representation of this Complex number. 218 @return x+i*y, x-i*y, x, or i*y as appropriate. 219 */ 220 public String toString() { 221 if (x!=0 && y>0) { 222 return x+" + "+y+"i"; 223 } 224 if (x!=0 && y<0) { 225 return x+" - "+(-y)+"i"; 226 } 227 if (y==0) { 228 return String.valueOf(x); 229 } 230 if (x==0) { 231 return y+"i"; 232 } 233 // shouldn't get here (unless Inf or NaN) 234 return x+" + i*"+y; 235 236 } 237 } 238