1 16 17 package org.apache.commons.math.complex; 18 19 import org.apache.commons.math.util.MathUtils; 20 21 33 public class ComplexUtils { 34 35 38 private ComplexUtils() { 39 super(); 40 } 41 42 48 public static Complex acos(Complex z) { 49 if (z.isNaN()) { 50 return Complex.NaN; 51 } 52 53 return Complex.I.negate().multiply(log(z.add( 54 Complex.I.multiply(sqrt1z(z))))); 55 } 56 57 63 public static Complex asin(Complex z) { 64 if (z.isNaN()) { 65 return Complex.NaN; 66 } 67 68 return Complex.I.negate().multiply(log(sqrt1z(z).add( 69 Complex.I.multiply(z)))); 70 } 71 72 78 public static Complex atan(Complex z) { 79 if (z.isNaN()) { 80 return Complex.NaN; 81 } 82 83 84 return Complex.I.multiply( 85 log(Complex.I.add(z).divide(Complex.I.subtract(z)))) 86 .divide(new Complex(2.0, 0.0)); 87 } 88 89 95 public static Complex cos(Complex z) { 96 if (z.isNaN()) { 97 return Complex.NaN; 98 } 99 100 double a = z.getReal(); 101 double b = z.getImaginary(); 102 103 return new Complex(Math.cos(a) * MathUtils.cosh(b), 104 -Math.sin(a) * MathUtils.sinh(b)); 105 } 106 107 113 public static Complex cosh(Complex z) { 114 if (z.isNaN()) { 115 return Complex.NaN; 116 } 117 118 double a = z.getReal(); 119 double b = z.getImaginary(); 120 121 return new Complex(MathUtils.cosh(a) * Math.cos(b), 122 MathUtils.sinh(a) * Math.sin(b)); 123 } 124 125 132 public static Complex exp(Complex z) { 133 if (z.isNaN()) { 134 return Complex.NaN; 135 } 136 137 double b = z.getImaginary(); 138 double expA = Math.exp(z.getReal()); 139 double sinB = Math.sin(b); 140 double cosB = Math.cos(b); 141 return new Complex(expA * cosB, expA * sinB); 142 } 143 144 150 public static Complex log(Complex z) { 151 if (z.isNaN()) { 152 return Complex.NaN; 153 } 154 155 return new Complex(Math.log(z.abs()), 156 Math.atan2(z.getImaginary(), z.getReal())); 157 } 158 159 160 166 public static Complex pow(Complex y, Complex x) { 167 return exp(x.multiply(log(y))); 168 } 169 170 176 public static Complex sin(Complex z) { 177 if (z.isNaN()) { 178 return Complex.NaN; 179 } 180 181 double a = z.getReal(); 182 double b = z.getImaginary(); 183 184 return new Complex(Math.sin(a) * MathUtils.cosh(b), 185 Math.cos(a) * MathUtils.sinh(b)); 186 } 187 188 194 public static Complex sinh(Complex z) { 195 if (z.isNaN()) { 196 return Complex.NaN; 197 } 198 199 double a = z.getReal(); 200 double b = z.getImaginary(); 201 202 return new Complex(MathUtils.sinh(a) * Math.cos(b), 203 MathUtils.cosh(a) * Math.sin(b)); 204 } 205 206 212 public static Complex sqrt(Complex z) { 213 if (z.isNaN()) { 214 return Complex.NaN; 215 } 216 217 double a = z.getReal(); 218 double b = z.getImaginary(); 219 220 double t = Math.sqrt((Math.abs(a) + z.abs()) / 2.0); 221 if (a >= 0.0) { 222 return new Complex(t, b / (2.0 * t)); 223 } else { 224 return new Complex(Math.abs(z.getImaginary()) / (2.0 * t), 225 MathUtils.indicator(b) * t); 226 } 227 } 228 229 235 public static Complex sqrt1z(Complex z) { 236 return sqrt(Complex.ONE.subtract(z.multiply(z))); 237 } 238 239 245 public static Complex tan(Complex z) { 246 if (z.isNaN()) { 247 return Complex.NaN; 248 } 249 250 double a2 = 2.0 * z.getReal(); 251 double b2 = 2.0 * z.getImaginary(); 252 double d = Math.cos(a2) + MathUtils.cosh(b2); 253 254 return new Complex(Math.sin(a2) / d, MathUtils.sinh(b2) / d); 255 } 256 257 264 public static Complex tanh(Complex z) { 265 if (z.isNaN()) { 266 return Complex.NaN; 267 } 268 269 double a2 = 2.0 * z.getReal(); 270 double b2 = 2.0 * z.getImaginary(); 271 double d = MathUtils.cosh(a2) + Math.cos(b2); 272 273 return new Complex(MathUtils.sinh(a2) / d, Math.sin(b2) / d); 274 } 275 } 276 | Popular Tags |