123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package holeg.power_flow;
- import java.util.Objects;
- public class ComplexNumber {
- public final double real;
- public final double imaginary;
- public final static ComplexNumber Zero = new ComplexNumber(0, 0);
- public final static ComplexNumber One = new ComplexNumber(1, 0);
- public final static ComplexNumber j = new ComplexNumber(0, -1);
- public ComplexNumber(double real) {
- this.real = real;
- this.imaginary = 0;
- }
- public ComplexNumber(double real, double imaginary) {
- this.real = real;
- this.imaginary = imaginary;
- }
- public static ComplexNumber fromPolar(double magnitude, double angle) {
- return new ComplexNumber(magnitude * Math.cos(angle), magnitude * Math.sin(angle));
- }
- public double lenSquared() {
- return real * real + imaginary * imaginary;
- }
- public double len() {
- return Math.sqrt(lenSquared());
- }
- public double angle() {
- return Math.atan2(imaginary, real);
- }
- public ComplexNumber add(ComplexNumber other) {
- return new ComplexNumber(real + other.real, imaginary + other.imaginary);
- }
- public ComplexNumber subtract(ComplexNumber other) {
- return new ComplexNumber(real - other.real, imaginary - other.imaginary);
- }
- public ComplexNumber multiply(double scalar) {
- return new ComplexNumber(real * scalar, imaginary * scalar);
- }
- public ComplexNumber multiply(ComplexNumber other) {
- return new ComplexNumber(real * other.real - imaginary * other.imaginary, imaginary * other.real + real * other.imaginary);
- }
- public ComplexNumber conjugate() {
- return new ComplexNumber(real, -imaginary);
- }
- public ComplexNumber reciprocal() {
- return new ComplexNumber(real / lenSquared(), -imaginary / lenSquared());
- }
- public ComplexNumber divide(ComplexNumber other) {
- return multiply(other.reciprocal());
- }
- public ComplexNumber negate() {
- return new ComplexNumber(-real, -imaginary);
- }
- public boolean isZero() {
- return Math.abs(real) < 0.0001 && Math.abs(imaginary) < 0.0001;
- }
- @Override
- public String toString() {
- if (imaginary >= 0)
- return String.format("%.4f + %.4fj", real, imaginary);
- return String.format("%.4f - %.4fj", real, Math.abs(imaginary));
- }
- public static boolean isNullOrZero(ComplexNumber number) {
- return number == null || number.isZero();
- }
- public static boolean equals(ComplexNumber a, ComplexNumber b) {
- if (a == b)
- return true;
- if (a == null || b == null)
- return false;
- return a.equals(b);
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- ComplexNumber that = (ComplexNumber) o;
- return Double.compare(that.real, real) == 0 && Double.compare(that.imaginary, imaginary) == 0;
- }
- @Override
- public int hashCode() {
- return Objects.hash(real, imaginary);
- }
- }
|