Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
52
Task/Arithmetic-Complex/Java/arithmetic-complex.java
Normal file
52
Task/Arithmetic-Complex/Java/arithmetic-complex.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
public class Complex {
|
||||
public final double real;
|
||||
public final double imag;
|
||||
|
||||
public Complex() {
|
||||
this(0, 0);
|
||||
}
|
||||
|
||||
public Complex(double r, double i) {
|
||||
real = r;
|
||||
imag = i;
|
||||
}
|
||||
|
||||
public Complex add(Complex b) {
|
||||
return new Complex(this.real + b.real, this.imag + b.imag);
|
||||
}
|
||||
|
||||
public Complex mult(Complex b) {
|
||||
// FOIL of (a+bi)(c+di) with i*i = -1
|
||||
return new Complex(this.real * b.real - this.imag * b.imag,
|
||||
this.real * b.imag + this.imag * b.real);
|
||||
}
|
||||
|
||||
public Complex inv() {
|
||||
// 1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
|
||||
double denom = real * real + imag * imag;
|
||||
return new Complex(real / denom, -imag / denom);
|
||||
}
|
||||
|
||||
public Complex neg() {
|
||||
return new Complex(-real, -imag);
|
||||
}
|
||||
|
||||
public Complex conj() {
|
||||
return new Complex(real, -imag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return real + " + " + imag + " * i";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Complex a = new Complex(Math.PI, -5); //just some numbers
|
||||
Complex b = new Complex(-1, 2.5);
|
||||
System.out.println(a.neg());
|
||||
System.out.println(a.add(b));
|
||||
System.out.println(a.inv());
|
||||
System.out.println(a.mult(b));
|
||||
System.out.println(a.conj());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue