| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Complex |
|
| 3.5833333333333335;3.583 |
| 1 | /* |
|
| 2 | * Copyright 2003-2004 The Apache Software Foundation. |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | ||
| 17 | package org.apache.commons.math.complex; |
|
| 18 | ||
| 19 | import java.io.Serializable; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * Representation of a Complex number - a number which has both a |
|
| 23 | * real and imaginary part. |
|
| 24 | * |
|
| 25 | * @author Apache Software Foundation |
|
| 26 | * @version $Revision$ $Date: 2005-08-22 19:27:17 -0700 (Mon, 22 Aug 2005) $ |
|
| 27 | */ |
|
| 28 | public class Complex implements Serializable { |
|
| 29 | ||
| 30 | /** Serializable version identifier */ |
|
| 31 | static final long serialVersionUID = -6530173849413811929L; |
|
| 32 | ||
| 33 | /** The square root of -1. A number representing "0.0 + 1.0i".*/ |
|
| 34 | 10 | public static final Complex I = new Complex(0.0, 1.0); |
| 35 | ||
| 36 | /** A complex number representing "(Double.NaN) + (Double.NaN)i" */ |
|
| 37 | 10 | public static final Complex NaN = new Complex(Double.NaN, Double.NaN); |
| 38 | ||
| 39 | /** A complex number representing "1.0 + 0.0i" */ |
|
| 40 | 10 | public static final Complex ONE = new Complex(1.0, 0.0); |
| 41 | ||
| 42 | /** The imaginary part. */ |
|
| 43 | protected double imaginary; |
|
| 44 | ||
| 45 | /** The real part. */ |
|
| 46 | protected double real; |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Create a complex number given the real and imaginary parts. |
|
| 50 | * |
|
| 51 | * @param real the real part. |
|
| 52 | * @param imaginary the imaginary part. |
|
| 53 | */ |
|
| 54 | public Complex(double real, double imaginary) { |
|
| 55 | 4674 | super(); |
| 56 | 4674 | this.real = real; |
| 57 | 4674 | this.imaginary = imaginary; |
| 58 | 4674 | } |
| 59 | ||
| 60 | /** |
|
| 61 | * Return the absolute value of this complex number. |
|
| 62 | * |
|
| 63 | * @return the absolute value. |
|
| 64 | */ |
|
| 65 | public double abs() { |
|
| 66 | 704 | if (isNaN()) { |
| 67 | 2 | return Double.NaN; |
| 68 | } |
|
| 69 | 702 | if (Math.abs(real) < Math.abs(imaginary)) { |
| 70 | 230 | if (imaginary == 0.0) { |
| 71 | 0 | return Math.abs(real); |
| 72 | } |
|
| 73 | 230 | double q = real / imaginary; |
| 74 | 230 | return (Math.abs(imaginary) * Math.sqrt(1 + q*q)); |
| 75 | } else { |
|
| 76 | 472 | if (real == 0.0) { |
| 77 | 36 | return Math.abs(imaginary); |
| 78 | } |
|
| 79 | 436 | double q = imaginary / real; |
| 80 | 436 | return (Math.abs(real) * Math.sqrt(1 + q*q)); |
| 81 | } |
|
| 82 | } |
|
| 83 | ||
| 84 | /** |
|
| 85 | * Return the sum of this complex number and the given complex number. |
|
| 86 | * |
|
| 87 | * @param rhs the other complex number. |
|
| 88 | * @return the complex number sum. |
|
| 89 | */ |
|
| 90 | public Complex add(Complex rhs) { |
|
| 91 | 1340 | if (isNaN() || rhs.isNaN()) { |
| 92 | 2 | return NaN; |
| 93 | } |
|
| 94 | ||
| 95 | 1338 | return new Complex(real + rhs.getReal(), |
| 96 | imaginary + rhs.getImaginary()); |
|
| 97 | } |
|
| 98 | ||
| 99 | /** |
|
| 100 | * Return the conjugate of this complex number. The conjugate of |
|
| 101 | * "A + Bi" is "A - Bi". Complex.NaN is returned if either the real or imaginary part of |
|
| 102 | * this Complex number equals Double.NaN. |
|
| 103 | * |
|
| 104 | * @return the conjugate of this Complex object |
|
| 105 | */ |
|
| 106 | public Complex conjugate() { |
|
| 107 | 4 | if (isNaN()) { |
| 108 | 2 | return NaN; |
| 109 | } |
|
| 110 | ||
| 111 | 2 | return new Complex(real, -imaginary); |
| 112 | } |
|
| 113 | ||
| 114 | /** |
|
| 115 | * Return the quotient of this complex number and the given complex number. |
|
| 116 | * @param rhs the other complex number. |
|
| 117 | * @return the complex number quotient. |
|
| 118 | */ |
|
| 119 | public Complex divide(Complex rhs) { |
|
| 120 | 246 | if (isNaN() || rhs.isNaN()) { |
| 121 | 2 | return NaN; |
| 122 | } |
|
| 123 | ||
| 124 | 244 | double c = rhs.getReal(); |
| 125 | 244 | double d = rhs.getImaginary(); |
| 126 | 244 | if (c == 0.0 && d == 0.0) { |
| 127 | 0 | throw new ArithmeticException("Error: division by zero."); |
| 128 | } |
|
| 129 | ||
| 130 | 244 | if (Math.abs(c) < Math.abs(d)) { |
| 131 | 70 | if (d == 0.0) { |
| 132 | 0 | return new Complex(real/c, imaginary/c); |
| 133 | } |
|
| 134 | 70 | double q = c / d; |
| 135 | 70 | double denominator = c * q + d; |
| 136 | 70 | return new Complex((real * q + imaginary) / denominator, |
| 137 | (imaginary * q - real) / denominator); |
|
| 138 | } else { |
|
| 139 | 174 | if (c == 0.0) { |
| 140 | 0 | return new Complex(imaginary/d, -real/c); |
| 141 | } |
|
| 142 | 174 | double q = d / c; |
| 143 | 174 | double denominator = d * q + c; |
| 144 | 174 | return new Complex((imaginary * q + real) / denominator, |
| 145 | (imaginary - real * q) / denominator); |
|
| 146 | } |
|
| 147 | } |
|
| 148 | ||
| 149 | /** |
|
| 150 | * Test for the equality of two Complex objects. If both the |
|
| 151 | * real and imaginary parts of two Complex numbers are exactly |
|
| 152 | * the same, the two Complex objects are considered to be equal. |
|
| 153 | * |
|
| 154 | * @param other Object to test for equality to this |
|
| 155 | * @return true if two Complex objects are equal, false if |
|
| 156 | * object is null, not an instance of Complex, or |
|
| 157 | * not equal to this Complex instance. |
|
| 158 | * |
|
| 159 | */ |
|
| 160 | public boolean equals(Object other) { |
|
| 161 | boolean ret; |
|
| 162 | ||
| 163 | 140 | if (this == other) { |
| 164 | 2 | ret = true; |
| 165 | 138 | } else if (other == null) { |
| 166 | 2 | ret = false; |
| 167 | } else { |
|
| 168 | try { |
|
| 169 | 136 | Complex rhs = (Complex)other; |
| 170 | 134 | ret = (Double.doubleToRawLongBits(real) == |
| 171 | Double.doubleToRawLongBits(rhs.getReal())) && |
|
| 172 | (Double.doubleToRawLongBits(imaginary) == |
|
| 173 | Double.doubleToRawLongBits(rhs.getImaginary())); |
|
| 174 | 2 | } catch (ClassCastException ex) { |
| 175 | // ignore exception |
|
| 176 | 2 | ret = false; |
| 177 | 134 | } |
| 178 | } |
|
| 179 | ||
| 180 | 140 | return ret; |
| 181 | } |
|
| 182 | ||
| 183 | /** |
|
| 184 | * Access the imaginary part. |
|
| 185 | * |
|
| 186 | * @return the imaginary part. |
|
| 187 | */ |
|
| 188 | public double getImaginary() { |
|
| 189 | 5690 | return imaginary; |
| 190 | } |
|
| 191 | ||
| 192 | /** |
|
| 193 | * Access the real part. |
|
| 194 | * |
|
| 195 | * @return the real part. |
|
| 196 | */ |
|
| 197 | public double getReal() { |
|
| 198 | 5786 | return real; |
| 199 | } |
|
| 200 | ||
| 201 | /** |
|
| 202 | * Returns true if this complex number is the special Not-a-Number (NaN) |
|
| 203 | * value. |
|
| 204 | * |
|
| 205 | * @return true if the value represented by this object is NaN; false |
|
| 206 | * otherwise. |
|
| 207 | */ |
|
| 208 | public boolean isNaN() { |
|
| 209 | 8338 | return Double.isNaN(real) || Double.isNaN(imaginary); |
| 210 | } |
|
| 211 | ||
| 212 | /** |
|
| 213 | * Return the product of this complex number and the given complex number. |
|
| 214 | * |
|
| 215 | * @param rhs the other complex number. |
|
| 216 | * @return the complex number product. |
|
| 217 | */ |
|
| 218 | public Complex multiply(Complex rhs) { |
|
| 219 | 1628 | if (isNaN() || rhs.isNaN()) { |
| 220 | 8 | return NaN; |
| 221 | } |
|
| 222 | ||
| 223 | 1620 | double p = (real + imaginary) * (rhs.getReal() + rhs.getImaginary()); |
| 224 | 1620 | double ac = real * rhs.getReal(); |
| 225 | 1620 | double bd = imaginary * rhs.getImaginary(); |
| 226 | 1620 | return new Complex(ac - bd, p - ac - bd); |
| 227 | } |
|
| 228 | ||
| 229 | /** |
|
| 230 | * Return the additive inverse of this complex number. |
|
| 231 | * |
|
| 232 | * @return the negation of this complex number. |
|
| 233 | */ |
|
| 234 | public Complex negate() { |
|
| 235 | 8 | if (isNaN()) { |
| 236 | 2 | return NaN; |
| 237 | } |
|
| 238 | ||
| 239 | 6 | return new Complex(-real, -imaginary); |
| 240 | } |
|
| 241 | ||
| 242 | /** |
|
| 243 | * Return the difference between this complex number and the given complex |
|
| 244 | * number. |
|
| 245 | * |
|
| 246 | * @param rhs the other complex number. |
|
| 247 | * @return the complex number difference. |
|
| 248 | */ |
|
| 249 | public Complex subtract(Complex rhs) { |
|
| 250 | 454 | if (isNaN() || rhs.isNaN()) { |
| 251 | 4 | return NaN; |
| 252 | } |
|
| 253 | ||
| 254 | 450 | return new Complex(real - rhs.getReal(), |
| 255 | imaginary - rhs.getImaginary()); |
|
| 256 | } |
|
| 257 | } |