|
| 1 | +package es.codeurjc.test.complex; |
| 2 | + |
| 3 | +import static java.lang.Math.*; |
| 4 | + |
| 5 | +public class Complex { |
| 6 | + |
| 7 | + private double realPart = 0; |
| 8 | + private double imaginaryPart = 0; |
| 9 | + |
| 10 | + public Complex(double realPart, double imaginaryPart) { |
| 11 | + this.realPart = realPart; |
| 12 | + this.imaginaryPart = imaginaryPart; |
| 13 | + } |
| 14 | + |
| 15 | + public Complex() { |
| 16 | + } |
| 17 | + |
| 18 | + public double getRealPart() { |
| 19 | + return this.realPart; |
| 20 | + } |
| 21 | + |
| 22 | + public double getImaginaryPart() { |
| 23 | + return this.imaginaryPart; |
| 24 | + } |
| 25 | + |
| 26 | + public double abs() { |
| 27 | + return sqrt(pow(this.realPart, 2.0) + pow(this.imaginaryPart, 2.0)); |
| 28 | + } |
| 29 | + |
| 30 | + public double phase() { |
| 31 | + return atan2(this.imaginaryPart, this.realPart); |
| 32 | + } |
| 33 | + |
| 34 | + // return a new Complex object whose value is the conjugate of this |
| 35 | + public Complex conjugate() { |
| 36 | + return new Complex(this.realPart, -this.imaginaryPart); |
| 37 | + } |
| 38 | + |
| 39 | + // return a new Complex object whose value is the reciprocal of this |
| 40 | + public Complex reciprocal() { |
| 41 | + double scale = pow(this.realPart, 2.0) + pow(this.imaginaryPart, 2.0); |
| 42 | + return new Complex(this.realPart / scale, this.imaginaryPart / scale); |
| 43 | + } |
| 44 | + |
| 45 | + // return a new Complex object whose value is (this + complex) |
| 46 | + public Complex add(Complex complex) { |
| 47 | + |
| 48 | + double newRealPart = this.realPart + complex.realPart; |
| 49 | + double newImaginaryPart = this.imaginaryPart + complex.imaginaryPart; |
| 50 | + |
| 51 | + return new Complex(newRealPart, newImaginaryPart); |
| 52 | + } |
| 53 | + |
| 54 | + // return a new Complex object whose value is (this - complex) |
| 55 | + public Complex minus(Complex complex) { |
| 56 | + double newRealPart = this.realPart - complex.realPart; |
| 57 | + double newImaginaryPart = this.imaginaryPart - complex.imaginaryPart; |
| 58 | + |
| 59 | + return new Complex(newRealPart, newImaginaryPart); |
| 60 | + } |
| 61 | + |
| 62 | + // return a new Complex object whose value is (this * complex) |
| 63 | + public Complex times(Complex complex) { |
| 64 | + double newRealPart = (this.realPart * complex.realPart) |
| 65 | + - (this.imaginaryPart * complex.imaginaryPart); |
| 66 | + double newImaginaryPart = (this.realPart * complex.imaginaryPart) |
| 67 | + - (this.imaginaryPart * complex.realPart); |
| 68 | + |
| 69 | + return new Complex(newRealPart, newImaginaryPart); |
| 70 | + } |
| 71 | + |
| 72 | + // return a new Complex object whose value is (this * alpha) |
| 73 | + public Complex times(double alpha) { |
| 74 | + return new Complex(alpha * this.realPart, alpha * this.imaginaryPart); |
| 75 | + } |
| 76 | + |
| 77 | + // return a new Complex object whose value is (this / complex) |
| 78 | + public Complex divides(Complex complex) { |
| 79 | + return this.times(complex.reciprocal()); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + @Override |
| 84 | + public int hashCode() { |
| 85 | + final int prime = 31; |
| 86 | + int result = 1; |
| 87 | + long temp; |
| 88 | + temp = Double.doubleToLongBits(imaginaryPart); |
| 89 | + result = prime * result + (int) (temp ^ (temp >>> 32)); |
| 90 | + temp = Double.doubleToLongBits(realPart); |
| 91 | + result = prime * result + (int) (temp ^ (temp >>> 32)); |
| 92 | + return result; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean equals(Object obj) { |
| 97 | + if (this == obj) |
| 98 | + return true; |
| 99 | + if (obj == null) |
| 100 | + return false; |
| 101 | + if (getClass() != obj.getClass()) |
| 102 | + return false; |
| 103 | + Complex other = (Complex) obj; |
| 104 | + if (Double.doubleToLongBits(imaginaryPart) != Double |
| 105 | + .doubleToLongBits(other.imaginaryPart)) |
| 106 | + return false; |
| 107 | + if (Double.doubleToLongBits(realPart) != Double |
| 108 | + .doubleToLongBits(other.realPart)) |
| 109 | + return false; |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + public String toString() { |
| 114 | + return this.realPart + " + " + this.imaginaryPart + "i "; |
| 115 | + } |
| 116 | +} |
0 commit comments