Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. import java.util.Objects;
  2.  
  3. public class ComplexNumber implements Comparable<ComplexNumber> {
  4.  
  5. private final float real;
  6. private final float complex;
  7.  
  8. private ComplexNumber(float real, float complex) {
  9. this.real = real;
  10. this.complex = complex;
  11. }
  12.  
  13. private ComplexNumber(ComplexNumber number) {
  14. this.real = number.real;
  15. this.complex = number.complex;
  16. }
  17.  
  18. private ComplexNumber(float a, boolean isReal) {
  19. if (isReal) {
  20. this.real = a;
  21. this.complex = 0f;
  22. } else {
  23. this.real = 0;
  24. this.complex = a;
  25. }
  26. }
  27.  
  28. public static ComplexNumber of(float real, float complex) {
  29. return new ComplexNumber(real, complex);
  30. }
  31.  
  32. public static ComplexNumber of(ComplexNumber complexNumber) {
  33. return new ComplexNumber(complexNumber);
  34. }
  35.  
  36. public static ComplexNumber of(float a, boolean isReal) {
  37. return new ComplexNumber(a, isReal);
  38. }
  39.  
  40. public static ComplexNumber fromPolar(float abs, float arg) {
  41. return new ComplexNumber((float) (abs * Math.cos(arg)), (float) (abs * Math.sin(arg)));
  42. }
  43.  
  44. public static ComplexNumber zero() {
  45. return new ComplexNumber(0, 0);
  46. }
  47.  
  48. public ComplexNumber conjugate() {
  49. return new ComplexNumber(this.real, -this.complex);
  50. }
  51.  
  52. public boolean isReal() {
  53. return Float.compare(this.complex, 0) == 0;
  54. }
  55.  
  56. public float abs() {
  57. return (float) Math.sqrt(this.real * this.real + this.complex * this.complex);
  58. }
  59.  
  60. public float arg() {
  61. return (float) Math.atan(Math.abs(this.complex) / Math.abs(this.real));
  62. }
  63.  
  64. public ComplexNumber copy() {
  65. return new ComplexNumber(this.real, this.complex);
  66. }
  67.  
  68. public ComplexNumber add(ComplexNumber complexNumber) {
  69. float newReal = this.real + complexNumber.real;
  70. float newComplex = this.complex + complexNumber.complex;
  71. return new ComplexNumber(newReal, newComplex);
  72. }
  73.  
  74. public ComplexNumber add(float real) {
  75. return new ComplexNumber(this.real + real, this.complex);
  76. }
  77.  
  78. public ComplexNumber minus(ComplexNumber complexNumber) {
  79. float newReal = this.real - complexNumber.real;
  80. float newComplex = this.complex - complexNumber.complex;
  81. return new ComplexNumber(newReal, newComplex);
  82. }
  83.  
  84. public ComplexNumber minus(float real) {
  85. return new ComplexNumber(this.real - real, this.complex);
  86. }
  87.  
  88. public ComplexNumber mult(ComplexNumber complexNumber) {
  89. float newReal = (this.real * complexNumber.real) - (this.complex * complexNumber.complex);
  90. float newComplex = (this.real * complexNumber.complex) + (this.complex * complexNumber.real);
  91. return new ComplexNumber(newReal, newComplex);
  92. }
  93.  
  94. public ComplexNumber mult(float real) {
  95. return new ComplexNumber(this.real * real, this.complex * real);
  96. }
  97.  
  98. public ComplexNumber divide(ComplexNumber complexNumber) {
  99. ComplexNumber num = this.mult(complexNumber.conjugate());
  100. ComplexNumber denom = complexNumber.mult(complexNumber.conjugate());
  101. return num.divide(denom.abs());
  102. }
  103.  
  104. public ComplexNumber divide(float real) {
  105. return new ComplexNumber(this.real / real, this.complex / real);
  106. }
  107.  
  108. public ComplexNumber pow(float pow) {
  109. float abs = (float) Math.pow(this.abs(), pow);
  110. float theta = pow * this.arg();
  111. return ComplexNumber.fromPolar(abs, theta);
  112. }
  113.  
  114. public ComplexNumber root(float root) {
  115. return this.pow(1f / root);
  116. }
  117.  
  118. @Override
  119. public boolean equals(Object o) {
  120. if (this == o) return true;
  121. if (o == null || getClass() != o.getClass()) return false;
  122. ComplexNumber that = (ComplexNumber) o;
  123. return Float.compare(that.real, real) == 0 &&
  124. Float.compare(that.complex, complex) == 0;
  125. }
  126.  
  127. @Override
  128. public int hashCode() {
  129. return Objects.hash(real, complex);
  130. }
  131.  
  132. @Override
  133. public String toString() {
  134. if (Float.compare(real, 0) == 0) {
  135. if (Float.compare(complex, 0) == 0) {
  136. return "0";
  137. } else {
  138. return String.format("%fi", this.complex);
  139. }
  140. }
  141. if (Float.compare(complex, 0) == 0) {
  142. if (Float.compare(real, 0) == 0) {
  143. return "0";
  144. } else {
  145. return String.format("%f", this.real);
  146. }
  147. }
  148. return String.format(
  149. "%f%c%fi",
  150. this.real,
  151. (Float.compare(this.complex, 0) > 0) ? '+' : '-',
  152. Math.abs(this.complex)
  153. );
  154. }
  155.  
  156. @Override
  157. public int compareTo(ComplexNumber o) {
  158. return Float.compare(this.abs(), o.abs());
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement