fork(5) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class ComplexNumber {
  6.  
  7. private double re;
  8. private double im;
  9.  
  10. public ComplexNumber(double re, double im) {
  11. this.re = re;
  12. this.im = im;
  13. }
  14.  
  15. public double getRe() {
  16. return re;
  17. }
  18.  
  19. public double getIm() {
  20. return im;
  21. }
  22.  
  23. private double getModule() {
  24. return Math.sqrt(this.re * this.re + this.im * this.im);
  25. }
  26.  
  27. public static ComplexNumber sum(ComplexNumber cn1, ComplexNumber cn2) {
  28. return new ComplexNumber(cn1.getRe() + cn2.getRe(), cn1.getIm() + cn2.getIm());
  29. }
  30.  
  31. public static ComplexNumber multiply(ComplexNumber cn1, ComplexNumber cn2) {
  32. return new ComplexNumber(cn1.getRe() * cn2.getRe() - cn1.getIm() * cn2.getIm(), cn1.getRe() * cn2.getIm() + cn1.getIm() * cn2.getRe());
  33. }
  34.  
  35. public static ComplexNumber subtract(ComplexNumber cn1, ComplexNumber cn2) {
  36. return new ComplexNumber(cn1.getRe() - cn2.getRe(), cn1.getIm() - cn2.getIm());
  37. }
  38.  
  39. public static ComplexNumber divide(ComplexNumber cn1, ComplexNumber cn2) {
  40. ComplexNumber temp = new ComplexNumber(cn2.getRe(), (-1)* cn2.getIm());
  41. temp = ComplexNumber.multiply(cn1, temp);
  42. double denominator = cn2.getRe()*cn2.getRe() + cn2.getIm() * cn2.getIm();
  43. return new ComplexNumber(temp.getRe()/denominator, temp.getIm()/denominator);
  44. }
  45.  
  46. private double GetArg() {
  47. if (this.re > 0) {
  48. return Math.atan(im / re);
  49. } else {
  50. if (re < 0 && im > 0) {
  51. return Math.PI + Math.atan(im / re);
  52. } else {
  53. return -Math.PI + Math.atan(im / re);
  54. }
  55. }
  56. }
  57.  
  58. public static ComplexNumber pow(ComplexNumber cn, int power) {
  59. double factor = Math.pow(cn.getModule(), power);
  60. return new ComplexNumber(factor*Math.cos(power * cn.GetArg()), factor*Math.sin(power * cn.GetArg()));
  61. }
  62.  
  63. public static ComplexNumber[] sqrt(ComplexNumber cn) {
  64. double a = cn.getModule() / 2;
  65. ComplexNumber pos = new ComplexNumber(Math.sqrt(a + cn.getRe() / 2), Math.signum(cn.getIm()) * Math.sqrt(a - cn.getRe() / 2));
  66. ComplexNumber neg = new ComplexNumber((-1) * pos.getRe(), (-1) * pos.getIm());
  67. ComplexNumber[] answer = {pos, neg};
  68. return answer;
  69. }
  70.  
  71.  
  72. private String sign() {
  73. if (im > 0) return " + ";
  74. else return " - ";
  75. }
  76.  
  77. @Override
  78. public String toString() {
  79. String string;
  80. if (im == 1 || im == -1) {
  81. if (re == 0) {
  82. string = sign() + "i";
  83. } else {
  84. string = Double.toString(re) + sign() + "i";
  85. }
  86. } else {
  87. string = Double.toString(re) + sign() + Double.toString(Math.abs(im)) + "i";
  88. }
  89. return string;
  90. }
  91.  
  92. @Override
  93. public boolean equals(Object obj) {
  94. if (this.getClass() != obj.getClass() || obj == null)
  95. return false;
  96. return true;
  97. }
  98.  
  99. public static void main(String[] args) {
  100. ComplexNumber x = new ComplexNumber(2, 3);
  101. ComplexNumber y = new ComplexNumber(-1, 2);
  102. System.out.println("z1 = " + x + ", z2 = " + y);
  103.  
  104. ComplexNumber z;
  105. z = ComplexNumber.sum(x, y);
  106. System.out.println("+ : " + z);
  107.  
  108. z = ComplexNumber.subtract(x, y);
  109. System.out.println("- : " + z);
  110.  
  111. z = ComplexNumber.divide(x, y);
  112. System.out.println("/ : " + z);
  113.  
  114. z = ComplexNumber.multiply(x, y);
  115. System.out.println(" * :" + z);
  116.  
  117. z = ComplexNumber.pow(y, 2);
  118. System.out.println("Pow 2 of z2 : " + z);
  119.  
  120. ComplexNumber b = new ComplexNumber(3, 4);
  121. ComplexNumber[] ans = ComplexNumber.sqrt(b);
  122. System.out.println("Sqrt of " + b + " = " + ans[0]+ ", " + ans[1]);
  123.  
  124. }
  125. }
  126.  
  127.  
  128.  
Success #stdin #stdout 0.05s 320576KB
stdin
Standard input is empty
stdout
z1 = 2.0 + 3.0i,     z2 = -1.0 + 2.0i
+ : 1.0 + 5.0i
- : 3.0 + i
/ : 0.8 - 1.4i
 * :-8.0 + i
Pow 2 of z2 : -3.000000000000001 - 4.0i
Sqrt of 3.0 + 4.0i = 2.0 + i,  -2.0 - i