package complex;
public class Complex {
private double re; //Instanzvariable
private double im;
public final static Complex I = new Complex(0., 1.0); //Konstanten der Klasse Complex
public final static Complex ZERO = new Complex(0.0);
public final static Complex HALF = new Complex(0.5, 0.);
public final static Complex ONE = new Complex(1.0);
public Complex(double r, double i) { //Konstruktore
this.re = r;
this.im = i;
}
public Complex(double r) { //Konstruktor
this.re = r;
this.im = 0;
}
public double real() {
return this.re;
}
public double imag() {
return this.im;
}
public String toString() {
return "(" + this.re + "," + this.im + ")";
}
public Complex readComplex() {
int r = readInteger("Bitte Realteil eingeben!");
int i = readInteger("Bitte Imaginärteil eingeben!");
Complex c = new Complex(r, i);
return c;
}
public static void test(Complex z1, Complex z2) {
System.out.println("\nComplex Test Start");
System.out.println("z1: " + z1 + " ... z2: " + z2);
System.out.println("z1 == z2: " + z1.equals(z2) + " ... z2 == z1: "
+ z2.equals(z1));
System.out.println("z1.conj(): " + z1.conj() + " ... z2.conj(): "
+ z2.conj());
if ( ! z1.equals(z1.conj().conj()) ) {
System.out.println("FEHLER in conj");
}
if ( ! z2.conj().conj().equals(z2) ) {
System.out.println("FEHLER in conj");
}
System.out.println("z1.real(): " + z1.real() + " ... z1.imag(): "
+ z1.imag());
System.out.println("z2.real(): " + z2.real() + " ... z2.imag(): "
+ z2.imag());
System.out.println("z1.abs(): " + z1.abs() + " ... z1.arg(): "
+ z1.arg());
System.out.println("z2.abs(): " + z2.abs() + " ... z2.arg(): "
+ z2.arg());
System.out.println("Add: " + z1.add(z2) + " ... " + z2.add(z1));
System.out.println("Sub: " + z1.sub(z2) + " ... " + z2.sub(z1));
System.out.println("Mul: " + z1.mul(z2) + " ... " + z2.mul(z1));
System.out.println("Div: " + z1.div(z2) + " ... " + z2.div(z1));
System.out.println("ARI: " + z1.add(z2).sub(z1).sub(z2) );
System.out.println("ARI: " + z1.add(z2.sub(z1)).sub(z2) );
System.out.println("ARI: " + z1.mul(z2).div(z1).div(z2) );
System.out.println("ARI: " + z1.mul(z2.div(z1)).div(z2) );
System.out.println("Complex Test Ende");
}
// Test der Grundfunktionalitaet
public static void main(String[] args) {
test(new Complex(2.0, -3.0), new Complex(5.0, -7.0));
test(new Complex(1.0), new Complex(0.0, 1.0));
test(new Complex(Math.PI, Math.E), new Complex(Math.PI, Math.E));
test(new Complex(3.0, 4.0), new Complex(4.0, 3.0));
if ( ! new Complex(13.0).equals(new Complex(13.0, 0.0)) ) {
System.out.println("FEHLER in Konstruktoren");
}
// Test, ob REFERENZ I wirklich nicht aenderbar?
// I = I.add(new Complex(1.0, -1.0)); // Compilezeitfehler
System.out.println("\nImaginaere Einheit: " + I);
// Test, ob WERT von I aenderbar?
I.re= 5.0;
System.out.println("Imaginaere Einheit? " + I + "\n");
}
}
-------------------------------------------------
package complex;
public class ComplexCalcUtility {
public static double abs() {
return Math.sqrt(this.re*this.re + this.im*this.im);
}
public static double arg() {
return Math.acos(this.re / this.abs() );
}
public static Complex conj() {
return new Complex(this.re,-this.im);
}
public static Complex add(Complex z1) {
return new Complex(this.re + z1.re, this.im + z1.im);
}
public static Complex sub(Complex z1) {
return new Complex(this.re - z1.re, this.im - z1.im);
}
public static Complex mul(Complex z1) {
return new Complex(this.re*z1.re - this.im*z1.im, this.re*z1.im + this.im*z1.re);
}
public static Complex div(Complex z1) {
return new Complex((this.re*z1.re + this.im*z1.re)/(z1.re*z1.re + this.im*this.im), (this.im*z1.re - this.re*z1.im)/(z1.re*z1.re + z1.im*z1.im));
}
public static boolean equals(Complex z1) {
if( this.re == z1.re && this.im == z1.im) {
return true;
}
else {
return false;
}
}
}
-----------------------------------------------------------------------
package complex;
public class MatrixC{
private Complex[][] elem;
public MatrixC() { //Einlesen der Spalten- und Zeilenanzahl von der Tastatur
int n = readInteger("Bitte Zeilenanzahl eingeben: ")-1;
int m = readInteger("Bitte Spaltenanzahl eingeben: ")-1;
elem = new Complex[n][m];
for( int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
elem[i][j] = readComplex();
}
}
}
public MatrixC(boolean keyboardIn) {
int n = readInteger("Bitte Zeilenanzahl eingeben: ");
int m = readInteger("Bitte Spaltenanzahl eingeben: ");
if(keyboardIn) {
elem = new Complex[n][m];
for( int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
elem[i][j] = readComplex();
}
}
}
}
public MatrixC(int row, int col) {
elem = new Complex[row][col];
}
public MatrixC(int row, int col, boolean keyboardIn) {
if(keyboardIn) {
elem = new Complex[row][col];
for( int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.print("Bitte Element der " + i + ". Zeile und der " + j + ".Spalte eingeben!");
elem[i][j] = readComplex();
}
}
}
}
public MatrixC(MatrixC orig) {
int n = orig.getRowCount();
int m = orig.getColCount();
elem = new Complex[n][m];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
elem[i][j] = orig.elem[i][j];
}
}
}
public boolean equals(MatrixC mat) {
boolean equal = false;
for(int i = 0; i < this.getRowCount(); i++) {
for(int j = 0; j < this.getColCount(); j++) {
if(this.elem[i][j].ComplexCalcUtility.equals(mat.elem[i][j])) {
equal =true;
}
else {
equal = false;
break;
}
}
}
return equal;
}
public boolean isSymm() {
boolean sym = false;
for(int i = 0; i < this.getRowCount(); i++) {
for(int j = 0; j < this.getColCount(); j++) {
if(this.elem[i][j] == this.elem[j][i]) {
sym =true;
}
else {
sym = false;
break;
}
}
}
return sym;
}
public boolean isAntiSymm() {
boolean antisym = true;
if(this.isSymm()) {
antisym = false;
}
return antisym;
}
public void print(String t) {
System.out.println(t);
String s = "";
for (int i = 0; i < this.getRowCount(); i++) {
for (int j = 0; j < this.getColCount(); j++) {
s += this.elem[i][j].toString() + ", ";
}
s += "\n";
}
System.out.println(s);
}
public int getRowCount() {
return elem.length;
}
public int getColCount() {
return elem[0].length;
}
public String toString() {
StringBuffer ret = new StringBuffer();
ret.append("{ ");
for (int i=0; i < elem.length; i++) {
ret.append("{");
for (int j=0; j < elem[i].length-1; j++) {
ret.append(elem[i][j]).append(", ");
}
ret.append(elem[i][elem[i].length-1]).append("}");
if (i+1 < elem.length ) {
ret.append(";\n");
}
}
ret.append(" }");
return ret.toString();
}
public static void test(MatrixC A, MatrixC B) {
System.out.println("MatrixC Test Start");
System.out.println("A ist eine " + A.getRowCount() + "x" + A.getColCount() + " Matrix");
A.print("A");
System.out.println("B ist eine " + B.getRowCount() + "x" + B.getColCount() + " Matrix");
B.print("B");
System.out.println("A==B?: " + A.equals(B) + " ... B==A?: " + B.equals(A));
System.out.println("A.isSymm(): " + A.isSymm() + " ... A.isAntiSymm(): " + A.isAntiSymm());
System.out.println("B.isSymm(): " + B.isSymm() + " ... B.isAntiSymm(): " + B.isAntiSymm());
A.transpose().print("A.transpose()");
B.transpose().print("B.transpose()");
A.symm().print("A.symm()");
B.symm().print("B.symm()");
A.antiSymm().print("A.antiSymm()");
B.antiSymm().print("B.antiSymm()");
A.add(B).print("A+B");
B.add(A).print("B+A");
A.sub(B).print("A-B");
B.sub(A).print("B-A");
A.transpose().print("A.transpose()");
B.transpose().print("B.transpose()");
System.out.println("A==(A.symm() + A.antiSymm()): " + A.equals(A.symm().add(A.antiSymm())));
System.out.println("B==(B.symm() + B.antiSymm()): " + B.equals(B.symm().add(B.antiSymm())));
System.out.println("A+A==A.mul(2.0): " + A.mul(2.0).equals(A.add(A)));
System.out.println("B+B==B.mul(2.0): " + B.mul(2.0).equals(B.add(B)));
System.out.println("MatrixC Test Ende");
}
public static void main(String[] args) {
MatrixC A = new MatrixC(2,2, true);
MatrixC B = new MatrixC(2,2, true);
test(A, B);
}
}
-----------------------------------------------------------
package complex;
public class MatrixCalcUtility {
public static MatrixC add(MatrixC mat) {
int n = this.getRowCount();
int m = this.getColCount();
MatrixC ad = new MatrixC(n, m);
if(this.getRowCount() == mat.getRowCount() && this.getColCount() == mat.getColCount()) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
ad.elem[i][j] = (elem[i][j]).ComplexCalcUtility.add(mat.elem[i][j]);
}
}
}
else {
System.out.println("Dimension der Matrizen stimmen nicht überein! Addition nicht möglich!");
}
return ad;
}
public static MatrixC sub(MatrixC mat) {
int n = this.getRowCount();
int m = this.getColCount();
MatrixC su = new MatrixC(n, m);
if(this.getRowCount() == mat.getRowCount() && this.getColCount() == mat.getColCount()) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
su.elem[i][j] = (elem[i][j]).ComplexCalcUtility.sub(mat.elem[i][j]);
}
}
}
else {
System.out.println("Dimension der Matrizen stimmen nicht überein! Subtraktion nicht möglich!");
}
return su;
}
public static MatrixC mul(double s) {
int n = this.getRowCount();
int m = this.getColCount();
MatrixC mu = new MatrixC(n, m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
mu.elem[i][j] = (Complex.Complex(s)).ComplexCalcUtility.mul(this.elem[i][j]);
}
}
return mu;
}
public static MatrixC transpose() {
int n = this.getRowCount();
int m = this.getColCount();
MatrixC trans = new MatrixC(m, n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
trans.elem[i][j] = this.elem[j][i];
}
}
return trans;
}
public static MatrixC symm() {
int n = this.getRowCount();
int m = this.getColCount();
MatrixC sym = new MatrixC(n, m);
sym = ( this.add( this.transpose() ) ).mul(0.5);
return sym;
}
public static MatrixC antiSymm() {
int n = this.getRowCount();
int m = this.getColCount();
MatrixC antisym = new MatrixC(n, m);
antisym = ( this.sub( this.transpose() ) ).mul(0.5);
return antisym;
}
public static boolean equals(MatrixC mat) {
boolean equal = false;
for(int i = 0; i < this.getRowCount(); i++) {
for(int j = 0; j < this.getColCount(); j++) {
if((this.elem[i][j]).ComplexCalcUtility.equal(mat.elem[i][j])) {
equal =true;
}
else {
equal = false;
break;
}
}
}
return equal;
}
public static boolean isSymm() {
boolean sym = false;
for(int i = 0; i < this.getRowCount(); i++) {
for(int j = 0; j < this.getColCount(); j++) {
if((this.elem[i][j]).ComplexCalcUtility.equal(this.elem[j][i])) {
sym =true;
}
else {
sym = false;
break;
}
}
}
return sym;
}
public static boolean isAntiSymm() {
boolean antisym = true;
if(this.isSymm()) {
antisym = false;
}
return antisym;
}
}