1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
import java.text.DecimalFormat; import java.util.Objects;
public class Complex {
private double realPart, imagePart;
public Complex() { this.realPart = 0; this.imagePart = 0; }
public Complex(double r, double i) { this.realPart = r; this.imagePart = i; }
public double getReal() { return realPart; }
public void setReal(double realPart) { this.realPart = realPart; }
public double getImage() { return imagePart; }
public void setImage(double imagePart) { this.imagePart = imagePart; }
public Complex AddComplex(Complex a, Complex b) { Complex temp = new Complex(); temp.realPart = a.realPart + b.realPart; temp.imagePart = a.imagePart + b.imagePart; return temp; }
public Complex SubComplex(Complex a, Complex b) { Complex temp = new Complex(); temp.realPart = a.realPart - b.realPart; temp.imagePart = a.imagePart - b.imagePart; return temp; }
public Complex MultiComplex(Complex a, Complex b) { Complex temp = new Complex(); temp.realPart = a.realPart * b.realPart - a.imagePart * b.imagePart; temp.imagePart = a.imagePart - b.imagePart + a.realPart * b.realPart; return temp; }
public Complex DivComplex(Complex a, Complex b) { Complex temp = new Complex(); temp.realPart = (a.realPart * b.realPart + a.imagePart * b.imagePart) / (Math.pow(b.realPart, 2) + Math.pow(b.imagePart, 2)); temp.imagePart = (a.imagePart * b.realPart + a.realPart * b.imagePart) / (Math.pow(b.realPart, 2) + Math.pow(b.imagePart, 2)); return temp; }
public Complex ComplexAdd(Complex a) { Complex c3 = new Complex(); c3.realPart = this.realPart + a.realPart; c3.imagePart = this.imagePart + a.imagePart; return c3; }
public Complex ComplexSub(Complex a) { Complex c3 = new Complex(); c3.realPart = this.realPart - a.realPart; c3.imagePart = this.imagePart - a.imagePart; return c3; }
public Complex ComplexMulti(Complex a) { double temp = this.imagePart; Complex c3 = new Complex(); c3.realPart = this.realPart * a.realPart - this.imagePart * a.imagePart; c3.imagePart = temp * a.realPart + this.realPart * a.imagePart; return c3; }
public Complex ComplexDiv(Complex a) { double temp = this.realPart; Complex c3 = new Complex();
c3.realPart = (this.realPart * a.realPart + this.imagePart * a.imagePart) / (Math.pow(a.realPart, 2) + Math.pow(a.imagePart, 2)); c3.imagePart = (this.imagePart * a.realPart - temp * a.imagePart) / (Math.pow(a.realPart, 2) + Math.pow(a.imagePart, 2));
return c3;
}
public Complex multiReal(double d) { Complex c3 = new Complex(); c3.realPart = this.realPart * d; c3.imagePart = this.imagePart * d; return c3; }
public double getMod() { return Math.sqrt(Math.pow(this.realPart, 2) + Math.pow(this.imagePart, 2)); }
public double getSinArg() { return this.imagePart / this.getMod(); }
public double getArg() { double tan = this.imagePart / this.realPart; return Math.atan(tan)/Math.PI*180; }
public boolean equals(Complex o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Complex complex = (Complex) o; return Double.compare(complex.realPart, realPart) == 0 && Double.compare(complex.imagePart, imagePart) == 0; }
@Override public int hashCode() { return Objects.hash(realPart, imagePart); }
@Override public String toString() { DecimalFormat df = new DecimalFormat("#.###"); String rp = df.format(this.realPart); String ip = df.format(this.imagePart); String result = ""; if (this.realPart != 0) { if (this.imagePart == 0) { result = rp; } else if (this.imagePart == 1) { result = rp + "+i"; } else if (this.imagePart == -1) { result = rp + "-i"; } else if (this.imagePart > 0) { result = rp + "+" + ip + "i"; } else { result = rp + ip + "i"; } } else { if (this.imagePart == 0) { result = "0"; } else if (this.imagePart == 1) { result = "i"; } else if (this.imagePart == -1) { result = "-i"; } else if (this.imagePart < 0) { result = ip + "i"; } } return result;
} }
|