EOS 2  1.1.0
Einfache Objektbasierte Sprache
Complex.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.base.math;
2 
14 public class Complex {
15 
19  public static final Complex ZERO = new Complex(0, 0);
23  public static final Complex ONE = new Complex(1, 0);
27  public static final Complex I = new Complex(0, 1);
31  public static final Complex E = new Complex(Math.E, 0);
35  public static final Complex PI = new Complex(Math.PI, 0);
39  private final double a;
43  private final double b;
44 
52  public Complex(double a, double b) {
53  this.a = a;
54  this.b = b;
55  }
56 
62  public double Re() {
63  return a;
64  }
65 
71  public double Im() {
72  return b;
73  }
74 
81  public Complex add(Complex c) {
82  return new Complex(a + c.a, b + c.b);
83  }
84 
92  return new Complex(a - c.a, b - c.b);
93  }
94 
100  public Complex negative() {
101  return new Complex(-a, -b);
102  }
103 
109  public Complex inverse() {
110  return conjugation().divide(a * a + b * b);
111  }
112 
119  public Complex multiply(double x) {
120  return new Complex(a * x, b * x);
121  }
122 
130  return new Complex(a * c.a - b * c.b, a * c.b + b * c.a);
131  }
132 
139  public Complex divide(double x) {
140  return new Complex(a / x, b / x);
141  }
142 
149  public Complex divide(Complex c) {
150  return multiply(c.inverse());
151  }
152 
159  public Complex pow(double x) {
160  double r = Math.pow(abs(), x);
161  double phi = x * angle();
162  return new Complex(Math.cos(phi) * r, Math.sin(phi) * r);
163  }
164 
171  public Complex pow(Complex c) {
172  return pow(c.a).multiply(pow(c.b).powI());
173  }
174 
182  public Complex nroot(double x) {
183  return pow(1 / x);
184  }
185 
193  public Complex nroot(Complex c) {
194  return pow(ONE.divide(c));
195  }
196 
202  private Complex powI() {
203  double r = Math.pow(Math.E, -angle());
204  double phi = Math.log(abs());
205  return new Complex(Math.cos(phi) * r, Math.sin(phi) * r);
206  }
207 
213  public Complex e() {
214  return E.pow(this);
215  }
216 
224  public Complex ln() {
225  return new Complex(Math.log(abs()), angle());
226  }
227 
235  public Complex log(Complex base) {
236  return ln().divide(base.ln());
237  }
238 
244  public double abs() {
245  return Math.sqrt(a * a + b * b);
246  }
247 
254  public double angle() {
255  if (b == 0) {
256  if (a < 0) {
257  return Math.PI;
258  } else if (a > 0) {
259  return 0;
260  } else {
261  throw new ArithmeticException("Complex.angle(" + this + ")");
262  }
263  } else {
264  return (b < 0) ? Math.atan(a / b) : Math.atan(a / b) + Math.PI;
265  }
266  }
267 
273  public Complex conjugation() {
274  return new Complex(a, -b);
275  }
276 
282  @Override
283  public String toString() {
284  return a + "+" + b + "i";
285  }
286 
293  public boolean equals(Complex c) {
294  return a == c.a && b == c.b;
295  }
296 
300  public Complex one() {
301  return ONE;
302  }
303 
307  public Complex zero() {
308  return ZERO;
309  }
310 }
Complex substract(Complex c)
Definition: Complex.java:91
Complex multiply(Complex c)
Definition: Complex.java:129
static final Complex PI
Definition: Complex.java:35
static final Complex ZERO
0
Definition: Complex.java:19
static final Complex E
e
Definition: Complex.java:31
Complex log(Complex base)
Definition: Complex.java:235
static final Complex ONE
1
Definition: Complex.java:23
Complex(double a, double b)
Definition: Complex.java:52
static final Complex I
i
Definition: Complex.java:27
Impressum