1 16 17 package org.apache.commons.math.complex; 18 19 import java.io.Serializable ; 20 21 28 public class Complex implements Serializable { 29 30 31 static final long serialVersionUID = -6530173849413811929L; 32 33 34 public static final Complex I = new Complex(0.0, 1.0); 35 36 37 public static final Complex NaN = new Complex(Double.NaN, Double.NaN); 38 39 40 public static final Complex ONE = new Complex(1.0, 0.0); 41 42 43 protected double imaginary; 44 45 46 protected double real; 47 48 54 public Complex(double real, double imaginary) { 55 super(); 56 this.real = real; 57 this.imaginary = imaginary; 58 } 59 60 65 public double abs() { 66 if (isNaN()) { 67 return Double.NaN; 68 } 69 return Math.sqrt(squareSum()); 70 } 71 72 78 public Complex add(Complex rhs) { 79 if (isNaN() || rhs.isNaN()) { 80 return NaN; 81 } 82 83 return new Complex(real + rhs.getReal(), 84 imaginary + rhs.getImaginary()); 85 } 86 87 94 public Complex conjugate() { 95 if (isNaN()) { 96 return NaN; 97 } 98 99 return new Complex(real, -imaginary); 100 } 101 102 107 public Complex divide(Complex rhs) { 108 if (isNaN() || rhs.isNaN()) { 109 return NaN; 110 } 111 112 if (Math.abs(rhs.getReal()) < Math.abs(rhs.getImaginary())) { 113 double q = rhs.getReal() / rhs.getImaginary(); 114 double d = (rhs.getReal() * q) + rhs.getImaginary(); 115 return new Complex(((real * q) + imaginary) / d, 116 ((imaginary * q) - real) / d); 117 } else { 118 double q = rhs.getImaginary() / rhs.getReal(); 119 double d = (rhs.getImaginary() * q) + rhs.getReal(); 120 return new Complex(((imaginary * q) + real) / d, 121 (imaginary - (real * q)) / d); 122 } 123 } 124 125 136 public boolean equals(Object other) { 137 boolean ret; 138 139 if (this == other) { 140 ret = true; 141 } else if (other == null) { 142 ret = false; 143 } else { 144 try { 145 Complex rhs = (Complex)other; 146 ret = (Double.doubleToRawLongBits(real) == 147 Double.doubleToRawLongBits(rhs.getReal())) && 148 (Double.doubleToRawLongBits(imaginary) == 149 Double.doubleToRawLongBits(rhs.getImaginary())); 150 } catch (ClassCastException ex) { 151 ret = false; 153 } 154 } 155 156 return ret; 157 } 158 159 164 public double getImaginary() { 165 return imaginary; 166 } 167 168 173 public double getReal() { 174 return real; 175 } 176 177 184 public boolean isNaN() { 185 return Double.isNaN(real) || Double.isNaN(imaginary); 186 } 187 188 194 public Complex multiply(Complex rhs) { 195 if (isNaN() || rhs.isNaN()) { 196 return NaN; 197 } 198 199 double p = (real + imaginary) * (rhs.getReal() + rhs.getImaginary()); 200 double ac = real * rhs.getReal(); 201 double bd = imaginary * rhs.getImaginary(); 202 return new Complex(ac - bd, p - ac - bd); 203 } 204 205 210 public Complex negate() { 211 if (isNaN()) { 212 return NaN; 213 } 214 215 return new Complex(-real, -imaginary); 216 } 217 218 223 private double squareSum() { 224 return real * real + imaginary * imaginary; 225 } 226 227 234 public Complex subtract(Complex rhs) { 235 if (isNaN() || rhs.isNaN()) { 236 return NaN; 237 } 238 239 return new Complex(real - rhs.getReal(), 240 imaginary - rhs.getImaginary()); 241 } 242 } 243 | Popular Tags |