package SWjtools.mathele;
import java.util.*;
/** Rechnen mit komplexen Zahlen
* im der Form (re|im)
* re ist der Realteil a der komplexen Zahl z = a + ib
* im ist der Imaginärteil b der komplexen Zahl z = a + ib
* @author hwr (Hans Wehner)
* @version 1.3 (02-06-14)
*/
public class komplex {
double re;
double im;
/** Not-a-Number vom Typ komplex (NaN|NaN)
*/
public static final komplex NaN = new komplex(Double.NaN, Double.NaN);
/** Erzeuge die komplexe Null (0|0)!
*/
public komplex() {
this.re = 0.0;
this.im = 0.0;
}
/** Erzeuge die komplexe Zahl (r|i)!
* @param r Realteil
* @param i Imaginärteil
*/
public komplex(double r, double i) {
this.re = r;
this.im = i;
}
/** Liefere den Realteil der komplexen Zahl!
*/
public double k_re() {
return this.re;
}
/** Liefere den Imaginärteil der komplexen Zahl!
*/
public double k_im() {
return this.im;
}
/** Prüfe, ob es die komplexe Null ist!
* @return true, wenn es (0|0) ist, false sonst
*/
public boolean istNull () {
return ((this.re == 0.0) && (this.im == 0.0));
}
/** Umrechnung in Polarkoordinaten (r|phi)
* @return Betrag r
*/
public double k_r() {
return Math.sqrt(this.re*this.re + this.im*this.im);
}
/** Umrechnung in Polarkoordinaten (r|phi)
* @return Winkel phi im Bogenmaß
*/
public double k_phi() {
double phi;
if (this.re != 0.0) phi = Math.atan(this.im/this.re);
else if (this.im > 0.0) return phi=Math.PI/2.0;
else if (this.im==0.0) return phi=0.0;
else return phi=3*Math.PI/2.0;
if (this.re >= 0.0)
if (this.im >= 0.0) return phi;
else return phi + 2.0 * Math.PI;
else return phi + Math.PI;
}
/** Umrechnung in Polarkoordinaten (r|phi)
* @return Winkel phi im Gradmaß
*/
public double k_phi_deg() {
double phi;
if (this.re != 0.0) phi = Math.atan(this.im/this.re)*180.0/Math.PI;
else if (this.im > 0.0) return phi=90.0;
else if (this.im == 0.0) return phi=0.0;
else return phi=270.0;
if (this.re >= 0.0)
if (this.im >= 0.0) return phi;
else return phi + 360;
else return phi + 180;
}
/** Liefere den Betrag von z (Wurzel aus der Summe der Quadrate von
* Real- und Imaginärteil)!
*/
public double k_Betrag() {
return Math.sqrt(this.re*this.re + this.im*this.im);
}
/** Addiere b hinzu!
* @param b die komplexe Zahl, die hinzu addiert werden soll
* @return Summe
*/
public komplex k_add(komplex b) {
komplex h = new komplex(this.re, this.im);
h.re += b.re;
h.im += b.im;
return h;
}
/** Subtrahiere b!
* @param b die komplexe Zahl, die subtrahiert werden soll
* @return Differenz
*/
public komplex k_sub(komplex b) {
komplex h = new komplex(this.re, this.im);
h.re -= b.re;
h.im -= b.im;
return h;
}
/** Multipliziere mit b!
* @param b die komplexe Zahl, mit der multipliziert werden soll
* @return Produkt
*/
public komplex k_mal(komplex b) {
komplex h = new komplex();
h.re = this.re*b.k_re() - this.im*b.k_im();
h.im = this.re*b.k_im() + this.im*b.k_re();
return h;
}
/** Bestimme das Inverse bezüglich der Addition!
* @return (-z)
*/
public komplex k_minus() {
komplex h = new komplex(-this.re, -this.im);
return h;
}
/** Bestimme das Inverse bezüglich der Multiplikation!
* @return z^(-1)
*/
public komplex k_invers() {
komplex h;
double b = this.re*this.re + this.im*this.im ;
if (this.k_Betrag() == 0.0) h = this;
else {
h = new komplex(this.re/b, -this.im/b);
}
return h;
}
/** Berechne die Summe zweier komplexer Zahlen x und y!
* @param x der erste Summand
* @param y der zweite Summand
* @return die Summe
*/
public komplex k_Summe(komplex x, komplex y) {
komplex h = new komplex(x.k_re(), x.k_im());
return h.k_add(y);
}
/** Berechne die Differenz zweier komplexer Zahlen x und y!
* @param x der Minuend
* @param y der Subtrahend
* @return die Differenz
*/
public komplex k_Differenz(komplex x, komplex y) {
komplex h = new komplex(x.re, x.im);
return h.k_sub(y);
}
/** Berechne das Produkt zweier komplexer Zahlen x und y!
* @param x der erste Faktor
* @param y der zweite Faktor
* @return das Produkt
*/
public komplex k_Produkt(komplex x, komplex y) {
komplex h = new komplex(x.re, x.im);
return h.k_mal(y);
}
/** Wandle die komplexe Zahl in eine Zeichenkette der Form
* (Realteil|Imaginärteil) um!
*/
public String toString() {
return "("+this.re+" | "+this.im+")";
}
}