Created
September 11, 2022 13:17
-
-
Save xsbee/d7e8cee470fab0c754f3d0d203980556 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Complex { | |
public double re; | |
public double im; | |
public Complex(double real, double imag) { | |
this.re = real; | |
this.im = imag; | |
} | |
public double mag() { | |
return Math.hypot(this.re, this.im); | |
} | |
public double arg() { | |
return Math.atan2(this.im, this.re); | |
} | |
public Complex add(Complex other) { | |
return new Complex(this.re + other.re, this.im + other.im); | |
} | |
public Complex sub(Complex other) { | |
return new Complex(this.re - other.re, this.im - other.im); | |
} | |
public Complex mul(Complex other) { | |
return new Complex( | |
this.re * other.re - this.im * other.im, | |
this.re * other.im + other.re * this.im); | |
} | |
public Complex div(Complex other) { | |
double denom = other.re * other.re + other.im * other.im; | |
return new Complex( | |
(this.re * other.re + this.im * other.im) / denom, | |
(other.re * this.im - this.re * other.im) / denom); | |
} | |
@Override | |
public String toString() { | |
return this.re + (Math.signum(this.im) < 0 ? "-" : "+") + this.im + "i"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment