ComplexNumber.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package holeg.power_flow;
  2. import java.util.Objects;
  3. public class ComplexNumber {
  4. public final double real;
  5. public final double imaginary;
  6. public final static ComplexNumber Zero = new ComplexNumber(0, 0);
  7. public final static ComplexNumber One = new ComplexNumber(1, 0);
  8. public final static ComplexNumber j = new ComplexNumber(0, -1);
  9. public ComplexNumber(double real) {
  10. this.real = real;
  11. this.imaginary = 0;
  12. }
  13. public ComplexNumber(double real, double imaginary) {
  14. this.real = real;
  15. this.imaginary = imaginary;
  16. }
  17. public static ComplexNumber fromPolar(double magnitude, double angle) {
  18. return new ComplexNumber(magnitude * Math.cos(angle), magnitude * Math.sin(angle));
  19. }
  20. public double lenSquared() {
  21. return real * real + imaginary * imaginary;
  22. }
  23. public double len() {
  24. return Math.sqrt(lenSquared());
  25. }
  26. public double angle() {
  27. return Math.atan2(imaginary, real);
  28. }
  29. public ComplexNumber add(ComplexNumber other) {
  30. return new ComplexNumber(real + other.real, imaginary + other.imaginary);
  31. }
  32. public ComplexNumber subtract(ComplexNumber other) {
  33. return new ComplexNumber(real - other.real, imaginary - other.imaginary);
  34. }
  35. public ComplexNumber multiply(double scalar) {
  36. return new ComplexNumber(real * scalar, imaginary * scalar);
  37. }
  38. public ComplexNumber multiply(ComplexNumber other) {
  39. return new ComplexNumber(real * other.real - imaginary * other.imaginary, imaginary * other.real + real * other.imaginary);
  40. }
  41. public ComplexNumber conjugate() {
  42. return new ComplexNumber(real, -imaginary);
  43. }
  44. public ComplexNumber reciprocal() {
  45. return new ComplexNumber(real / lenSquared(), -imaginary / lenSquared());
  46. }
  47. public ComplexNumber divide(ComplexNumber other) {
  48. return multiply(other.reciprocal());
  49. }
  50. public ComplexNumber negate() {
  51. return new ComplexNumber(-real, -imaginary);
  52. }
  53. public boolean isZero() {
  54. return Math.abs(real) < 0.0001 && Math.abs(imaginary) < 0.0001;
  55. }
  56. @Override
  57. public String toString() {
  58. if (imaginary >= 0)
  59. return String.format("%.4f + %.4fj", real, imaginary);
  60. return String.format("%.4f - %.4fj", real, Math.abs(imaginary));
  61. }
  62. public static boolean isNullOrZero(ComplexNumber number) {
  63. return number == null || number.isZero();
  64. }
  65. public static boolean equals(ComplexNumber a, ComplexNumber b) {
  66. if (a == b)
  67. return true;
  68. if (a == null || b == null)
  69. return false;
  70. return a.equals(b);
  71. }
  72. @Override
  73. public boolean equals(Object o) {
  74. if (this == o) return true;
  75. if (o == null || getClass() != o.getClass()) return false;
  76. ComplexNumber that = (ComplexNumber) o;
  77. return Double.compare(that.real, real) == 0 && Double.compare(that.imaginary, imaginary) == 0;
  78. }
  79. @Override
  80. public int hashCode() {
  81. return Objects.hash(real, imaginary);
  82. }
  83. }